[100] 210 poly, FastRead-Tutorial by www.msharpmath.com [100] 210 revised on 2012.12.03 cemmath The Simple is the Best poly In general, a polynomial is defined in an ascending order although both ascending and descending polynomials are handled. Only real coefficients are of interest in Cemmath. p(x) = a0 + a1 x + a2 x^2 + a3 x^3 + ... + an x^n 𝑝(𝑥) = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + 𝑎3 𝑥 3 + … + 𝑎𝑛 𝑥 𝑛 // // // // // // // // // // // // // // // // // // // // // ascending order descending order by known roots Newton polynomial constant polynomial declaring variables as polynomials unary operations binary operations with double d, integer n Synthetic division by a scalar c is related with a factor (𝑥 − 𝑐) binary operations partial fraction compound assignment (double d, integer n) deconvolution evaluation subscripts piecewise polynomials figure-returning member functions 'double'-returning member functions 'poly'-returning member functions 'matrix'-returning member functions sequence and series special polynomials polynomial array 1 [100] 210 poly, FastRead-Tutorial by www.msharpmath.com // coefficient functions // displaying polynomials //---------------------------------------------------------------------// ascending order //---------------------------------------------------------------------poly( a0,a1,a2, ... , an ) // list of double poly( [a0,a1,a2, ... ,an] ) // a single matrix [a0,a1,a2, ... ,an] .apoly #> poly( 1,2,3,4,5 ) ; ans = poly( 1 2 3 4 5 ) = 5x^4 +4x^3 +3x^2 +2x +1 #> poly( [ 1,2,3,4,5 ] ) ; ans = poly( 1 2 3 4 5 ) = 5x^4 +4x^3 +3x^2 +2x +1 #> [ 1,2,3,4,5 ] .apoly ; ans = poly( 1 2 3 4 5 ) = 5x^4 +4x^3 +3x^2 +2x +1 //---------------------------------------------------------------------// descending order //---------------------------------------------------------------------A .dpoly [ an, ... , a2, a1, a0 ] .dpoly // convert a matrix into a polynomial // general order, n >= 4 A leading dot (.) in front of a pair of [] converts a matrix into a polynomial. .[ an, ... , a2, a1, a0 ] .[ .[ .[ .[ a a, b a, b, c a, b, c, d ] ] ] ] // general order, n >= 4 // // // 2nd-order // 3rd-order constant polynomial 1st-order poly, line ax + b poly, parabola ax^2 + bx + c poly, cubic ax^3 + bx^2 + cx + d poly(an, ... ,a2,a1,a0) .rev #> .[ 1,2,3,4,5 ] ; // a leading dot converts a matrix into a poly 2 [100] 210 poly, FastRead-Tutorial by www.msharpmath.com ans = poly( 5 4 3 2 1 ) = x^4 +2x^3 +3x^2 +4x +5 #> .[ 1,3,2 ] ; y = x^2 + 3x + 2 roots // 2nd-order with information [ -1 ] [ -2 ] vertex at ( -1.5, -0.25 ) y_minimum = -0.25 y-axis intersect ( 0, 2 ) symmetry at x = -1.5 directrix at y = -0.5 focus at ( -1.5, 0 ) //---------------------------------------------------------------------// by known roots //---------------------------------------------------------------------// (x-x1)(x-x2) ... (x-xn) [x1,x2, ... , xn ] .roots .roots(x1,x2,…,xn) .roots(matrix) // good for complex roots #> [0,1,2,3] .roots ; ans = poly( 0 -6 11 -6 = x^4 -6x^3 +11x^2 -6x #> A = [ 1+1!; 1-1! ]; A = [ 1 + i 1 [ 1 - i 1 1 ) // 1! = 1i = 1j for digits ] ] #> .roots(A); // complex roots ans = poly( 2 -2 1 ) = x^2 -2x +2 //---------------------------------------------------------------------// Newton polynomial //---------------------------------------------------------------------p(x) =a0 +a1(x-x1) +a2(x-x1)(x-x2) +a3(x-x1)(x-x2)(x-x3) + ... 𝑝(𝑥) = 𝑎0 + 𝑎1 (𝑥 − 𝑥1 ) + 𝑎2 (𝑥 − 𝑥1 )(𝑥 − 𝑥2 ) 3 [100] 210 poly, FastRead-Tutorial by www.msharpmath.com +𝑎3 (𝑥 − 𝑥1 )(𝑥 − 𝑥2 )(𝑥 − 𝑥3 ) + … %> 1 + 2(x-3) + 3(x-3)(x-4) + 4(x-3)(x-4)(x-5) #> poly(1,2,3,4) .newton( [3,4,5] ); ans = poly( -209 169 -45 4 ) = 4x^3 -45x^2 +169x -209 // newton polynomial #> 1 +2*[3].roots+3*[3,4].roots+ 4*[3,4,5].roots ; ans = poly( -209 169 -45 = 4x^3 -45x^2 +169x -209 4 ) #> 1 +2*.[1,-3] +3*.[1,-3]*.[1,-4] + 4*.[1,-3]*.[1,-4]*.[1,-5]; ans = poly( -209 169 -45 4 ) = 4x^3 -45x^2 +169x -209 //---------------------------------------------------------------------// constant polynomial //---------------------------------------------------------------------- Assignment by a constant results in a constant polynomial #> p = (1:5).dpoly ; p = poly( 5 4 3 2 1 ) = x^4 +2x^3 +3x^2 +4x +5 #> p = 2 ; // constant polynomial by assignment ( p was a polynomial) p = poly( 2 ) = 2 #> (1:5).dpoly - (1:5).dpoly ; // constant polynomial from operation ans = poly( 0 ) = 0 //---------------------------------------------------------------------// declaring variables as polynomials //---------------------------------------------------------------------- Declaring variables as polynomials uses a constant polynomial .[] or poly(). #> a = b = c = d = .[] #> a; b; c; d; ;; // zero polynomial, equal to poly() a = poly( 0 ) 4 [100] 210 poly, FastRead-Tutorial by www.msharpmath.com = 0 b = poly( 0 ) = 0 c = poly( 0 ) = 0 d = poly( 0 ) = 0 //---------------------------------------------------------------------// unary operations //---------------------------------------------------------------------|p| -p p ' p ~ p.' = p` // // // // // element-by-element absolute unary minus derivative, dp/dx integration, int_0^x p(x) dx finite-difference p(x)-p(x-1), p.~ // p(1)+p(2)+...+p(n), cumulative sum p.~ = p.cumsum #> .[1,0,0,0] ' ; ans = poly( 0 0 3 ) = 3x^2 #> .[1,0,0,0].' ; ans = poly( 1 -3 = 3x^2 -3x +1 p.' = p.diff = p` // dp(x)/dx = d(x^3)/dx // p(x)-p(x-1) = x^3 - (x-1)^3 3 ) #> .[1,0,0,0] ~ ; // int_0^x ans = poly( 0 0 0 0 0.25 ) = 0.25x^4 #> .[ 1,0,0,0 ].~ ; ans = poly( 0 0 x^3 dx, integration constant is zero // 1^3 + 2^3 + 3^3 + .. + x^3 = [1/2(x)(x+1)]^2 0.25 0.5 0.25 ) = 0.25x^4 +0.5x^3 +0.25x^2 //---------------------------------------------------------------------// binary operations with double d, integer n //---------------------------------------------------------------------p p p p + * / d, d, d, d, d d d d + * \ p p p p // // // // addition subtraction multiplication right and left divisions, respectively 5 [100] 210 poly, FastRead-Tutorial by www.msharpmath.com p ^ n, p.^ d, // right integer (>=0) power only // right and left element-by-element power d.^ p p %% d // synthetic division, p %% .[ 1,-d ] #> p = .[ 1,1 ] ; // x + 1 p = poly( 1 = x +1 1 ) #> for.i(1,6) p^i ; ans = poly( 1 1 ) // Pascal's triangle = x +1 ans = poly( 1 2 1 ) = x^2 +2x +1 ans = poly( 1 3 3 1 ) = x^3 +3x^2 +3x +1 ans = = x^4 ans = = x^5 poly( +4x^3 poly( +5x^4 1 4 6 4 1 ) +6x^2 +4x +1 1 5 10 10 5 1 ) +10x^3 +10x^2 +5x +1 Synthetic division by a scalar c is related with a factor (𝑥 − 𝑐) 𝑝(𝑥) = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + … + 𝑎𝑛 𝑥 𝑛 = 𝑏0 + 𝑏1 (𝑥 − 𝑐) + 𝑏2 (𝑥 − 𝑐)2 + ⋯ + 𝑏𝑛 (𝑥 − 𝑐)𝑛 For example, 𝑥 3 + 4𝑥 2 + 3𝑥 + 2 = (𝑥 + 3)3 − 5(𝑥 + 3)2 + 6(𝑥 + 3) + 2 %> synthetic division #> p = .[1,4,3,2]; // x^3 + 4x^2 +3x +2 p = poly( 2 3 4 1 ) = x^3 +4x^2 +3x +2 #> p %% -3; // x^3 + 4x^2 +3x +2 = (x+3)^2 -5(x+3)^2 +6(x+3) +2 ans = poly( 2 6 -5 1 ) = x^3 -5x^2 +6x +2 #> ans ( .[1,3] ); // ans = p %% -3 ; from the above command 6 [100] 210 poly, FastRead-Tutorial by www.msharpmath.com ans = poly( 2 3 4 1 ) = x^3 +4x^2 +3x +2 #> p %% .[1,3]; // x^3 + 4x^2 +3x +2 = (x+3)^2 -5(x+3)^2 +6(x+3) +2 ans = [ [ [ [ 2 6 -5 1 ] ] ] ] //---------------------------------------------------------------------// binary operations //---------------------------------------------------------------------p + q // addition p - q // subtraction p p p p p * q / q \ q % q %% q p == q p != q // // // // // multiplication quotient (right division) quotient (left division) remainder synthetic division // equality, return (1) if true, otherwise return (0) // inequality #> p = .[3,4,5]; // 3x^2 +4x + 5 p = poly( 5 4 3 ) = 3x^2 +4x +5 #> q = .[1,2]; // x +2 q = poly( 2 1 ) = x +2 #> p + q ; // (3x^2 +4x +5) + (x +2) ans = poly( 7 5 3 ) = 3x^2 +5x +7 #> p - q ; // (3x^2 +4x +5) - (x +2) ans = poly( 3 3 3 ) = 3x^2 +3x +3 7 [100] 210 poly, FastRead-Tutorial by www.msharpmath.com #> p * q ; // (3x^2 +4x +5) * (x +2) ans = poly( 10 13 10 3 ) = 3x^3 +10x^2 +13x +10 #> p / q ; // (3x^2 +4x +5) = (x +2)(3x-2) + 9 ans = poly( -2 3 ) = 3x -2 #> q \ p ; // (3x^2 +4x +5) = (x +2)(3x-2) + 9 ans = poly( -2 3 ) = 3x -2 #> p % q ; // (3x^2 +4x +5) = (x +2)(3x-2) + 9 ans = poly( 9 ) = 9 #> p == q ; ans = // (3x^2 +4x +5) == (x+2) 0 #> p != q ; ans = // (3x^2 +4x +5) != (x+2) 1 partial fraction 𝑥 5 + 8𝑥 4 + 6𝑥 3 + 3𝑥 2 − 10𝑥 + 2 −6𝑥 + 4 −9𝑥 − 8 = + + (𝑥 + 6) (𝑥 2 + 𝑥 + 1)2 (𝑥 2 + 𝑥 + 1)2 (𝑥 2 + 𝑥 + 1) This has a nature similar to synthetic division 𝑥 5 + 8𝑥 4 + 6𝑥 3 + 3𝑥 2 − 10𝑥 + 2 = (𝑥 + 6)(𝑥 2 + 𝑥 + 1)2 + (−9𝑥 − 8)(𝑥 2 + 𝑥 + 1) + (−6𝑥 + 4) %> synthetic division by a polynomial %> results are expressed in terms of a matrix %> each row of which corresponds to coefficients of a polynomial. #> p = .[ 1,8,6,3,-10,2 ]; p = poly( 2 -10 3 6 8 1 ) = x^5 +8x^4 +6x^3 +3x^2 -10x +2 8 [100] 210 poly, FastRead-Tutorial by www.msharpmath.com #> q = .[ 1,1,1 ]; q = poly( 1 1 1 ) = x^2 +x +1 #> A = p %% q ; // matrix for ascending poly A = [ [ [ 4 -8 6 -6 ] -9 ] 1 ] 𝑥 5 + 8𝑥 4 + 6𝑥 3 + 3𝑥 2 − 10𝑥 + 2 = (4 − 6𝑥) + (−8 − 9𝑥)(1 + 𝑥 + 𝑥 2 ) + (6 + 𝑥)(1 + 𝑥 + 𝑥 2 )2 #> -p + A.row(1).apoly+ A.row(2).apoly*q + A.row(3).apoly*q^2; ans = poly( 0 ) = 0 //---------------------------------------------------------------------// compound assignment (double d, integer n) //---------------------------------------------------------------------p += q // p = p + q compound addition p -= q // p = p - q compound subtraction p *= q p /= q p %= q // // // p = p * q p = p / q p = p % q p += d // p = p + d p p p p // // // // p p p p -= *= /= ^= d d d n = = = = p p p p * / ^ compound multiplication compound quotient compound remainder d d d n //---------------------------------------------------------------------// deconvolution //---------------------------------------------------------------------(q,r) = a /% b ; // deconvolution, a = bq + r %> deconvolution 9 [100] 210 poly, FastRead-Tutorial by www.msharpmath.com %> a = x^3 + 4x^2 + 3x + 2, b = x^2 + x + 3 %> x^3+4x^2+3x+2 = (x^2+x+3) (x+3) -3x-7 #> a = .[ 1,4,3,2 ]; b = .[ 1,1,3 ]; #> (q,r) = a /% b; // a = bq + r, quotient q and remainder r q = poly( 3 1 ) r = poly( -7 -3 ) #> a %% b ; ans = [ [ // synthetic division is the same as /% if two rows occur -7 3 -3 ] 1 ] //---------------------------------------------------------------------// evaluation //---------------------------------------------------------------------p(x) // double p(z) p(q) p(A) p[A] #> p = q = // // // // complex polynomial matrix (element-by-element) matrix polynomial a_0 I + a_1 A + a_2 A^2 + ... p = .[ 1,4,3,2 ]; q = .[ 1,1,3 ]; = poly( 2 3 4 1 ) x^3 +4x^2 +3x +2 = poly( 3 1 1 ) x^2 +x +3 A = [ [ #> p(2); ans = 1 3 #> (p''+q')(A); ans = [ [ 2 ] 4 ] // 2^3 + 4*2^2 + 3*2 32 #> p''(2+3!); ans = 20 + 18! 17 33 A = [ 1,2; 3,4 ]; + 2 // p''(x)=6x+8 // q'(x)=2x+1, p''+q'=8x+9 25 ] 41 ] 10 [100] 210 poly, FastRead-Tutorial by www.msharpmath.com #> (p/q)( .[1,2,3,4] ); // (p/q)=x+3 ans = poly( 7 3 2 1 ) = x^3 +2x^2 +3x +7 from (x+3)(x^2 +x +3)-3x-7 //---------------------------------------------------------------------// subscripts //---------------------------------------------------------------------p[ i ] a_i p[ end ] a_(n) #> p = (1:5).dpoly ; p = poly( 5 4 3 2 1 ) = x^4 +2x^3 +3x^2 +4x +5 #> p[0]; ans = 5 // constant of a polynomial, #> p[1]; ans = 4 // coefficient of p_o x #> p[2]; ans = 3 #> p[3]; ans = 2 #> p[end]; ans = 1 #> p[end-1]; ans = // coefficient of x^3 // coefficient of the highest power, x^n // p_(n-1) 2 //---------------------------------------------------------------------// piecewise polynomials //---------------------------------------------------------------------- Piecewise continuous polynomials are defined with a number of polynomials and corresponding intervals 𝑥11 ≤ 𝑥 ≤ 𝑥12 ∶ 𝑝1 (𝑥) = 𝑎10 + 𝑎11 𝑥 + 𝑎12 𝑥 2 + … + 𝑎1𝑛 𝑥 𝑛 + … 𝑥21 ≤ 𝑥 ≤ 𝑥22 ∶ 𝑝2 (𝑥) = 𝑎20 + 𝑎21 𝑥 + 𝑎22 𝑥 2 + … + 𝑎2𝑛 𝑥 𝑛 + … 11 [100] 210 poly, FastRead-Tutorial by www.msharpmath.com 𝑥𝑚1 ≤ 𝑥 ≤ 𝑥𝑚2 ⋮ ∶ 𝑝𝑚 (𝑥) = 𝑎𝑚0 + 𝑎𝑚1 𝑥 + 𝑎𝑚2 𝑥 2 + … + 𝑎𝑚𝑛 𝑥 𝑛 + … Combination of these piecewise polynomials can be expressed by the following matrix 𝑥11 𝑥21 𝐀=[ ⋮ 𝑥𝑚1 𝑥12 𝑥22 ⋮ 𝑥𝑚2 𝑎10 𝑎20 ⋮ 𝑎𝑚0 𝑎11 𝑎21 ⋮ 𝑎𝑚1 ⋯ ⋯ ⋮ ] ⋯ This special representation by a matrix can be handled in various ways A.spl(x) A.spl(B) A.spldiff A.splint A.splplot ; // // // // // evaluation for double evaluation for matrix differentiate piecewise polynomials integrate piecewise polynomials plot piecewise polynomials #> x = (0:9).tr; #> [ x, x+1, x+1 ].splplot; // spline plot Data set { (𝑥1 , 𝑦1 ), (𝑥2 , 𝑦2 ), … , (𝑥𝑛 , 𝑦𝑛 )} can be connected by lines and represented by piecewise continuous polynomials as follows. #> x = [ 1, 2, 4, 5 ];; #> y = [ 3, 5, 4, 7 ];; #> P = .spline1(x,y); // spline1 for the 1st-order splines P = 12 [100] 210 poly, FastRead-Tutorial by www.msharpmath.com [ [ [ 1 2 4 2 4 5 1 6 -8 2 ] -0.5 ] 3 ] 1 ≤ 𝑥 ≤ 2 ∶ 𝑝1 (𝑥) = 1 + 2𝑥 2 ≤ 𝑥 ≤ 4 ∶ 𝑝2 (𝑥) = 6 − 0.5𝑥 4 ≤ 𝑥 ≤ 5 ∶ 𝑝3 (𝑥) = −8 + 3𝑥 #> P.splplot; // plot piecewise polynomials #> P.spl(2.5); ans = 4.75 // p2(2.5) = 6 - 0.5(2.5) #> P.spl( [ 2.5, 3, 4.5 ] ); ans = [ 4.75 // evaluation by matrix 4.5 5.5 ] #> Q = P.splint; Q = [ [ [ 1 2 4 2 4 5 𝑥 -2 -7 21 1 6 -8 𝑥 1 ≤ 𝑥 ≤ 2 ∶ 𝑞1 (𝑥) = ∫ 𝑝1 (𝑧)𝑑𝑧 = ∫ 1 + 2𝑧 𝑑𝑧 = − 2 + 𝑥 + 𝑥 2 1 𝑥 1 1 2 ≤ 𝑥 ≤ 4 ∶ 𝑞2 (𝑥) = ∫ 𝑝2 (𝑧)𝑑𝑧 + 𝑞1 (2) = −7 + 6𝑥 − 𝑥 2 4 2 𝑥 3 4 ≤ 𝑥 ≤ 5 ∶ 𝑞3 (𝑥) = ∫ 𝑝3 (𝑧)𝑑𝑧 + 𝑞2 (4) = 21 − 8𝑥 + 𝑥 2 2 4 #> Q.spl(4.5) - Q.spl(2.5); ans = 8.9375 13 1 ] -0.25 ] 1.5 ] [100] 210 poly, FastRead-Tutorial by www.msharpmath.com 4 4.5 4 4.5 1 3 ∫ 𝑝2 (𝑥)𝑑𝑥 + ∫ 𝑝3 (𝑥) 𝑑𝑥 = [−7 + 6𝑥 − 𝑥 2 ] + [21 − 8𝑥 + 𝑥 2 ] 4 2 2.5 4 2.5 4 = 8.9375 For spline interpolation using 3rd-degree polynomials, example is #> x = [ 1, 2, 4, 5 ];; #> y = [ 3, 5, 4, 7 ];; #> P = .spline(x,y); // 1st and 2nd columns for interval P = [ [ [ 1 2 4 2 4 5 1 -10.5 89.5 0.625 17.875 -57.125 2.0625 -6.5625 12.1875 -0.6875 ] 0.75 ] -0.8125 ] 1 ≤ 𝑥 ≤ 2 ∶ 𝑝1 (𝑥) = 1 + 0.625𝑥 + 2.0625𝑥 2 − 0.6875𝑥 3 2 ≤ 𝑥 ≤ 4 ∶ 𝑝2 (𝑥) = −10.5 + 17.875𝑥 − 6.5625𝑥 2 + 0.75𝑥 3 4 ≤ 𝑥 ≤ 5 ∶ 𝑝3 (𝑥) = 89.5 − 57.125𝑥 + 12.1875𝑥 2 − 0.8125𝑥 3 #> P.splplot; // .spline(x,y).splplot; #> [ x', y' ].plot+ ; #> P.splint.spl(4.5) - P.splint.spl(2.5) ; ans = 8.5068359 4 4.5 ∫ 𝑝2 (𝑥)𝑑𝑥 + ∫ 𝑝3 (𝑥) 𝑑𝑥 2.5 4 4 = ∫ −10.5 + 17.875𝑥 − 6.5625𝑥 2 + 0.75𝑥 3 𝑑𝑥 2.5 4.5 + ∫ 89.5 − 57.125𝑥 + 12.1875𝑥 2 − 0.8125𝑥 3 𝑑𝑥 4 = 8.5068359 14 [100] 210 poly, FastRead-Tutorial by www.msharpmath.com #> P.splint.splplot; #> P.spldiff.splplot; // .spline(x,y) .splint .splplot; integrate // .spline(x,y) .spldiff .splplot; differentiate //---------------------------------------------------------------------// figure-returning member functions //---------------------------------------------------------------------p .plot(a,b) // plot between a <= x <= b #> .[ 1, 3, 2 ].plot(-3,1); y = x^2 + 3x + 2 //---------------------------------------------------------------------// 'double'-returning member functions //---------------------------------------------------------------------p .denom(nmax=10000) p .len // n+1 p .max // p_i >= all p_j 15 [100] 210 poly, FastRead-Tutorial by www.msharpmath.com p p p p p p .maxentry .mean .min .minentry .n .prod // // // // // // |p_i| >= all |p_j| (sum p_i )/(n+1) p_i <= all p_j |p_i| <= all |p_j| n (prod p_i ) #> p = .[ 3, 2, -5 ]; p = poly( -5 2 3 ) = 3x^2 +2x -5 #> p .maxentry; ans = // |p_i| >= all |p_j| -5 #> p .minentry; ans = // |p_i| <= all |p_j| 2 #> p .max; ans = // p_i >= all p_j 3 #> p .min; // p_i <= all p_j ans = -5 #> p .mean; // (-5+2+3)/3 ans = 0 #> p .prod; // (-5)*(2)*(3) ans = -30 #> p .n ; ans = // highest order x^n 2 #> p .len; ans = // n+1 3 //---------------------------------------------------------------------// 'poly'-returning member functions //---------------------------------------------------------------------p .cumsum // p.~ = p(1)+p(2)+...+p(n), cumulative sum, p .decimal // deviation from closest interger for coefficients p .dfact // element-by-element double factorial 16 [100] 210 poly, FastRead-Tutorial by www.msharpmath.com p p p p p p .diff .even .fact .inv .invtay .iratio // // // // // // p` = p.' = p(x)-p(x-1), finite difference remove odd terms, p[2i-1] = 0 element-by-element factorial inverse each coefficient, 1/p[i] multiply by i!, p[i] *= i! termwise rational approximation p p p p .monic .newton(u) .odd .pm // // // // monic polynomial, p[i] /= p[n] a0 +a1(x-u1) +a2(x-u1)(x-u2) + ... remove even terms, p[2i] = 0 alternating signs, (-1)^i *p[i] p p p p p .pow(k) .quad(r,s) .ratio .rev .round // // // // // replace x by x^k, p[i*k] = p[i] divisor x^2 +rx +s by Bairstow method rational approximation reverse order, p[n-i] the closest interger for each coefficient p p p p p .shift(k) .syndiv(c) .tay .trun(eps) .trun1 // // // // // replace x by x-k p %% c, synthetic division coefficient divide by i!, p[i] /= i! truncate if |a_i| < eps, eps=1.e-30 is default truncate with eps=10^-1, p .trun2 ... p .trun16 p .up(k) // truncate with eps=10^-2, // truncate with eps=10^-16 // multiply by x^k, p[i+k] = p[i] #> p = .[ 1,2,3,4,5 ]; p = poly( 5 4 3 2 1 ) = x^4 +2x^3 +3x^2 +4x +5 #> p.inv ; ans = poly( 0.2 0.25 0.333333 0.5 1 ) = x^4 +0.5x^3 +0.333333x^2 +0.25x +0.2 #> p.even ; // only even powers, degree may change ans = poly( 5 0 3 0 1 ) = x^4 +3x^2 +5 #> p.odd ; // only odd powers, degree may change ans = poly( 0 4 0 2 ) = 2x^3 +4x 17 [100] 210 poly, FastRead-Tutorial by www.msharpmath.com #> p.pm ; // actually cos(n*pi) = (-1)^n ans = poly( 5 -4 3 -2 1 ) = x^4 -2x^3 +3x^2 -4x +5 #> p.fact ; // termwise factorial ans = poly( 120 24 6 2 1 ) = x^4 +2x^3 +6x^2 +24x +120 #> p.up(3) ; // multiplied by x^3 ans = poly( 0 0 0 5 4 3 2 1 ) = x^7 +2x^6 +3x^5 +4x^4 +5x^3 #> p.pow(3) ; // replace x by x^3 ans = poly( 5 0 0 4 0 0 3 0 0 1 ) 0 2 0 = x^12 +2x^9 +3x^6 +4x^3 +5 %> p(x) = x^4 + 2x^3 + 3x^2 + 4x + 5 --------------------------------%> q(x) = x^4 - 6x^3 + 15x^2 - 16x + 9 // q(x) = p(x-2) = (x-2)^4 + 2(x-2)^3 + 3(x-2)^2 + 4(x-2) + 5 // p(x) = q(x+2) = (x+2)^4 - 6(x+2)^3 + 15(x+2)^2 - 16(x+2) + 9 #> q = p.shift(2); // q(x) = p(x-2), replace x by x-2 q = poly( 9 -16 15 -6 1 ) = x^4 -6x^3 +15x^2 -16x +9 #> q( .[1,2] ); // confirm p(x) = q(x+2) ans = poly( 5 4 3 2 1 ) = x^4 +2x^3 +3x^2 +4x +5 // sum_{k=1}^{k=n} (k^2) = (1/6) (n)(n+1)(2n+1) #> .[1,0,0] .~ .ratio ; ans = (1/6) * poly( 0 1 3 2 ) //---------------------------------------------------------------------// 'matrix'-returning member functions //---------------------------------------------------------------------p .compan // companion matrix p .solve // solution matrix 18 [100] 210 poly, FastRead-Tutorial by www.msharpmath.com p .syndiv(q) #> p = A // p %% q, synthetic division by polynomial q p = .[ 1,-10,23,6 ]; A = p .compan ; = poly( 6 23 -10 1 ) x^3 -10x^2 +23x +6 = [ [ [ #> 10 1 0 p[A] ; -23 0 1 -6 ] 0 ] 0 ] // matrix polynomial, a_n A^n + a_(n-1) A^(n-1) ... + ao I ans = [ [ [ 0 0 0 0 0 0 #> .[1,3,2] .solve ; ans = [ -1 ] [ -2 ] 0 ] 0 ] 0 ] // x^2 + 3x + 2 = 0 //---------------------------------------------------------------------// sequence and series //---------------------------------------------------------------------sum 1 = n sum sum sum sum sum sum k = k^2 k^3 k^4 k^5 k^6 (1/2)n(n+1) = (1/6)n(n+1)(2n+1) = (1/4)n^2(n+1)^2 = (1/30)n(n+1)(2n+1)(3n^2+3n-1) = (1/12)n^2(n+1)^2(2n^2+2n-1) = (1/42)n(n+1)(2n+1)(3n^4+6n^3-n^2-4n+2) sum k^7 = (1/24)n^2(n+1)^2(3n^4+6n^3-n^2-4n+2) sum k^8 = (1/90)n(n+1)(2n+1)(5n^6+15n^5+5n^4-15n^3-n^2+9n-3) sum k^9 = (1/20)n^2(n+1)^2(n^2+n-1)(2n^4+4n^3-n^2-3n+3) #> poly(1) .~ ; // a_k = 1, ans = poly( 0 = x sum a_k = n 1 ) #> .[ 1,0 ] .~ ; // a_k=k, sum a_k = (1/2)n(n+1) 19 [100] 210 poly, FastRead-Tutorial by www.msharpmath.com ans = poly( 0 0.5 0.5 ) = 0.5x^2 +0.5x #> p = .[ 1,0,0,0 ] ; p = poly( 0 0 0 1 ) = x^3 #> p .~ ; p .~ .solve ; // a_k=k^3, ans = poly( 0 0 0.25 0.5 0.25 ) = 0.25x^4 +0.5x^3 +0.25x^2 ans = [ [ [ [ 0 0 -1 -1 sum a_k = (1/4)[n(n+1)]^2 ] ] ] ] #> p = .[1].up(9) ; // a_k=k^9 p = poly( 0 0 0 0 0 0 0 0 = x^9 #> q = p.~ .trun9; 0 1 ) // trun(10^-9) q = poly( 0 0 -0.15 0 0.5 0 -0.7 0 0.75 0.5 = 0.1x^10 +0.5x^9 +0.75x^8 -0.7x^6 +0.5x^4 -0.15x^2 0.1 ) #> q.ratio ; ans = (1/20) * poly( 0 0 -3 0 10 0 -14 0 15 10 2 ) #> q.solve ; // (1/20) n^2(n+1)^2(n^2+n-1)(2n^4+4n^3-n^2-3n+3) ans = [ 0 ] [ 0 ] [ [ [ [ [ -1.594 -1.594 -1.618 0.5936 0.5936 [ [ [ -1 -1 0.618 - i 0.4427 + i 0.4427 - i 0.4427 + i 0.4427 ] ] ] ] ] ] ] ] 20 [100] 210 poly, FastRead-Tutorial by www.msharpmath.com //---------------------------------------------------------------------// special polynomials //---------------------------------------------------------------------poly .P(n) // Legendre polynomial of degree n poly .T(n) // Tchebyshev polynomial of degree n poly .H(n) // Hermite polynomial of degree n poly .L(n) poly .binom(n) // Laguerre polynomial of degree n // binomial polynomial of degree n // equivalent to poly.binom(5) = s(s-1)(s-2)(s-3)(s-4)/5! #> (0:4).roots / 5.fact ; ans = poly( -0 0.2 -0.416667 0.291667 -0.0833333 0.00833333 ) = 0.00833333x^5 -0.0833333x^4 +0.291667x^3 -0.416667x^2 +0.2x #> poly.binom(5) ; ans = poly( 0 0.2 -0.416667 0.291667 -0.0833333 0.00833333 ) = 0.00833333x^5 -0.0833333x^4 +0.291667x^3 -0.416667x^2 +0.2x #> poly .binom(5) .plot(0,4) ; // p(x) = s(s-1)(s-2)(s-3)(s-4)/5! #> .hold; for.n(0,9) plot.x(-1,1) ( .P_n(x) ); plot; #> .hold; for.n(0,9) plot.x(-1,1) ( .T_n(x) ); plot; 21 [100] 210 poly, FastRead-Tutorial by www.msharpmath.com //---------------------------------------------------------------------// polynomial array //---------------------------------------------------------------------- An array of polynomials is defined according to the following syntax #> poly P[7], Q[21]; where P and Q are the names of arrays. The dimension of an array can be a variable of type 'double'. #> n=10; n = poly T[n]; 10 #> for.i(0,4) T[i] = poly.T(i) ;; // Tchebyshev #> T[0]; T[1]; T[2]; T[3]; T[4]; ans = poly( 1 ) = 1 ans = poly( 0 1 ) = x ans = poly( -1 0 2 ) = 2x^2 -1 ans = poly( -0 -3 0 = 4x^3 -3x ans = poly( 1 -0 -8 = 8x^4 -8x^2 +1 4 ) 0 8 ) //---------------------------------------------------------------------// coefficient functions //---------------------------------------------------------------------poly[N+1] .i ( f(i) ) // creates a polynomial with coefficient f(i) 22 [100] 210 poly, FastRead-Tutorial by www.msharpmath.com poly[a,b, m=1].i ( f(i) ) #> p = poly[ 1,13,2 ].i ( ((i-1)/2).pm / i ).iratio; ans = poly( 0 1 0 -1/3 0 1/5 0 -1/7 0 1/9 0 -1/11 0 1/13 ) #> p ; p = poly( 0 1 0 -0.333333 0 0.2 0 -0.142857 0 0.111111 0 -0.0909091 0 0.0769231 ) = 0.0769231x^13 -0.0909091x^11 +0.111111x^9 -0.142857x^7 +0.2x^5 -0.333333x^3 +x //---------------------------------------------------------------------// displaying polynomials //---------------------------------------------------------------------poly .format(string) // set the format for screen output poly .left(string) // set the left delimiter for screen output poly .right(string) poly .maxcol(n=10) // set the right delimiter for screen output // set the maximum column length for screen output %> default display #> p = poly(2,3,4,1); p = poly( 2 3 4 1 ) = x^3 +4x^2 +3x +2 #> q = poly(0:20); q = poly( 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ) = 20x^20 +19x^19 +18x^18 +17x^17 +16x^16 +15x^15 +14x^14 +13x^13 +12x^12 +11x^11 +10x^10 +9x^9 +8x^8 +7x^7 +6x^6 +5x^5 +4x^4 +3x^3 +2x^2 +x //--------------------------------------#> poly.format("%5.1f"); #> poly.left (" "); // left delimiter blank #> poly.right(" "); // right delimiter blank #> poly.maxcol(5); // maximum column length ans = 5 #> p = poly(2,3,4,1); p = 2.0 3.0 4.0 1.0 23 [100] 210 poly, FastRead-Tutorial by www.msharpmath.com = x^3 +4x^2 +3x +2 #> q = poly(0:20); q = 0.0 1.0 2.0 3.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 4.0 5.0 16.0 17.0 18.0 19.0 20.0 = 20x^20 +19x^19 +18x^18 +17x^17 +16x^16 +15x^15 +14x^14 +13x^13 +12x^12 +11x^11 +10x^10 +9x^9 +8x^8 +7x^7 +6x^6 +5x^5 +4x^4 +3x^3 +2x^2 +x //---------------------------------------------------------------------// end of file //---------------------------------------------------------------------- 24