# manipulating polynomials

• + -- a unary or binary operator, usually used for addition
• - -- a unary or binary operator, usually used for negation or subtraction
• * -- a binary operator, usually used for multiplication
• ^ -- a binary operator, usually used for powers
• // -- a binary operator, usually used for quotient
• % -- a binary operator, usually used for remainder and reduction
• terms -- provide a list of terms of a polynomial
• diff -- differentiate or take difference
• listForm -- convert to list form
• degree
• homogenize -- homogenize with respect to a variable
• exponents -- the exponents of a polynomial
• leadTerm -- get the greatest term
• size -- the size of an object
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 of a polynomial is always 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
Of course, if the list of selected terms is empty, the sum would turn out to be the zero integer, rather than the zero element of the ring R. Fortunately, we have another way to select the elements of given degree or multi-degree (see part).
 i10 : part(0,g) o10 = 1 o10 : R i11 : part(1,g) o11 = 6a + 3b o11 : R i12 : part(2,g) 2 2 o12 = 12a + 12a*b + 3b o12 : R i13 : part(3,g) 3 2 2 3 o13 = 8a + 12a b + 6a*b + b o13 : R
A string representing the polynomial, suitable for entry into other programs, can be obtained with toString.
 i14 : toString f o14 = 16*a^4+96*a^3+216*a^2+216*a+86 i15 : toString g o15 = 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

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.
 i16 : quot = f//g o16 = 2a - 3b + 9 o16 : R i17 : rem = f%g 2 2 3 4 2 2 2 2 o17 = 24a b + 16a*b + 3b - 96a b - 24a*b + 96a - 96a*b - 18b + 160a - ----------------------------------------------------------------------- 24b + 77 o17 : R i18 : f == quot * g + rem o18 = true
Notice that as in the example above, comparison of polynomials is done with the operator ==.

Polynomials can be homogenized with respect to one of the variables in the ring with homogenize.
 i19 : homogenize(f,b) 4 3 2 2 3 4 o19 = 16a + 96a b + 216a b + 216a*b + 86b o19 : R

The ring containing a ring element can be obtained with ring.
 i20 : ring f o20 = R o20 : PolynomialRing
You can use this in a program to check whether two ring elements come from the same ring.
 i21 : ring f === ring g o21 = true
Notice that in the comparison above, the strict equality operator === is used.

The coefficient of a monomial in a polynomial can be obtained with _.
 i22 : part(1,f) o22 = 216a o22 : R i23 : f_a o23 = 216 ZZ o23 : ----- 10007 i24 : g_(a*b) o24 = 12 ZZ o24 : ----- 10007
(Notice that the coefficients are elements of the coefficient ring.)

We may get parts of the leading term of a polynomial as follows.
 i25 : leadTerm g 3 o25 = 8a o25 : R i26 : leadCoefficient g o26 = 8 ZZ o26 : ----- 10007 i27 : leadMonomial g 3 o27 = a o27 : R
The exponents of a monomial or term can be extracted with exponents.
 i28 : exponents leadMonomial g o28 = {{3, 0}} o28 : List i29 : exponents leadTerm g o29 = {{3, 0}} o29 : List
We can get all of the coefficients at once, assembled in a one-rowed matrix, along with a matrix containing the corresponding monomials.
 i30 : coefficients f o30 = (| a4 a3 a2 a 1 |, {4} | 16 |) {3} | 96 | {2} | 216 | {1} | 216 | {0} | 86 | o30 : Sequence i31 : coefficients g o31 = (| a3 a2b ab2 b3 a2 ab b2 a b 1 |, {3} | 8 |) {3} | 12 | {3} | 6 | {3} | 1 | {2} | 12 | {2} | 12 | {2} | 3 | {1} | 6 | {1} | 3 | {0} | 1 | o31 : Sequence
A list of lists of exponents appearing in a polynomial can be obtained with exponents.
 i32 : exponents f o32 = {{4, 0}, {3, 0}, {2, 0}, {1, 0}, {0, 0}} o32 : List i33 : exponents g o33 = {{3, 0}, {2, 1}, {1, 2}, {0, 3}, {2, 0}, {1, 1}, {0, 2}, {1, 0}, {0, ----------------------------------------------------------------------- 1}, {0, 0}} o33 : List
The entire structure of a polynomial can be provided in an accessible form based on lists with listForm.
 i34 : listForm f o34 = {({4, 0}, 16), ({3, 0}, 96), ({2, 0}, 216), ({1, 0}, 216), ({0, 0}, ----------------------------------------------------------------------- 86)} o34 : List i35 : S = listForm g o35 = {({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)} o35 : 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.
 i36 : 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.
 i37 : S = standardForm f o37 = HashTable{HashTable{} => 86 } HashTable{0 => 1} => 216 HashTable{0 => 2} => 216 HashTable{0 => 3} => 96 HashTable{0 => 4} => 16 o37 : HashTable i38 : standardForm g o38 = HashTable{HashTable{} => 1 } HashTable{0 => 1} => 12 1 => 1 HashTable{0 => 1} => 6 1 => 2 HashTable{0 => 1} => 6 HashTable{0 => 2} => 12 1 => 1 HashTable{0 => 2} => 12 HashTable{0 => 3} => 8 HashTable{1 => 1} => 3 HashTable{1 => 2} => 3 HashTable{1 => 3} => 1 o38 : HashTable
The hash tables above present the same information, except that only nonzero exponents need to be provided. The information can be extracted with #.
 i39 : S#(new HashTable from {0 => 2}) o39 = 216 ZZ o39 : ----- 10007

Comparison of polynomials is possible, and proceeds by simply examining the lead monomials and comparing them.
 i40 : f < g o40 = false i41 : sort {b^2-1,a*b,a+1,a,b} 2 o41 = {b, a, a + 1, b - 1, a*b} o41 : List
The comparison operator ? returns a symbol indicating how two polynomials, or rather, their lead monomials, stand with respect to each other in the monomial ordering.
 i42 : f ? g o42 = > o42 : Keyword