Polynomial greatest common divisor


In algebra, the greatest common divisor of two polynomials is a polynomial, of the highest possible degree, that is a factor of both the two original polynomials. This concept is analogous to the greatest common divisor of two integers.
In the important case of univariate polynomials over a field the polynomial GCD may be computed, like for the integer GCD, by Euclid's algorithm using long division. The polynomial GCD is defined only up to the multiplication by an invertible constant.
The similarity between the integer GCD and the polynomial GCD allows us to extend to univariate polynomials all the properties that may be deduced from Euclid's algorithm and Euclidean division. Moreover, the polynomial GCD has specific properties that make it a fundamental notion in various areas of algebra. Typically, the roots of the GCD of two polynomials are the common roots of the two polynomials, and this allows us to get information on the roots without computing them. For example, the multiple roots of a polynomial are the roots of the GCD of the polynomial and its derivative, and further GCD computations allow us to compute the square-free factorization of the polynomial, which provides polynomials whose roots are the roots of a given multiplicity.
The greatest common divisor may be defined and exists, more generally, for multivariate polynomials over a field or the ring of integers, and also over a unique factorization domain. There exist algorithms to compute them as soon as one has a GCD algorithm in the ring of coefficients. These algorithms proceed by a recursion on the number of variables to reduce the problem to a variant of Euclid's algorithm. They are a fundamental tool in computer algebra, because computer algebra systems use them systematically to simplify fractions. Conversely, most of the modern theory of polynomial GCD has been developed to satisfy the need for efficiency of computer algebra systems.

General definition

Let p and q be polynomials with coefficients in an integral domain F, typically a field or the integers.
A greatest common divisor of p and q is a polynomial d that divides p and q and such that every common divisor of p and q also divides d. Every pair of polynomials has a GCD if and only if F is a unique factorization domain.
If F is a field and p and q are not both zero, for d to be a greatest common divisor it is sufficient that it divides both p and q and it has the greatest degree among the polynomials having this property. If p = q = 0, the GCD is 0. However, some authors consider that it is not defined in this case.
The greatest common divisor of p and q is usually denoted "gcd".
The greatest common divisor is not unique: if d is a GCD of p and q, then the polynomial f is another GCD if and only if there is an invertible element u of F such that
and
In other words, the GCD is unique up to the multiplication by an invertible constant.
In the case of the integers, this indetermination has been settled by choosing, as the GCD, the unique one which is positive. With this convention, the GCD of two integers is also the greatest common divisor. However, since there is no natural total order for polynomials over an integral domain, one cannot proceed in the same way here. For univariate polynomials over a field, one can additionally require the GCD to be monic, but in more general cases there is no general convention. Therefore, equalities like d = gcd or gcd = gcd are usual abuses of notation which should be read "d is a GCD of p and q" and "p, q has the same set of GCD as r, s". In particular, gcd = 1 means that the invertible constants are the only common divisors. In this case, by analogy with the integer case, one says that p and q are .

Properties

There are several ways to find the greatest common divisor of two polynomials. Two of them are:
  1. Factorization of polynomials, in which one finds the factors of each expression, then selects the set of common factors held by all from within each set of factors. This method may be useful only in simple cases, as factoring is usually more difficult than computing the greatest common divisor.
  2. The Euclidean algorithm, which can be used to find the GCD of two polynomials in the same manner as for two numbers.

    Factoring

To find the GCD of two polynomials using factoring, simply factor the two polynomials completely. Then, take the product of all common factors. At this stage, we do not necessarily have a monic polynomial, so finally multiply this by a constant to make it a monic polynomial. This will be the GCD of the two polynomials as it includes all common divisors and is monic.
Example one: Find the GCD of and.
Thus, their GCD is.

Euclidean algorithm

