跳到正文

算法·理论

FFT

面条小王子约 13 分钟
原文

这是 2026年3月23日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年3月22日
收录
2026年3月23日
更新
2026年11月14日
篇幅
约 13 分钟
获赞
1

版本

  1. v7当前
  2. v6
  3. v5
  4. v4
更早的 3 个版本
  1. v3
  2. v2
  3. v1

目录

一、多项式基本知识

二、系数表达 到 点值表达(DFT初步)

三、虚数相关知识

第四步:FFT、DFT

第五步:点值->多项式,IDFT

第六步:FFT迭代实现

第七步:代码

参考资料

一、多项式

多项式

A(x)=∑i=0n−1aixi\displaystyle{ A(x) = \sum_{i = 0}^{n - 1} a_{i} x^i }

称 a0,a1,a2,...an−1a_{0}, a_{1}, a_{2}, ... a_{n - 1} 为多项式的 系数

这个多项式最高次(最高次系数不为 0)为 n−1n - 1,记 A(x)A(x) 的 次数 为 n−1n - 1

严格大于一个多项式次数的整数为该多项式的 次数界,A(x)A(x) 的 次数界 为 nn

多项式加法、乘法

对于 A(x)=∑i=0n−1aixi\displaystyle{ A(x) = \sum_{i = 0}^{n - 1} a_{i} x^i },B(x)=∑i=0n−1bixi\displaystyle{ B(x) = \sum_{i = 0}^{n - 1} b_{i} x^i }

多项式加法

如果 C(x)=A(x)+B(x)\displaystyle{ C(x) = A(x) + B(x) }

则有以下表示 C(x)=∑i=0n−1(ai+bi)xi\displaystyle{C(x) = \sum_{i = 0}^{n - 1} (a_{i} + b_{i}) x^{i}}

多项式乘法

如果 C(x)=A(x)∗B(x)\displaystyle{ C(x) = A(x) * B(x) }

则有以下表示

  • C(x)=∑i=0n−1∑j=0n−1aibjxi+j\displaystyle{C(x) = \sum_{i = 0}^{n - 1} \sum_{j = 0}^{n - 1} a_{i} b_{j} x^{i + j}}

  • ci=∑j=0iajbi−j\displaystyle{c_{i} = \sum_{j = 0}^{i} a_{j} b_{i - j}},C(x)=∑i=02n−2cixi\displaystyle{C(x) = \sum_{i = 0}^{2n - 2} c_{i} x^{i}}

对于 A(x)A(x) 的次数界为 nan_a 的多项式,B(x)B(x) 的次数界为 nbn_b 的多项式

如果C(x)=A(x)∗B(x)\displaystyle{C(x) = A(x) * B(x)}

则 C(x)C(x) 次数界为 na+nb−1n_a + n_b - 1

系数表达

对于多项式 A(x)=∑i=0n−1aixi\displaystyle{ A(x) = \sum_{i = 0}^{n - 1} a_{i} x^i }

我们称一个向量 a=(a0,a1,...,an−1)a = (a_{0}, a_{1}, ... ,a_{n - 1}) 为多项式 A(x)A(x) 的 系数表达

点值表达

一个多项式 A(x)A(x) 的 点值表达 为 nn 个点值所组成的集合 {(x0,y0),(x1,y1),(x2,y2),...(xn−1,yn−1)}\{ (x_{0}, y_{0}), (x_{1}, y_{1}), (x_{2}, y_{2}), ... (x_{n - 1}, y_{n - 1})\}

其中 yi=A(xi)y_{i} = A(x_{i})

求出一个多项式的 点值表达 (求值) 需要 Θ(n2)\Theta(n^2)

通过 点值表达 推回 原多项式 的操作叫做 插值

没错 拉格朗日插值 的 插值 就是这个

证明 nn 个点,可以求出最高次数为 n−1n - 1 的多项式(充分必要且唯一)

咕咕

这种表达方式有个很好的性质

多项式加法

对于 A(x)A(x) 的 点值表达 {(x0,A(x0)),(x1,A(x1)),(x2,A(x2)),...(xn−1,A(yn−1))}\{ (x_{0}, A(x_{0})), (x_{1}, A(x_{1})), (x_{2}, A(x_{2})), ... (x_{n - 1}, A(y_{n - 1}))\}

B(x)B(x) 的 点值表达 {(x0,B(x0)),(x1,B(x1)),(x2,B(x2)),...(xn−1,B(yn−1))}\{ (x_{0}, B(x_{0})), (x_{1}, B(x_{1})), (x_{2}, B(x_{2})), ... (x_{n - 1}, B(y_{n - 1}))\}

如果 C(x)=A(x)+B(x)C(x) = A(x) + B(x)

C(x)C(x) 的 点值表达 为{(x0,A(x1)+B(x0)),(x1,A(x1)+B(x1)),...(xn−1,A(xn−1)+B(yn−1))}\{ (x_{0}, A(x_{1}) + B(x_{0})), (x_{1}, A(x_{1}) + B(x_{1})), ... (x_{n - 1}, A(x_{n - 1}) + B(y_{n - 1}))\}

