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 + x \\
B = 2x^2 + 2 \\
C = A*B = ?
$$
We can easily find C in $O(n^2)$ time by brute-force multiplication of coefficients. By applying FFT (and inverse FFT), we could achieve this in $O(n\log(n))$ time. Explicitly:
- Convert the coefficient representation of A and B to its value representation. This process is called evaluation. Performing Divide-and-Conquer (D&C) for this would take $O(n\log(n))$ time.
- 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(n\log(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 = 2^k, n \geq deg(C) $. Thus $n = 4$. Choosing a power of two provides us a way to recursively apply our divide-and-conquer algorithm.
$$
\begin{align}
&A = 3+x+0x^2+0x^3 \Rightarrow &\vec{a} = [3, 1, 0, 0]\\
&B = 2+0x+2x^+0x^3 \Rightarrow & \vec{b} = [2, 0, 2, 0]
\end{align}
$$
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' = M \overrightarrow{a} \\
B' = M \overrightarrow{b}
$$
We define $M = M_n(\omega)$ where $\omega$ is complex roots $n^{th}$ complex roots of unity. Notice n = 4
, in this example. Also notice that the entry in the $j^{th}$ row and $k^{th}$ column is $\omega_n^{jk}$ . See more about the DFT matrix here
$$
M_4(w) = \begin{bmatrix}
1 & 1 & 1 & ... & 1\\
1 & \omega^1 & \omega^2 & ... & \omega^{n-1}\\
1 & \omega^2 & \omega^{4} & ... & ... \\
... & ... & ... & \omega^{jk} & ...\\
1 & \omega^{n-1} & \omega^{2(n-1)} & ... & \omega^{(n-1)(n-1)}
\end{bmatrix} =
\begin{bmatrix}
1 & 1 & 1 &1 \\
1 &\omega & \omega^2 & \omega^3\\
1 &\omega^2 & \omega^4 & \omega^6\\
1 &\omega^3 & \omega^6 & \omega^9
\end{bmatrix}
$$
Given the $\omega_4 = 4^{th}$ roots of unity, we have the ordered set equality:
$$\{\omega^{0},\omega^1, \omega^2, \omega^3, \omega^4, \omega^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. $\omega^6 = \omega^{6 \mod n} = \omega^{2} = -1 $ and $-i = \omega^{3} = \omega^{3+n}$
To complete step 1 (evaluation) we find $A', B'$ by performing
$$
A' = M * \vec{a} = \begin{bmatrix}
1 & 1 & 1 &1 \\
1 &\omega & \omega^2 & \omega^3\\
1 &\omega^2 & \omega^4 & \omega^6\\
1 &\omega^3 & \omega^6 & \omega^9
\end{bmatrix}
\begin{bmatrix}
3 \\
1 \\
0 \\
0
\end{bmatrix} =
\begin{bmatrix}
3 + 1 \\
3 + 1 \omega \\
3 + \omega^2 \\
3 + \omega^3
\end{bmatrix} =
\begin{bmatrix}
4 \\
3 + i \\
2 \\
3 - i
\end{bmatrix} \\
B' = M * \vec{b} = \begin{bmatrix}
1 & 1 & 1 &1 \\
1 &\omega & \omega^2 & \omega^3\\
1 &\omega^2 & \omega^4 & \omega^6\\
1 &\omega^3 & \omega^6 & \omega^9
\end{bmatrix}
\begin{bmatrix}
2 \\
0 \\
2 \\
0
\end{bmatrix} =
\begin{bmatrix}
2 + 2 \\
2 + 2 \omega^2 \\
2 + 2\omega^4 \\
2 + 2\omega^6
\end{bmatrix} =
\begin{bmatrix}
4 \\
0 \\
4 \\
0
\end{bmatrix}
$$
This step can be achieved using D&C algorithms (beyond the scope of this answer).
Multiply $A' * B'$ component-wise (step 2)
$$ A' * B' = \begin{bmatrix}
4 \\
3 + i \\
2 \\
3 - i
\end{bmatrix}
\begin{bmatrix}
4 \\
0 \\
4 \\
0
\end{bmatrix} =
\begin{bmatrix}
16 \\
0\\
8 \\
0
\end{bmatrix} = C'\\
$$
Finally, the last step is to represent C' into coefficients. Notice
$$
C' = M \vec{c} \\
\Rightarrow M^{-1}C' = M^{-1} M \vec{c} \\
\Rightarrow \vec{c} = M^{-1}C'
$$
Notice $M_n^{-1} = \frac{1}{n} M_n(\omega^{-1})$1 and $\omega^j = -\omega^{n/2 + j}$.
$$
M_n^{-1} = \frac{1}{4}
\begin{bmatrix}
1 & 1 & 1 &1 \\
1 &\omega^{-1} & \omega^{-2} & \omega^{-3}\\
1 &\omega^{-2} & \omega^{-4} & \omega^{-6}\\
1 &\omega^{-3} & \omega^{-6} & \omega^{-9}
\end{bmatrix}
=
\frac{1}{4}
\begin{bmatrix}
1 & 1 & 1 & 1\\
1 & -i & -1 & i \\
1 & -1 & 1 & -1\\
1 & i & -1 & -i
\end{bmatrix}
$$
$\omega^{-j}$ can be visualized as iterating thru roots of the unit circle in the clockwise direction.
$$\{\omega^{0},\omega^{-1}, \omega^{-2}, \omega^{-3}, \omega^{-4}, \omega^{-5}, ... \} = \{1, -i, -1, i, 1, -i, ...\}$$
Also, it is true that, given the $n^{th}$ root of unity, the equality $\omega^{-j} = \omega^{n-j}$ holds. (Do you see why?)
Then,
$$
\vec{c} = M^{-1}C' = \frac{1}{n} M_n(w^{-1}) =
\frac{1}{4}
\begin{bmatrix}
1 & 1 & 1 & 1\\
1 & -i & -1 & i \\
1 & -1 & 1 & -1\\
1 & i & -1 & -i
\end{bmatrix}
\begin{bmatrix}
16 \\
0\\
8 \\
0
\end{bmatrix}
=
\begin{bmatrix}
(16 + 8) /4 \\
(16 - 8) /4 \\
(16 + 8) /4 \\
(16 - 8) /4
\end{bmatrix}
=
\begin{bmatrix}
6 \\
2\\
6 \\
2
\end{bmatrix}
$$
Thus, we get the polynomial $$ C = A * B = 6 + 2x + 6x^2 + 2x^3$$
1: Inversion Formula pg 73, Algorithms by Dasgupta et. al. (C) 2006