Factoring polynomials can be difficult, especially if the polynomials have a large degree. The Euclidean algorithm is a method that works for any pair of polynomials. It makes repeated use of Euclidean division. When using this algorithm on two numbers, the size of the numbers decreases at each stage. With polynomials, the degree of the polynomials decreases at each stage. The last nonzero remainder, made monic if necessary, is the GCD of the two polynomials.
More specifically, assume we wish to find the gcd of two polynomials and, where we can suppose
We can find two polynomials and which satisfy
The polynomial is called the quotient and is the remainder. Notice that a polynomial divides and if and only if it divides and. We deduce
Then set
Repeat the polynomial long division to get new polynomials and so on. At each stage we have
so the sequence will eventually reach a point at which
and we will have found our GCD:
Example: Find the GCD of and.
Since is the last nonzero remainder, the GCD of these polynomials is.
This method works only if one may test the equality to zero of the elements of the field of the coefficients, so one needs a description of the coefficients as elements of some finitely generated field, for which the generators and relations are known exactly. If the coefficients are floating-point numbers, known only approximately, then one uses completely different techniques, usually based on SVD.
This induces a new difficulty: For all these fields except the finite ones, the coefficients are fractions. If the fractions are not simplified during the computation, the size of the coefficients grows exponentially during the computation, which makes it impossible except for very small degrees. On the other hand, it is highly time-consuming to simplify the fractions immediately. Therefore, two different alternative methods have been introduced :
The case of univariate polynomials over a field is especially important for several reasons. Firstly, it is the most elementary case and therefore appears in most first courses in algebra. Secondly, it is very similar to the case of the integers, and this analogy is the source of the notion of Euclidean domain. A third reason is that the theory and the algorithms for the multivariate case and for coefficients in a unique factorization domain are strongly based on this particular case. Last but not least, polynomial GCD algorithms and derived algorithms allow one to get useful information on the roots of a polynomial, without computing them.

Euclidean division

Euclidean division of polynomials, which is used in Euclid's algorithm for computing GCDs, is very similar to Euclidean division of integers. Its existence is based on the following theorem: Given two univariate polynomials a and b ≠ 0 defined over a field, there exist two polynomials q and r which satisfy
and
where "deg" denotes the degree and the degree of the zero polynomial is defined as being negative. Moreover, q and r are uniquely defined by these relations.
The difference from Euclidean division of the integers is that, for the integers, the degree is replaced by the absolute value, and that to have uniqueness one has to suppose that r is non-negative. The rings for which such a theorem exists are called Euclidean domains.
Like for the integers, the Euclidean division of the polynomials may be computed by the long division algorithm. This algorithm is usually presented for paper-and-pencil computation, but it works well on computers when formalized as follows. In the following computation "deg" stands for the degree of its argument, and "lc" stands for the leading coefficient, the coefficient of the highest degree of the variable.
The proof of the validity of this algorithm relies on the fact that during the whole "while" loop, we have and is a non-negative integer that decreases at each iteration. Thus the proof of the validity of this algorithm also proves the validity of the Euclidean division.

Euclid's algorithm

As for the integers, the Euclidean division allows us to define Euclid's algorithm for computing GCDs.
Starting from two polynomials a and b, Euclid's algorithm consists of recursively replacing the pair by , until b = 0. The GCD is the last non zero remainder.
Euclid's algorithm may be formalized in the recursive programming style as:
In the imperative programming style, the same algorithm becomes, giving a name to each intermediate remainder:
The sequence of the degrees of the is strictly decreasing. Thus after, at most, steps, one get a null remainder, say. As and have the same divisors, the set of the common divisors is not changed by Euclid's algorithm and thus all pairs have the same set of common divisors. The common divisors of and are thus the common divisors of and 0. Thus is a GCD of and.
This not only proves that Euclid's algorithm computes GCDs but also proves that GCDs exist.

Bézout's identity and extended GCD algorithm

