**3.3.4 Algebraic Operations on Polynomials**

For many kinds of practical calculations, the only operations you will need to perform on polynomials are essentially the structural ones discussed in the preceding sections.

If you do more advanced algebra with polynomials, however, you will have to use the algebraic operations discussed in this section.

You should realize that most of the operations discussed in this section work only on ordinary polynomials, with integer exponents and rational-number coefficients for each term.

Reduction of polynomials.

Given two polynomials and , one can always uniquely write , where the degree of is less than the degree of . PolynomialQuotient gives the quotient , and PolynomialRemainder gives the remainder

.

This gives the remainder from dividing by

.
In[1]:= **PolynomialRemainder[x^2, x+1, x]**

Out[1]=

Here is the quotient of and

, with the remainder dropped.
In[2]:= **PolynomialQuotient[x^2, x+1, x]**

Out[2]=

This gives back the original expression.
In[3]:= **Simplify[ (x+1) % + %% ]**

Out[3]=

Here the result depends on whether the polynomials are considered to be in x or y.
In[4]:= **{PolynomialRemainder[x+y, x-y, x],**

PolynomialRemainder[x+y, x-y, y]}

Out[4]=

PolynomialGCD[

,

] finds the highest degree polynomial that divides the exactly. It gives the analog for polynomials of the integer function GCD

.

PolynomialGCD gives the greatest common divisor of the two polynomials.
In[5]:= **PolynomialGCD[ (1-x)^2 (1+x) (2+x), (1-x) (2+x) (3+x) ]**

Out[5]=

PolynomialMod is essentially the analog for polynomials of the function Mod for integers. When the modulus m is an integer, PolynomialMod[poly,m] simply reduces each coefficient in poly modulo the integer m. If m is a polynomial, then PolynomialMod[poly,m] effectively tries to get as low degree a polynomial as possible by subtracting from poly appropriate multiples qm of m. The multiplier q can itself be a polynomial, but its degree is always less than the degree of poly. PolynomialMod yields a final polynomial whose degree and leading coefficient are both as small as possible.

This reduces modulo

. The result is simply the remainder from dividing the polynomials.
In[6]:= **PolynomialMod[x^2, x+1]**

Out[6]=

In this case, PolynomialMod and PolynomialRemainder do not give the same result.
In[7]:= **{PolynomialMod[x^2, 2x+1],**

PolynomialRemainder[x^2, 2x+1, x]}

Out[7]=

The main difference between PolynomialMod and PolynomialRemainder is that while the former works simply by multiplying and subtracting polynomials, the latter uses division in getting its results. In addition, PolynomialMod allows reduction by several moduli at the same time. A typical case is reduction modulo both a polynomial and an integer.

This reduces the polynomial modulo both and

.
In[8]:= **PolynomialMod[x^2 + 1, {x + 1, 2}]**

Out[8]=

The function Resultant[

,

,x] is used in a number of classical algebraic algorithms. The resultant of two polynomials and , both with leading coefficient one, is given by the product of all the differences

between the roots of the polynomials. It turns out that for any pair of polynomials, the resultant is always a polynomial in their coefficients. By looking at when the resultant is zero, one can tell for what values of their parameters two polynomials have a common root.

Here is the resultant with respect to of two polynomials in and . The original polynomials have a common root in only for values of

at which the resultant vanishes.
In[9]:= **Resultant[(x-y)^2-2, y^2-3, y]**

Out[9]=

Gröbner bases appear in many modern algebraic algorithms and applications. The function GroebnerBasis[

,

, ...

,

,

, ...

] takes a set of polynomials, and reduces this set to a canonical form from which many properties can conveniently be deduced. An important feature is that the set of polynomials obtained from GroebnerBasis always has exactly the same collection of common roots as the original set.

The

is effectively redundant, and so does not appear in the Gröbner basis.
In[10]:= **GroebnerBasis[{(x+y), (x+y)^2}, {x, y}]**

Out[10]=

The polynomial 1 has no roots, showing that the original polynomials have no common roots.
In[11]:= **GroebnerBasis[{x+y,x^2-1,y^2-2x}, {x, y}]**

Out[11]=

The polynomials are effectively unwound here, and can now be seen to have exactly five common roots.
In[12]:= **GroebnerBasis[{x y^2+2 x y+x^2+1, x y+y^2+1}, {x, y}]**

