28
[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 p ( x) =a 0 +a 1 x + a 2 x 2 +a 3 x 3 + +a n x n // 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 ( xc) // binary operations partial fraction // compound assignment (double d, integer n) // deconvolution // evaluation 1

Web viewSynthetic division by a scalar c is related with a factor // binary operations. partial fraction // compound assignment (double d, integer n) // deconvolution

Embed Size (px)

Citation preview

Page 1: Web viewSynthetic division by a scalar c is related with a factor // binary operations. partial fraction // compound assignment (double d, integer n) // deconvolution

[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^np ( x )=a0+a1 x+a2 x

2+a3 x3+…+an x

n

// 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 (x−c)// 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

Page 2: Web viewSynthetic division by a scalar c is related with a factor // binary operations. partial fraction // compound assignment (double d, integer n) // deconvolution

[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 // convert a matrix into a polynomial[ an, ... , a2, a1, a0 ] .dpoly // general order, n >= 4

A leading dot (.) in front of a pair of [] converts a matrix into a polynomial. .[ an, ... , a2, a1, a0 ] // general order, n >= 4

.[ a ] // constant polynomial .[ a, b ] // 1st-order poly, line ax + b .[ a, b, c ] // 2nd-order poly, parabola ax^2 + bx + c .[ a, b, c, d ] // 3rd-order 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 ans = poly( 5 4 3 2 1 ) = x^4 +2x^3 +3x^2 +4x +5

#> .[ 1,3,2 ] ; // 2nd-order with information y = x^2 + 3x + 2

2

Page 3: Web viewSynthetic division by a scalar c is related with a factor // binary operations. partial fraction // compound assignment (double d, integer n) // deconvolution

[100] 210 poly, FastRead-Tutorial by www.msharpmath.com

roots [ -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 1 ) = x^4 -6x^3 +11x^2 -6x

#> A = [ 1+1!; 1-1! ]; // 1! = 1i = 1j for digits A = [ 1 + i 1 ] [ 1 - i 1 ]

#> .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) + ...

p ( x )=a0+a1 (x−x1 )+a2 (x−x1 ) (x−x2 )+a3 (x−x1 ) (x−x2 )(x−x3)+…

%> 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] ); // newton polynomial ans = poly( -209 169 -45 4 ) = 4x^3 -45x^2 +169x -209

#> 1 +2*[3].roots+3*[3,4].roots+ 4*[3,4,5].roots ;

3

Page 4: Web viewSynthetic division by a scalar c is related with a factor // binary operations. partial fraction // compound assignment (double d, integer n) // deconvolution

[100] 210 poly, FastRead-Tutorial by www.msharpmath.com

ans = poly( -209 169 -45 4 ) = 4x^3 -45x^2 +169x -209

#> 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 = .[] ;; // zero polynomial, equal to poly()#> a; b; c; d; a = poly( 0 ) = 0 b = poly( 0 ) = 0 c = poly( 0 ) = 0 d = poly( 0 ) = 0 //----------------------------------------------------------------------// unary operations//---------------------------------------------------------------------- |p| // element-by-element absolute -p // unary minus

4

Page 5: Web viewSynthetic division by a scalar c is related with a factor // binary operations. partial fraction // compound assignment (double d, integer n) // deconvolution

[100] 210 poly, FastRead-Tutorial by www.msharpmath.com

p ' // derivative, dp/dx p ~ // integration, int_0^x p(x) dx p.' = p` // finite-difference p(x)-p(x-1), p.' = p.diff = p` p.~ // p(1)+p(2)+...+p(n), cumulative sum p.~ = p.cumsum

#> .[1,0,0,0] ' ; // dp(x)/dx = d(x^3)/dx ans = poly( 0 0 3 ) = 3x^2

#> .[1,0,0,0].' ; // p(x)-p(x-1) = x^3 - (x-1)^3 ans = poly( 1 -3 3 ) = 3x^2 -3x +1

#> .[1,0,0,0] ~ ; // int_0^x x^3 dx, integration constant is zero ans = poly( 0 0 0 0 0.25 ) = 0.25x^4

#> .[ 1,0,0,0 ].~ ; // 1^3 + 2^3 + 3^3 + .. + x^3 = [1/2(x)(x+1)]^2 ans = poly( 0 0 0.25 0.5 0.25 ) = 0.25x^4 +0.5x^3 +0.25x^2

//----------------------------------------------------------------------// binary operations with double d, integer n//---------------------------------------------------------------------- p + d, d + p // addition p - d, d - p // subtraction p * d, d * p // multiplication p / d, d \ p // right and left divisions, respectively p ^ n, // right integer (>=0) power only p.^ d, d.^ p // right and left element-by-element power

p %% d // synthetic division, p %% .[ 1,-d ]

#> p = .[ 1,1 ] ; // x + 1 p = poly( 1 1 ) = x +1

#> for.i(1,6) p^i ; // Pascal's triangle ans = poly( 1 1 ) = x +1 ans = poly( 1 2 1 ) = x^2 +2x +1 ans = poly( 1 3 3 1 ) = x^3 +3x^2 +3x +1 ans = poly( 1 4 6 4 1 ) = x^4 +4x^3 +6x^2 +4x +1

5

Page 6: Web viewSynthetic division by a scalar c is related with a factor // binary operations. partial fraction // compound assignment (double d, integer n) // deconvolution

[100] 210 poly, FastRead-Tutorial by www.msharpmath.com

ans = poly( 1 5 10 10 5 1 ) = x^5 +5x^4 +10x^3 +10x^2 +5x +1

Synthetic division by a scalar c is related with a factor (x−c)

p ( x )=a0+a1 x+a2 x2+…+an x

n

¿b0+b1(x−c )+b2(x−c)2+…+bn(x−c )

n

For example,

x3+4 x2+3 x+2=( x+3 )3−5 ( x+3 )2+6 ( x+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 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 // additionp - q // subtractionp * q // multiplicationp / q // quotient (right division)p \ q // quotient (left division)p % q // remainderp %% q // synthetic division

6

Page 7: Web viewSynthetic division by a scalar c is related with a factor // binary operations. partial fraction // compound assignment (double d, integer n) // deconvolution

[100] 210 poly, FastRead-Tutorial by www.msharpmath.com

p == q // equality, return (1) if true, otherwise return (0)p != q // 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

#> 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 ; // (3x^2 +4x +5) == (x+2) ans = 0

#> p != q ; // (3x^2 +4x +5) != (x+2) ans = 1

partial fraction

7

Page 8: Web viewSynthetic division by a scalar c is related with a factor // binary operations. partial fraction // compound assignment (double d, integer n) // deconvolution

[100] 210 poly, FastRead-Tutorial by www.msharpmath.com

x5+8 x4+6 x3+3 x2−10 x+2(x2+x+1 )2

= −6 x+4(x2+ x+1 )2 +

−9 x−8(x2+x+1 )

+(x+6)

This has a nature similar to synthetic division

x5+8 x4+6 x3+3 x2−10 x+2¿ ( x+6 ) (x2+x+1 )2+(−9x−8 ) ( x2+x+1 )+(−6 x+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

#> q = .[ 1,1,1 ]; q = poly( 1 1 1 ) = x^2 +x +1

#> A = p %% q ; // matrix for ascending poly A = [ 4 -6 ] [ -8 -9 ] [ 6 1 ]

x5+8 x4+6 x3+3 x2−10 x+2¿ (4−6x )+ (−8−9 x ) ( 1+ x+x2 )+(6+x ) (1+x+ x2 )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 = p * q compound multiplication p /= q // p = p / q compound quotient p %= q // p = p % q compound remainder

8

Page 9: Web viewSynthetic division by a scalar c is related with a factor // binary operations. partial fraction // compound assignment (double d, integer n) // deconvolution

[100] 210 poly, FastRead-Tutorial by www.msharpmath.com

p += d // p = p + d p -= d // p = p - d p *= d // p = p * d p /= d // p = p / d p ^= n // p = p ^ n

//----------------------------------------------------------------------// deconvolution//---------------------------------------------------------------------- (q,r) = a /% b ; // deconvolution, a = bq + r

%> deconvolution %> 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 rq = poly( 3 1 )r = poly( -7 -3 )

#> a %% b ; // synthetic division is the same as /% if two rows occur ans = [ -7 -3 ] [ 3 1 ]

//----------------------------------------------------------------------// evaluation//---------------------------------------------------------------------- p(x) // double p(z) // complex p(q) // polynomial p(A) // matrix (element-by-element) p[A] // matrix polynomial a_0 I + a_1 A + a_2 A^2 + ...

#> p = .[ 1,4,3,2 ]; q = .[ 1,1,3 ]; A = [ 1,2; 3,4 ]; p = poly( 2 3 4 1 ) = x^3 +4x^2 +3x +2 q = poly( 3 1 1 ) = x^2 +x +3 A = [ 1 2 ] [ 3 4 ]

#> p(2); // 2^3 + 4*2^2 + 3*2 + 2 ans = 32

9

Page 10: Web viewSynthetic division by a scalar c is related with a factor // binary operations. partial fraction // compound assignment (double d, integer n) // deconvolution

[100] 210 poly, FastRead-Tutorial by www.msharpmath.com

#> p''(2+3!); // p''(x)=6x+8 ans = 20 + 18!

#> (p''+q')(A); // q'(x)=2x+1, p''+q'=8x+9 ans = [ 17 25 ] [ 33 41 ]

#> (p/q)( .[1,2,3,4] ); // (p/q)=x+3 from (x+3)(x^2 +x +3)-3x-7 ans = poly( 7 3 2 1 ) = x^3 +2x^2 +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]; // constant of a polynomial, p_o ans = 5

#> p[1]; // coefficient of x ans = 4

#> p[2]; ans = 3

#> p[3]; // coefficient of x^3 ans = 2

#> p[end]; // coefficient of the highest power, x^n ans = 1

#> p[end-1]; // p_(n-1) ans = 2

//----------------------------------------------------------------------// piecewise polynomials//----------------------------------------------------------------------Piecewise continuous polynomials are defined with a number of polynomials

10

Page 11: Web viewSynthetic division by a scalar c is related with a factor // binary operations. partial fraction // compound assignment (double d, integer n) // deconvolution

[100] 210 poly, FastRead-Tutorial by www.msharpmath.com

and corresponding intervals

x11≤x ≤x12 : p1 ( x )=a10+a11 x+a12 x2+…+a1n x

n+…x21≤ x≤ x22 : p2 ( x )=a20+a21 x+a22 x

2+…+a2n xn+…

xm1≤ x≤ xm2: pm ( x )=am0+am1 x+am2 x2+…+amn x

n+…

Combination of these piecewise polynomials can be expressed by the following matrix

A=[ x11 x12 a10 a11 ⋯x21 x22 a20 a21 ⋯⋮ ⋮ ⋮ ⋮ ⋮xm1 xm2 am 0 am1 ⋯ ]

This special representation by a matrix can be handled in various ways

A.spl(x) // evaluation for double A.spl(B) // evaluation for matrix A.spldiff // differentiate piecewise polynomials A.splint // integrate piecewise polynomials A.splplot ; // plot piecewise polynomials

#> x = (0:9).tr; #> [ x, x+1, x+1 ].splplot; // spline plot

Data set {( x1 , y1) , (x2 , y2 ) ,…, (xn, yn )} can be connected by lines and represented by piecewise continuous polynomials as follows.

11

Page 12: Web viewSynthetic division by a scalar c is related with a factor // binary operations. partial fraction // compound assignment (double d, integer n) // deconvolution

[100] 210 poly, FastRead-Tutorial by www.msharpmath.com

#> x = [ 1, 2, 4, 5 ];; #> y = [ 3, 5, 4, 7 ];; #> P = .spline1(x,y); // spline1 for the 1st-order splines P = [ 1 2 1 2 ] [ 2 4 6 -0.5 ] [ 4 5 -8 3 ]

1≤x ≤2 : p1 ( x )=1+2 x2≤x ≤4 : p2 (x )=6−0.5x4 ≤x ≤5 : p3 ( x )=−8+3 x

#> P.splplot; // plot piecewise polynomials

#> P.spl(2.5); // p2(2.5) = 6 - 0.5(2.5) ans = 4.75

#> P.spl( [ 2.5, 3, 4.5 ] ); // evaluation by matrix ans = [ 4.75 4.5 5.5 ]

#> Q = P.splint; Q = [ 1 2 -2 1 1 ] [ 2 4 -7 6 -0.25 ] [ 4 5 21 -8 1.5 ]

1≤x ≤2 :q1 ( x )=∫1

x

p1 (z )dz=¿∫1

x

1+2 z dz=¿−2+x+x2 ¿¿

2≤x ≤4 :q2 ( x )=∫2

x

p2 ( z )dz+q1 (2 )=−7+6 x− 14x2

4 ≤x ≤5 :q3 ( x )=∫4

x

p3 ( z )dz+q2 (4 )=21−8x+ 32x2

12

Page 13: Web viewSynthetic division by a scalar c is related with a factor // binary operations. partial fraction // compound assignment (double d, integer n) // deconvolution

[100] 210 poly, FastRead-Tutorial by www.msharpmath.com

#> Q.spl(4.5) - Q.spl(2.5); ans = 8.9375

∫2.5

4

p2 (x )dx+∫4

4.5

p3 ( x )dx=[−7+6x−14x2]

2.5

4

+[21−8 x+32x2]

4

4.5

=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 1 0.625 2.0625 -0.6875 ] [ 2 4 -10.5 17.875 -6.5625 0.75 ] [ 4 5 89.5 -57.125 12.1875 -0.8125 ]

1≤x ≤2 : p1 ( x )=1+0.625 x+2.0625 x2−0.6875 x3

2≤x ≤4 : p2 (x )=−10.5+17.875 x−6.5625 x2+0.75 x3

4 ≤x ≤5 : p3 ( x )=89.5−57.125 x+12.1875 x2−0.8125 x3

#> P.splplot; // .spline(x,y).splplot;#> [ x', y' ].plot+ ;

#> P.splint.spl(4.5) - P.splint.spl(2.5) ; ans = 8.5068359

∫2.5

4

p2 (x )dx+∫4

4.5

p3 ( x )dx

¿∫2.5

4

−10.5+17.875 x−6.5625 x2+0.75x3dx

+∫4

4.5

89.5−57.125 x+12.1875 x2−0.8125 x3dx

13

Page 14: Web viewSynthetic division by a scalar c is related with a factor // binary operations. partial fraction // compound assignment (double d, integer n) // deconvolution

[100] 210 poly, FastRead-Tutorial by www.msharpmath.com

¿8.5068359

#> P.splint.splplot; // .spline(x,y) .splint .splplot; integrate

#> P.spldiff.splplot; // .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 p .maxentry // |p_i| >= all |p_j|

14

Page 15: Web viewSynthetic division by a scalar c is related with a factor // binary operations. partial fraction // compound assignment (double d, integer n) // deconvolution

[100] 210 poly, FastRead-Tutorial by www.msharpmath.com

p .mean // (sum p_i )/(n+1) p .min // p_i <= all p_j p .minentry // |p_i| <= all |p_j| p .n // n p .prod // (prod p_i )

#> p = .[ 3, 2, -5 ]; p = poly( -5 2 3 ) = 3x^2 +2x -5

#> p .maxentry; // |p_i| >= all |p_j| ans = -5

#> p .minentry; // |p_i| <= all |p_j| ans = 2

#> p .max; // p_i >= all p_j ans = 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 ; // highest order x^n ans = 2

#> p .len; // n+1 ans = 3

//----------------------------------------------------------------------// 'poly'-returning member functions//----------------------------------------------------------------------p .cumsum // p.~ = p(1)+p(2)+...+p(n), cumulative sum,

p .decimal // deviation from closest interger for coefficientsp .dfact // element-by-element double factorialp .diff // p` = p.' = p(x)-p(x-1), finite difference

p .even // remove odd terms, p[2i-1] = 0 p .fact // element-by-element factorial p .inv // inverse each coefficient, 1/p[i] p .invtay // multiply by i!, p[i] *= i! p .iratio // termwise rational approximation

15

Page 16: Web viewSynthetic division by a scalar c is related with a factor // binary operations. partial fraction // compound assignment (double d, integer n) // deconvolution

[100] 210 poly, FastRead-Tutorial by www.msharpmath.com

p .monic // monic polynomial, p[i] /= p[n] p .newton(u) // a0 +a1(x-u1) +a2(x-u1)(x-u2) + ... p .odd // remove even terms, p[2i] = 0 p .pm // alternating signs, (-1)^i *p[i] p .pow(k) // replace x by x^k, p[i*k] = p[i] p .quad(r,s) // divisor x^2 +rx +s by Bairstow method p .ratio // rational approximation p .rev // reverse order, p[n-i] p .round // the closest interger for each coefficient p .shift(k) // replace x by x-kp .syndiv(c) // p %% c, synthetic division coefficient

p .tay // divide by i!, p[i] /= i! p .trun(eps) // truncate if |a_i| < eps, eps=1.e-30 is default p .trun1 // truncate with eps=10^-1, p .trun2 // truncate with eps=10^-2, ... p .trun16 // truncate with eps=10^-16 p .up(k) // 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

#> 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 )

16

Page 17: Web viewSynthetic division by a scalar c is related with a factor // binary operations. partial fraction // compound assignment (double d, integer n) // deconvolution

[100] 210 poly, FastRead-Tutorial by www.msharpmath.com

= 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 2 0 0 1 ) = 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 p .syndiv(q) // p %% q, synthetic division by polynomial q

#> p = .[ 1,-10,23,6 ]; A = p .compan ; p = poly( 6 23 -10 1 ) = x^3 -10x^2 +23x +6 A = [ 10 -23 -6 ] [ 1 0 0 ] [ 0 1 0 ]

#> p[A] ; // matrix polynomial, a_n A^n + a_(n-1) A^(n-1) ... + ao I ans = [ 0 0 0 ] [ 0 0 0 ] [ 0 0 0 ]

17

Page 18: Web viewSynthetic division by a scalar c is related with a factor // binary operations. partial fraction // compound assignment (double d, integer n) // deconvolution

[100] 210 poly, FastRead-Tutorial by www.msharpmath.com

#> .[1,3,2] .solve ; // x^2 + 3x + 2 = 0 ans = [ -1 ] [ -2 ]

//----------------------------------------------------------------------// sequence and series//---------------------------------------------------------------------- sum 1 = n sum k = (1/2)n(n+1) sum k^2 = (1/6)n(n+1)(2n+1) sum k^3 = (1/4)n^2(n+1)^2 sum k^4 = (1/30)n(n+1)(2n+1)(3n^2+3n-1) sum k^5 = (1/12)n^2(n+1)^2(2n^2+2n-1) sum k^6 = (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, sum a_k = n ans = poly( 0 1 ) = x

#> .[ 1,0 ] .~ ; // a_k=k, sum a_k = (1/2)n(n+1) 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, sum a_k = (1/4)[n(n+1)]^2 ans = poly( 0 0 0.25 0.5 0.25 ) = 0.25x^4 +0.5x^3 +0.25x^2 ans = [ 0 ] [ 0 ] [ -1 ] [ -1 ]

#> p = .[1].up(9) ; // a_k=k^9 p = poly( 0 0 0 0 0 0 0 0 0 1 ) = x^9

#> q = p.~ .trun9; // trun(10^-9)

18

Page 19: Web viewSynthetic division by a scalar c is related with a factor // binary operations. partial fraction // compound assignment (double d, integer n) // deconvolution

[100] 210 poly, FastRead-Tutorial by www.msharpmath.com

q = poly( 0 0 -0.15 0 0.5 0 -0.7 0 0.75 0.5 0.1 ) = 0.1x^10 +0.5x^9 +0.75x^8 -0.7x^6 +0.5x^4 -0.15x^2

#> 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 - i 0.4427 ] [ -1.594 + i 0.4427 ] [ -1.618 ] [ 0.5936 - i 0.4427 ] [ 0.5936 + i 0.4427 ] [ -1 ] [ -1 ] [ 0.618 ]

//----------------------------------------------------------------------// 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) // Laguerre polynomial of degree n poly .binom(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!

19

Page 20: Web viewSynthetic division by a scalar c is related with a factor // binary operations. partial fraction // compound assignment (double d, integer n) // deconvolution

[100] 210 poly, FastRead-Tutorial by www.msharpmath.com

#> .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;

//----------------------------------------------------------------------// 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; poly T[n]; n = 10

#> for.i(0,4) T[i] = poly.T(i) ;; // Tchebyshev

20

Page 21: Web viewSynthetic division by a scalar c is related with a factor // binary operations. partial fraction // compound assignment (double d, integer n) // deconvolution

[100] 210 poly, FastRead-Tutorial by www.msharpmath.com

#> 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 4 ) = 4x^3 -3x ans = poly( 1 -0 -8 0 8 ) = 8x^4 -8x^2 +1

//----------------------------------------------------------------------// coefficient functions//---------------------------------------------------------------------- poly[N+1] .i ( f(i) ) // creates a polynomial with coefficient f(i) 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 outputpoly .left(string) // set the left delimiter for screen outputpoly .right(string) // set the right delimiter for screen outputpoly .maxcol(n=10) // 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

21

Page 22: Web viewSynthetic division by a scalar c is related with a factor // binary operations. partial fraction // compound assignment (double d, integer n) // deconvolution

[100] 210 poly, FastRead-Tutorial by www.msharpmath.com

+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 = x^3 +4x^2 +3x +2

#> q = poly(0:20); q = 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.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//----------------------------------------------------------------------

22