is a GCD related theorem, initially proved for the integers, which is valid for every principal ideal domain. In the case of the univariate polynomials over a field, it may be stated as follows.
The interest of this result in the case of the polynomials is that there is an efficient algorithm to compute the polynomials and, This algorithm differs from Euclid's algorithm by a few more computations done at each iteration of the loop. It is therefore called extended GCD algorithm. Another difference with Euclid's algorithm is that it also uses the quotient, denoted "quo", of the Euclidean division instead of only the remainder. This algorithm works as follows.
The proof that the algorithm satisfies its output specification relies on the fact that, for every we have
the latter equality implying
The assertion on the degrees follows from the fact that, at every iteration, the degrees of and increase at most as the degree of decreases.
An interesting feature of this algorithm is that, when the coefficients of Bezout's identity are needed, one gets for free the quotient of the input polynomials by their GCD.

Arithmetic of algebraic extensions

An important application of the extended GCD algorithm is that it allows one to compute division in algebraic field extensions.
Let an algebraic extension of a field, generated by an element whose minimal polynomial has degree. The elements of are usually represented by univariate polynomials over of degree less than.
The addition in is simply the addition of polynomials:
The multiplication in is the multiplication of polynomials followed by the division by :
The inverse of a non zero element of is the coefficient in Bézout's identity, which may be computed by extended GCD algorithm.. The degrees inequality in the specification of extended GCD algorithm shows that a further division by is not needed to get deg < deg.

Subresultants

In the case of univariate polynomials, there is a strong relationship between the greatest common divisors and resultants. More precisely, the resultant of two polynomials P, Q is a polynomial function of the coefficients of P and Q which has the value zero if and only if the GCD of P and Q is not constant.
The subresultants theory is a generalization of this property that allows characterizing generically the GCD of two polynomials, and the resultant is the 0-th subresultant polynomial.
The i-th subresultant polynomial Si of two polynomials P and Q is a polynomial of degree at most i whose coefficients are polynomial functions of the coefficients of P and Q, and the i-th principal subresultant coefficient si is the coefficient of degree i of Si. They have the property that the GCD of P and Q has a degree d if and only if
In this case, Sd is a GCD of P and Q and
Every coefficient of the subresultant polynomials is defined as the determinant of a submatrix of the Sylvester matrix of P and Q. This implies that subresultants "specialize" well. More precisely, subresultants are defined for polynomials over any commutative ring R, and have the following property.
Let φ be a ring homomorphism of R into another commutative ring S. It extends to another homomorphism, denoted also φ between the polynomials rings over R and S. Then, if P and Q are univariate polynomials with coefficients in R such that
and
then the subresultant polynomials and the principal subresultant coefficients of φ and φ are the image by φ of those of P and Q.
The subresultants have two important properties which make them fundamental for the computation on computers of the GCD of two polynomials with integer coefficients.
Firstly, their definition through determinants allows bounding, through Hadamard inequality, the size of the coefficients of the GCD.
Secondly, this bound and the property of good specialization allow computing the GCD of two polynomials with integer coefficients through modular computation and Chinese remainder theorem.

Technical definition

Let
be two univariate polynomials with coefficients in a field K. Let us denote by the K vector space of dimension i the polynomials of degree less than i. For non-negative integer i such that im and in, let
be the linear map such that
The resultant of P and Q is the determinant of the Sylvester matrix, which is the matrix of on the bases of the powers of X. Similarly, the i-subresultant polynomial is defined in term of determinants of submatrices of the matrix of
Let us describe these matrices more precisely;
Let pi = 0 for i < 0 or i > m, and qi = 0 for i < 0 or i > n. The Sylvester matrix is the × -matrix such that the coefficient of the i-th row and the j-th column is pm+ji for jn and qji for j > n:
The matrix Ti of is the × -submatrix of S which is obtained by removing the last i rows of zeros in the submatrix of the columns 1 to ni and n + 1 to m + ni of S. The principal subresultant coefficient si is the determinant of the m + n − 2i first rows of Ti.
Let Vi be the × matrix defined as follows. First we add columns of zeros to the right of the × identity matrix. Then we border the bottom of the resulting matrix by a row consisting in zeros followed by Xi, Xi−1,..., X, 1:
With this notation, the i-th subresultant polynomial is the determinant of the matrix product ViTi. Its coefficient of degree j is the determinant of the square submatrix of Ti consisting in its m + n − 2i − 1 first rows and the -th row.

