Define the polynomials, where deg(A) = q
and deg(B) = p
. The deg(C) = q + p
.
In this case, deg(C) = 1 + 2 = 3
.
A=3+xB=2x2+2C=A∗B=?
Podemos encontrar fácilmente C en el tiempo O(n2) multiplicando por fuerza bruta los coeficientes. Al aplicar FFT (y FFT inversa), podríamos lograr esto en el tiempo O(nlog(n)) . Explícitamente:
- Convierta el coeficiente de representación de A y B en su representación de valor. Este proceso se llama evaluación . Realizar Divide-and-Conquer (D&C) para esto llevaría tiempo O(nlog(n)) .
- Multiply component-wise the polynomials in their value representation. This returns the value representation of C = A*B. This take O(n) time.
- Invert C using inverse FFT to get C in its coefficient representation. This process is called interpolation and it also takes O(nlog(n)) time.
Continuing along, we represent each polynomial as a vector whose value are its coefficients. We pad the vector with 0's up to the smallest power of two, n=2k,n≥deg(C). Thus n=4. Choosing a power of two provides us a way to recursively apply our divide-and-conquer algorithm.
A=3+x+0x2+0x3⇒B=2+0x+2x+0x3⇒a⃗ =[3,1,0,0]b⃗ =[2,0,2,0]
Let A′,B′ be the value representation of A and B, respectively. Notice that FFT (Fast Fourier Transform) is a linear transformation (linear map) and can be represented as a matrix, M. Thus
A′=Ma→B′=Mb→
We define M=Mn(ω) where ω is complex roots nth complex roots of unity. Notice n = 4
, in this example. Also notice that the entry in the jth row and kth column is ωjkn . See more about the DFT matrix here
M4(w)=⎡⎣⎢⎢⎢⎢⎢⎢111...11ω1ω2...ωn−11ω2ω4...ω2(n−1).........ωjk...1ωn−1......ω(n−1)(n−1)⎤⎦⎥⎥⎥⎥⎥⎥=⎡⎣⎢⎢⎢⎢11111ωω2ω31ω2ω4ω61ω3ω6ω9⎤⎦⎥⎥⎥⎥
Given the ω4=4th roots of unity, we have the ordered set equality:
{ω0,ω1,ω2,ω3,ω4,ω5,...}={1,i,−1,−i,1,i,...}
This can be visualized as iterating thru roots of the unit circle in the counter-clockwise direction.
Also, notice the mod n
nature, i.e. ω6=ω6modn=ω2=−1 and −i=ω3=ω3+n
To complete step 1 (evaluation) we find A′,B′ by performing
A′=M∗a⃗ =⎡⎣⎢⎢⎢⎢11111ωω2ω31ω2ω4ω61ω3ω6ω9⎤⎦⎥⎥⎥⎥⎡⎣⎢⎢⎢3100⎤⎦⎥⎥⎥=⎡⎣⎢⎢⎢⎢3+13+1ω3+ω23+ω3⎤⎦⎥⎥⎥⎥=⎡⎣⎢⎢⎢43+i23−i⎤⎦⎥⎥⎥B′=M∗b⃗ =⎡⎣⎢⎢⎢⎢11111ωω2ω31ω2ω4ω61ω3ω6ω9⎤⎦⎥⎥⎥⎥⎡⎣⎢⎢⎢2020⎤⎦⎥⎥⎥=⎡⎣⎢⎢⎢⎢2+22+2ω22+2ω42+2ω6⎤⎦⎥⎥⎥⎥=⎡⎣⎢⎢⎢4040⎤⎦⎥⎥⎥
This step can be achieved using D&C algorithms (beyond the scope of this answer).
Multiply A′∗B′ component-wise (step 2)
A′∗B′=⎡⎣⎢⎢⎢43+i23−i⎤⎦⎥⎥⎥⎡⎣⎢⎢⎢4040⎤⎦⎥⎥⎥=⎡⎣⎢⎢⎢16080⎤⎦⎥⎥⎥=C′
Finally, the last step is to represent C' into coefficients. Notice
C′=Mc⃗ ⇒M−1C′=M−1Mc⃗ ⇒c⃗ =M−1C′
Notice M−1n=1nMn(ω−1)1 and ωj=−ωn/2+j.
M−1n=14⎡⎣⎢⎢⎢⎢11111ω−1ω−2ω−31ω−2ω−4ω−61ω−3ω−6ω−9⎤⎦⎥⎥⎥⎥=14⎡⎣⎢⎢⎢11111−i−1i1−11−11i−1−i⎤⎦⎥⎥⎥
ω−j can be visualized as iterating thru roots of the unit circle in the clockwise direction.
{ω0,ω−1,ω−2,ω−3,ω−4,ω−5,...}={1,−i,−1,i,1,−i,...}
Also, it is true that, given the nth root of unity, the equality ω−j=ωn−j holds. (Do you see why?)
Then,
c⃗ =M−1C′=1nMn(w−1)=14⎡⎣⎢⎢⎢11111−i−1i1−11−11i−1−i⎤⎦⎥⎥⎥⎡⎣⎢⎢⎢16080⎤⎦⎥⎥⎥=⎡⎣⎢⎢⎢⎢(16+8)/4(16−8)/4(16+8)/4(16−8)/4⎤⎦⎥⎥⎥⎥=⎡⎣⎢⎢⎢6262⎤⎦⎥⎥⎥
Thus, we get the polynomial C=A∗B=6+2x+6x2+2x3
1: Inversion Formula pg 73, Algorithms by Dasgupta et. al. (C) 2006