= #JxnPortable\docs\programmer_examples\commons_math_examples\CommonsMath_Polynomial~Demo.jxn

#import org.apache.commons.math.analysis.polynomials.*
 = 7. added, View > PackageSearchList shows all (java.lang.String)
#import org.apache.commons.math3.analysis.polynomials.*
 = 8. added, View > PackageSearchList shows all (java.lang.String)
!
pf = @PolynomialFunction( { 7., 5., 3., 2. } )
 = 7.0 + 5.0 x + 3.0 x^2 + 2.0 x^3 (org.apache.commons.math.analysis.polynomials.PolynomialFunction)
pd = pf.polynomialDerivative()
 = 5.0 + 6.0 x + 6.0 x^2 (org.apache.commons.math.analysis.polynomials.PolynomialFunction)
! arithmetic works, because PolynomialFunction implements multiply, add, subtract:
p5 = pf pd
 = 35.0 + 67.0 x + 87.0 x^2 + 58.0 x^3 + 30.0 x^4 + 12.0 x^5 (org.apache.commons.math.analysis.polynomials.PolynomialFunction)
!
! but div or divide is not implemented in PolynomialFunction
! => dividing p5 / pd manually:
!
px = @PolynomialFunction({0.,1.})
 = x (org.apache.commons.math.analysis.polynomials.PolynomialFunction)
cDenom = pd.getCoefficients()[ pd.degree() ]
 = 6.0 (double)
!
b3 = p5.getCoefficients()[ p5.degree() ] / cDenom
 = 2.0 (double)
p4 = p5 - @PolynomialFunction({b3}) pd px px px
 = 35.0 + 67.0 x + 87.0 x^2 + 48.0 x^3 + 18.0 x^4 (org.apache.commons.math.analysis.polynomials.PolynomialFunction)
! Note: b3 * px (mixed arithmetic: double op PolynomialFunction) doesn't work, because
! neither of the following is implemented in class PolynomialFunction:
! - constructor PolynomialFunction( double c0 )
! - method mulL( double x )
! - method mul( double x, PolynomialFunction pf )
! workaround used: @PolynomialFunction({b3}) * px
!
b2 = p4.getCoefficients()[ p4.degree() ] / cDenom
 = 3.0 (double)
p3 = p4 - @PolynomialFunction({b2}) pd px px
 = 35.0 + 67.0 x + 72.0 x^2 + 30.0 x^3 (org.apache.commons.math.analysis.polynomials.PolynomialFunction)
!
b1 = p3.getCoefficients()[ p3.degree() ] / cDenom
 = 5.0 (double)
p2 = p3 - @PolynomialFunction({b1}) pd px
 = 35.0 + 42.0 x + 42.0 x^2 (org.apache.commons.math.analysis.polynomials.PolynomialFunction)
!
b0 = p2.getCoefficients()[ p2.degree() ] / cDenom
 = 7.0 (double)
p1 = p2 - @PolynomialFunction({b0}) pd
 = 0 (org.apache.commons.math.analysis.polynomials.PolynomialFunction)
! => no remainder
!
@PolynomialFunction({ b0, b1, b2, b3 })
 = 7.0 + 5.0 x + 3.0 x^2 + 2.0 x^3 (org.apache.commons.math.analysis.polynomials.PolynomialFunction)
pf
 = 7.0 + 5.0 x + 3.0 x^2 + 2.0 x^3 (org.apache.commons.math.analysis.polynomials.PolynomialFunction)