Sketch of the proof

It is not obvious that, as defined, the subresultants have the desired properties. Nevertheless, the proof is rather simple if the properties of linear algebra and those of polynomials are put together.
As defined, the columns of the matrix Ti are the vectors of the coefficients of some polynomials belonging to the image of. The definition of the i-th subresultant polynomial Si shows that the vector of its coefficients is a linear combination of these column vectors, and thus that Si belongs to the image of
If the degree of the GCD is greater than i, then Bézout's identity shows that every non zero polynomial in the image of has a degree larger than i. This implies that Si=0.
If, on the other hand, the degree of the GCD is i, then Bézout's identity again allows proving that the multiples of the GCD that have a degree lower than m + ni are in the image of. The vector space of these multiples has the dimension m + n − 2i and has a base of polynomials of pairwise different degrees, not smaller than i. This implies that the submatrix of the m + n − 2i first rows of the column echelon form of Ti is the identity matrix and thus that si is not 0. Thus Si is a polynomial in the image of, which is a multiple of the GCD and has the same degree. It is thus a greatest common divisor.

GCD and root finding

Square-free factorization

Most root-finding algorithms behave badly with polynomials that have multiple roots. It is therefore useful to detect and remove them before calling a root-finding algorithm. A GCD computation allows detection of the existence of multiple roots, since the multiple roots of a polynomial are the roots of the GCD of the polynomial and its derivative.
After computing the GCD of the polynomial and its derivative, further GCD computations provide the complete square-free factorization of the polynomial, which is a factorization
where, for each i, the polynomial fi either is 1 if f does not have any root of multiplicity i or is a square-free polynomial whose roots are exactly the roots of multiplicity i of f.
Thus the square-free factorization reduces root-finding of a polynomial with multiple roots to root-finding of several square-free polynomials of lower degree. The square-free factorization is also the first step in most polynomial factorization algorithms.

Sturm sequence

The Sturm sequence of a polynomial with real coefficients is the sequence of the remainders provided by a variant of Euclid's algorithm applied to the polynomial and its derivative. For getting the Sturm sequence, one simply replaces the instruction
of Euclid's algorithm by
Let V be the number of changes of signs in the sequence, when evaluated at a point a. Sturm's theorem asserts that VV is the number of real roots of the polynomial in the interval . Thus the Sturm sequence allows computing the number of real roots in a given interval. By subdividing the interval until every subinterval contains at most one root, this provides an algorithm that locates the real roots in intervals of arbitrary small length.

GCD over a ring and its field of fractions

In this section, we consider polynomials over a unique factorization domain R, typically the ring of the integers, and over its field of fractions F, typically the field of the rational numbers, and we denote R and F the rings of polynomials in a set of variables over these rings.

Primitive part–content factorization

The content of a polynomial pR, denoted "cont", is the GCD of its coefficients. A polynomial qF may be written
where pR and cR: it suffices to take for c a multiple of all denominators of the coefficients of q and p = cq. The content of q is defined as:
In both cases, the content is defined up to the multiplication by a unit of R.
The primitive part of a polynomial in R or F is defined by
In both cases, it is a polynomial in R that is primitive, which means that 1 is a GCD of its coefficients.
Thus every polynomial in R or F may be factorized as
and this factorization is unique up to the multiplication of the content by a unit of R and of the primitive part by the inverse of this unit.
Gauss's lemma implies that the product of two primitive polynomials is primitive. It follows that
and

Relation between the GCD over ''R'' and over ''F''

