Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently...

47
[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com [100] 010 revised on 2012.12.03 cemmath The Simple is the Best Chapter 10 Polynomial 10-1 Class ‘poly’ 10-2 Operations for Polynomials 10-3 Evaluation of Polynomials 10-4 Subscripts 10-5 Piecewise Continuous Polynomials 10-6 Member Functions of Polynomial 10-7 Sequence and Series 10-8 Special Polynomials 10-9 Coefficient Functions 10-10 Summary ■ class functions. ■ ascending-order polynomial. ■ descending-order polynomial. ■ 'poly' by known roots. ■ Newton polynomial. ■ unary operations. ■ binary operations ■ compound assignments. ■ compound operator for deconvolution. ■ piecewise continuous polynomials. ■ ‘double’-returning. ■ ‘poly’-returning. ■ ‘matrix’-returning. 1

Transcript of Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently...

Page 1: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

[100] 010 revised on 2012.12.03 cemmath

The Simple is the Best

Chapter 10 Polynomial

10-1 Class ‘poly’10-2 Operations for Polynomials10-3 Evaluation of Polynomials10-4 Subscripts10-5 Piecewise Continuous Polynomials10-6 Member Functions of Polynomial10-7 Sequence and Series10-8 Special Polynomials10-9 Coefficient Functions10-10 Summary ■ class functions. ■ ascending-order polynomial. ■ descending-order polynomial. ■ 'poly' by known roots. ■ Newton polynomial. ■ unary operations. ■ binary operations ■ compound assignments. ■ compound operator for deconvolution. ■ piecewise continuous polynomials. ■ ‘double’-returning. ■ ‘poly’-returning. ■ ‘matrix’-returning. ■ special polynomials.

Polynomials are one of the most important data types in science and engineering. To accommodate this, Cemmath employs class ‘poly’ to handle a number of valuable operations of polynomials. Due to a structure similar to matrix, conversion from matrix to polynomial and vice versa can be easily

1

Page 2: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

performed just by assignment operation.

Section 10-1 Class ‘poly’

In order to treat polynomials efficiently, class ‘poly’ is innovated in Cemmath. This contains a large number of member functions to handle various operations of polynomials.

■ Syntax of poly. In Cemmath, polynomials are expressed by default in ascending order of power, i.e.

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

For practical reason, the maximum degree of polynomials is set to be 100, and a serious error will be encountered when this limit is exceeded.

The class ‘poly’ is used to create a polynomial, and the syntax is

poly(a0,a1,a2, …, an); // list of coefficientspoly( [a0,a1,a2,…,an] ); // a single matrixpoly( A ); // A is a matrix[a0,a1,a2,…,an] .apolyA .apoly

The zero polynomial is created simply by

poly() or .[]

#> poly(); ans = poly( 0 ) = 0#> 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 )

2

Page 3: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

= 5x^4 +4x^3 +3x^2 +2x +1

■ Descending-order polynomial. When a descending-order in power is preferred, either of the followings

[an, …,a2,a1,a0].dpoly; // general order, n >= 4 .[an, …,a2,a1,a0]; // general order, n >= 4

.[ a ] // constant polynomial

.[ a,b ] // 1st-order poly, line y = ax+b

.[ a,b,c ] // 2nd-order poly, parabola y = ax^2 + bx + c

.[ a,b,c,d ] // 3rd-order poly, cubic y = ax^3 + bx^2 + cx +d

poly(an, …,a2,a1,a0).rev;poly(A).rev;

can be used ('rev' is an abbreviation of reverse).

%> descending order by Matrix#> .[] ; // zero polynomial not a 0 x 0 matrix ans = poly( 0 ) = 0

#> [1,2] .dpoly; // 1st-order polynomial, line y = x + 2, root = -2

#> .[1,2] ; // a leading dot is equal to [].dpoly y = x + 2, root = -2

#> .[1,2,3] ; // 2nd-order poly, parabola y = x^2 + 2x + 3 roots [ -1 - i 1.414 ] [ -1 + i 1.414 ] vertex at ( -1, 2 ) y_minimum = 2 y-axis intersect ( 0, 3 ) symmetry at x = -1 directrix at y = 1.75 focus at ( -1, 2.25 )

#> .[1,2,3,4] ; // 3rd-order poly, cubic y = x^3 + 2x^2 + 3x + 4 roots

3

Page 4: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

[ -1.651 ] [ -0.1747 - i 1.547 ] [ -0.1747 + i 1.547 ] no maxima, no minima inflection pt. ( -0.666667, 2.59259 ) y-axis intersect ( 0, 4 )

#> .[1,2,3,4,5] ; // no information if order >= 4 ans = poly( 5 4 3 2 1 ) = x^4 +2x^3 +3x^2 +4x +5

#> (1:10).dpoly; // a leading dot is only for .[] grammar ans = poly( 10 9 8 7 6 5 4 3 2 1 ) = x^9 +2x^8 +3x^7 +4x^6 +5x^5 +6x^4 +7x^3 +8x^2 +9x +10

■ 'poly' by known roots. When roots of a polynomial are given, a polynomial can be obtained by

[ x1,x2, ... , xn ] .roots // (x-x1)(x-x2) .. (x-xn), .roots(x1,x2, ... , xn) // (x-x1)(x-x2) .. (x-xn).roots( A ) // A is a matrix including complex numbers

#> [ 0,1,2,3 ] .roots ; ans = poly( 0 -6 11 -6 1 ) = x^4 -6x^3 +11x^2 -6x

#> A = [ 1+1i; 1-1i ]; A = [ 1 + i 1 ] [ 1 - i 1 ]

#> .roots(A); ans = poly( 2 -2 1 ) = x^2 -2x +2

#> poly(A) ; // be careful ! This disregards complex parts ans = poly( 1 1 ) = x +1

■ Newton polynomial. A Newton polynomial is of the form

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

4

Page 5: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

+a3 ( x−x1 ) ( x−x2 )(x−x3)+… poly(a0,a1,...) .newton( [x0,x1,x2, ...] )

This special polynomial occurs in many applications including interpolation and curve-fitting. For example

%> 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

#> 1 + 2*[3].roots + 3*[3,4].roots + 4*[3,4,5].roots ; 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 ; p = poly( 2 ) = 2

#> (1:5).dpoly - (1:5).dpoly ; ans = poly( 0 )

= 0

■ Declaring variables as polynomials. Variables can be declared as polynomials by .[] or poly().

%> poly a,b,c,d; // can be alternatively written as#> a = b = c = d = .[] ;; // declaring variables as polynomials#> a; b; c; d; a = poly( 0 ) = 0 b = poly( 0 )

5

Page 6: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

= 0 c = poly( 0 ) = 0 d = poly( 0 ) = 0

■ Plot of polynomial. Since a polynomial is of course a kind of function, member function ‘plot’ draws a polynomial over an interval a ≤ x≤ b by the following syntax

.plot[n=101,g=1](a,b)

where preceding data must a polynomial, either named or unnamed. It is believed here that readers are familiar with the span operation ‘[n=101,g=1](a,b)’. An example is

%> plot of polynomial#> p = .[ 1, -7, 14, 5 ]; #> p.plot(0,5); // equivalent to plot.x(0,5) ( p(x) )

The resulting plot is shown in Figure 1.

Figure 1 Plot of a polynomial

■ Displaying Polynomials. Consider two polynomials

p ( x )=2+3 x+4 x2+ x3

q ( x )=x+2 x2+3 x3+…+19 x19+20 x20

6

Page 7: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

and create them by the following Cemmath commands

%> default display #> p = poly(2,3,4,1);#> q = poly(0:20);

The results shown on the screen are

p = poly( 2 3 4 1 ) = x^3 +4x^2 +3x +2

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

The maximum column length displayed on the screen is set by default to be 10 (excluding a constant coefficient a0). The maximum column length, the delimiters (i.e. a pair of parenthesis) and number format for polynomial can be changed, for example, by

%> display#> poly.format("%5.1f");#> poly.left (" "); // left delimiter blank#> poly.right(" "); // right delimiter blank#> poly.maxcol(5); // maximum column length#> p = poly(2,3,4,1);#> q = poly(0:20);

Then, blanks replace both ‘(’ and ‘)’, until delimiters are changed again. Also, the number format as well as the maximum column length will be kept until redefined. Thus, newly displayed results on the screen are

ans = 5p = 2.0 3.0 4.0 1.0 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

7

Page 8: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

Section 10-2 Operations for Polynomials

To explain the syntax for polynomial operations, representative polynomials p(x ), q (x) and r (x ) are used.

■ Unary Operations. The syntax for unary operations of polynomials is explained

|p| // termwise absolute-p // unary minus

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

The derivative (denoted by prime) and integration (denoted by tilde) can be repeated depending on the order. Several examples can be seen below.

%> unary operations #> p = .[1,-4,-3,2]; // polynomial not a matrix due to a leading dot p = poly( 2 -3 -4 1 ) = x^3 -4x^2 -3x +2

#> |p|; ans = poly( 2 3 4 1 )#> -p; ans = poly( -2 3 4 -1 )#> p' ; ans = poly( -3 -8 3 )#> p'' ; ans = poly( -8 6 )#> p''' ; ans = poly( 6 )#> p~ ; ans = poly( 0 2 -1.5 -1.33333 0.25 )#> p~~ ; ans = poly( 0 0 1 -0.5 -0.333333 0.05 )#> p~~~ ; ans = poly( 0 0 0 0.333333 -0.125 -0.0666667 0.00833333 )

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

8

Page 9: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

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

It is noteworthy that the integration constants are always set to be zero.

■ Binary Operations with Scalars. Polynomials can operate with ‘double’ through binary operations

p + d, d + p // additionp - d, d - p // subtractionp * d, d * p // multiplicationp / d, d \ p // right and left divisions p ^ n // right power onlyp .^ d, d .^ p // element-by-element power

p %% d // synthetic division

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

■ Synthetic Division. A synthetic division is defined as

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

p ( x )

9

Page 10: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

the coefficients are determined by comparing two expressions such that

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

¿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

can be confirmed by

%> synthetic division #> p = .[1,4,3,2]; p = poly( 2 3 4 1 ) = x^3 +4x^2 +3x +2

#> p %% -3; 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

■ Binary Operations. Binary operations between polynomials are also presented

p + q // addition p - q // subtraction p * q // multiplication p \ q // quotient (left division)p / q // quotient (right division) p % q // remainder p %% q // synthetic division

p == q // conditional equality p != q // conditional inequality

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

10

Page 11: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

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

In Cemmath, deconvolution of polynomials a=bq+r can be found efficiently by using a compound operator ‘/%’, similar to the class ‘double’. The syntax is

(q,r) = a /% b;

This can be confirmed by

%> deconvolution

11

Page 12: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

#> a = .[1,4,3,2]; b = .[1,1,3]; a = poly( 2 3 4 1 ) = x^3 +4x^2 +3x +2 b = poly( 3 1 1 ) = x^2 +x +3#> (q,r) = a /% b; // a = bq + r, x^3+4x^2+3x+2 = (x^2+x+3)(x+3) -3x-7 q = poly( 3 1 ) = x +3 r = poly( -7 -3 ) = -3x -7

The synthetic division by higher degrees can be also treated by an operator ‘%%’. For example

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)

