Next: Reading and Saving Up: Programming in Maple Previous: Numerical Computation in

Computing with Polynomials in Maple

Computing with polynomials and also rational functions is Maple's forte. Here is a program that computes the Euclidean norm of a polynomial. I.e. given the polynomial , it computes the .


EuclideanNorm := proc(a)
    sqrt( convert( map( x -> x^2, [coeffs( expand(a) )] ), `+` ) )
end;

Reading this one liner inside out, the input polynomial is first expanded. The coeffs function returns a sequence of the coefficients which we have put in a list. Each element of the list is squared yielding a new list. Then the list of squares is converted to a sum.

Why is the polynomial expanded? The coeff and coeffs functions insist on having the input polynomial expanded because you cannot compute the coefficient(s) otherwise. The following example should clarify


> p := x^3 - (x-3)*(x^2+x) + 1;

                               3             2
                         p := x  - (x - 3) (x  + x) + 1

> coeffs(p);
Error, invalid arguments to coeffs
> expand(p);
                                    2
                                 2 x  + 3 x + 1

> coeffs(expand(p));
                                    1, 3, 2

> EuclideanNorm(p);
                                       1/2
                                     14

The EuclideanNorm procedure works for multivariate polynomials in the sense that it computes the square root of the sum of the squares of the numerical coefficients. E.g. given the polynomial , the EuclideanNorm procedure returns . However, you may want to view this polynomial as a polynomial in whose coefficients are symbolic coefficients in . We really want to be able to tell the EuclideanNorm routine what the polynomial variables are. We can do that by specifying an additional optional parameter, the variables, as follows


EuclideanNorm := proc(a,v)
    if nargs = 1 then
        sqrt( convert( map( x -> x^2, [coeffs(expand(a))] ), `+` ) )
    elif type(v,{name,set(name),list(name)}) then
        sqrt( convert( map( x -> x^2, [coeffs(expand(a),v)] ), `+` ) )
    else ERROR(`invalid 2nd argument (variables)`) 
    fi
end;

The type {name,set(name),list(name)} means that the 2nd parameter may be a single variable, or a set of variables, or a list of variables. Notice that the coeffs function itself accepts this argument as a 2nd optional argument. Finally, our routine doesn't ensure that the input is a polynomial. Let's add that to


EuclideanNorm := proc(a,v)
    if nargs = 1 then
        if not type(a,polynom) then
            ERROR(`1st argument is not a polynomial`,a) fi;
        sqrt( convert( map( x -> x^2, [coeffs(expand(a))] ), `+` ) )
    elif type(v,{name,set(name),list(name)}) then
        if not type(a,polynom(anything,v)) then
            ERROR(`1st argument is not a polynomial in`,v) fi;
        sqrt( convert( map( x -> x^2, [coeffs(expand(a),v)] ), `+` ) )
    else ERROR(`invalid 2nd argument (variables)`) 
    fi
end;

The type polynom has the following general syntax

polynom( , )

which means a polynomial whose coefficients are of type in the variables . An example is polynom(rational,x) which specifies a univariate polynomial in with rational coefficients, i.e. a polynomial in . If and are not specified, as in the first case above, the expression must be a polynomial in all its variables.

Basic functions for computing with polynomials are degree, coeff, expand, divide, and collect. There are many other functions for computing with polynomials in Maple, including facilities for polynomial division, greatest common divisors, resultants, etc. Maple can also factor polynomials over different number fields including finite fields. See the on-line help for ?polynom for a list of facilities.

To illustrate facilities for computing with polynomials over finite fields, we conclude with a program to compute the first primitive trinomial of degree over if one exists . That is, we want to find an irreducible polynomial of the form such that is a primitive element in . The iquo function computes the integer quotient of two integers.


trinomial := proc(n) local i,t;
    for i to iquo(n+1,2) do
        t := x^n+x^i+1;
        if Primitive(t) mod 2 then RETURN(t) fi;
    od;
    FAIL
end;


bondaren@thsun1.jinr.ru