Let's set up some polynomials.
i1 : R = ZZ/10007[a,b]; |
i2 : f = (2*a+3)^4 + 5 |
i3 : g = (2*a+b+1)^3 |
The number of terms in a polynomial is obtained with size.
i4 : size f, size g |
The degree of a polynomial is obtained with degree.
i5 : degree f |
i6 : degree g |
(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 |
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}) |
i9 : sum oo |
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 RingElement _ ZZ).
i10 : g_0 |
i11 : g_1 |
i12 : g_2 |
i13 : g_3 |
A string representing the polynomial, suitable for entry into other programs, can be obtained with toString.
i14 : toString f |
i15 : toString g |
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 |
i17 : rem = f%g |
i18 : f == quot * g + rem |
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) |
The ring containing a ring element can be obtained with ring.
i20 : ring f |
You can use this in a program to check whether two ring elements come from the same ring.
i21 : ring f === ring g |
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 : f_1 |
i23 : f_a |
i24 : g_(a*b) |
(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 |
i26 : leadCoefficient g |
i27 : leadMonomial g |
Notice that the lead monomial is an element of a monoid whose name is [a,b]. Its exponents can be extracted with exponents.
i28 : exponents leadMonomial g |
We can get all of the coefficients at once, assembled a one-rowed matrix, along with a matrix containing the corresponding monomials.
i29 : coefficients f |
i30 : coefficients g |
A list of lists of exponents appearing in a polynomial can be obtained with exponents.
i31 : exponents f |
i32 : exponents g |
The entire structure of a polynomial can be provided in an accessible form based on lists with listForm.
i33 : listForm f |
i34 : S = listForm g |
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.
i35 : S / print; |
The structure of a polynomial can also be provided in a form based on hash tables with standardForm.
i36 : S = standardForm f |
i37 : standardForm g |
The hash tables above present the same information, except that only nonzero exponents need to be provided. The information can be extracted with #.
i38 : S#(new HashTable from {0 => 2}) |
Monomials (monoid elements) have an accessible form that is implicitly used above.
i39 : listForm leadMonomial g |
i40 : standardForm leadMonomial g |
Comparison of polynomials is possible, and proceeds by simply examining the lead monomials and comparing them.
i41 : f < g |
i42 : sort {b^2-1,a*b,a+1,a,b} |
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.
i43 : f ? g |