The relations of the preceding section imply a strong relation between the GCD's in R and in F. To avoid ambiguities, the notation "gcd" will be indexed, in the following, by the ring in which the GCD is computed.
If q1 and q2 belong to F, then
If p1 and p2 belong to R, then
and
Thus the computation of polynomial GCD's is essentially the same problem over F and over R.
For univariate polynomials over the rational numbers, one may think that Euclid's algorithm is a convenient method for computing the GCD. However, it involves simplifying a large number of fractions of integers, and the resulting algorithm is not efficient. For this reason, methods have been designed to modify Euclid's algorithm for working only with polynomials over the integers. They consist of replacing the Euclidean division, which introduces fractions, by a so-called pseudo-division, and replacing the remainder sequence of the Euclid's algorithm by so-called pseudo-remainder sequences.

Proof that GCD exists for multivariate polynomials

In the previous section we have seen that the GCD of polynomials in R may be deduced from GCDs in R and in F. A closer look on the proof shows that this allows us to prove the existence of GCDs in R, if they exist in R and in F. In particular, if GCDs exist in R, and if X is reduced to one variable, this proves that GCDs exist in R.
A polynomial in n variables may be considered as a univariate polynomial over the ring of polynomials in variables. Thus a recursion on the number of variables shows that if GCDs exist and may be computed in R, then they exist and may be computed in every multivariate polynomial ring over R. In particular, if R is either the ring of the integers or a field, then GCDs exist in R, and what precedes provides an algorithm to compute them.
The proof that a polynomial ring over a unique factorization domain is also a unique factorization domain is similar, but it does not provide an algorithm, because there is no general algorithm to factor univariate polynomials over a field.

Pseudo-remainder sequences

In this section, we consider an integral domain Z and its field of fractions Q. Given two polynomials A and B in the univariate polynomial ring Z, the Euclidean division of A by B provides a quotient and a remainder which may not belong to Z.
For, if one applies Euclid's algorithm to the following polynomials
and
the successive remainders of Euclid's algorithm are
One sees that, despite the small degree and the small size of the coefficients of the input polynomials, one has to manipulate and simplify integer fractions of rather large size.
The pseudo-division has been introduced to allow a variant of Euclid's algorithm for which all remainders belong to Z.
If and and ab, the pseudo-remainder of the pseudo-division of A by B, denoted by prem is
where is the leading coefficient of B.
The pseudo-remainder of the pseudo-division of two polynomials in Z belongs always to Z.
A pseudo-remainder sequence is the sequence of the remainders ri obtained by replacing the instruction
of Euclid's algorithm by
where α is an element of Z that divides exactly every coefficient of the numerator. Different choices of α give different pseudo-remainder sequences, which are described in the next subsections.
As the common divisors of two polynomials are not changed if the polynomials are multiplied by invertible constants, the last nonzero term in a pseudo-remainder sequence is a GCD of the input polynomials. Therefore, pseudo-remainder sequences allows computing GCD's in Q without introducing fractions in Q.
In some contexts, it is essential to control the sign of the leading coefficient of the pseudo-remainder. This is typically the case when computing resultants and subresultants, or for using Sturm's theorem. This control can be done either by replacing by its absolute value in the definition of the pseudo-remainder, or by controlling the sign of .

Trivial pseudo-remainder sequence

The simplest remainder sequence consists in taking always. In practice, it is not interesting, as the size of the coefficients grows exponentially with the degree of the input polynomials. This appears clearly on the example of the preceding section, for which the successive pseudo-remainders are
The number of digits of the coefficients of the successive remainders is more than doubled at each iteration of the algorithm. This is typical behavior of the trivial pseudo-remainder sequences.

Primitive pseudo-remainder sequence

