1 fast multiplication 1.polynomial multiplication 2.toom–cook method 3. primitive roots of unity...
Post on 22-Dec-2015
237 views
TRANSCRIPT
1
Fast Multiplication
1. Polynomial multiplication2. Toom–Cook method3. Primitive Roots of Unity 4. The Discrete Fourier Transform 5. The FFT Algorithm 6. Schönhage-Strassen’s method7. Conclusion
2
Polynomial 表示式 :225)( xxxp
{(-1,-4),(0,-5),(1,-2)}或者
52
5522
2
21
1
5
2
4
)01)(11(
)0)(1(2
)10)(10(
)1)(1(5
)11)(01(
)1)(0(4)(
2
222
222
xx
xxxxx
xxxxx
xxxxxxxp
by Lagrange Interpolating Polynomial
(p(-1), p(0), p(1))
(-5, 1, 2)evaluation interpolation
1. Polynomial multiplication
3
),(,),,(),,( 2211 mm yxyxyx
Lagrange Interpolating Polynomial
m
i
m
j ji
ji
mmmm
mm
m
m
m
m
ij
xx
xxyxp
xxxxxx
xxxxxxy
xxxxxx
xxxxxxy
xxxxxx
xxxxxxyxp
1 1
121
121
23212
312
13121
321
)(
)()(
)())((
)())((
)())((
)())((
)())((
)())(()(
1. Polynomial multiplication
4
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
0
)()()(n
i
ii xcxqxpxC
i
jjiji bac
0
Polynomial Multiplication
1. Polynomial multiplication
5
(a0,a1,a2,…,an-1)(b0,b1,b2,…,bn-1)
Polynomial Multiplication
選 2n+1 個點 (p(w0), p(w1),…, p(w2n) )(q(w0), q(w1),…, q(w2n) )evaluation
multiplication
(p(w0)*q(w0), p(w1)*q(w1),…, p(w2n)*q(w2n) )
interpolation
(c0,c1,c2,…,cn,…,c2n-1)
P(x)=a0+a1x+…+an-1xn-1
q(x)=b0+b1x+…+bn-1xn-1
Polynomial Multiplication
1. Polynomial multiplication
6
(5,1)(1,2)
Polynomial Multiplication
選 3 個點 (p(0)=5, p(1)=6, p(2)=7 )(q(0)=1, q(1)=3, q(2)=5 )
evaluation
multiplication
C(0)=5, C(1)=18, C(2)=3
interpolation
(5,11,2)
P(x)=5+xq(x)=1+2x
Polynomial Multiplication
22
222
21155112
2
3521823
2
5
)12)(02(
)1)(0(35
)21)(01(
)2)(0(18
)20)(10(
)2)(1(5)(C
xxxx
xxxxxx
xxxxxxx
1. Polynomial multiplication
7
1. Polynomial multiplication
Evaluation O(n) Total O(n2)
慎選特殊的點可以降低運算
8
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)
1
0
)(n
i
ii xaxp
)))((()( 12210 nn xaaxaxaxaxp
1. Polynomial multiplication
9
2.Toom–Cook methodToom-k : a simplification of a description of Toom–Cook polynomial multiplication described by Marco Bodrato in 2007.
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
Splitting ( 將整數轉成多項式 )Evaluation ( 找出多項式對應的點 )Pointwise multiplication ( 點値相乘 )Interpolation ( 由點找出多項式 )Recomposition ( 將多項式轉成整數 )
five main steps:
給定兩整數 M, N ,擬計算 M*N
10
2.Toom–Cook method:Toom-3Toom-3 : 例子說明 *
M = 12 3456 7890 1234 5678 9012 N= 9 8765 4321 9876 5432 1098
切成 3 等份,取 base B=108
m0 = 56789012 m1 = 78901234 m2 = 123456 n0 = 54321098 n1 = 43219876 n2 = 98765
p(x) = m0 + m1x + m2x2
q(x) = n0 + n1x + n2x2
(1) Splitting ( 將整數轉成多項式 )
11
2.Toom–Cook method:Toom-3Toom-3 : 例子說明 *
(2) Evaluation ( 找出多項式對應的點 )
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
p(x) = m0 + m1x + m2x2
q(x) = n0 + n1x + n2x2
12
2.Toom–Cook method:Toom-3Toom-3 : 例子說明 *
(3) Pointwise multiplication ( 點値相乘 )
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
r(x)=p(x)q(x)
13
2.Toom–Cook method:Toom-3Toom-3 : 例子說明 *
(4) Interpolation ( 由點找出多項式 )
14
2.Toom–Cook method:Toom-3Toom-3 : 例子說明 *
(4) Interpolation ( 由點找出多項式 )
15
2.Toom–Cook method:Toom-3Toom-3 : 例子說明 *
(4) Interpolation ( 由點找出多項式 )one sequence given by Bodrato
16
2.Toom–Cook method:Toom-3Toom-3 : 例子說明 *
(4) Interpolation ( 由點找出多項式 )
17
2.Toom–Cook method:Toom-3Toom-3 : 例子說明 *
(5) Recomposition ( 將多項式轉成整數 )
18
2.Toom–Cook method:
)()3
(5)T( nOn
Tn )()3
(9)T( nOn
Tn
ComplexityToom-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
)()T( 5log3nOn )()T( )12(log kknOn )()T( 1 nOn
)log2()T( lg2 nnOn n (in Knuth’s book)
19
Polynomial multiplication
Evaluation O(n) Total O(n2)
慎選特殊的點可以降低運算 -Primitive Roots of Unity
執行 DFT 得到只需 O(nlogn)
,)A(,)A(,)A(,)1A( 32 ,,,,1 32
20
3. Primitive Roots of Unity A number is a primitive n-th root of unity, for n>1, if
n = 1 The numbers 1, , 2, …, n-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 e2i/n is a primitive n-th root
of unity, where
x x^2 x^3 x^4 x^5 x^6 x^7 x^8 x^9 x^101 1 1 1 1 1 1 1 1 12 4 8 5 10 9 7 3 6 13 9 5 4 1 3 9 5 4 14 5 9 3 1 4 5 9 3 15 3 4 9 1 5 3 4 9 16 3 7 9 10 5 8 4 2 17 5 2 3 10 4 6 9 8 18 9 6 4 10 3 2 5 7 19 4 3 5 1 9 4 3 5 110 1 10 1 10 1 10 1 10 1
1i
21
Inverse Property: If is a primitive root of unity, then -1=n-1
Proof: n-1=n=1 Cancellation Property: For non-zero -n<k<n,
Proof:
Reduction Property: If w is a primitve (2n)-th root of unity, then 2 is a primitive n-th root of unity.
Proof: If 1,,2,…,2n-1 are all distinct, so are 1,2,(2)2,…,(2)n-1
Reflective Property: If n is even, then n/2 = -1. Proof: By the cancellation property, for k=n/2:
Corollary: k+n/2= -k.
01
0
n
j
kj
01
11
1
1)1(
1
1)(
1
1)(1
0
kk
k
k
kn
k
nkn
j
kj
)1)(2/(0 2/2/02/02/01
0
)2/( nnnnn
j
jn n
3. Primitive Roots of Unity
22
4.The Discrete Fourier Transform Given coefficients (a0,a1,a2,…,an-1) for an (n-1)-degree polynomi
al p(x)
The Discrete Fourier Transform is to evaluate p at the values 1,,2,…,n-1
We produce (y0,y1,y2,…,yn-1), where yj=p(j)
1
1
0
11
211
11
211
10
200
1
1
0
1
1
1
)(
)(
)(
nn
nnn
n
n
n a
a
a
xxx
xxx
xxx
xp
xp
xp
1
1
0
11211
12
11
1
0
1
1
1 1 1 1
)(
)(
)1(
nnnnn
n
nn a
a
a
py
py
py
23
Matrix form: y=Fa, where F[i,j]=ij.
The Inverse Discrete Fourier Transform recovers the coefficients of an (n-1)-degree polynomial given its values at 1,,2,…,n-1
Matrix form: a=F -1y, where F -1[i,j]=-ij/n.
1
0
n
i
ijij ay
Fa
1
1
0
11211
12
11
1
0
1
1
1 1 1 1
)(
)(
)1(
n
nnnn
n
nn a
a
a
py
py
py
註 : the matrix F 中 column 彼此互相 orthogonal 滿足 FF*=nI ,其中 F* 為 F 之 conjugate
4.The Discrete Fourier Transform
24
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
1
0
1],[
n
k
kjki
njiA
Property)onCancellati(by 01
],[1
0
)(
n
k
kij
njiA
1111
],[1
0
01
0
nnnn
iiAn
k
n
k
kiki
4.The Discrete Fourier Transform
25
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…
Pad with n 0's Pad with n 0's
[a0,a1,a2,...,an-1] [b0,b1,b2,...,bn-1]
DFT DFT
[a0,a1,a2,...,an-1,0,0,...,0] [b0,b1,b2,...,bn-1,0,0,...,0]
[y0,y1,y2,...,y2n-1] [z0,z1,z2,...,z2n-1]
ComponentMultiply
inverse DFT
[y0z0,y1z1,...,y2n-1z2n-1]
[c0,c1,c2,...,c2n-1]
(Convolution)
4.The Discrete Fourier Transform
26
Convolutions127356
1 2 7
3 5 6×6 12 42
5 10 35
3 6 21
3 11 37 47 42
(3,11,37,47,42)Convolutions
4.The Discrete Fourier Transform
27
Convolutions1273561 2 73 5 6×6 12 42
5 10 35
3 6 21
3 11 37 47 42
(37,50,53)acyclic convolutions
+ 3 11
37 50 53
1 2 73 5 6×6 12 42
5 10 35
3 6 21
3 11 37 47 42
(37,44,31)negacyclic convolutions
- 3 11
37 44 31
4.The Discrete Fourier Transform
28
Convolution theorem
CyclicConvolution(X, Y) = IDFT(DFT(X) · DFT(Y))
4.The Discrete Fourier Transform
29
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
30
7
6
5
4
3
2
1
0
884942352821147
4236302418126
3530252015105
282420161284
21181512963
1412108642
765432
1
1
1
1
1
1
1
1 1 1 1 1 1 1 1
M
a
a
a
a
a
a
a
a
7
5
3
1
6
4
2
0
884935217422814
4230186362412
3525155302010
282012424168
21159318126
1410621284
753642
1
1
1
1
1
1
1
1 1 1 1 1 1 1 1
a
a
a
a
a
a
a
a
7
5
3
1
4418126
1284
642
3
2
6
4
2
0
4418126
1284
642
7
5
3
1
44211593
141062
753
6
4
2
0
4418126
1284
642
1
1
1
1 1 1 1
0 0 0
0 0 0
0 0 0
0 0 0 1
1
1
1
1 1 1 1
1 1 1 1
1
1
1
1 1 1 1
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
5.The Fast Fourier Transform
Given coefficients (a0,a1,a2,…,a7) and w primitive 8-th root of unity
31
7
5
3
1
4418126
1284
642
3
2
6
4
2
0
4418126
1284
642
7
5
3
1
44211593
141062
753
6
4
2
0
4418126
1284
642
1
1
1
1 1 1 1
0 0 0
0 0 0
0 0 0
0 0 0 1
1
1
1
1 1 1 1
1 1 1 1
1
1
1
1 1 1 1
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
5.The Fast Fourier Transform
32
7
5
3
1
6
4
2
0
884935217422814
4230186362412
3525155302010
282012424168
4
1
1
1
1
,1
a
a
a
a
a
a
a
a
7
5
3
1
44211593
141062
753
6
4
2
0
4418126
1284
642
1 1 1 1
1
1
1
1 1 1 1
a
a
a
a
a
a
a
a
5.The Fast Fourier Transform
33
1
1
1
1 1 1 1
M
18126
1284
642
n/2
令
7
5
3
1
2/
3
2
6
4
2
0
2/
7
5
3
1
2/
3
2
6
4
2
0
2/
M
0 0 0
0 0 0
0 0 0
0 0 0 1
M
M
0 0 0
0 0 0
0 0 0
0 0 0 1
M
M
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
nn
nn
5.The Fast Fourier Transform
34
5.The Fast Fourier Transform a0 a1 a2 a3 a4 a5 a6 a7
35The running time is O(n log n). [inverse FFT is similar]
5.The Fast Fourier Transform
T(n)=2T(n/2)+O(n)
36
(a0,a1,a2,…,an-1)(b0,b1,b2,…,bn-1)
Polynomial Multiplication
選 w primitive m-th root of unity
(p(w0), p(w1),…, p(wm-1) )(q(w0), q(w1),…, q(wm-1) )Evaluation
FFTmultiplication
(p(w0)*q(w0), p(w1)*q(w1),…, p(wm-1)*q(wm-1) )
InterpolationIFFT
(c0,c1,c2,…,cn,…,cm-1)
p(x)=a0+a1x+…+an-1xn-1
q(x)=b0+b1x+…+bn-1xn-1
Polynomial Multiplication
Polynomial multiplication
The running time is O(n log n).
(m2n)
37
6.Schönhage-Strassen’s method給定兩大數 M,N 。計算 M*N
(1) Splitting (將整數轉成多項式 )
令 B=2k, 則可產生 degree 最多 n-1 之兩個多項式
(2) Evaluation (找出多項式對應的點 )
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)
))(,),(),1((),FFT(Q
))(,),(),1((),FFT(P1
1
m
m
qqq
ppp
選 w primitive m-th root of unity (m2n)
38
))()()(,),()()(),1()1()1(( 111 mmm qpCqpCqpC
(3) Pointwise multiplication (點値相乘 )
221210
111 ))),()()(,),()()(),1()1()1((IFFT(C(x)
n
n
mmn
xcxcc
qpCqpCqpC
(4) Interpolation (由點找出多項式 )
對上述點執行 the inverse Fourier transform
6. Schönhage-Strassen’s method
39
222210 BBN*M
nnccc
(5)Recomposition (將多項式轉成整數 )
令 x=B
222210C(x)
nn xcxcc
6. Schönhage-Strassen’s method
40
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 basicnumbers should be of the order of log(B)+log(n)
41
6. Schönhage-Strassen’s method
,BB,B 121 N
)logloglog()( nnnONT
Complexity 分析
The running time is O(n log n(log(B) + log(n))2)
然而如果針對所有乘法都採用 recursive 觀念,即
得到 )log()()( nnONTNNT
42
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
)O( 2n
)O( 46.1n
)O( 58.1n
)log2O( lg2 nn n
1522
1722
)logloglogO( nnn
)2logO( )(log* nOnn