manipulating polynomials

Let's set up some polynomials.

i1 : R = ZZ/10007[a,b];
i2 : f = (2*a+3)^4 + 5

        4      3       2
o2 = 16a  + 96a  + 216a  + 216a + 86

o2 : R
i3 : g = (2*a+b+1)^3

       3      2        2    3      2             2
o3 = 8a  + 12a b + 6a*b  + b  + 12a  + 12a*b + 3b  + 6a + 3b + 1

o3 : R

The number of terms in a polynomial is obtained with size.

i4 : size f, size g

o4 = (5, 10)

o4 : Sequence

The degree of a polynomial is obtained with degree.

i5 : degree f

o5 = {4}

o5 : List
i6 : degree g

o6 = {3}

o6 : List

(Notice that the degree is a list containing one integer, rather than an integer. The degree is actually a vector of integers, represented as a list, with one component by default.)

The list of terms of a polynomial is obtained with terms.

i7 : terms g

        3     2       2   3     2           2
o7 = {8a , 12a b, 6a*b , b , 12a , 12a*b, 3b , 6a, 3b, 1}

o7 : List

We may combine that with select to select terms satisfying certain conditions. Here we select the terms of degree 2, subsequently summing them, keeping in mind that the degree is a list of integers.

i8 : select(terms g, i -> degree i == {2})

         2           2
o8 = {12a , 12a*b, 3b }

o8 : List
i9 : sum oo

        2             2
o9 = 12a  + 12a*b + 3b 

o9 : R

A string representing the polynomial, suitable for entry into other programs, can be obtained with name.

i10 : name f

o10 = 16*a^4+96*a^3+216*a^2+216*a+86

o10 : String
i11 : name g

o11 = 8*a^3+12*a^2*b+6*a*b^2+b^3+12*a^2+12*a*b+3*b^2+6*a+3*b+1

o11 : String

The usual algebraic operations on polynomials are available, but there are some special remarks to make about division. The result of division depends on the ordering of monomials chosen when the ring is created, for division of f by g proceeds by locating monomials in f divisible by the leading monomial of g, and substituting for it the negation of the rest of g. The quotient is provided by the expression f//g, and the remainder is obtained with f%g.

i12 : quot = f//g

o12 = 2a - 3b + 9

o12 : R
i13 : rem = f%g

         2 2        3     4      2         2      2              2
o13 = 24a b  + 16a*b  + 3b  - 96a b - 24a*b  + 96a  - 96a*b - 18b  + 160a - 24b + 77

o13 : R
i14 : f == quot * g + rem

o14 = true

(Notice that comparison of polynomials is done with the operator ==.)

Polynomials can be homogenized with respect to one of the variables in the ring with homogenize.

i15 : homogenize(f,b)

         4      3        2 2         3      4
o15 = 16a  + 96a b + 216a b  + 216a*b  + 86b 

o15 : R

Polynomials can be factored with factor.

i16 : S = factor f

        2
o16 = (a  + 3a - 2301)(a - 402)(a + 405)(16)

o16 : Product
i17 : T = factor g

                        3
o17 = (a - 5003b - 5003) (8)

o17 : Product