An operator ‘%%’ for this case can be applied

%> synthetic division (degree >= 2)

#> 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]; // equal to q = poly(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 ]

#> -p + A.row(1).apoly+ A.row(2).apoly*q + A.row(3).apoly*q^2; ans = poly( 0 )

12

Page 13: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

= 0

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

The final command confirms that the partial fraction is correct. Note that a matrix is returned by p instead of polynomial array.

■ Compound Assignments. Compound assignments such as

p+¿q⇒

p=p+q

are important in C language, and they are implemented in Cemmath as well. The syntax for compound assignments are

p += q // p = p + q p -= q // p = p - q p *= q // p = p * q p /= q // p = p / q p %= q // p = p % q

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

where the left-operand must be l-value polynomial (i.e. a named variable that can store a polynomial).

Section 10-3 Evaluation of Polynomials

Evaluation of polynomials is performed as

p ( x ) // doublep ( z ) // complexp ( A ) // matrix (element-by-element)p [ A ] // matrix polynomial

13

Page 14: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

p ( q ) // polynomial (composition of polynomial)

Whenever the final data type is polynomial, evaluation of polynomials is always possible. For example

( p+q ) ( x ) , ( p ''+q ' ) ( x ) ,( p / q)(x)

are valid expressions, when p and q are polynomials.

■ Scalar Argument. Evaluation of polynomials with scalar is tested below.

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

#> p(2); ans = 32 // p=(2,3,4,1) 2 + 3*2 + 4*4 + 8#> p'(2); ans = 31 // p'=(3,8,3) 3 + 8*2 + 3*4 #> p''(2); ans = 20 // p''=(8,6) 8 + 6*2#> p~(2); ans = 24.6666667 // p~ = (0,2,3/2,4/3,1/4) #> p~~(2); ans = 14.9333333#> (p+q)(2); ans = 41 // p+q=(5,4,5,1) 5 + 4*2 + 5*4 + 8#> (p''+q')(2); ans = 25 // p''+q'=(8+1,6+2) 9 + 8*2#> (p/q)(2); ans = 5 // p/q = (3,1) 3 + 2

■ Complex Argument. For a complex number z, p(z ) returns a complex evaluation. For example,

%> complex argument #> q = .[1,1,3]; #> q(1+2!); ans = 1 + 6!

■ Matrix Argument. For a polynomial p(x ) and a matrix A, p( A) in Cemmath represents an element-by-element evaluation

14

Page 15: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

A=[a11 a12 a13

a21 a22 a23 ] , p( A)=[ p ( a11 ) p (a12) p (a13 )p ( a21) p (a22) p (a23 )]

Using both unnamed matrix and unnamed polynomial, an example is

%> element-by-element evaluation #> .[1,1,3] ( [ 1,2; 3,4 ] ); // poly(3,1,1) ( [ 1,2; 3,4 ] );ans = [ 5 9 ] [ 15 23 ]

Meanwhile, a matrix polynomial is defined as

p ( x )=a0 I+a1 A+a2 A2+a3 A3+…+an An

where matrix A must be square, and matrix I is a unity matrix. This is completely different from the element-by-element evaluation, and is calculated by

p[A]

For a simple case

A=[1 23 4 ] , p ( x )=3+x+x2

this can be illustrated as

%> matrix polynomial #> p = .[1,1,3]; // polynomial#> A = [1,2; 3,4]; // matrix#> p(A); // element-by-element#> p[A]; // matrix polynomialA = [ 1 2 ] [ 3 4 ] ans = [ 5 9 ] [ 15 23 ] ans =

15

Page 16: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

[ 11 12 ] [ 18 29 ]

■ Polynomial Argument. A polynomial can have a polynomial as its arguments. For example,

p ( x )=2+3 x+4 x2+ x3 , q ( x )=3+x+x2

can be used to generate another polynomial

p '' ( q ' )=8+6 (1+2 x ) , p ''=8+6 x , q '=1+2 x

We can examine such a polynomial evaluation by

%> polynomial argument #> p = .[1,4,3,2]; q = .[1,1,3]; #> p''(q');#> p''(q')(3);ans = poly( 14 12 )ans = 50.0000000

Note that unnamed polynomials can always replace the above expressions, for example

%> polynomial argument #> poly(2,3,4,1)'' ( poly(3,1,1)' ); ans = poly( 14 12 )#> .[1,4,3,2]'' ( .[1,1,3]' ); ans = poly( 14 12 )

Section 10-4 Subscripts

■ Reference to Element. For a polynomial of the form

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

p = poly(a0,a1,a2, …, an); p = [a0,a1,a2, …, an].apoly;

16

Page 17: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

subscripting has a syntax

p [ i ]

to refer the i-th element of the polynomial. However, an integer close to the given index is used, i.e. p[12.35] is interpreted as p[12]. The constant coefficient a0 is denoted by p[0], while the coeffient of the highest power is denoted by p[end]. Examples are as follows.

%> subscript #> p = .[1,4,3,2];; // x^3 + 4x^2 + 3x + 2 #> p[0]; ans = 2#> p[1]; ans = 3#> p[end-1]; ans = 4#> p[end]; ans = 1#> p[end] = 17;; p; p = poly( 2 3 4 17 )

■ Compound Operations. For the coefficients of a polynomial, compound operations are also available

%> compound operations for subscript #> p = poly(0:5); p = poly( 0 1 2 3 4 5 )#> p[0] += 100; p; p = poly( 100 1 2 3 4 5 )#> p[1] -= 100; p; p = poly( 100 -99 2 3 4 5 )#> p[2] *= 100; p; p = poly( 100 -99 200 3 4 5 )#> p[3] /= 100; p; p = poly( 100 -99 200 0.03 4 5 )#> p[4] ^= 5; p; p = poly( 100 -99 200 0.03 1024 5 )

Section 10-5 Piecewise Continuous Polynomials

■ Piecewise Continuous Polynomials. Piecewise continuous polynomials are defined with a number of polynomials and corresponding intervals

x11 ≤ x ≤ x12 : p1 ( x )=a10+a11 x+a12 x2+…+a1 n xn+…x21≤ x ≤ x22 : p2 ( x )=a20+a21 x+a22 x2+…+a2 n xn+…

xm 1≤ x ≤ xm2: pm ( x )=am0+am1 x+am2 x2+…+amn xn+…

17

Page 18: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

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.

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

18

Page 19: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

[ 4 5 -8 3 ]

1 ≤ x ≤2 : p1 ( x )=1+2 x2 ≤ x ≤ 4 : p2 (x )=6−0.5 x4 ≤ 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−14

x2

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

x

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

x2

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

∫2.5

4

p2 (x ) dx+∫4

4.5

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

x2]2.5

4

+[21−8 x+32

x2]4

4.5

=8.9375

19

Page 20: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

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.75x3 dx

+∫4

4.5

89.5−57.125 x+12.1875 x2−0.8125 x3 dx

¿8.5068359

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

20

Page 21: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

#> P.spldiff.splplot; // .spline(x,y) .spldiff .splplot; differentiate

Section 10-6 Member Functions of Polynomial

In this section, we discuss a variety of member functions, although some of them were already addressed. All over this section, it is supposed that a polynomial and its Cemmath variable

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

p = poly(a0,a1,a2, …, an);

are defined a priori.

■ ‘double’-Returning. Member functions which return ‘double’ are listed below. The mathematical expression on the right-side explains straightforward the meaning of each member function. Therefore, no further discussion is made in that case.

p.denom(nmax=10000) returns an integer denominator up to nmax p.len n+1p.max max \{ai \}p.maxentry max \{|ai |\}

21

Page 22: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

p.mean ∑i=0

n

ai /(n+1)

p.min min \{ai \}p.minentry min \{|a i |\}p.n n

p.prod ∏i=0

n

ai

p.sum ∑i=0

n

ai

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

#> p.maxentry; ans = -5#> p.minentry; ans = 2#> p.max; ans = 3#> p.min; ans = -5#> p.mean; ans = 0#> p.prod; ans = -30#> p.n; ans = 2#> p.len; ans = 3

■ ‘poly’-Returning. Member functions returning polynomials are

p.cumsum ∑ p=p . ,cumulative sump.decimal deviation from closest interger p.dfact element-by-element double factorialp.diff Δ p= p .'=p , finite difference

p.even a0+a2 x2+a4 x4+a6 x6+…p.fact element-by-element factorial p.inv 1 / a0+(1 / a1) x+(1/ a2 ) x2+…+¿p.invtay a0+a1 x+2 !a2 x2+3 ! a3 x3+…+n! an xn

p.iratio shows a rational approximation of each coefficientp.monic ¿¿

p.newton(x) a0+a1 ( x−x1)+a2 ( x−x1) ( x−x2 )+…

22

Page 23: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

p.odd a1 x+a3 x3+a5 x5+a7 x7+…p.pm a0−a1 x+a2 x2−a3 x3+…+ (−1 )n an xn

p.pow(k) a0+a1 xk+a2 x2 k+a3 x3 k …+an xnk

p.quad(r,s) divisor x2+rx+s by Bairstow methodp.ratio shows a rational approximationp.rev an+an−1 x+an−2 x2+an−3 x3+…+a0 xn

p.round the closest interger for each coefficientp.shift(c) a0+a1 (x−c )+a2 ( x−c )2+…+an(x−c )n

p.syndiv(c) p %% c = p.syndiv(c)p.tay a0+a1 x+a2 x2 / 2 !+a3 x3 / 3 !+…+an xn / n !p.trun(eps=1.e-30) // truncate a i, i.e. make a i=0 if |ai |<ϵp.trun1, ..., trun16 // truncate a i with ϵ=10−1 ,…, 10−16

p.up(k) a0 xk+a1 x1+k+a2 x2+k+…+an xn+k

The resulting mathematical expression explains the meaning straightforward. Some of member functions depend on the order of appearance. For example, member functions ‘even’ and ‘shift’ are not commutative.

#> 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.up(3) ; // multiplied by x^3 ans = poly( 0 0 0 5 4 3 2 1 )

23

Page 24: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, 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

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

#> q = p.shift(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

The first seven terms of the exponential function

ex=1+ x1 !

+ x2

2 !+ x3

3 !+...+ xn

n !+…

can be chosen easily by

%> inv/tay/iratio #> poly(0:6).fact .invans = poly( 1 1 0.5 0.166667 0.0416667 0.00833333 0.00138889 )

#> poly[0,6].n (1).tay;ans = poly( 1 1 0.5 0.166667 0.0416667 0.00833333 0.00138889 )

#> poly[0,6].n (1).tay.iratio;ans = poly( 1 1 1/2 1/6 1/24 1/120 1/720 )

#> poly[0,6].n (1).tay.inv;ans = poly( 1 1 2 6 24 120 720 )

where the third command is to confirm the value of factorials. ◀

24

Page 25: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

Deviation from the integer closest to each coefficient can be obtained by ‘decimal’, for example

%> decimal less than 1 #> poly.P(5).ratio;ans = (1/8) * poly( 0 15 0 -70 0 63 )

#> poly.P(5);ans = poly( 0 1.875 0 -8.75 0 7.875 )

#> poly.P(5).round;ans = poly( 0 2 0 -9 0 8 )

#> poly.P(5).decimal;ans = poly( 0 -0.125 0 0.25 0 -0.125 )

#> poly.P(5).round + poly.P(5).decimal;ans = poly( 0 1.875 0 -8.75 0 7.875 )

The last line shows the relation between ‘round’ and ‘decimal’. ◀

■ ‘matrix’-Returning. Member functions returning matrix are

p.compan // companion matrix whose eigenvalues are the roots of p(x)p.solve // solution matrixp.syndiv(q) // p %% q , synthetic division by polynomial q

A companion matrix A of a polynomial p(x ) has eigen-polynomial which is exactly the same as p(x ).

A companion matrix of p ( x )=x3−10 x2+23 x+6 is determined by using the coefficients

A=[10 −23 −61 0 00 1 0 ]

This can be confirmed by

25

Page 26: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

%> companion matrix #> p = .[ 1, -10, 23, 6 ]; p = poly( 6 23 -10 1 )

#> A = [ 10,-23,-6 ; 1; 0,1 ]; A = [ 10 -23 -6 ] [ 1 0 0 ] [ 0 1 0 ] #> p.compan;ans = [ 10 -23 -6 ] [ 1 0 0 ] [ 0 1 0 ]

#> A.eigpoly;ans = poly( 6 23 -10 1 )

#> p.compan.eigpoly; // equal to pans = poly( 6 23 -10 1 )

#> A.eigpoly.compan; // equal to Aans = [ 10 -23 -6 ][ 1 0 0 ] [ 0 1 0 ]

We can observe an interesting relation

p = p.compan.eigpoly A = A.eigpoly.compan

due to dual nature of operations between polynomial and matrix. ◀

Section 10-7 Sequence and Series

■ Definition. A sequence \{ai \}

\{ ai \}=a1 , a2 , a3 , …

is defined as an array. A series is defined as a sum of a sequence

26

Page 27: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

Sn=∑ai=a1+a2+a3+…+an

Polynomial sequence means that ak is a polynomial of k For example,

ak=2 k3−5 k2+3 k−1

is a polynomial sequence.

■ Δ p=p.'=p`=p.diff, finite difference. Finite difference of p(x ) is defined

as

Δ p=p .'=p =p . diff =p (n )−p (n−1) For example, p (n )=n2 has a finite difference

Δ p=p (n )−p (n−1 )=n2− (n−1 )2=2n−1

#> p = poly(0,0,1); p.' ; p = poly( 0 0 1 ) = x^2 ans = poly( -1 2 ) = 2x -1

■ ∑ p= p.~ = p.cumsum, cumulative sum. Cumulative sum of p(x ) is

defined as

∑ p ( x )=p . =p . cumsum=p (1 )+ p (2 )+ p (3 )+…+ p(n)

For example, series sums for particular powers are

Σ1=nΣ k=(1 / 2 )n (n+1)Σ k2=(1 /6 ) n ( n+1 )(2n+1)

27

Page 28: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

Σ k3=(1 / 4 )n2 (n+1 )2

Σ k4=(1 / 30 ) n (n+1 ) (2n+1 )(3n2+3 n−1)Σ k5=(1 /12 )n2 (n+1 )2(2 n2+2 n−1)Σ k6=(1 / 42 ) n (n+1 ) (2 n+1 )(3n4+6 n3−3 n+1)Σ k7=(1 / 24 ) n2 (n+1 )2(3 n4+6 n3−n2−4 n+2)Σ k8=(1 / 90 ) n (n+1 ) (2 n+1 )(5 n6+15 n5+5 n4−15n3−n2+9n−3)Σ k9=(1 / 20 ) n2 (n+1 )2 (n2+n−1 )(2 n4+4 n3−n2−3n+3)

#> poly(1).~ ; // a_k = 1, n#> .[ 1,0 ].~ ; // a_k=k, (1/2)n(n+1)ans = poly( 0 1 )ans = poly( 0 0.5 0.5 )

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

#> p = .[1].up(9) ; // a_k=k^9#> q = p.~ .trun9; // trun(10^-9)#> q.ratio ;p = poly( 0 0 0 0 0 0 0 0 0 1 )q = poly( 0 0 -0.15 0 0.5 0 -0.7 0 0.75 0.5 0.1 )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 ]

28

Page 29: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

[ 0.5936 + i 0.4427 ] [ -1 ] [ -1 ] [ 0.618 ]

■ Dual nature of Δ p=p .'=p and Σ p=p . . Finite differnce and cumulative sum of a polynomial is inherently of dual nature.

#> p = .[1,0,0,0] ; ans = poly( 0 0 0 1 )#> p.~ .' ; ans = poly( 0 0 0 1 )#> p.' .~ ; ans = poly( 0 0 0 1 )

Section 10-8 Special Polynomials

Special polynomials famous in mathematics are treated as the class functions the syntax of which are

poly.T(n) // Tchebyshevpoly.P(n) // Legendrepoly.H(n) // Hermitepoly.L(n) // Laguerrepoly.binom(n) // binomial

■ Tchebyshev Polynomials. The Tchebyshev polynomial of degree n is defined for −1≤ x ≤1 and can be obtained from the following recurrence relation

T 0 ( x )=1 , T 1 ( x )=x ,T n (x )=2 x Tn−1 ( x )−T n−2(x )

For example, the command

%> Tchebyshev polynomial #> poly.T(5); ans = poly( 0 5 -0 -20 0 16 ) = 16x^5 -20x^3 +5x

indeed shows that T 5 ( x )=5 x−20 x3+16 x5. Also for −1 ≤ x ≤1, ten curves are created by

29

Page 30: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

%> plot Tchebyshev polynomials #> .hold; for.n(0,9) poly.T(n).plot[501](-1,1); plot;

and are plotted in Figure 2.

Figure 2 Tchebyshev polynomials

■ Other polynomials. Listed below are the recurrence relations for the Legendre, Hermite, Laguerre polynomials of degree n

P0 ( x )=1 , P1 ( x )=xPn ( x )=(1 / n ) [ (2 n−1 ) x Pn−1 ( x )−(n−1 ) Pn−2(x )]

H 0 ( x )=1 ,H1 ( x )=2xH n ( x )=2 x H n−1 ( x )−2 (n−1 ) H n−2(x )

L0 ( x )=1 , L1 (x )=1−xLn ( x )=(2 n−x−1 ) Ln−1 ( x )−(n−1 )2 Ln−2(x )

where the range of x is −1≤ x ≤1 for the Legendre polynomials, and −∞<x<∞ for others. Figure 3 illustrates a few Legendre polynomials drawn by

%> plot Legendre polynomials #> .hold; for.n(0,10) plot.x(-1,1) ( .P_n(x) ); plot;

30

Page 31: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

Figure 3 Legendre polynomials

■ Binomial Polynomials. The binomial polynomials of degree n is defined as

(sn)= 1

n !s (s−1 ) (s−2 ) ×…× (s−n−2 )(s−n−1)

Although this polynomial can be easily created by

#> ( 0:(n-1) ).roots /n.fact;

we introduce a unique class-function due to its importance in numerical theories. The corresponding syntax is for example

%> binomial polynomial #> (0:4).roots / 5.fact;#> poly.binom(5); ans = poly( 0 0.2 -0.416667 0.291667 -0.0833333 0.00833333 )ans = poly( 0 0.2 -0.416667 0.291667 -0.0833333 0.00833333 )

In order to list polynomials in ratio, member functions ‘ratio’ and ‘iratio’ are utilized

%> binomial polynomial, ratio#> poly.binom(5) .ratio; ans = (1/120) * poly( 0 24 -50 35 -10 1 )#> poly.binom(5)'.ratio; ans = (1/120) * poly( 24 -100 105 -40 5 )

#> poly.binom(5)~.ratio;

31

Page 32: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

ans = (1/1440) * poly( 0 0 144 -200 105 -24 2 )

and

%> binomial polynomial, iratio #> poly.binom(5) .iratio; ans = poly( 0 1/5 -5/12 7/24 -1/12 1/120 )

#> poly.binom(5)'.iratio; ans = poly( 1/5 -5/6 7/8 -1/3 1/24 )

#> poly.binom(5)~.iratio; ans = poly( 0 0 1/10 -5/36 7/96 -1/60 1/720 )

In the above example, three polynomials have three common denomenators (120, 120 and 1440). These integer denomenators can be extracted by a member function ‘denom’. For example,

%> binomial polynomial, denom #> poly.binom(5) .denom; ans = 120.0000000 #> poly.binom(5)'.denom; ans = 120.0000000#> poly.binom(5)~.denom; ans = 1440.0000000

Section 10-9 Coefficient Functions

■ Coefficient Function. The coefficients of a polynomial can be expressed a function pi=f (i)

p ( x )=∑i=0

n

f (i ) xi=f (0 )+ f (1 ) x1+…+ f (n ) xn

which can be created by the following syntax

p = poly[ n+1 ].i ( f(i) )

or equivalently

p = poly[ 0,n ].i ( f(i) )

32

Page 33: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

A general syntax for the coefficient function is

p ( x )=f (a ) xa+ f (a+m ) xa+m+f (a+2m) xa+2m+…p = poly[ a,b, m=1 ] .i ( f(i) )

where all other coefficients are zeros. For example, the following polynomials

p ( x )=1+ x3+ x2

5+ x3

7+ x4

9+ x5

11

can be easily created by

%> coefficient function#> poly[6] .i ( 1/(2*i+1) ).iratio;#> poly[0,5].i ( 1/(2*i+1) ).iratio;ans = poly( 1 1/3 1/5 1/7 1/9 1/11 )ans = poly( 1 1/3 1/5 1/7 1/9 1/11 )

It happens frequently that only even/odd powers of a polynomial are nonzero, e.g.

p1 (x )=1−2 x2+4 x4−6 x6+8 x8−10 x10+12 x12

p2 (x )=x− x3

3+ x5

5− x7

7+ x9

9− x11

11+ x13

13

These two polynomials can be created by

%> coefficient functions#> 1 + poly[ 2,12,2 ].i ( (i-1).pm * i );#> poly[ 1,13,2 ].i ( ((i-1)/2).pm / i ).iratio;ans = poly( 1 0 -2 0 4 0 -6 0 8 0 -10 0 12 )ans = poly( 0 1 0 -1/3 0 1/5 0 -1/7 0 1/9 0 -1/11 0 1/13 )

Note that an extra constant a0=1 is required for the first polynomial. ◀

Section 10-10 Summary

33

Page 34: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

■ class functions.

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 .roots( … ) creates a polynomial with prescribed roots

■ ascending-order polynomial.poly(a0,a1,a2, …, an); // list of coefficientspoly( [a0,a1,a2,…,an] ); // a single matrixpoly( A ); // A is a matrix[a0,a1,a2,…,an].apolyA.apoly

■ descending-order polynomial. [an, …,a2,a1,a0].dpoly; // general order, n >= 4 .[an, …,a2,a1,a0]; // general order, n >= 4

.[ a ] // constant polynomial

.[ a,b ] // 1st-order poly, line y = ax+b

.[ a,b,c ] // 2nd-order poly, parabola y = ax^2 + bx + c

.[ a,b,c,d ] // 3rd-order poly, cubic y = ax^3 + bx^2 + cx +d poly(an, …,a2,a1,a0).rev;

■ 'poly' by known roots. [ x1,x2, ... , xn ] .roots // (x-x1)(x-x2) .. (x-xn), .roots(x1,x2, ... , xn) // (x-x1)(x-x2) .. (x-xn) .roots( A ) // A is a matrix including complex numbers

■ Newton polynomial. The Newton polynomial is of the form

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

poly(a0,a1,...) .newton( [x0,x1,x2, ...] )

■ unary operations. |p| // termwise absolute-p // unary minus

p' // differentiation, d(p(x))/dx p~ // integration p.' = p` // p(x)-p(x-1), finite-difference, p.' = p.diff = p`

34

Page 35: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

p.~ // p(1)+p(2)+...+p(n), cumulative sum p.~ = p.cumsum

■ binary operationsp + d, d + p // additionp - d, d - p // subtractionp * d, d * p // multiplicationp / d, d \ p // right and left divisions p ^ n // right power onlyp .^ d, d .^ p // element-by-element power

p %% d // synthetic division

p + q // addition p - q // subtraction p * q // multiplication p \ q // quotient (left division)p / q // quotient (right division) p % q // remainder p %% q // synthetic division

p == q // conditional equality p != q // conditional inequality

■ compound assignments. p += q // p = p + q p -= q // p = p - q p *= q // p = p * q p /= q // p = p / q p %= q // p = p % q

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

■ compound operator for deconvolution.

(q,r) = a /% b

■ piecewise continuous polynomials.

// piecewise polynomials over a number of intervals

35

Page 36: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

// x11<=x<=x12: p1(x)// x21<=x<=x22: p2(x)// ...// xn1<=x<=xn2: pn(x)// is stacked as a spline matrix in which // the first two columns are intervals// [ x11, x12, p1 ]// A = [ x21, x22, p2 ]// [ ... ]// [ xn1, xn2, pn ] 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

■ ‘double’-returning. p.denom(nmax=10000) returns an integer denominator up to nmax p.len n+1p.max max \{ai \}p.maxentry max \{|ai |\}

p.mean ∑i=0

n

ai /(n+1)

p.min min \{ai \}p.minentry min \{|a i |\}p.n n

p.prod ∏i=0

n

ai

p.sum ∑i=0

n

ai

■ ‘poly’-returning. p.cumsum ∑ p= p . cumulative sump.decimal deviation from closest interger p.dfact element-by-element double factorialp.diff Δ p= p .'=p , finite difference

p.even a0+a2 x2+a4 x4+a6 x6+…p.fact element-by-element factorial

36

Page 37: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

p.inv 1 / a0+(1 / a1) x+(1 / a2 ) x2+…+¿p.invtay a0+a1 x+2 !a2 x2+3 ! a3 x3+…+n! an xn

p.iratio shows a rational approximation of each coefficientp.monic ¿¿

p.newton(x) a0+a1 ( x−x1)+a2 ( x−x1) ( x−x2 )+…

p.odd a1 x+a3 x3+a5 x5+a7 x7+…p.pm a0−a1 x+a2 x2−a3 x3+…+ (−1 )n an xn

p.pow(k) a0+a1 xk+a2 x2 k+a3 x3 k …+an xnk

p.quad(r,s) divisor x2+rx+s by Bairstow methodp.ratio shows a rational approximationp.rev an+an−1 x+an−2 x2+an−3 x3+…+a0 xn

p.round the closest interger for each coefficientp.shift(c) a0+a1 (x−c )+a2 ( x−c )2+…+an(x−c )n

p.syndiv(c) p %% c = p.syndiv(c)p.tay a0+a1 x+a2 x2 / 2 !+a3 x3 / 3 !+…+an xn / n !p.trun(eps=1.e-30) // truncate a i, i.e. make a i=0 if |ai |<ϵp.trun1 ,... , trun16 // truncate a i with ϵ=10−1 ,…,10−16

p.up(k) a0 xk+a1 x1+k+a2 x2+k+…+an xn+k

■ ‘matrix’-returning. p.compan // companion matrix whose eigenvalues are the roots of p(x)p.solve // solution matrixp.syndiv(q) // p %% q , synthetic division by polynomial q

■ special polynomials.

poly[n+1] .i ( f(i) ) = f(0) + f(1)x + f(2)x^2 + …poly[0,n] .i ( f(i) ) = f(0) + f(1)x + f(2)x^2 + …poly[a,b,m=1] .i ( f(i) ) = f(a)x^a + f(a+m)x^(a+m) + …

poly.T(n) = a_0 + a_1 x + a_2 x^2 + .. + a_n x^npoly.P(n) = a_0 + a_1 x + a_2 x^2 + .. + a_n x^npoly.H(n) = a_0 + a_1 x + a_2 x^2 + .. + a_n x^npoly.L(n) = a_0 + a_1 x + a_2 x^2 + .. + a_n x^npoly.binom(n) = a_0 + a_1 x + a_2 x^2 + .. + a_n x^n

37

Page 38: Web view · 2013-05-04Finite differnce and cumulative sum of a polynomial is inherently of dual nature.#> p = .[1,0,0,0] ; ans = poly ... p.dfact element-by-element double factorial.

[100] 010 Chapter 10 Polynomial, Tutorial by www.msharpmath.com

//----------------------------------------------------------------------// end of file//----------------------------------------------------------------------

38