# FFT算法的优化——提高FFT算法一倍的时空效率

## FFT的基本运算

$\omega_N$ 表示 $e^{\frac {2\pi i} {N}}$

$f(x)=\sum_{i=0}^{n}a_ix^i$

$X$$f$$DFT$ ，则有

$X_i=\sum_{j=0}^{N-1}a_j \omega_{N}^{ij},a_i=\frac{1}{N}\sum_{j=0}^{N-1}X_j \omega_{N}^{-ij}$

$DFT(f\cdot g)=DFT(f)\cdot DFT(g)$$DFT(f+g)=DFT(f)+DFT(g)$

$\overline {z_1z_2}=\bar z_1 \cdot \bar z_2$

## 如何优化？

$X_i=\sum_{j=0}^{N-1}a_j \omega_{N}^{ij}$$X_{N-i}=\sum_{j=0}^{N-1}a_j \omega_{N}^{(N-i)j}=\sum_{j=0}^{N-1}a_j \omega_{N}^{-ij}=\overline {X_i}$

$f(x)=\sum_{i=0}^{n}(a_i+ib_i)x^i$

$X_i=\sum_{j=0}^{N-1}(a_j+ib_j)\omega_{N}^{ij}$$X_{N-i}=\sum_{j=0}^{N-1}(a_j+ib_j) \omega_{N}^{(N-i)j}=\sum_{j=0}^{N-1}(a_j+ib_j) \omega_{N}^{-ij} \sout{= \overline {X_i}}$

$\overline {X_{N-i}}=\sum_{j=0}^{N-1}\overline{(a_j+ib_j)} \omega_{N}^{ij} =\sum_{j=0}^{N-1}{(a_j-ib_j)} \omega_{N}^{ij}$

$X_i+\overline {X_{N-i}}=2\sum_{j=0}^{N-1}a_j\omega_{N}^{ij}$$X_i-\overline {X_{N-i}}=2i\sum_{j=0}^{N-1}b_j\omega_{N}^{ij}$

$f(x)=\sum_{i=0}^{n}a_ix^i$

$f(x)=\sum_{i=0}^{ \lfloor \frac{n} {2}\rfloor}(a_{2i}+ia_{2i+1})x^i$

## 在多项式乘法上的应用

$FFT$ 最大的应用是解决多项式乘法的问题，我们将这个优化回归应用到多项式乘法上。

#### 普通的多项式乘法

$f(x)=\sum_{i=0}^{n}a_ix^i,g(x)=\sum_{i=0}^{m}b_ix^i$

$f(x)=\sum_{i=0}^{ \l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ega_N^{\ i})(X_i-\overline{X_{N-i}})(Y_i-\overline{Y_{N-i}})} {4}\end{aligned}

$X_i-\overline {X_{N-i}}=2i\sum_{j=0}^{N-1}a_{2j+1}\omega_{N}^{ij}$

$Z_i=\frac{4X_iY_i-(1+\omega_N^{\ i})(X_i-\overline{X_{N-i}})(Y_i-\overline{Y_{N-i}})} {4}$

#### 对任意模多项式乘法的优化

$a$ 拆成 $A_1,A_2$$b$ 拆成 $B_1,B_2$ ，并构造
$f(x)=\sum_{i=0}^{n} (A_{1,i}+iA_{2,i})x^i,g(x)=\sum_{i=0}^{m} (B_{1,i}+iB_{2,i})x^i$

（第一次写blog，写得不好，可能还有写挂的地方，求轻喷）