The results above are represented as products of powers. (Exponents equal to 1 don't appear in the display.) We can see the internal structure to a specified depth (in this case, 2) with peek2.

i18 : peek2(S,2)

                     2
o18 = Product{Power{a  + 3a - 2301,1},Power{a - 402,1},Power{a + 405,1},Power{16,1}}

o18 : Net
i19 : peek2(T,2)

o19 = Product{Power{a - 5003b - 5003,3},Power{8,1}}

o19 : Net

The components of the expressions above (Products and Powers) are types of lists, and the parts can be extracted with #.

i20 : T#0

                        3
o20 = (a - 5003b - 5003)

o20 : Power
i21 : T#0#0

o21 = a - 5003b - 5003

o21 : R
i22 : T#0#1

o22 = 3

The ring containing a ring element can be obtained with ring.

i23 : ring f

o23 = R

o23 : PolynomialRing

You can use this in a program to check whether two ring elements come from the same ring.

i24 : ring f === ring g

o24 = true

The coefficient of a monomial in a polynomial can be obtained with _.

i25 : f_1

o25 = 86

        ZZ
o25 : -----
      10007
i26 : f_a

o26 = 216

        ZZ
o26 : -----
      10007
i27 : g_(a*b)

o27 = 12

        ZZ
o27 : -----
      10007

(Notice that the coefficients are elements of the coefficient ring.)

We may get parts of the leading term of a polynomial as follows.

i28 : leadTerm g

        3
o28 = 8a 

o28 : R
i29 : leadCoefficient g

o29 = 8

        ZZ
o29 : -----
      10007
i30 : leadMonomial g

       3
o30 = a 

o30 : [a,b]

Notice that the lead monomial is an element of a monoid whose name is [a,b]. Its exponents can be extracted with exponents.

i31 : exponents leadMonomial g

o31 = {3, 0}

o31 : List

We can get all of the coefficients at once, assembled into matrices.

i32 : coefficients f

o32 = {{0} | a4 a3 a2 a 1 |, {0} | 16 96 216 216 86 |}

o32 : List
i33 : coefficients g

o33 = {{0} | a3 a2b ab2 b3 a2 ab b2 a b 1 |, {0} | 8 12 6 1 12 12 3 6 3 1 |}

o33 : List

A list of lists of exponents appearing in a polynomial can be obtained with exponents.

i34 : exponents f

o34 = {{4, 0}, {3, 0}, {2, 0}, {1, 0}, {0, 0}}

o34 : List
i35 : exponents g

o35 = {{3, 0}, {2, 1}, {1, 2}, {0, 3}, {2, 0}, {1, 1}, {0, 2}, {1, 0}, {0, 1}, {0, 0}}

o35 : List

The entire structure of a polynomial can be provided in an accessible form based on lists with listForm.

i36 : listForm f

o36 = {({4, 0}, 16), ({3, 0}, 96), ({2, 0}, 216), ({1, 0}, 216), ({0, 0}, 86)}

o36 : List
i37 : S = listForm g

o37 = {({3, 0}, 8), ({2, 1}, 12), ({1, 2}, 6), ({0, 3}, 1), ({2, 0}, 12), ({1, 1}, 12), ({0, 2}, 3), ({1, 0}, 6), ({0, 1}, 3), ({0, 0}, 1)}

o37 : List

The lists above are lists of pairs, where the first member of each pair is a list of exponents in a monomial, and the second member is the corresponding coefficient. Standard list operations can be used to manipulate the result.

i38 : S / print;
({3, 0}, 8)
({2, 1}, 12)
({1, 2}, 6)
({0, 3}, 1)
({2, 0}, 12)
({1, 1}, 12)
({0, 2}, 3)
({1, 0}, 6)
({0, 1}, 3)
({0, 0}, 1)

The structure of a polynomial can also be provided in a form based on hash tables with standardForm.

i39 : S = standardForm f

o39 = HashTable{HashTable{0 => 1} => 216}
                HashTable{0 => 2} => 216
                HashTable{0 => 3} => 96
                HashTable{0 => 4} => 16
                HashTable{} => 86

o39 : HashTable
i40 : standardForm g

o40 = HashTable{HashTable{0 => 1} => 12}
                          1 => 1
                HashTable{0 => 1} => 6
                HashTable{0 => 1} => 6
                          1 => 2
                HashTable{0 => 2} => 12
                HashTable{0 => 2} => 12
                          1 => 1
                HashTable{0 => 3} => 8
                HashTable{1 => 1} => 3
                HashTable{1 => 2} => 3
                HashTable{1 => 3} => 1
                HashTable{} => 1

o40 : HashTable

The hash tables above present the same information, except that only nonzero exponents need to be provided. The information can be extracted with #.

i41 : S#(new HashTable from {0 => 2})

o41 = 216

        ZZ
o41 : -----
      10007

Monomials (monoid elements) have an accessible form which is implicitly used above.

i42 : listForm leadMonomial g

o42 = {3, 0}

o42 : List
i43 : standardForm leadMonomial g

o43 = HashTable{0 => 3}

o43 : HashTable

Comparison of polynomials is possible, and proceeds by simply examining the lead monomials and comparing them.

i44 : f < g

o44 = false
i45 : f ? g

o45 = quote >

o45 : Symbol
i46 : sort {b^2-1,a*b,a+1,a,b}

                     2
o46 = {b, a, a + 1, b  - 1, a*b}

o46 : List


topindexpreviousupnext