Out[12]=

PolynomialReduce[poly,

,

, ...

,

,

, ...

] yields a list

,

, ...

,b of polynomials with the property that b is minimal and

+

+ ... +b is exactly poly.

This writes in terms of and , leaving a remainder that depends only on

.
In[13]:= **PolynomialReduce[x^2 + y^2, {x - y, y + a}, {x, y}]**

Out[13]=

Functions for factoring polynomials.

Factor, FactorTerms and FactorSquareFree perform various degrees of factoring on polynomials. Factor does full factoring over the integers. FactorTerms extracts the "content" of the polynomial. FactorSquareFree pulls out any multiple factors that appear.

Here is a polynomial, in expanded form.
In[14]:= **t = Expand[ 2 (1 + x)^2 (2 + x) (3 + x) ]**

Out[14]=

FactorTerms pulls out only the factor of 2 that does not depend on x.
In[15]:= **FactorTerms[t, x]**

Out[15]=

FactorSquareFree factors out the 2 and the term (1+x)^2, but leaves the rest unfactored.
In[16]:= **FactorSquareFree[t]**

Out[16]=

Factor does full factoring, recovering the original form.
In[17]:= **Factor[t]**

Out[17]=

Particularly when you write programs that work with polynomials, you will often find it convenient to pick out pieces of polynomials in a standard form. The function FactorList gives a list of all the factors of a polynomial, together with their exponents. The first element of the list is always the overall numerical factor for the polynomial.

The form that FactorList returns is the analog for polynomials of the form produced by FactorInteger for integers.

Here is a list of the factors of the polynomial in the previous set of examples. Each element of the list gives the factor, together with its exponent.
In[18]:= **FactorList[t]**

Out[18]=

Factoring polynomials with complex coefficients.

Factor and related functions usually handle only polynomials with ordinary integer or rational-number coefficients. If you set the option GaussianIntegers->True, however, then Factor will allow polynomials with coefficients that are complex numbers with rational real and imaginary parts. This often allows more extensive factorization to be performed.

This polynomial is irreducible when only ordinary integers are allowed.
In[19]:= **Factor[1 + x^2]**

Out[19]=

When Gaussian integer coefficients are allowed, the polynomial factors.
In[20]:= **Factor[1 + x^2, GaussianIntegers -> True]**

Out[20]=

Cyclotomic polynomials.

Cyclotomic polynomials arise as "elementary polynomials" in various algebraic algorithms. The cyclotomic polynomials are defined by , where runs over all positive integers less than that are relatively prime to

.

This is the cyclotomic polynomial

.
In[21]:= **Cyclotomic[6, x]**

Out[21]=

appears in the factors of

.
In[22]:= **Factor[x^6 - 1]**

Out[22]=

Decomposing polynomials.

Factorization is one important way of breaking down polynomials into simpler parts. Another, quite different, way is decomposition. When one factors a polynomial , one writes it as a product of polynomials . Decomposing a polynomial consists of writing it as a composition of polynomials of the form

.

Here is a simple example of Decompose. The original polynomial can be written as the polynomial , where is the polynomial

.
In[23]:= **Decompose[x^4 + x^2 + 1, x]**

Out[23]=

Here are two polynomial functions.
In[24]:= **( q1[x_] = 1 - 2x + x^4 ;**

q2[x_] = 5x + x^3 ; )

This gives the composition of the two functions.
In[25]:= **Expand[ q1[ q2[ x ] ] ]**

Out[25]=

Decompose recovers the original functions.
In[26]:= **Decompose[%, x]**

Out[26]=

Decompose[poly,x] is set up to give a list of polynomials in x, which, if composed, reproduce the original polynomial. The original polynomial can contain variables other than x, but the sequence of polynomials that Decompose produces are all intended to be considered as functions of x.

Unlike factoring, the decomposition of polynomials is not completely unique. For example, the two sets of polynomials and , related by and give the same result on composition, so that . Mathematica follows the convention of absorbing any constant terms into the first polynomial in the list produced by Decompose

.

Generating interpolating polynomials.

This yields a quadratic polynomial which goes through the specified three points.
In[27]:= **InterpolatingPolynomial[{{-1, 4}, {0, 2}, {1, 6}}, x]**

Out[27]=

When x is 0, the polynomial has value 2.
In[28]:= **% /. x -> 0**

Out[28]=