Fast Multiplication 1 Polynomial multiplication 2 ToomCook method






















![4. The Discrete Fourier Transform n n Matrix form: y=Fa, where F[i, j]=wij. The 4. The Discrete Fourier Transform n n Matrix form: y=Fa, where F[i, j]=wij. The](https://slidetodoc.com/presentation_image_h2/b3d710304e9562e60294cf2f7a17e22f/image-23.jpg)



















- Slides: 42
Fast Multiplication 1. Polynomial multiplication 2. Toom–Cook method 3. Primitive Roots of Unity 4. The Discrete Fourier Transform 5. The FFT Algorithm 6. Schönhage-Strassen’s method 7. Conclusion 1
1. Polynomial multiplication n Polynomial 表示式: (-5, 1, 2) evaluation 或者 {(-1, -4), (0, -5), (1, -2)} interpolation (p(-1), p(0), p(1)) by Lagrange Interpolating Polynomial 2
1. Polynomial multiplication Lagrange Interpolating Polynomial 3
1. Polynomial multiplication Polynomial Multiplication n Given coefficients (a 0, a 1, a 2, …, an-1) and (b 0, b 1, b 2, …, bn-1) defining two polynomials, p() and q(), and number x, compute p(x)q(x). 4
1. Polynomial multiplication Polynomial Multiplication (a 0, a 1, a 2, …, an-1) (b 0, b 1, b 2, …, bn-1) P(x)=a 0+a 1 x+…+an-1 xn-1 q(x)=b 0+b 1 x+…+bn-1 xn-1 選 2 n+1個點 (p(w 0), p(w 1), …, p(w 2 n) ) (q(w 0), q(w 1), …, q(w 2 n) ) evaluation multiplication Polynomial Multiplication (c 0, c 1, c 2, …, cn, …, c 2 n-1) (p(w 0)*q(w 0), p(w 1)*q(w 1), … , p(w 2 n)*q(w 2 n) ) interpolation 5
1. Polynomial multiplication Polynomial Multiplication (5, 1) (1, 2) 選 3個點 evaluation P(x)=5+x q(x)=1+2 x (p(0)=5, p(1)=6, p(2)=7 ) (q(0)=1, q(1)=3, q(2)=5 ) multiplication Polynomial Multiplication C(0)=5, C(1)=18, C(2)=3 (5, 11, 2) interpolation 6
1. Polynomial multiplication 慎選特殊的點可以降 低運算 Evaluation O(n) Total O(n 2) 7
1. Polynomial multiplication Polynomial Evaluation n n Horner’s Rule: n Given coefficients (a 0, a 1, a 2, …, an-1), defining polynomial n Given x, we can evaluate p(x) in O(n) time using the equation Eval(A, x): n n [Where A=(a 0, a 1, a 2, …, an-1)] If n=1, then return a 0 Else, n n Let A’=(a 1, a 2, …, an-1) return a 0+x*Eval(A’, x) [assume this can be done in constant time] 8
2. Toom–Cook method Toom-k : a simplification of a description of Toom–Cook polynomial multiplication described by Marco Bodrato in 2007. 給定兩整數M, N,擬計算M*N five main steps: Splitting (將整數轉成多項式) Evaluation (找出多項式對應的點) Pointwise multiplication (點値相乘) Interpolation (由點找出多項式) Recomposition (將多項式轉成整數) Marco Bodrato. Towards Optimal Toom–Cook Multiplication for Univariate and Multivariate Polynomials in Characteristic 2 and 0. In WAIFI'07 proceedings, volume 4547 of LNCS, pages 116 -133. June 21 -22, 2007. author website 9
2. Toom–Cook method: Toom-3 : 例子說明* M = 12 3456 7890 1234 5678 9012 N= 9 8765 4321 9876 5432 1098 (1) Splitting (將整數轉成多項式) 切成 3等份,取base B=108 m 0 = 56789012 m 1 = 78901234 m 2 = 123456 n 0 = 54321098 n 1 = 43219876 n 2 = 98765 p(x) = m 0 + m 1 x + m 2 x 2 q(x) = n 0 + n 1 x + n 2 x 2 10
2. Toom–Cook method: Toom-3 : 例子說明* (2) Evaluation (找出多項式對應的點) p(x) = m 0 + m 1 x + m 2 x 2 q(x) = n 0 + n 1 x + n 2 x 2 p(0) = m 0 = 56789012 p(1) = m 0 + m 1 + m 2 = 56789012 + 78901234 + 123456 = 135813702 p(− 1) = m 0 − m 1 + m 2 = 56789012 − 78901234 + 123456 = − 21988766 p(− 2) = m 0 − 2 m 1 + 4 m 2 = 56789012 − 2× 78901234 + 4× 123456 = − 100519632 p(∞) = m 2 = 123456 q(0) = n 0 = 54321098 q(1) = n 0 + n 1 + n 2 = 54321098 + 43219876 + 98765 = 97639739 q(− 1) = n 0 − n 1 + n 2 = 54321098 − 43219876 + 98765 = 11199987 q(− 2) = n 0 − 2 n 1 + 4 n 2 = 54321098 − 2× 43219876 + 4× 98765 = − 31723594 q(∞) = n 2 = 98765 11
2. Toom–Cook method: Toom-3 : 例子說明* (3) Pointwise multiplication (點値相乘) r(x)=p(x)q(x) r(0) = p(0)q(0) = 56789012 × 54321098 = 3084841486175176 r(1) = p(1)q(1) = 135813702 × 97639739 = 13260814415903778 r(− 1) = p(− 1)q(− 1) = − 21988766 × 11199987 = − 246273893346042 r(− 2) = p(− 2)q(− 2) = − 100519632 × − 31723594 = 3188843994597408 r(∞) = p(∞)q(∞) = 123456 × 98765 = 12193131840 12
2. Toom–Cook method: Toom-3 : 例子說明* (4) Interpolation (由點找出多項式) 13
2. Toom–Cook method: Toom-3 : 例子說明* (4) Interpolation (由點找出多項式) 14
2. Toom–Cook method: Toom-3 : 例子說明* (4) Interpolation (由點找出多項式) one sequence given by Bodrato 15
2. Toom–Cook method: Toom-3 : 例子說明* (4) Interpolation (由點找出多項式) 16
2. Toom–Cook method: Toom-3 : 例子說明* (5) Recomposition (將多項式轉成整數) 17
2. Toom–Cook method: Complexity Toom-3 reduces 9 multiplications to 5, and runs in Θ(nlog(5)/log(3)), about Θ(n 1. 465). In general, Toom-k runs in Θ(c(k)ne), where e = log(2 k-1) / log(k), ne is the time spent on sub-multiplications, and c is the time spent on additions and multiplication by small constants (in Knuth’s book) 18
Polynomial multiplication 慎選特殊的點可以降低運算Primitive Roots of Unity 執行DFT 得到 只需O(nlogn) Evaluation O(n) Total O(n 2) 19
3. Primitive Roots of Unity n A number w is a primitive n-th root of unity, for n>1, if n n n Example 1: n n wn = 1 The numbers 1, w, w 2, …, wn-1 are all distinct Z*11: 22=4, 62=3, 72=5, 82=9 are 5 -th roots of unity in Z*11 2 -1=6, 3 -1=4, 4 -1=3, 5 -1=9, 6 -1=2, 7 -1=8, 8 -1=7, 9 -1=5 Example 2: The complex number e 2 pi/n is a primitive n-th root of unity, where 20
3. Primitive Roots of Unity n Inverse Property: If w is a primitive root of unity, then w -1=wn-1 n n Proof: wwn-1=wn=1 Cancellation Property: For non-zero -n<k<n, n Proof: Reduction Property: If w is a primitve (2 n)-th root of unity, then w 2 is a primitive n-th root of unity. n n Proof: If 1, w, w 2, …, w 2 n-1 are all distinct, so are 1, w 2, (w 2)2, …, (w 2)n-1 Reflective Property: If n is even, then wn/2 = -1. n Proof: By the cancellation property, for k=n/2: n Corollary: wk+n/2= -wk. 21
4. The Discrete Fourier Transform n n Given coefficients (a 0, a 1, a 2, …, an-1) for an (n-1)-degree polynomial p(x) The Discrete Fourier Transform is to evaluate p at the values n 1, w, w 2, …, wn-1 n We produce (y 0, y 1, y 2, …, yn-1), where yj=p(wj) 22
4. The Discrete Fourier Transform n n Matrix form: y=Fa, where F[i, j]=wij. The Inverse Discrete Fourier Transform recovers the coefficients of an (n-1)-degree polynomial given its values at 1, w, w 2, …, wn-1 n Matrix form: a=F -1 y, where F -1[i, j]=w-ij/n. 註: the matrix F 中column彼此互相orthogonal滿足 FF*=n. I,其中F*為F之conjugate 23
4. The Discrete Fourier Transform n The DFT and inverse DFT really are inverse operations Proof: Let A=F -1 F. We want to show that A=I, where n If i=j, then n If i and j are different, then n 24
4. The Discrete Fourier Transform Convolution n n The DFT and the inverse DFT can be used to multiply two polynomials So we can get the coefficients of the product polynomial quickly if we can compute the DFT (and its inverse) quickly… 25
4. The Discrete Fourier Transform n Convolutions 127 356 (3, 11, 37, 42) 1 2 7 3 5 6 6 12 42 5 10 35 6 21 × 3 Convolutions 3 11 37 47 42 26
4. The Discrete Fourier Transform n Convolutions 127 356 × 1 3 2 5 7 6 6 12 42 5 10 35 3 6 21 3 11 37 + 37 47 42 3 11 37 3 11 - 50 53 acyclic convolutions 37 (37, 50, 53) 47 42 3 11 44 31 negacyclic convolutions (37, 44, 31) 27
4. The Discrete Fourier Transform n Convolution theorem Cyclic. Convolution(X, Y) = IDFT(X) · DFT(Y)) 28
5. The Fast Fourier Transform n n The FFT is an efficient algorithm for computing the DFT The FFT is based on the divide-and-conquer paradigm: n If n is even, we can divide a polynomial into two polynomials and we can write 29
5. The Fast Fourier Transform n Given coefficients (a 0, a 1, a 2, …, a 7) and w primitive 8 -th root of unity 30
5. The Fast Fourier Transform 31
5. The Fast Fourier Transform 32
5. The Fast Fourier Transform 令 33
5. The Fast Fourier Transform a 0 a 1 a 2 a 3 a 4 a 5 a 6 a 7 34
5. The Fast Fourier Transform T(n)=2 T(n/2)+O(n) The running time is O(n log n). [inverse FFT is similar] 35
Polynomial multiplication Polynomial Multiplication (a 0, a 1, a 2, …, an-1) (b 0, b 1, b 2, …, bn-1) p(x)=a 0+a 1 x+…+an-1 xn-1 q(x)=b 0+b 1 x+…+bn-1 xn-1 選w primitive m-th root of unity Evaluation FFT Polynomial Multiplication (m 2 n) (p(w 0), p(w 1), …, p(wm-1) ) (q(w 0), q(w 1), …, q(wm-1) ) multiplication (p(w 0)*q(w 0), p(w 1)*q(w 1), … , p(wm-1)*q(wm-1) ) (c 0, c 1, c 2, …, cn, …, cm-1) The running time is O(n log n). Interpolation IFFT 36
6. Schönhage-Strassen’s method 給定兩大數M, N。計算M*N (1) Splitting (將整數轉成多項式) 令B=2 k, 則可產生degree 最多n-1之兩個多項式 p(x)=a 0+a 1 B+…+an-1 Bn-1 P=(a 0, a 1, a 2, …, an-1) q(x)=b 0+b 1 B+…+bn-1 Bn-1 Q=(b 0, b 1, b 2, …, bn 1) (2) Evaluation (找出多項式對應的點) 選w primitive m-th root of unity (m 2 n) 37
6. Schönhage-Strassen’s method (3) Pointwise multiplication (點値相乘) (4) Interpolation (由點找出多項式) 對上述點執行the inverse Fourier transform 38
6. Schönhage-Strassen’s method (5)Recomposition (將多項式轉成整數) 令x=B 39
6. Schönhage-Strassen’s method Complexity 分析 The running time is O(n log n) 單位? The bound on the numerical errors α on the ci after the FFT process can be proved to be α ≤ 6 n 2 B 2 log(n)ε where ε ≅ 1. e-16 the number of digits of these basic numbers should be of the order of log(B)+log(n) 40
6. Schönhage-Strassen’s method Complexity 分析 The running time is O(n log n(log(B) + log(n))2) 然而如果針對所有乘法都採用recursive觀念,即 得到 41
7. Conclusion Method* complexity integer classical Karatsuba 1962 10, 000位數 Toom– 3 1963 Toom–Cook (knuth) 1966 40, 000位數 Schönhage. Strassen 1971 Fürer 2007 42