多项式乘法

对于 次数界为 2n−12n - 1 的 C(x)C(x)

对于 A(x)A(x) 的 点值表达 {(x0,A(x0)),(x1,A(x1)),(x2,A(x2)),...(x2n−1,A(y2n−1))}\{ (x_{0}, A(x_{0})), (x_{1}, A(x_{1})), (x_{2}, A(x_{2})), ... (x_{2n - 1}, A(y_{2n - 1}))\}

B(x)B(x) 的 点值表达 {(x0,B(x0)),(x1,B(x1)),(x2,B(x2)),...(x2n−1,B(y2n−1))}\{ (x_{0}, B(x_{0})), (x_{1}, B(x_{1})), (x_{2}, B(x_{2})), ... (x_{2n - 1}, B(y_{2n - 1}))\}

如果 C(x)=A(x)∗B(x)C(x) = A(x) * B(x)

C(x)C(x) 的 点值表达 为 {(x0,A(x1)∗B(x0)),(x1,A(x1)∗B(x1)),...(x2n−1,A(x2n−1)∗B(y2n−1))}\{ (x_{0}, A(x_{1}) * B(x_{0})), (x_{1}, A(x_{1}) * B(x_{1})), ... (x_{2n - 1}, A(x_{2n - 1}) * B(y_{2n - 1}))\}

时间复杂度均为 Θ(n)\Theta(n)

也就是说,如果要求 A(x)∗B(x)A(x) * B(x),先将 A(x)A(x)、B(x)B(x) 转化为 点值表达,在 相乘,最后转化为 系数表达 就行了

坏消息,求值 和 插值 都需要 Θ(n2)\Theta(n^2),我们能优化吗?

二、求值(DFT初步)

点值表达 中我们发现点需要取 nn 个,我们尝试从此入手

对于一个偶函数 y=x2y = x^2,我们发现知道点 (x,y)(x, y),我们就知道点 (−x,y)(-x, y)

对于一个奇函数 y=x3y = x^3,我们发现知道点 (x,y)(x, y),我们就知道点 (−x,−y)(-x, -y)

这启发我们可以将一个函数分成 偶函数 和 奇函数 再相加

对于 P(x)=a0+a1x+a2x2+...+an−1xn−1P(x) = a_{0} + a_{1} x + a_{2} x^2 + ... + a_{n - 1} x^{n - 1}

⇒P(x)=(a0+a2x2+a3x4+...)+(a1x+a3x3+a5x5+...)\Rightarrow P(x) = (a_{0} + a_{2} x^2 + a_{3} x^4 + ...) + (a_{1} x + a_{3} x^3 + a_{5} x^5 + ...)

⇒P(x)=(a0+a2x2+a3x4+...)+x(a1+a3x2+a5x4+...)\Rightarrow P(x) = (a_{0} + a_{2} x^2 + a_{3} x^4 + ...) + x(a_{1} + a_{3} x^2 + a_{5} x^4 + ...)

⇒P(x)=P0(x2)+xP1(x2)\Rightarrow P(x) = P_{0} (x^2) + x P_{1}(x^2)

其中 P0(x)P_{0} (x) 和 P1(x)P_{1}(x) 是另外两个多项式

假如我们需要点的横坐标 {±x0,±x1,...,±xn/2}\{ \pm x_{0}, \pm x_{1}, ... , \pm x_{n / 2} \}

知道 (xi,y)(x_{i}, y) 我们就可以知道 (−xi,y)(-x_{i}, y) 了

然后一直递归,就赢了……吗?

注意到 递归时我们需要 {x02,x12,...,xn/22}\{ x_{0}^2, x_{1}^2, ... , x_{n / 2}^2 \},都是正数没有负数,我们输了

怎么样让它能递归下去?

三、虚数相关知识

以下 i=−1i = \sqrt{-1}

欧拉公式

eiπ−1=0\displaystyle{ e^{i\pi} - 1 = 0 } 相信大家都知道

它的一般形式是 eiθ=cos⁡θ+isin⁡θ\displaystyle{ e^{i\theta} = \cos \theta + i \sin \theta }

证明

你在期待什么

棣莫弗公式

(cos⁡θ+isin⁡θ)n=cos⁡(nθ)+isin⁡(nθ)\displaystyle{ (\cos \theta + i \sin \theta)^n = \cos (n\theta) + i\sin (n\theta) }

证明1

如果你熟知欧拉公式

(cos⁡θ+isin⁡θ)n=(eiθ)n=eiθn=cos⁡(nθ)+isin⁡(nθ)\displaystyle{ (\cos \theta + i \sin \theta)^n = (e^{i \theta})^n = e^{i \theta n} = \cos (n\theta) + i\sin (n\theta) }

证明2

能证明 nn 为整数时成立

