Fast Fourier Transform
In questa pagina 4
The FFT is a divide-and-conquer algorithm (Divide and Conquer - Paradigm and Call TreeA divide-and-conquer algorithm comes from a substructure property (threshold n0, Divide and Conquer functions) with strictly smaller subinstances; the call tree describes the execution, and D&C has no memory (equal subinstances are recomputed). Complete example: MAX of an array, with correctness by induction, the recurrence, and the exact count n-1 via the theorem on full binary trees.Divide and Conquer - Paradigm and Call Tree →) that computes the DFT (DFT and Roots of UnityThe DFT of order n evaluates a polynomial with degree bound n at the n n-th roots of unity, y_i = p(w^i) with w = e^{2 pi i/n}; it is the product by the Fourier matrix F_n, an invertible Vandermonde matrix. The roots form a multiplicative group and satisfy three lemmas: cancellation (w_{dn}^{dk} = w_n^k, w_n^{n/2} = -1), halving (the squares of the n roots are the n/2 roots of order n/2, each twice) and summation (the sum of the powers of w^k is n if k is a multiple of n, 0 otherwise). Linear convolution theorem: (c|0) = DFT^{-1}{2n}(DFT{2n}(a|0) times DFT_{2n}(b|0)).DFT and Roots of Unity →) in instead of . From now on is a power of two; for convolution this is no restriction, since one can pad up to the next power of two.
Substructure
Base. : , evaluated at : .
Divide. For split the coefficients by index parity: the coefficients of the polynomials with bound (Not two contiguous halves, unlike MAX or the matrix blocks.)
Polynomial identity. For all :
Proof. is the even-degree part, the odd-degree part.
Example. : , , and .
Theorem (substructure). Let , , . For :
Proof. First half: by the identity and the halving lemma, Second half: and , so
Each pair of evaluations at the same point gives, with the two combinations , two values of : common terms are computed once. This pair of formulas is the butterfly.
The algorithm
FFT(a)
n <- a.len
if n = 1 then return a { base }
a0 <- (a[0], a[2], ..., a[n-2]) { divide: even indices }
a1 <- (a[1], a[3], ..., a[n-1]) { divide: odd indices }
y0 <- FFT(a0); y1 <- FFT(a1) { recurse }
ON <- e^(i 2 pi / n) { conquer: ON = w_n }
ONI <- 1 { at the start of iteration i, ONI = w_n^i }
for i <- 0 to n/2 - 1
t <- ONI * y1[i]
y[i] <- y0[i] + t
y[i + n/2] <- y0[i] - t
ONI <- ONI * ON
return yCorrectness. Induction on with . is the base. For , by hypothesis y0, y1 are , ; the loop computes the two formulas of the substructure theorem with ONI (invariant: at the end of iteration it equals ).
Exact cost. Each iteration does complex operations (two products, a sum and a difference), iterations: Conquer work . Exact solution (induction: ; ). Also from the Master TheoremThe Master Theorem for T(n) = aT(n/b) + w(n) compares w(n) with the threshold n^(log_b a). Case 1 (w polynomially smaller): Theta(n^(log_b a)); case 2 (same order): Theta(n^(log_b a) log n); case 3 (polynomially larger and regular): Theta(w(n)). Full proof from the levels of the tree, extended version with floors and ceilings, a table of applications, and the misuse when no case applies (w = n log n).Master Theorem →: , threshold , , Case 2, . The tree has levels, leaves, and every level has total work (Recurrence Relations - Recursion Tree and General FormulaThe recurrence tree has the shape of the call tree of the worst instance and T(n) is the sum of the works of its nodes. For the family T(n) = s(n) T(f(n)) + w(n) with base T0 for n <= n0, counting nodes per level with the iterates of f and the level f*(n,n0) gives the closed formula T(n) = sum over levels of [product of s] w + [product of s] T0, proved by induction and applied to merge sort, Hanoi and square-root recurrences.Recurrence Relations - Recursion Tree and General Formula →).
Remark. With precomputed powers , each iteration is one product and two sums: in total. The course count includes updating ONI.
Example
, . Subinstances and , with : . , . : , . : , , . Result .
Example
. Leaves read left to right: (bit-reversed indices). Order-2 level: , , , . Order-4 level: even part ; odd part . Last level ():
| 0 | ||||
| 1 | ||||
| 2 | ||||
| 3 |
, with conjugate symmetry because is real.
The inverse transform
is the interpolation from .
Theorem. .
Proof. Let . Then . If the ratio is and the sum is : entry . If , contains no multiple of , so by the summation lemma the sum is . Hence (a left inverse of a square matrix is the inverse).
Explicitly with : the interpolation from is an evaluation of the polynomial with coefficients at the points , followed by a division by .
Method 1: FFT on negative powers. The lemmas hold for too, so the same FFT works with :
FFT_NEG(y)
n <- y.len
if n = 1 then return y
y0 <- (y[0], y[2], ..., y[n-2]); y1 <- (y[1], y[3], ..., y[n-1])
a0 <- FFT_NEG(y0); a1 <- FFT_NEG(y1)
ON_NEG <- e^(-i 2 pi / n) { = w_n^(-1) }
ONI <- 1
for i <- 0 to n/2 - 1
t <- ONI * a1[i]
a[i] <- a0[i] + t
a[i + n/2] <- a0[i] - t
ONI <- ONI * ON_NEG
return a
INV_FFT(y)
n <- y.len
a <- FFT_NEG(y)
for i <- 0 to n-1
a[i] <- a[i] / n
return aThe division by must be done outside the recursion, once: inside, each level would divide by its own size and the wrong factor would accumulate. Cost .
Method 2: same FFT and reversal. Since ,
INV_FFT(y)
n <- y.len
a' <- FFT(y)
a[0] <- a'[0] / n
for i <- 1 to n-1
a[i] <- a'[n-i] / n
return aExample. : (e.g. ); reversed ; divided by : .
Polynomial product with the FFT
LIN_CONV(a, b)
n <- a.len { n power of two }
ya <- FFT((a | 0_n)); yb <- FFT((b | 0_n)) { evaluation on Omega_2n }
for i <- 0 to 2n-1
yc[i] <- ya[i] * yb[i]
return INV_FFT(yc) { returns (c | 0) }Exact cost. Three transforms of order , each ; plus products and divisions in the inverse: Direct convolution costs :
| 8 | 16 | 32 | 64 | 128 | 1024 | |
|---|---|---|---|---|---|---|
| definition | 113 | 481 | 1985 | 8065 | 32513 | 2095105 |
| LIN_CONV | 416 | 1024 | 2432 | 5632 | 12800 | 139264 |
The FFT wins from (8065 against 5632); for small the definition is cheaper (lower constants), the same phenomenon that motivates Hybrid AlgorithmsHybridization combines two algorithms for the same problem so the result has the asymptotics of the better one and, if possible, lower constants. Choosing the algorithm only by the starting size (STUPID_HYBRID) does not help; changing the base case of the recursion (H_MUL: iterative product for n <= n0 at every leaf of Strassen's tree) gives cost n^(log2 7)(2n0+5)/n0^(log2 7 - 2) - 6n^2, minimal for n0 = 8 with leading constant about 3.92.Hybrid Algorithms →.
General sizes. If is not a power of two, pad both vectors to , the first power of two at least (and below ). For lengths pad to the first power of two . The cost remains .
Numerical note. In floating point the roots are approximate and the result has small errors (e.g. imaginary parts of order instead of ); with integer coefficients one rounds.
Versione ripasso
- Substructure (, DFT and Roots of UnityThe DFT of order n evaluates a polynomial with degree bound n at the n n-th roots of unity, y_i = p(w^i) with w = e^{2 pi i/n}; it is the product by the Fourier matrix F_n, an invertible Vandermonde matrix. The roots form a multiplicative group and satisfy three lemmas: cancellation (w_{dn}^{dk} = w_n^k, w_n^{n/2} = -1), halving (the squares of the n roots are the n/2 roots of order n/2, each twice) and summation (the sum of the powers of w^k is n if k is a multiple of n, 0 otherwise). Linear convolution theorem: (c|0) = DFT^{-1}{2n}(DFT{2n}(a|0) times DFT_{2n}(b|0)).DFT and Roots of Unity →): . Split the coefficients into even and odd ; identity (even-degree plus odd-degree monomials). For : , . Proof: identity, halving ; second half uses , .
- FFT: base ; divide even/odd; two calls of size ; loop
for i <- 0 to n/2-1:t <- ONI*y1[i],y[i] <- y0[i]+t,y[i+n/2] <- y0[i]-t,ONI <- ONI*ON. Correctness by induction on with (hypothesis on the two subinstances,ONI). - Cost: operations per iteration, iterations: , . Exact (induction: ); Case 2 of the Master TheoremThe Master Theorem for T(n) = aT(n/b) + w(n) compares w(n) with the threshold n^(log_b a). Case 1 (w polynomially smaller): Theta(n^(log_b a)); case 2 (same order): Theta(n^(log_b a) log n); case 3 (polynomially larger and regular): Theta(w(n)). Full proof from the levels of the tree, extended version with floors and ceilings, a table of applications, and the misuse when no case applies (w = n log n).Master Theorem → (threshold , work ). With precomputed powers . Tree: levels, leaves, work per level.
- Example : : , ; , , , .
- Example : ; even , odd ; conjugate symmetry since is real.
- Inverse: . Proof: is if , otherwise because is not a multiple of (summation lemma). So with : interpolation from is an evaluation at negative powers.
- INV_FFT, method 1: FFT_NEG (same FFT with ), then divide by outside the recursion; cost . Method 2: with , since . Example: , reversed , divided by : .
- LIN_CONV: pad to , two FFT, product , INV_FFT; cost against by definition (FFT wins from : against ). Non-power-of-two sizes: pad to ; lengths : first power of two .
- Pseudocode of the inverse:
FFT_NEGis the FFT withON_NEG <- e^(-i 2 pi / n);INV_FFT(y):a <- FFT_NEG(y); for i <- 0 to n-1: a[i] <- a[i]/n; return a. Method 2:a' <- FFT(y); a[0] <- a'[0]/n; for i <- 1 to n-1: a[i] <- a'[n-i]/n. - Common mistakes: dividing by inside the recursion; using instead of in the inverse; forgetting to reverse the indices in method 2 (index stays); applying the FFT to non-powers of two without padding.