lec14.mws

L-14 MCS 320 Friday 23 September 2005

> restart;

1. The canonical and a normal form

> e1 := x*(1 + y); e2 := x + x*y;

e1 := x*(1+y)

e2 := x+x*y

The test whether two expressions are equal can be done as

> e1 = e2; # Maple repeats just the equality

x*(1+y) = x+x*y

> evalb(%); # ask Maple to evaluate the equality

false

The equality fails, because Maple just compared the data structures.

> evalb(expand(e1) = e2);

true

For polynomials in one variable, we can define the canonical form.  The canonical form of a univariate polynomial is unique, it is the polynomial obtained after fully expanding, removing the superfluous terms, and sorting the terms form high to low degree.

For rational polynomials in one variable, we have the normal command.  The normal command removes the common factors.  But even for rational polynomial there are many choices, we may choose to factor the numerator and/or denominator.

For multivariate polynomials, we have many choices in how we define the order of monomials.

In this case we have several normal forms.

2. Collecting and Sorting

Collection and sorting are two tools to normalize multivariate polynomials.

> collect(e1,x);

x*(1+y)

> collect(e2,x);

x*(1+y)

The collect command collects terms with regard to one leading variable.  The above commands have written e1 and e2 as a polynomial in x.

> p := randpoly([x,y,z],degree=3,dense);

p := 74-4*x^3-83*x^2*y-10*x^2*z+62*x^2-82*x*y^2+80*x*y*z-44*x*y+71*x*z^2-17*x*z-75*x-10*y^3-7*y^2*z-40*y^2+42*y*z^2-50*y*z+23*y+75*z^3-92*z^2+6*zp := 74-4*x^3-83*x^2*y-10*x^2*z+62*x^2-82*x*y^2+80*x*y*z-44*x*y+71*x*z^2-17*x*z-75*x-10*y^3-7*y^2*z-40*y^2+42*y*z^2-50*y*z+23*y+75*z^3-92*z^2+6*z

> pxy := collect(p,[x,y]);

pxy := -4*x^3+(-10*z+62-83*y)*x^2+(-82*y^2+(-44+80*z)*y-75+71*z^2-17*z)*x-10*y^3+(-7*z-40)*y^2+(23+42*z^2-50*z)*y+74-92*z^2+6*z+75*z^3pxy := -4*x^3+(-10*z+62-83*y)*x^2+(-82*y^2+(-44+80*z)*y-75+71*z^2-17*z)*x-10*y^3+(-7*z-40)*y^2+(23+42*z^2-50*z)*y+74-92*z^2+6*z+75*z^3

> pyz := collect(p,[y,z]);

pyz := -10*y^3+(-7*z-40-82*x)*y^2+(42*z^2+(-50+80*x)*z-44*x-83*x^2+23)*y+75*z^3+(-92+71*x)*z^2+(-10*x^2+6-17*x)*z+74-4*x^3-75*x+62*x^2pyz := -10*y^3+(-7*z-40-82*x)*y^2+(42*z^2+(-50+80*x)*z-44*x-83*x^2+23)*y+75*z^3+(-92+71*x)*z^2+(-10*x^2+6-17*x)*z+74-4*x^3-75*x+62*x^2

These are various normal forms of p.

> hxy := convert(p,`horner`,[x,y,z]);

hxy := 74+(6+(-92+75*z)*z)*z+(23+(-50+42*z)*z+(-7*z-40-10*y)*y)*y+(-75+(-17+71*z)*z+(-44+80*z-82*y)*y+(-10*z+62-83*y-4*x)*x)*xhxy := 74+(6+(-92+75*z)*z)*z+(23+(-50+42*z)*z+(-7*z-40-10*y)*y)*y+(-75+(-17+71*z)*z+(-44+80*z-82*y)*y+(-10*z+62-83*y-4*x)*x)*x

> hyz := convert(p,`horner`,[y,z,x]);

hyz := 74+(-75+(62-4*x)*x)*x+(6+(-17-10*x)*x+(-92+71*x+75*z)*z)*z+(23+(-44-83*x)*x+(-50+80*x+42*z)*z+(-7*z-40-82*x-10*y)*y)*yhyz := 74+(-75+(62-4*x)*x)*x+(6+(-17-10*x)*x+(-92+71*x+75*z)*z)*z+(23+(-44-83*x)*x+(-50+80*x+42*z)*z+(-7*z-40-82*x-10*y)*y)*y

These are interesting forms for evaluating the polynomial.  For a dense polynomial of degree 2, the order of variables does not matter for efficiency, but in general if some degrees for some variables are larger than others, the order in Horner scheme will matter.

We can sort polynomials according to degree or pure lexicographically.

> q := randpoly([x,y,z],degree=5);

q := -23+10*x*y-61*y*z-8*x^2*z-29*x*z^2+95*x^2*y*z^2

> sort(q,[z,y,x],plex); # pure lexicographic order

95*z^2*y*x^2-29*z^2*x-61*z*y-8*z*x^2+10*y*x-23

We see that z*y precedes the higher degree monomial z*x^2 because we have declared y>x and we are sorting pure lexicographically.  We interpret the x^2 as xx.

> sort(q,[z,y,x],tdeg); # total degree order

95*z^2*y*x^2-29*z^2*x-8*z*x^2-61*z*y+10*y*x-23

3. A probability one equality test

We may evaluate expressions at random numbers to test equality.

> r1x,r1y := stats[random,uniform[0,1]](2);

r1x, r1y := .8836600253, .2175636209

> subs(x=r1x,y=r1y,e1);

1.075912300

> subs(x=r1x,y=r1y,e2);

1.075912300

Since we took random nice numbers, the same evaluation of the two expression can give a conclusion about the equality or inequality.  With probability one, the test will be decisive.

For expressions with floating point numbers, fnormal removes terms with coefficients smaller than a given tolerance.  See exercise 5 for an application.