当 n=0n = 0 和 11 时显然成立

  • n>0n > 0 时成立

    设 n=k(k>0)n = k (k > 0) 时成立

    (cos⁡θ+isin⁡θ)k+1=(cos⁡(kθ)+isin⁡(kθ))(cos⁡θ+isin⁡θ)\displaystyle{ (\cos \theta + i \sin \theta)^{k+1} = (\cos (k\theta) + i \sin (k \theta))(\cos \theta + i \sin \theta) }

    ⇒(cos⁡θ+isin⁡θ)k+1=cos⁡(kθ)cos⁡θ+isin⁡(kθ)cos⁡θ+icos⁡(kθ)sin⁡θ−sin⁡(kθ)sin⁡θ\Rightarrow \displaystyle{ (\cos \theta + i \sin \theta)^{k+1} = \cos (k\theta) \cos \theta + i \sin (k \theta) \cos \theta + i \cos (k\theta)\sin \theta - \sin (k \theta) \sin \theta }

    ⇒(cos⁡θ+isin⁡θ)k+1=cos⁡(kθ)cos⁡θ−sin⁡(kθ)sin⁡θ+i(sin⁡(kθ)cos⁡θ+cos⁡(kθ)sin⁡θ)\Rightarrow \displaystyle{ (\cos \theta + i \sin \theta)^{k+1} = \cos (k\theta) \cos \theta - \sin (k \theta) \sin \theta + i (\sin (k \theta) \cos \theta + \cos (k\theta)\sin \theta) }

    ⇒(cos⁡θ+isin⁡θ)k+1=cos⁡((k+1)θ)+i(sin⁡((k+1)θ))\Rightarrow \displaystyle{ (\cos \theta + i \sin \theta)^{k+1} = \cos ((k + 1)\theta) + i (\sin ((k + 1) \theta)) }

  • n<0n < 0 时成立

    n>0n > 0 时成立已证

    设 n=−k(k<0)n = -k (k < 0)

    (cos⁡θ+isin⁡θ)k=1(cos⁡θ+isin⁡θ)n\displaystyle{ (\cos \theta + i \sin \theta)^k = \frac{1}{(\cos \theta + i \sin \theta)^{n}} }

    ⇒(cos⁡θ+isin⁡θ)k=cos⁡(nθ)−sin⁡(nθ)(cos⁡(nθ)+sin⁡(nθ))(cos⁡(nθ)−sin⁡(nθ))\Rightarrow \displaystyle{ (\cos \theta + i \sin \theta)^k = \frac{\cos (n \theta) - \sin (n \theta)}{(\cos (n \theta) + \sin (n \theta))(\cos (n \theta) - \sin (n \theta))} }

    ⇒(cos⁡θ+isin⁡θ)k=cos⁡(nθ)−sin⁡(nθ)cos⁡2(nθ)+sin⁡2(nθ)\Rightarrow \displaystyle{ (\cos \theta + i \sin \theta)^k = \frac{\cos (n \theta) - \sin (n \theta)}{\cos^2 (n \theta) + \sin^2 (n \theta)} }

    ⇒(cos⁡θ+isin⁡θ)k=cos⁡(nθ)−sin⁡(nθ)\Rightarrow \displaystyle{ (\cos \theta + i \sin \theta)^k = \cos (n \theta) - \sin (n \theta) }

    ⇒(cos⁡θ+isin⁡θ)k=cos⁡(kθ)+sin⁡(kθ)\Rightarrow \displaystyle{ (\cos \theta + i \sin \theta)^k = \cos (k \theta) + \sin (k \theta) }

    小tip:cos⁡\cos 是偶函数,sin⁡\sin 是奇函数哦~

单位复数根

nn 次单位复数根 为满足 xn=1(n≠0)x^{n} = 1 (n \neq 0) 的复数 xx

总所周知 eiπ=1\displaystyle{e^{i \pi} = 1}

因此 e2πik=1\displaystyle{e^{2\pi ik} = 1}

可得 (e2πikn)n=1\displaystyle{(e^{\frac{2 \pi ik}{n}})^n = 1}

e2πikn\displaystyle{e^{\frac{2\pi ik}{n}}},发现 k=ndk = nd 时与 k=dk = d 的值一样

所以,xn=1(n≠0)x^{n} = 1 (n \neq 0) 的复数 xx 有 nn 个不相同的解,xk=e2πikn\displaystyle{x_{k} = e^{\frac{2\pi ik}{n}}}

x0,x1,...,xn−1x_{0}, x_{1}, ... , x_{n - 1} 被称为 主 nn 次单位根

记 wni=eiπin\displaystyle{w_{n}^{i} = e^{\frac{i\pi i}{n}}}, 则 wn0,wn1,...,wnn−1w_{n}^{0}, w_{n}^{1}, ... , w_{n}^{n - 1} 为 主 nn 次单位根

给个图参考理解

这图槽点有点多,但我们不用在意

上图 为横轴为实数轴,数轴为虚数轴, wiw_{i} 为 w8iw_{8}^{i}

参考资料