The primitive pseudo-remainder sequence consists in taking for α the content of the numerator. Thus all the ri are primitive polynomials.
The primitive pseudo-remainder sequence is the pseudo-remainder sequence, which generates the smallest coefficients. However it requires to compute a number of GCD's in Z, and therefore is not sufficiently efficient to be used in practice, especially when Z is itself a polynomial ring.
With the same input as in the preceding sections, the successive remainders, after division by their content are
The small size of the coefficients hides the fact that a number of integers GCD and divisions by the GCD have been computed.

Subresultant pseudo-remainder sequence

A subresultant sequence can be also computed with pseudo-remainders. The process consists in choosing α in such a way that every ri is a subresultant polynomial. Surprisingly, the computation of α is very easy. On the other hand, the proof of correctness of the algorithm is difficult, because it should take into account all the possibilities for the difference of degrees of two consecutive remainders.
The coefficients in the subresultant sequence are rarely much larger than those of the primitive pseudo-remainder sequence. As GCD computations in Z are not needed, the subresultant sequence with pseudo-remainders gives the most efficient computation.
With the same input as in the preceding sections, the successive remainders are
The coefficients have a reasonable size. They are obtained without any GCD computation, only exact divisions. This makes this algorithm more efficient than that of primitive pseudo-remainder sequences.
The algorithm computing the subresultant sequence with pseudo-remainders is given [|below]. In this algorithm, the input is a pair of polynomials in Z. The are the successive pseudo remainders in Z, the variables i and are non negative integers, and the Greek letters denote elements in Z. The functions deg and rem denote the degree of a polynomial and the remainder of the Euclidean division. In the algorithm, this remainder is always in Z. Finally the divisions denoted / are always exact and have their result either in Z or in Z.
Note: "lc" stands for the leading coefficient, the coefficient of the highest degree of the variable.
This algorithm computes not only the greatest common divisor, but also all the subresultant polynomials: The remainder is the -th subresultant polynomial. If, the -th subresultant polynomial is. All the other subresultant polynomials are zero.

Sturm sequence with pseudo-remainders

One may use pseudo-remainders for constructing sequences having the same properties as Sturm sequences. This requires to control the signs of the successive pseudo-remainders, in order to have the same signs as in the Sturm sequence. This may be done by defining a modified pseudo-remainder as follows.
If and and ab, the modified pseudo-remainder prem2 of the pseudo-division of A by B is
where |lc| is the absolute value of the leading coefficient of B.
For input polynomials with integer coefficients, this allows retrieval of Sturm sequences consisting of polynomials with integer coefficients. The subresultant pseudo-remainder sequence may be modified similarly, in which case the signs of the remainders coincide with those computed over the rationals.
Note that the algorithm for computing the subresultant pseudo-remainder sequence given above will compute wrong subresultant polynomials if one uses instead of.

Modular GCD algorithm

If f and g are polynomials in F for some finitely generated field F, the Euclidean Algorithm is the most natural way to compute their GCD. However, modern computer algebra systems only use it if F is finite because of a phenomenon called intermediate expression swell. Although degrees keep decreasing during the Euclidean algorithm, if F is not finite then the bit size of the polynomials can increase during the computations because repeated arithmetic operations in F tends to lead to larger expressions. For example, the addition of two rational numbers whose denominators are bounded by b leads to a rational number whose denominator is bounded by b2, so in the worst case, the bit size could nearly double with just one operation.
To expedite the computation, take a ring D for which f and g are in D, and take an ideal I such that D/I is a finite ring. Then compute the GCD over this finite ring with the Euclidean Algorithm. Using reconstruction techniques one can recover the GCD of f and g from its image modulo a number of ideals I. One can prove that this works provided that one discards modular images with non-minimal degrees, and avoids ideals I modulo which a leading coefficient vanishes.
Suppose,, and. If we take then is a finite ring. The Euclidean algorithm applied to the images of in succeeds and returns 1. This implies that the GCD of in must be 1 as well. Note that this example could easily be handled by any method because the degrees were too small for expression swell to occur, but it illustrates that if two polynomials have GCD 1, then the modular algorithm is likely to terminate after a single ideal.