Fast Multiplication Polynomial multiplication 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. Polynomial multiplication (-5, 1, 2) evaluation interpolation 或者 {(-1,-4),(0,-5),(1,-2)} (p(-1), p(0), p(1)) by Lagrange Interpolating Polynomial
1. Polynomial multiplication Lagrange Interpolating Polynomial
1. Polynomial multiplication Given coefficients (a0,a1,a2,…,an-1) and (b0,b1,b2,…,bn-1) defining two polynomials, p() and q(), and number x, compute p(x)q(x).
1. Polynomial multiplication P(x)=a0+a1x+…+an-1xn-1 q(x)=b0+b1x+…+bn-1xn-1 (a0,a1,a2,…,an-1) (b0,b1,b2,…,bn-1) 選2n+1個點 (p(w0), p(w1),…, p(w2n) ) (q(w0), q(w1),…, q(w2n) ) evaluation multiplication Polynomial Multiplication (p(w0)*q(w0), p(w1)*q(w1),… , p(w2n)*q(w2n) ) (c0,c1,c2,…,cn,…,c2n-1) interpolation
1. Polynomial multiplication P(x)=5+x q(x)=1+2x (5,1) (1,2) 選3個點 (p(0)=5, p(1)=6, p(2)=7 ) (q(0)=1, q(1)=3, q(2)=5 ) evaluation multiplication Polynomial Multiplication C(0)=5, C(1)=18, C(2)=3 (5,11,2) interpolation
1. Polynomial multiplication 慎選特殊的點可以降低運算 Evaluation O(n) Total O(n2)
1. Polynomial multiplication Polynomial Evaluation Horner’s Rule: Given coefficients (a0,a1,a2,…,an-1), defining polynomial Given x, we can evaluate p(x) in O(n) time using the equation Eval(A,x): [Where A=(a0,a1,a2,…,an-1)] If n=1, then return a0 Else, Let A’=(a1,a2,…,an-1) [assume this can be done in constant time] return a0+x*Eval(A’,x)
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
2.Toom–Cook method:Toom-3 N= 9 8765 4321 9876 5432 1098 (1) Splitting (將整數轉成多項式) 切成3等份,取base B=108 *http://en.wikipedia.org/wiki/Toom%E2%80%93Cook_multiplication m0 = 56789012 m1 = 78901234 m2 = 123456 n0 = 54321098 n1 = 43219876 n2 = 98765 p(x) = m0 + m1x + m2x2 q(x) = n0 + n1x + n2x2
2.Toom–Cook method:Toom-3 (2) Evaluation (找出多項式對應的點) p(x) = m0 + m1x + m2x2 q(x) = n0 + n1x + n2x2 p(0) = m0 = 56789012 = 56789012 p(1) = m0 + m1 + m2 = 56789012 + 78901234 + 123456 = 135813702 p(−1) = m0 − m1 + m2 = 56789012 − 78901234 + 123456 = −21988766 p(−2) = m0 − 2m1 + 4m2 = 56789012 − 2×78901234 + 4×123456 = −100519632 p(∞) = m2 = 123456 = 123456 q(0) = n0 = 54321098 = 54321098 q(1) = n0 + n1 + n2 = 54321098 + 43219876 + 98765 = 97639739 q(−1) = n0 − n1 + n2 = 54321098 − 43219876 + 98765 = 11199987 q(−2) = n0 − 2n1 + 4n2 = 54321098 − 2×43219876 + 4×98765 = −31723594 q(∞) = n2 = 98765 = 98765 *http://en.wikipedia.org/wiki/Toom%E2%80%93Cook_multiplication
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 *http://en.wikipedia.org/wiki/Toom%E2%80%93Cook_multiplication
2.Toom–Cook method:Toom-3 (4) Interpolation (由點找出多項式) *http://en.wikipedia.org/wiki/Toom%E2%80%93Cook_multiplication
2.Toom–Cook method:Toom-3 (4) Interpolation (由點找出多項式) *http://en.wikipedia.org/wiki/Toom%E2%80%93Cook_multiplication
2.Toom–Cook method:Toom-3 (4) Interpolation (由點找出多項式) one sequence given by Bodrato *http://en.wikipedia.org/wiki/Toom%E2%80%93Cook_multiplication
2.Toom–Cook method:Toom-3 (4) Interpolation (由點找出多項式) *http://en.wikipedia.org/wiki/Toom%E2%80%93Cook_multiplication
2.Toom–Cook method:Toom-3 (5) Recomposition (將多項式轉成整數) *http://en.wikipedia.org/wiki/Toom%E2%80%93Cook_multiplication
2.Toom–Cook method: Complexity Toom-3 reduces 9 multiplications to 5, and runs in Θ(nlog(5)/log(3)), about Θ(n1.465). In general, Toom-k runs in Θ(c(k)ne), where e = log(2k-1) / log(k), ne is the time spent on sub-multiplications, and c is the time spent on additions and multiplication by small constants *http://en.wikipedia.org/wiki/Toom%E2%80%93Cook_multiplication (in Knuth’s book)
Polynomial multiplication 慎選特殊的點可以降低運算-Primitive Roots of Unity 執行DFT 得到 只需O(nlogn) Evaluation O(n) Total O(n2)
3. Primitive Roots of Unity A number w is a primitive n-th root of unity, for n>1, if wn = 1 The numbers 1, w, w2, …, wn-1 are all distinct Example 1: 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 e2pi/n is a primitive n-th root of unity, where
3. Primitive Roots of Unity Inverse Property: If w is a primitive root of unity, then w -1=wn-1 Proof: wwn-1=wn=1 Cancellation Property: For non-zero -n<k<n, Proof: Reduction Property: If w is a primitve (2n)-th root of unity, then w2 is a primitive n-th root of unity. Proof: If 1,w,w2,…,w2n-1 are all distinct, so are 1,w2,(w2)2,…,(w2)n-1 Reflective Property: If n is even, then wn/2 = -1. Proof: By the cancellation property, for k=n/2: Corollary: wk+n/2= -wk.
4.The Discrete Fourier Transform Given coefficients (a0,a1,a2,…,an-1) for an (n-1)-degree polynomial p(x) The Discrete Fourier Transform is to evaluate p at the values 1,w,w2,…,wn-1 We produce (y0,y1,y2,…,yn-1), where yj=p(wj)
4.The Discrete Fourier Transform 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,w2,…,wn-1 Matrix form: a=F -1y, where F -1[i,j]=w-ij/n. 註: the matrix F 中column彼此互相orthogonal滿足 FF*=nI,其中F*為F之conjugate
4.The Discrete Fourier Transform The DFT and inverse DFT really are inverse operations Proof: Let A=F -1F. We want to show that A=I, where If i=j, then If i and j are different, then
4.The Discrete Fourier Transform Convolution 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…
4.The Discrete Fourier Transform Convolutions Convolutions 127356 (3,11,37,47,42) 1 2 7 × 3 5 6 6 12 42 5 10 35 3 6 21 3 11 37 47 42
4.The Discrete Fourier Transform Convolutions 127356 1 2 7 1 2 7 × × 3 5 6 3 5 6 6 12 42 6 12 42 5 10 35 5 10 35 3 6 21 3 6 21 3 11 37 47 42 3 11 37 47 42 + 3 11 - 3 11 37 50 53 37 44 31 acyclic convolutions (37,50,53) negacyclic convolutions (37,44,31)
4.The Discrete Fourier Transform Convolution theorem CyclicConvolution(X, Y) = IDFT(DFT(X) · DFT(Y))
5.The Fast Fourier Transform The FFT is an efficient algorithm for computing the DFT The FFT is based on the divide-and-conquer paradigm: If n is even, we can divide a polynomial into two polynomials and we can write
5.The Fast Fourier Transform Given coefficients (a0,a1,a2,…,a7) and w primitive 8-th root of unity
5.The Fast Fourier Transform
5.The Fast Fourier Transform
5.The Fast Fourier Transform 令
5.The Fast Fourier Transform a0 a1 a2 a3 a4 a5 a6 a7
5.The Fast Fourier Transform T(n)=2T(n/2)+O(n) The running time is O(n log n). [inverse FFT is similar]
Polynomial multiplication p(x)=a0+a1x+…+an-1xn-1 q(x)=b0+b1x+…+bn-1xn-1 選w primitive m-th root of unity (m2n) (a0,a1,a2,…,an-1) (b0,b1,b2,…,bn-1) (p(w0), p(w1),…, p(wm-1) ) (q(w0), q(w1),…, q(wm-1) ) Evaluation FFT multiplication Polynomial Multiplication (p(w0)*q(w0), p(w1)*q(w1),… , p(wm-1)*q(wm-1) ) (c0,c1,c2,…,cn,…,cm-1) The running time is O(n log n). Interpolation IFFT
6.Schönhage-Strassen’s method 給定兩大數M,N。計算M*N (1) Splitting (將整數轉成多項式) 令B=2k,則可產生degree 最多n-1之兩個多項式 p(x)=a0+a1B+…+an-1Bn-1 q(x)=b0+b1B+…+bn-1Bn-1 P=(a0,a1,a2,…,an-1) Q=(b0,b1,b2,…,bn-1) (2) Evaluation (找出多項式對應的點) 選w primitive m-th root of unity (m2n)
6. Schönhage-Strassen’s method (3) Pointwise multiplication (點値相乘) (4) Interpolation (由點找出多項式) 對上述點執行the inverse Fourier transform
6. Schönhage-Strassen’s method (5)Recomposition (將多項式轉成整數) 令x=B
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 where ε ≅ 1.e-16 α ≤ 6n2B2log(n)ε the number of digits of these basic numbers should be of the order of log(B)+log(n)
6. Schönhage-Strassen’s method Complexity 分析 The running time is O(n log n(log(B) + log(n))2) 然而如果針對所有乘法都採用recursive觀念,即 得到
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 M. Bodrato. Toward Optimal Toom-Cook Multiplication.... In WAIFI'07, Springer, 2007 Schönhage and V. Strassen, "Schnelle Multiplikation großer Zahlen", Computing 7 (1971), pp. 281–292. Martin Fürer, "Faster integer multiplication", STOC 2007 Proceedings, pp. 57-66