lec11.mws

L-11 MCS 320 Friday 16 September 2005

> restart;

1. Rational Expressions

Maple recognizes rational polynomials as a separate type:

> p := x^3 - 1; q := x^2 - 1;

p := x^3-1

q := x^2-1

> f := p/q;

f := (x^3-1)/(x^2-1)

> whattype(f); type(f,ratpoly);

*

true

For rational numbers, Maple automatically divides numerator and denominator by their common divisor:

> 246/34;

123/17

This is done because the results is always simpler.  But this is not the case for rational polynomials.

> normal(f);

(x^2+x+1)/(x+1)

The normal form of a rational polynomial is obtained after removing the common divisors.

There are several formats :

> factor(numer(f))/denom(f);

(x-1)*(x^2+x+1)/(x^2-1)

> numer(f)/factor(denom(f));

(x^3-1)/((x-1)*(x+1))

> factor(numer(f))/factor(denom(f));

(x^2+x+1)/(x+1)

In the last case, the division by the common factor was a simplification.

> nf := factor(numer(f));

nf := (x-1)*(x^2+x+1)

> ff := freeze(nf)/factor(denom(f));

ff := freeze/R0/((x-1)*(x+1))

By the freeze command, the numerator remained and was not divided by x-1.

> thaw(ff); # this "unfreezes" the numerator

(x^2+x+1)/(x+1)

> q := (x^1000 - 1)/(x-1);

q := (x^1000-1)/(x-1)

This is an example, where the 1000 can be made even larger to demonstrate why Maple does not normalize rational polynomials automatically.

> normal(q); # this leads to 1000 monomials

1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...1+x^3+x^2+x+x^4+x^6+x^5+x^999+x^997+x^998+x^996+x^995+x^994+x^993+x^992+x^991+x^990+x^989+x^988+x^987+x^986+x^985+x^984+x^983+x^982+x^981+x^980+x^979+x^978+x^977+x^976+x^975+x^974+x^973+x^972+x^971+x^...

This is an instance where Maple is careful to avoid expression swell.

2. Conversions

For univariate polynomials, to evaluate efficiently a dense polynomial, we may want to convert to a Horner form.

> p := randpoly(x,degree=7,dense);

p := 56-85*x^7-55*x^6-37*x^5-35*x^4+97*x^3+50*x^2+79*x

> codegen[cost](p)

7*additions+28*multiplications

> ;

> h := convert(p,horner)

h := 56+(79+(50+(97+(-35+(-37+(-55-85*x)*x)*x)*x)*x)*x)*x

> ;

> codegen[cost](h);

7*additions+7*multiplications

> codegen[C](p,'optimized');

     t1 = x*x;
     t2 = t1*x;

     t3 = t1*t1;

     t14 = 56.0-85.0*t3*t2-55.0*t3*t1-37.0*t3*x-35.0*t3+97.0*t2+50.0*t1+79.0*x

;

Here the "optimized" codegeneration via repeated squaring does not produce an optimal way to evaluate.  In general, when sorting the monomials in p in ascending order, it takes twice as many multiplications as additions.  The repeated squaring technique for powers is only recommended for "sparse" polynomials with very few terms.  For dense polynomials, Horner's scheme is optimal.

> r := randpoly(x,degree=5,dense)/randpoly(x,degree=5,dense):

> codegen[cost](r);

10*additions+30*multiplications+divisions

> rh := convert(numer(r),horner)/convert(denom(r),horner):

> codegen[cost](rh);

10*additions+10*multiplications+divisions

> rc := convert(r,`confrac`,x):

> codegen[cost](rc);

10*additions+5*divisions

Via continued fractions, we have traded in multiplications for divisions.  And since divisions cost about as much as multiplications, we have gained.

> rc;

-49/93-10367/8649/(x-1642708/964131+169537484/107474689/(x+732375739398/439398774157+4403225959377553/7185739620262564/(x-37845883244738499738/18002118516358554589+1197739516204071072049387/7215986956...-49/93-10367/8649/(x-1642708/964131+169537484/107474689/(x+732375739398/439398774157+4403225959377553/7185739620262564/(x-37845883244738499738/18002118516358554589+1197739516204071072049387/7215986956...-49/93-10367/8649/(x-1642708/964131+169537484/107474689/(x+732375739398/439398774157+4403225959377553/7185739620262564/(x-37845883244738499738/18002118516358554589+1197739516204071072049387/7215986956...-49/93-10367/8649/(x-1642708/964131+169537484/107474689/(x+732375739398/439398774157+4403225959377553/7185739620262564/(x-37845883244738499738/18002118516358554589+1197739516204071072049387/7215986956...-49/93-10367/8649/(x-1642708/964131+169537484/107474689/(x+732375739398/439398774157+4403225959377553/7185739620262564/(x-37845883244738499738/18002118516358554589+1197739516204071072049387/7215986956...-49/93-10367/8649/(x-1642708/964131+169537484/107474689/(x+732375739398/439398774157+4403225959377553/7185739620262564/(x-37845883244738499738/18002118516358554589+1197739516204071072049387/7215986956...

> convert(f,`confrac`,x); # a nice representation of our first f

x+1/(x+1)

The last form we will see today is the conversion to partial fractions:

> convert(r,`parfrac`,x);

-49/93+1/93*(-2490-10367*x^4-7408*x^3+8525*x^2-7958*x)/(-66+93*x^5-92*x^4-43*x^3+62*x^2-77*x)

We do not see that much because the polynomials do not factor over the rational numbers.

> convert(r,`parfrac`,x,complex); # numerical conversion

-49/93+(-.9292224370+0.2196785755e-10*I)/(x-1.435140904)+(0.6651200808e-1-.9046746939*I)/(x+(.6958467566+.1047206754*I))+(0.6651200788e-1+.9046746938*I)/(x+(.6958467566-.1047206754*I))+(-.2012186296-....-49/93+(-.9292224370+0.2196785755e-10*I)/(x-1.435140904)+(0.6651200808e-1-.9046746939*I)/(x+(.6958467566+.1047206754*I))+(0.6651200788e-1+.9046746938*I)/(x+(.6958467566-.1047206754*I))+(-.2012186296-....

> fsolve(denom(r),x,complex);

-.6958467566-.1047206754*I, -.6958467566+.1047206754*I, .4728999606-.8803491666*I, .4728999606+.8803491666*I, 1.435140904-.6958467566-.1047206754*I, -.6958467566+.1047206754*I, .4728999606-.8803491666*I, .4728999606+.8803491666*I, 1.435140904