LCM: Lectures on Computational Mathematics
Przemysław Koprowski
Septempber 10, 2015 – April 25, 2021
These notes were compiled on April 25, 2021. Since I update the notes quite frequently, it is possible that a newer version is already available. Please check on: http://www.pkoprowski.eu/lcm
1
Contents
Contents 3
1 Fundamental algorithms 9 1.1 Integers and univariate polynomials ...... 9 1.2 Floating point numbers ...... 33 1.3 Euclidean algorithm ...... 38 1.4 Rational numbers and rational functions ...... 57 1.5 Multivariate polynomials ...... 58 1.6 Transcendental constants ...... 58 1.A Scaled Bernstein polynomials ...... 58
2 Modular arithmetic 79 2.1 Basic modular arithmetic ...... 79 2.2 Chinese remainder theorem ...... 82 2.3 Modular exponentiation ...... 87 2.4 Modular square roots ...... 92 2.5 Applications of modular arithmetic ...... 109
3 Matrices 115 3.1 Matrix arithmetic ...... 115 3.2 Gauss elimination ...... 118 3.3 Matrix decomposition ...... 126 3.4 Determinant ...... 126 3.5 Systems of linear equations ...... 147 3.6 Eigenvalues and eigenvectors ...... 147 3.7 Short vectors ...... 155 3.8 Special matrices ...... 168 4 Contents
4 Reconstruction and interpolation 175 4.1 Polynomial interpolation ...... 175 4.2 Runge’s phenomenon and Chebyshev nodes ...... 187 4.3 Piecewise polynomial interpolation ...... 193 4.4 Fourier transform and fast multiplication ...... 194
5 Factorization and irreducibility tests 205 5.1 Primality tests ...... 206 5.2 Integer factorization ...... 215 5.3 Square-free decomposition of polynomials ...... 216 5.4 Applications of square-free decomposition ...... 227 5.5 Polynomial factorization over finite fields ...... 232 5.6 Polynomial factorization over rationals ...... 237
6 Univariate polynomial equations 239 6.1 Generalities ...... 239 6.2 Explicit formulæ for low degree polynomials ...... 242 6.3 Bounds on roots ...... 246 6.4 Distances between roots ...... 264 6.5 Root counting and isolating ...... 268 6.6 Root approximation ...... 286 6.7 Higher degree solvable polynomials ...... 305 6.8 Strategies for solving univariate polynomial equations ...... 305 6.9 Thom’s lemma ...... 305
7 Integration 309 7.1 Symbolic integration of rational functions ...... 309 7.2 Classical numeric integration schemes ...... 312 7.3 Monte Carlo integration ...... 312
8 Classical elimination theory 313 8.1 Labatie–Kalkbrener triangulation ...... 315 8.2 Resultant ...... 327 8.3 Applications of resultants ...... 358 8.4 Subresultants ...... 375
9 Gröbner bases 381 9.1 Algebraic prerequisites ...... 381 9.2 Monomial orders ...... 383 9.3 Gauss elimination revisited ...... 385 9.4 Multivariate polynomial reduction ...... 386 9.5 Gröbner bases ...... 389 9.6 Buchberger’s criterion ...... 390 9.7 Minimal and reduced Gröbner bases ...... 392
Notes homepage: http://www.pkoprowski.eu/lcm Contents 5
9.8 Elimination of variables ...... 394
10 Zero-dimensional polynomial systems 399
A Fundamental concepts from algebra 401
Bibliography 403
Built: April 25, 2021
Introduction
Witchcraft to the ignorant,. . . simple science to the learned.
“The Sorcerer of Rhiannon”, L. Brackett
Computational mathematics is the least common multiple of mathematics and al- gorithms. This is a ‘work-in-progress’ version of lecture notes to “Introduction to com- putational mathematics” and “Computational mathematics” courses. Terry Pratchett in his novel “Interesting times” says that “Magic isn’t like maths”. Assuming symmetry of the relation, this implies that mathematics is not like magic, either. Even if they both begin with ‘ma’. They both provide their adepts with some tools to perform certain actions: spells in the case of magic and theorem in mathemat- ics. The difference is that in mathematics one knows why and how a given tool works. One may read a proof of the theorem and verify its correctness. A mathematician can check how assumptions of the theorem are used and what would happen if they were omitted. On the contrary, in magic (as far as I understand) a wizard casts a spell to obtain the desired result but he does not know why and how the spell works, which ingredients of the spell are important and what would happen if he said ‘dabraabraca’ instead of ‘abracadabra’. Nowadays, there are many mathematically oriented computer programs (co-called Computer Algebra Systems or CASs for short). Some of them are paid. Some are free. Unfortunately, many users treat them like oracles. Without understanding the princi- ples of how these systems works, using them is more magic than mathematics. Issuing a command like (x^6 - 5*x^4 + 4*x^2 - x).roots() is not very different from casting a spell, when one doesn’t understand what a system can or cannot do with an unsolvable polynomial (and what it really means that a 8 Contents polynomial is unsolvable). Thus, the aim of these notes is to explain mathematics that operates behind the scene in computer algebra systems. To show that there is really no magic in them.
Notes homepage: http://www.pkoprowski.eu/lcm 1
Fundamental algorithms
A journey of a thousand miles begins with a single step.
“Tao Te Ching”, Laozi
This chapter is devoted to introducing representations of the most elementary math- ematical objects, together with accompanying fundamental algorithm, like standard arithmetic operations. These fundamentals seem to be often neglected, but they are indispensable for developing “more serious” algorithms, as they form the basic build- ing blocks of the latter ones. Moreover, these basic arithmetic operations, although shyly hidden inside the big algorithms, are executed over and over again. Thus, their speed have a major impact on the overall performance of their bigger brothers. For foundations of mathematics the most fundamental objects are sets. In the realm of computational mathematics the elementary objects are integers (for symbolic com- putations) and floating-point numbers (for numerical methods). Hence, these two types of objects will be are main subject in the next sections. Moreover, since univari- ate polynomials are very like integers (and algorithms designed for polynomials are usually a little bit simpler) we discuss integers and polynomials together.
1.1 Integers and univariate polynomials
Integers and polynomials exhibit lots of similarities from algebraic and algorithmic points of view. On the algebraic side, the ring of integers and the ring of univariate polynomials (over a field) are both Euclidean domains. From the algorithmic point of view, integers and polynomials are just finite sequences of digits and coefficients, respectively. An integer n Z is represented by a signed, finite sequence of digits ∈ (nk nk 1 ... n0)β such that ± − k n = nkβ + + n1β + n0 , ± ··· 10 Chapter 1. Fundamental algorithms
where the integer β 2 is a fixed base of the representation and every digit ni is an element of the set 0,≥ . . . , β 1 . For example, let n be a number one hundred ninety- seven thousand six hundred{ forty-one− } , then at base ten, the number n is represented by a 6-tuple (197641)10, since
5 4 3 2 1 n = 1 10 + 9 10 + 7 10 + 6 10 + 4 10 + 1. · · · · · On the other hand, the same number is represented as (110000010000001001)2 at base 2 and as (349)256 at base 256. The choice of the base is quite arbitrary. As human beings we are used to the base 10, but the only reason for it is that we have a total of ten fingers on both hands. For computers a more natural choice is a power of 2. The base 2 is used mostly at the lowest, hardware level (in this case there are also some other representations in use, like for instance two’s-complement notation, which we will not discuss in these notes). While at higher levels of abstraction, say in computer algebra systems, the base β is commonly chosen in such a way that a single digit fits into a single memory slot. So for example, at base 256 the digits are stored as bytes, at base 65 536 as 16-bit words. . . and so on. Here we will not place any restrictions on the base nor on the length of an integer, understood as the number of digits. We will always assume that we are able to store as many digits as we need. Like integers, univariate polynomials are represented as finite sequences. Let R be a (fixed) ring of coefficients. Assume that we have means to represent elements of R and to operate on them. The simplest and most common approach is to represent a polynomial f R[x] as a sequence of its coefficients (fn,..., f1, f0), where f = n ∈ fn x + + f1 x + f0. This is called the power basis representation of f , since f0,..., fn are nothing··· else but the coordinates of f with respect to the basis 1, x,..., x k,... of the free R-module R[x]. This is not the only possible representation{ of a polynomial} and later we will discuss scaled Bernstein-basis representation (see Appendix 1.A). n Let f = fn x + + f1 x + f0 be a nonzero polynomial. The maximal index k ··· such that fk = 0 is called the degree of f and denoted deg f . The degree of the zero polynomial is6 defined to be . It will be often convenient to allow the coefficients of f to be defined for all integer−∞ indices, with the convention that f j 0 whenever j < 0 or j > deg f . Formally speaking, we view polynomials as infinite≡ sequences indexed by elements of Z with only finitely many nonzero entries. The set supp f := k Z fk = 0 of the indices of nonzero terms is called the support of f . In this {book∈ we| will6 sometimes} (see e.g. Section 6.3 of Chapter 6) encounter the notion of a reciprocal polynomial, that we now introduce.
n n Definition 1.1.1. Let f = fn x + + f1 x + f0 be a polynomial. A polynomial f0 x + n 1 ··· + fn 1 x − + fn, obtained from f by reversing the order of coefficients, is called reciprocal··· − polynomial polynomial of f and denoted rev f .
Observation 1.1.2. Let f be a polynomial, then its reciprocal polynomial equals
n 1 rev(f ) = x f . · x
Notes homepage: http://www.pkoprowski.eu/lcm 1.1. Integers and univariate polynomials 11
In what follows we assume that basic arithmetic operations on single-digit integers and coefficients of a polynomial are already available in our computing environment. How these function are internally implemented by the environment/hardware is not important for us. In case of integers, they can be as simple as hard-coded tables (re- member how we all had to memorize multiplication table at primary school) or hard- ware circuits. Using this low-level operations we will define arithmetic operations on integers and polynomials of arbitrary sizes.
Addition and subtraction The first arithmetic operation we discuss is addition. We begin with addition of poly- nomials since—as mentioned in the introduction—they are slightly easier to deal with. There are no carry to worry about. Since we represent polynomials as finite sequences of coordinates with respect to the power basis, the sum and difference are computed coefficient-wisely. If the two polynomials differ in degree we simply treat the “missing” m n coefficients as zeros. Hence, if f = fm x + + f1 x + f0 and g = gn x + + g1 x + g0, ··· k ··· then their sum h = f + g has coefficients h = hk x + + h1 x + h0, where hi = fi + gi, ··· with the convention that the exceeding coefficients are null, i.e.: fi = 0 for i > m and gi = 0 for i > n. More precisely fi + gi, if i min m, n , ≤ { } hi = fi, if n < i m, ≤ gi, if m < i n. ≤ The degree deg h of the sum satisfies h max m, n and the equality holds if m = n. ≤ { } m 6 Algorithm 1.1.3. Given two univariate polynomials f = fm x + + f1 x + f0 and n ··· g = gn x + + g1 x + g0, this algorithm computes their sum (respectively difference) h. 1. Let l be··· the minimum and k the maximum of the degrees of f and g.
2. For every i 0, . . . , l , set hi := fi + gi (respectively h := fi gi). ∈ { } − 3. If deg f > deg g, then set hi := fi for i l + 1, . . . , m . ∈ { } 4. If deg f < deg g, then set hi := gi (respectively h := gi) for i l + 1, . . . , n . k − ∈ { } 5. Return h = hk x + + h1 x + h0. ··· The above algorithm is so trivial and well known that we take the liberty to omit a rigorous proof of its correctness. Addition and subtraction of integers is very similar to their polynomial counterparts. There are only two differences. First, when dealing with integers we must take care of signs of the numbers. Secondly, if a sum of two digits exceeds β 1, then a carry appears. − Algorithm 1.1.4. Let a, b N bet two non-negative integers represented as sequences of digits (with respect to an arbitrary∈ base β): m a = (amam 1 ... a0)β = amβ + + a1β + a0 − n ··· b = (bn bn 1 ... b0)β = bnβ + + b1β + b0. − ···
Built: April 25, 2021 12 Chapter 1. Fundamental algorithms
This algorithm computes their sum c = a + b. 1. Set k := max m, n . { } 2. Initialize a carry h := 0. 3. For every i 0, . . . , k do: ∈ { } a) Compute the sum a1 + bi + h = d0 + d1β, where d0, d1 0, . . . , β 1 , with the convention that the exceeding digits are null, by definition∈ { (i.e.− ai } 0 for i > m and bi 0 for i > n). ≡ ≡ b) Set the next digit ci := d0 and update the carry setting h := d1. k+1 k 4. If the carry h is nonzero, set ck+1 := h and output c = ck+1β +ckβ + +c1β+c0; k ··· 5. Otherwise return c = ckβ + + c1β + c0. ··· Let us only remark that the sum of three digits in step 3a is well defined since the 2 left-hand-side ai + bi + h is at most (β 1) + (β 1) + 1 β for every input data, as the carry h never exceeds 1. The subtraction− of− natural numbers≤ a b for a b is analogous to the addition. − ≥
m n Algorithm 1.1.5. Let a = amβ + + a1β + a0 and b = bnβ + + b1β + b0 be two non-negative integers represented as··· sequences of digits (with respect··· to an arbitrary base β). Assume that a b. This algorithm computes their difference c = a b. ≥ − 1. Compute the difference a0 b0 = sd, where s is the sign equal either + or and − − d 0, . . . , β 1 . If s = +, then set the first digit c0 := d and initialize a carry ∈ { − } h := 0, otherwise take c0 := β d and h := 1. − 2. For all 0 < i m do: ≤ a) Compute the difference ai bi h = sd, with a convention that βi 0 for i > n. − − ≡
b) If s = +, then set the next digit ci := d and update the carry setting h := 0, otherwise take ci := β d and h := 1. − 3. Prune the trailing zeros setting k := min i 1 cj = 0 for all j i . k { ≥ | ≥ } 4. Output the result c = ckβ + + c1β + c0. ··· Addition of (signed) integers can be implemented using the two above algorithms and the formula: sgn(a) ( a + b ), if sgn(a) = sgn(b), · | | | | a + b = sgn(a) ( a b ), if sgn(a) = sgn(b) and a b , · | | − | | 6 | | ≥ | | sgn(b) ( b a ), if sgn(a) = sgn(b) and a < b . · | | − | | 6 | | | | Finally, the subtraction of arbitrary integers is expressed in terms of the addition:
a b = a + ( b), − − where the negation b ( b), boils down to the change of the sign of the sequence of 7→ − digits: (bn ... b0)β (bn ... b0)β . The only thing left is to decide, which of the two ± 7→ ∓ Notes homepage: http://www.pkoprowski.eu/lcm 1.1. Integers and univariate polynomials 13 natural numbers is bigger. This is easily achieved by the following ordering relation on the set of finite sequences. Let (am,..., a0) and (bn,..., b0) be two finite sequences with ai, bi 0, . . . , β 1 and am = 0 = bn. Define a relation as follows ∈ { − } 6 6 ≺ m < n (am,..., a0) (bn,..., b0) or ≺ ⇐⇒ m = n and am < bm.
Observation 1.1.6. Let a, b N bet two non-negative integers represented as sequences of digits (with respect to an arbitrary∈ base β):
m a = (amam 1 ... a0)β = amβ + + a1β + a0 − n ··· b = (bn bn 1 ... b0)β = bnβ + + b1β + b0. − ···
Assume that am and bn are non zero, then
a < b (am,..., a0) (bn,..., b0). ⇐⇒ ≺
Multiplication
The well known formula for polynomial multiplication is the following one. Let f = m n fm x + + f1 x + f0 and g = gn x + + g1 x + g0 be two polynomials, then their ··· k ··· product h = f g has the form hk x + + h1 x + h0, where k = m + n and · ··· i min i,m X X{ } hi := f j gi j = f j gi j, for i 0, . . . , k . (1.1) − − j=0 j=max 0,i n ∈ { } { − } This method of computing a product is known as the long multiplication. For the sake of completeness let us write it down in form of a pseudo-code.
m n Algorithm 1.1.7. Given two polynomials f = fm x + + f1 x + f0 and g = gn x + ··· + g1 x + g0, this algorithm computes their product h = f g. ··· · 1. If any of the two input polynomials is zero, then output h := 0 and terminate. 2. Set the degree of the output k := deg f + deg g. 3. For every i 0, . . . , k set ∈ { } min i,m X{ } hi := f j gi j. − j=max 0,i n { − }
k 4. Return h = hk x + + h1 x + h0. ···
Built: April 25, 2021 14 Chapter 1. Fundamental algorithms
Time complexity analysis. Let f and g be as above. When computing their product 2 using the above method, we need m n products and up to 1/4 (m + n) + 2(m + n) additions of the coefficients. Provided· that the multiplication· of coefficients is the dominating operation, the time complexity of long multiplication of polynomials is
O D2 complexity of arithmetic operations on coefficients, · where D := max deg f , deg g . Multiplication{ of integers} is basically the same as multiplication of polynomials, except that again we must handle the carry and sign.
m n Algorithm 1.1.8. Let a = (amβ + + a1β + a0) and b = (bnβ + + b1β + b0) be two integers. This algorithm± computes··· their product c := a ±b. ··· · 1. If any of the two integers is zero, then output c := 0 and terminate. 2. Set the sign s of the result to be + if the signs of a and b coincide, and to otherwise. − 3. Set k := m + n and initialize the carry h := 0. 4. For every i 0, . . . , k do: ∈ { } a) Compute d : h Pmin i,m a b . = + j=max{ 0,} i n j i j { − } − b) Write d as d = d0 + d1β, where d0, d1 0, . . . , β 1 . ∈ { − } c) Set the next digit ci := d0 and update the carry setting h := d1. k+1 k 5. If h = 0 then set ck+1 := d1 and output c = s(ck+1β + ckβ + + c1β + c0). 6 k ··· 6. Otherwise output c = s(ckβ + + c1β + c0). ··· It is clear that—like for polynomials—the time complexity of long multiplication of integers is quadratic in number of their digits. A faster multiplication algorithm was developed in early 1960s by a Russian mathematician A.A. Karatsuba1. Again, let f , g be two univariate polynomials. Take d := 1/2 max deg f , deg g . We can split both polynomials into their “lower” and “upper” parts,· as{ follows }
d d f = fu x + fl and g = gu x + gl . · · If we compute the product of f and g in a straight-forward way, we have
d d 2d d f g = (fu x + fl ) (gu x + gl ) = fu gu x + (fu gl + fl gu)x + fl gl . · · This formula computes four polynomial products: fu gu, fu gl , fl gu and fl gl . However, with a cost of some extra additions and subtractions, we may get rid of one of these multiplications. To this end write
2d d f g = fu gu x + (fu + fl )(gu + gl ) fu gu fl gl x + fl gl . · − − Here we have only three products to compute: fu gu, fl gl and (fu + fl )(gu + gl ). This observation leads us directly to the following divide et impera algorithm.
1Anatolii Alexeyevich Karatsuba (born on January 31, 1937, died on September 28, 2008) was a Russian mathematician.
Notes homepage: http://www.pkoprowski.eu/lcm 1.1. Integers and univariate polynomials 15
m n Algorithm 1.1.9 (Karatsuba, 1962). Let f = fm x + + f1 x + f0 and g = gn x + ···1 + g1 x + g0 be two univariate polynomials and let d := /2 max deg f , deg g . This algorithm··· computes the product h = f g. · { } 1. If any of the two polynomials is zero,· then output 0 and terminate. 2. If any of the two polynomials is constant, then multiply all the coefficients of the other polynomial by this constant, output the result and terminate. 3. Split both polynomials into their “upper” and “lower parts” taking
m d d 1 fu := fm x − + + fd+1 x + fd , fl := fd 1 x − + + f1 x + f0, n d ··· − d 1 ··· gu := gn x − + + gd+1 x + gd , gl := gd 1 x − + + g1 x + g0. ··· − ··· 4. Recursively compute the products:
hu := fu gu, hc := (fu + fl )(gu + gl ), hl := fl gl .
5. Combine the partial results setting
2d d h = hu x + (hc hu hl ) x + hl . · − − · 6. Return h.
Time complexity analysis. Every step of Karatsuba algorithm reduces the degrees of the polynomials involved by half. It follows that the total number of multiplications lg D lg 3 1.585 of coefficients in this method is 3d e 3 D 3 D , where D is the maximum of the degrees of f and g. Therefore the≤ time· complexity' · of integer multiplication with Karatsuba algorithm is
log 3 O(D 2 complexity of arithmetic operations on coefficients), · provided that the dominating operation is the multiplication of the coefficients. Example 1. Take two polynomials
3 2 3 2 f = x + 4x + 6x + 3, g = x + 3x + 2x + 1. The diagram in Figure 1.1 illustrates consecutive steps of Karatsuba multiplication of these two polynomials. To be completely honest, we must admit that Karatsuba originally invented his al- gorithm to multiply integers rather than polynomials. We leave it as an easy exercise for the reader to adapt Algorithm 1.1.9 to integers. As it is evident from the complexity analysis, Karatsuba multiplication is asymptotically much faster than the long multi- plication. Yet it is still not the fastest known multiplication algorithm. In Section 4.4 we discus an even faster algorithm. k Exercise 1. Multiplication of a polynomial by a monomial fk x is extremely simple. Optimize Algorithm 1.1.9 taking this into account.
Built: April 25, 2021 16 Chapter 1. Fundamental algorithms 3 3 · ? 1 ( 6 12
x - + x 2 3 + 9 ) 27 · · 12 ? ? ? 3 ( 2
x x + + iue11 aasb utpiainof multiplication Karatsuba 1.1: Figure 3 1
) - 6 12 · ? 2 7 28 · ? 4
x ( 6 x 3 + + 7 4 x 5 x
2 + - + 20 (
21 7 6 x - x x x 4 + + 2 + x 7 3 + 14 30 3 ) ) 98 49 · · + ? ? ? ? ? · x ( ( 7 3 x 4 x 3 x 3 x + + + 2 + 25 28 + 3 4 x 6 )
x 2 2 x + + + 2 12 and 3 x x + + 1
) 3
x - 3 + 7 21 3 · ? x 3 2 + 2 x 4 12 + · ? 3
1. - ( x x
+ 2 - + 4 5 ) 7 20 · · ? ? ? x 4 ( + x + 12 3
) - 1 1 · ? 1
Notes homepage: http://www.pkoprowski.eu/lcm 1.1. Integers and univariate polynomials 17
Exponentiation
Once we have dealt with multiplication, it is time to concentrate at exponentiation. A general method is the same for every input type. Hence, although we present this algorithm in the section devoted to integers and polynomials it can be used with any type for which multiplication is defined. The multiplication does not even have to be commutative for this algorithm to work correctly. However, for some types of objects, there are far more efficient solutions than the one discussed below. Let a be an element of a ring R, in which we can compute products. The natural ex- ponent an is just a product n copies of a. Therefore the simplest algorithm to compute the n-th power is to multiply the intermediate result by a in a loop. 1. Initialize r := a. 2. For i 1, . . . , n 1 update r setting r := r a. ∈ { − } · 3. Output r. This method requires n 1 multiplications. We may significantly reduce the number − of multiplications using repeated squaring. Write n as a binary number (dk dk 1 ... d0)2 so that − k k 1 n = 2 dk + 2 − dk 1 + + 2 d1 + d0, · · − ··· · n where di 0, 1 for i 0, . . . , k . It follows that the power a can be expressed as a product ∈ { } ∈ { }
n d 2d1 2k dk a = a 0 a a . · ··· If a digit di is null, then the corresponding power of a does not contribute to the overall result and may be omitted. Thus, we can find the n-th power of a by computing i successive powers a2 and taking a product of those that correspond to nonzero digits in binary expansion of the exponent.
Algorithm 1.1.10. Given an element a of a ring R and a natural exponent n 1, this algorithm computes an. ≥
1. Let (dk ... d0)2 be the binary representation of the exponent n; 2. Initialize r to be the unit element of R (i.e. r = 1 in R) and set b := a; 3. For each i 0, . . . , k do: ∈ { } a) If di = 1, then update r setting r := r b. · b) Replace b by its square b2. 4. Output r.
Time complexity analysis. This algorithm needs at most 2 lg(n) multiplication in the ring R. · d e
Built: April 25, 2021 18 Chapter 1. Fundamental algorithms
Division and pseudo-division The last of the four basic arithmetic operations is division. In general a quotient of two integers may no longer be an integer. Take for example 7/2. It is a rational number but not an integer. Likewise a quotient of two polynomials is a rational function, but not necessarily a polynomial. It is where the division with a reminder comes into play. We claim that in a ring K[x] of polynomial in variable x over a field K the quotient and remainder of polynomial division are well defined and unique. (The same is true for integers.) Take two polynomials f and g with coefficients in some field. Denote their degrees respectively by m := deg f , n := deg g and assume that g = 0. If m < n, then we can write 6
f = 0 g + r, where r = f , deg r < deg g. (1.2) · Suppose now that m n. Let cf := lc(f ) and cg := lc(g) be the leading coefficients of the two polynomials.≥ Take c f m n r := f g x − . (1.3) − cg · · It is clear that the leading term of f gets canceled and so deg r < deg f . Now, if deg r < deg g then we are in situation of (1.2). Otherwise we may repeat the above reduction using r in place of f . This procedure produces a sequence of polynomials with decreasing degrees. Hence it must terminate after finitely many steps.
Proposition 1.1.11. Let f , g K[x] be two polynomials with coefficients in some field K. If g is nonzero, then there are∈ unique polynomials q, r K[x] such that ∈ f = q g + r and deg r < deg g. (1.4) · Proof. The existence follows from the preceding discussion. As for the uniqueness, suppose that there are two pairs (q1, r1) and (q2, r2) satisfying the assertion. Then
q1 g + r1 = f = q2 g + r2, · · thus r1 r2 is divisible by g, but deg(r1 r2) max deg r1, deg r2 < deg g. Hence − − ≤ { } r1 r2 = 0 and it follows that q1 g = q2 g so also q1 = q2. − · · The polynomials q and r of Proposition 1.1.11 are called respectively the quotient and remainder of the division of f by g. In what follows we will write (f mod g) for the remainder. In other words, we think of the computation of the remainder as of a binary operator. The earlier discussion provides a method for computing q and r. Let us rewrite is in form of an algorithm.
m Algorithm 1.1.12 (Polynomial long division). Let f = fm x + + f1 x + f0 and g = n ··· gn x + + g1 x + g0 be two polynomials with coefficients in a field K. Assume that g = 0. This algorithm··· computes the quotient q and the remained r of f modulo g. 6 1 1. Let c := /gn be the inverse of the leading coefficient of g.
Notes homepage: http://www.pkoprowski.eu/lcm 1.1. Integers and univariate polynomials 19
2. Initialize the quotient q := 0 and remainder r := f . 3. As long as the degree of r is greater than or equal to the degree of g do: a) Compute the next coefficient of the quotient setting d := c lc(r). · b) Update the quotient setting q := d + x q (i.e. prepend d at the beginning of the list of coefficients of q). · deg r n c) Update the remainder setting r := r d g x − . − · · 4. Output the quotient q and remainder r.
Time complexity analysis. We should count the number of arithmetic operations used by the algorithm. Each iteration of the main loop reduces the degree of r, hence the loop will be executed at most m n + 1 times. Step (3a) uses exactly one scalar multiplication. Step (3b) does not involve− any arithmetic operations and step (3c) multiplies a polynomial by a scalar and then subtracts two polynomials, hence uses up to n scalar multiplications and the same number of subtractions. Thus, the total number of arithmetic operations in the algorithm does not exceed: •2 (m n + 1) (n + 1) multiplications, · − · • (m n + 1) n subtractions. Consequently,− we· see that the time complexity of Algorithm 1.1.12 is
O(m n complexity of arithmetic operations on coefficients). · ·
Exercise 2. The reduction of the remainder in step (3c) always cancels the leading term of r, nevertheless it may possibly kill some more terms, too. If these additionally removed terms directly succeed the leading term, then next few iterations of the loop can be omitted. They would just prepend zero to the list of coefficients of q and leave r untouched. Improve Algorithm 1.1.12 so that this empty iterations are not executed (the quotient q must still be correctly computed). An integer counterpart of Proposition 1.1.11 is the following one:
Proposition 1.1.13. If a, b are two integers with b = 0, then there are unique integers q, r such that 6 a = q b + r and 0 r < b . · ≤ | | The arguments are fully analogous to the polynomial case, except that in place of the degree we now use absolute value. We let the reader write down a rigorous proof. The corresponding algorithm resembles Algorithm 1.1.12 but is significantly more complicated. For details we refer the reader to a throughout exposition in Knuth’s book [42, Algorithm D, Section 4.3.1]. The assumption of Proposition 1.1.11 that K is a field is indispensable as the as- sertion no longer holds if one replaces K with an arbitrary ring. Yet, there are many situations, when we need to divide polynomials over rings which are not fields. As we
Built: April 25, 2021 20 Chapter 1. Fundamental algorithms will later see, the rings that are very important include: the integers Z, the polyno- mial rings over the integers Z[x] and Z[x1,..., xk] and polynomial rings over fields K[x], K[x1,...,.xk]. Neither of them is a field, though. Anyway, if we look carefully at Algorithm 1.1.12, we may notice that there is only one place where coefficients are divided, it is step (3a). Furthermore it is always the leading coefficient of g that we are dividing by. One consequence of this fact is:
Observation 1.1.14. If the polynomial g is monic (which means that lc(g) = 1), then Algorithm 1.1.12 works correctly without the assumption that K is a field.
Now suppose that g is not monic. We can still remedy the problem of the division being possibly undefined, by pre-multiplying the polynomial f by an appropriate power of lc(g), so that all the divisions that occur will be well defined.
Proposition 1.1.15. Let R be an integral domain, f , g R[x] be two polynomials and assume that g is nonzero. Then there are unique polynomials∈ q, r R[x] such that ∈ d lc(g) f = q g + r, (1.5) · · where deg r < deg g and d = max 0, deg f deg g + 1 . { − } Proof. Proceed by induction on the degree of f . For deg f < deg g, the trivial solution is q = 0 and f = r. Assume that deg f deg g > 0 and the assertion holds for all polynomials of degree smaller than deg f≥. Denote the degree of f by m, the degree of g by n and take a difference
m n h := lc(g) f lc(f ) g x − . (1.6) · − · · It is clear that the leading terms cancel, hence deg h < m and we may apply the induc- tive hypothesis to it. There are unique polynomials q1, r1 R[x] such that ∈ d1 lc(g) h = q1 g + r1, (1.7) · · with d1 = max 0, deg h n + 1 and deg r1 < n. We claim that d > d1. Indeed, the { − } number d1 equals either 0 or deg h n + 1. In the first case, d1 < 1 m n + 1 = d, − ≤ − since m n, by assumption. On the other hand, if d1 = deg h n+1, the claim follows from the≥ fact that deg h < m. − Combining (1.6) with (1.7) and rearranging the terms we write
d1+1 d1 m n lc(g) f = q1 + lc(g) lc(f ) x − g + r1. · · · ·
d d1 1 Multiply both sides by lc(g) − − . By the previous paragraph, it is a non-negative power of an element of R, hence an element of R itself. This yields:
d d d1 1 d 1 m n d d1 1 lc(g) f = q1 lc(g) − − + lc(g) − lc(f ) x − g + lc(g) − − r1 = q g + r. · · · · · · · Notes homepage: http://www.pkoprowski.eu/lcm 1.1. Integers and univariate polynomials 21
It remains to prove the uniqueness of q and r. Suppose there are q1, q2 and r1, r2 such that deg r1, deg r2 < deg g and d q1 g + r1 = lc(f ) f = q2 g + r2. · · · It follows that r1 r2 is divisible by g, but this is possible only when r1 r2 = 0 due − − to the fact that deg g > deg(r1 r2). Thus also q1 g = q2 g and so q1 = q2, as claimed. − · · Definition 1.1.16. The polynomials q and r in the above propositions are called re- spectively: the pseudo-quotient and pseudo-remainder of f modulo g. Algorithm 1.1.17 (Polynomial pseudo-division). Let f and g be two polynomials with coefficients in an integral domain R. Denote m := deg f , n := deg g and assume that g is nonzero. This algorithm computes the pseudo-quotient q and pseudo-remained r of f modulo g. 1. If the degree of g is strictly greater that the degree of f , then output q = 0, r = f and terminate. 2. Let c := lc(g) be the leading coefficient of g. 3. Initialize the pseudo-quotient q := 0 and pseudo-remainder r := f . 4. As long as the degree of r is is greater than or equal to the degree of g do: m n a) Compute the next coefficient of the pseudo-quotient d := lc(r) c − . b) Update the pseudo-quotient setting q := d + x q, · · deg r n c) Update the pseudo-remainder setting r := c r lc(r) g x − . 5. Output q and r. · − · · It is evident that the asymptotic behavior of pseudo-division (in terms of number of arithmetic operations on coefficients) is the same as of polynomial division (Algo- rithm 1.1.12), although the exact number of operations is slightly greater. The follow- ing result is a useful consequence of Proposition 1.1.15. It is sometimes called “Little Bézout Theorem”, however it was proved by R. Descartes2 already in 1637, hence long before É. Bézout3 was even born. Proposition 1.1.18. Let f be a polynomial over an integral domain R and ξ R. Then ξ is a root of f if an only if (x ξ) divides f in R[x]. ∈ − Proof. Take any ξ R, not necessarily a root of f . Proposition 1.1.15 assert that there are unique polynomials∈ q and r such that
f = q (x ξ) + r · − and deg r < deg(x ξ) = 1. This means that r must be a constant. Evaluating both sides at ξ we get f−(ξ) = 0 + r(ξ) = r. In particular f (ξ) = 0 if and only if r = 0. Where the latter condition means that x ξ divides f . − 2René Descartes (born 31 March 1596, died 11 February 1650) was a French philosopher and mathe- matician. 3Étienne Bézout (born 31 March 1730, died 27 September 1783) was a French mathematician.
Built: April 25, 2021 22 Chapter 1. Fundamental algorithms
Corollary 1.1.19. Over an integral domain, a polynomial of degree n > 0 has no more than n roots.
Binomial coefficients Binomial coefficients appear in many algorithm, hence we should learn how to com- pute them. In Section 4.4 of Chapter 4 we will discuss an effective method to compute binomial coefficients in bulk using fast Fourier transform. But before we deal with ad- vanced techniques, we should learn elementary ones. This is the goal of this section. In school we usually learn the following definition of a binomial coefficient:
n n! := , (1.8) k (n k)! k! − where n, k are two non-negative integers and k n. In addition, for k > n we set n ≤ n k := 0. This definition has a natural interpretation in combinatorics, since k is the numbers of subsets of size k of a set of size n. Unfortunately, formula (1.8) is horrible when it comes to actually computing a binomial coefficient. Imagine we want 100 to calculate 98 . The answer is 4 950. But if we try to compute the numerator and denominator separately, we would have to deal with numbers having over 150 decimal digits. A better way to compute the binomial coefficient is to use the following recursive formula n n 1, if n k > 0, n k k−1 1,· − if n ≥ 0 and k 0, (1.9) k = = 0, otherwise.≥ Additionally we have the following well known identity to help: