Salta al contenuto
Note per Studenti Polynomials - Representations and Convolution Report

Polynomials - Representations and Convolution

In questa pagina 7

The FFT chapter starts from an algebraic problem: multiply two polynomials. The idea is to have two representations of the same object, each with different easy operations, and to convert quickly between them. Here: the two representations, exact costs of the operations and of the general conversions. Polynomials are functions p:C→Cp:\mathbb C\to\mathbb C with complex coefficients; the cost is the number of arithmetic operations between complex scalars (each counts 11).

Representation by coefficients

Definition (degree bound nn). p(x)=∑i=0n−1aixip(x)=\sum_{i=0}^{n-1}a_ix^i is represented by a=(a0,…,an−1)∈Cn\mathbf a=(a_0,\dots,a_{n-1})\in\mathbb C^n, written p≡ap\equiv\mathbf a. nn is the degree bound; the actual degree is max⁡{i:ai≠0}≤n−1\max\{i:a_i\ne0\}\le n-1.

The vector can be padded with zeros without changing the polynomial: x2≡(0,0,1)≡(0,0,1,0)x^2\equiv(0,0,1)\equiv(0,0,1,0). Padding serves algorithms that require a fixed size, e.g. a power of two (the FFT).

Representation by values and the interpolation theorem

Theorem (interpolation). Given nn pairs (xi,yi)∈C2(x_i,y_i)\in\mathbb C^2 with distinct xix_i, there is exactly one polynomial pp with degree bound nn such that p(xi)=yip(x_i)=y_i.

Proof. p(xi)=yip(x_i)=y_i is the linear system Xa=yX\mathbf a=\mathbf y with Xij=xijX_{ij}=x_i^j, a Vandermonde matrix V(x0,…,xn−1)V(x_0,\dots,x_{n-1}). If det⁡V≠0\det V\ne0 there is a unique solution, and det⁡V(x0,…,xn−1)=∏0≤i<j≤n−1(xj−xi)≠0(distinct points).\det V(x_0,\dots,x_{n-1})=\prod_{0\le i<j\le n-1}(x_j-x_i)\ne0\quad\text{(distinct points).} Determinant, by induction on nn. n=1n=1: det⁡=1\det=1 (empty product). Step: subtract x0x_0 times the previous column from each column, from the last to the second (Cj←Cj−x0Cj−1C_j\leftarrow C_j-x_0C_{j-1}, j=n−1,…,1j=n-1,\dots,1): the determinant is unchanged, the new entry (i,j)(i,j) is xij−1(xi−x0)x_i^{j-1}(x_i-x_0), and row 00 becomes (1,0,…,0)(1,0,\dots,0). Expanding along it and extracting (xi−x0)(x_i-x_0) from each row, det⁡V(x0,…,xn−1)=∏i=1n−1(xi−x0)⋅det⁡V(x1,…,xn−1). □\det V(x_0,\dots,x_{n-1})=\prod_{i=1}^{n-1}(x_i-x_0)\cdot\det V(x_1,\dots,x_{n-1}).\ \square

Definition (representation by values). For a base x∈Cn\mathbf x\in\mathbb C^n with distinct components, pp is represented by (x,y)(\mathbf x,\mathbf y), yi=p(xi)y_i=p(x_i). Different bases give different representations.

Example. p=x2p=x^2 on x=(0,1,2)\mathbf x=(0,1,2) is y=(0,1,4)\mathbf y=(0,1,4). This representation can also be extended (adding x3=3x_3=3, y3=9y_3=9) but less trivially than coefficient padding.

Sum and product by coefficients

Sum. s=a+b\mathbf s=\mathbf a+\mathbf b, cost TS(n)=nT_S(n)=n (with bounds m≤nm\le n: Θ(min⁡{m,n})\Theta(\min\{m,n\})).

Product. If p≡ap\equiv\mathbf a, q≡bq\equiv\mathbf b have bound nn, c=pqc=pq has degree at most 2n−22n-2: bound 2n−12n-1. The coefficient cjc_j collects all akbha_kb_h with k+h=jk+h=j, with max⁡{0,j−n+1}≤k≤min⁡{j,n−1}\max\{0,j-n+1\}\le k\le\min\{j,n-1\}.

Definition (linear convolution). For a,b∈Cn\mathbf a,\mathbf b\in\mathbb C^n, c=a⋆b∈C2n−1\mathbf c=\mathbf a\star\mathbf b\in\mathbb C^{2n-1} with cj=∑k=max⁡{0,j−n+1}min⁡{j,n−1}ak bj−k,0≤j≤2n−2.c_j=\sum_{k=\max\{0,j-n+1\}}^{\min\{j,n-1\}}a_k\,b_{j-k},\qquad0\le j\le2n-2. Then p(x)q(x)≡a⋆bp(x)q(x)\equiv\mathbf a\star\mathbf b. For lengths m,nm,n the result has length m+n−1m+n-1.

Example. a=(1,2,3)\mathbf a=(1,2,3), b=(4,5,6)\mathbf b=(4,5,6): c0=4c_0=4, c1=5+8=13c_1=5+8=13, c2=6+10+12=28c_2=6+10+12=28, c3=12+15=27c_3=12+15=27, c4=18c_4=18; so (1+2x+3x2)(4+5x+6x2)=4+13x+28x2+27x3+18x4(1+2x+3x^2)(4+5x+6x^2)=4+13x+28x^2+27x^3+18x^4.

Theorem (cost by definition). a⋆b\mathbf a\star\mathbf b with a,b∈Cn\mathbf a,\mathbf b\in\mathbb C^n takes n2n^2 products and (n−1)2(n-1)^2 sums: 2n2−2n+1=Θ(n2)2n^2-2n+1=\Theta(n^2).

Proof. Each pair (k,h)(k,h), 0≤k,h≤n−10\le k,h\le n-1, contributes to exactly one component ck+hc_{k+h}: n2n^2 products. There are 2n−12n-1 components and one with tt addends needs t−1t-1 sums; total addends n2n^2, so sums are n2−(2n−1)=(n−1)2n^2-(2n-1)=(n-1)^2. □\square

For n=8n=8: 64+49=11364+49=113. ⋆\star is commutative and associative (as the polynomial product).

Sum and product by values

Only possible if both are represented on the same base x\mathbf x: ys=yp+yq,yc=yp⊙yq(⊙=componentwise product).\mathbf y_s=\mathbf y_p+\mathbf y_q,\qquad\mathbf y_c=\mathbf y_p\odot\mathbf y_q\quad(\odot=\text{componentwise product}). Both cost Θ(n)\Theta(n): the product by values is linear, against quadratic by coefficients.

Beware of the degree bound. The product has bound 2n−12n-1, so it needs at least 2n−12n-1 points. With only nn points yp⊙yq\mathbf y_p\odot\mathbf y_q does not represent the product. Counterexample: p=q=1+xp=q=1+x on base (0,1)(0,1): yp⊙yq=(1,4)\mathbf y_p\odot\mathbf y_q=(1,4), whose bound-22 polynomial is 1+3x≠(1+x)2=1+2x+x21+3x\ne(1+x)^2=1+2x+x^2. On the extended base (0,1,2)(0,1,2): (1,2,3)⊙(1,2,3)=(1,4,9)(1,2,3)\odot(1,2,3)=(1,4,9), the values of (1+x)2(1+x)^2.

Coefficients →\to values: evaluation

By definition:

V(a, z)
n <- a.len
y <- a[0]; pow <- 1
for i <- 1 to n-1
    pow <- pow * z
    y <- y + a[i] * pow
return y

One sum and two products per iteration: TV(n)=3(n−1)T_V(n)=3(n-1).

Horner's rule. p(z)=a0+z(a1+z(a2+⋯+z(an−2+z an−1)… ))p(z)=a_0+z\bigl(a_1+z(a_2+\dots+z(a_{n-2}+z\,a_{n-1})\dots)\bigr):

HORNER(a, z)
n <- a.len
y <- a[n-1]
for j <- 2 to n
    y <- a[n-j] + z * y
return y

Correctness (invariant). At the end of iteration jj (2≤j≤n2\le j\le n), y=∑i=n−jn−1aiz i−n+jy=\sum_{i=n-j}^{n-1}a_iz^{\,i-n+j}. Before the loop y=an−1y=a_{n-1} (the formula for "j=1j=1"). If true for j−1j-1, the new value is an−j+z∑i=n−j+1n−1aiz i−n+j−1a_{n-j}+z\sum_{i=n-j+1}^{n-1}a_iz^{\,i-n+j-1}, where an−ja_{n-j} has exponent 00: true for jj. At j=nj=n, y=p(z)y=p(z). □\square

One sum and one product per iteration: TH(n)=2(n−1)T_H(n)=2(n-1), a third fewer than V. Example: 3+4x+5x2+6x33+4x+5x^2+6x^3 at z=2z=2: 6→5+12=17→4+34=38→3+76=796\to5+12=17\to4+34=38\to3+76=79.

Evaluation on a base x∈Cn\mathbf x\in\mathbb C^n repeats Horner nn times:

VAL(a, x)
n <- a.len
for i <- 0 to n-1
    y[i] <- HORNER(a, x[i])
return y

TVAL(n)=2n(n−1)=Θ(n2)T_{VAL}(n)=2n(n-1)=\Theta(n^2). No better is known for a generic base; the FFT exploits a special one.

Values →\to coefficients: interpolation

Linear system. Solve V(x)a=yV(\mathbf x)\mathbf a=\mathbf y with Gauss-Jordan: TI(n)=Θ(n3)T_I(n)=\Theta(n^3).

Lagrange formula. p(x)=∑k=0n−1yk∏j≠k(x−xj)∏j≠k(xk−xj)p(x)=\sum_{k=0}^{n-1}y_k\frac{\prod_{j\ne k}(x-x_j)}{\prod_{j\ne k}(x_k-x_j)}: each fraction is 11 at xkx_k, 00 at the other xjx_j, and has degree n−1n-1; by uniqueness it is the interpolating polynomial. Coefficients in Θ(n2)\Theta(n^2):

  1. coefficients of Q(x)=∏j=0n−1(x−xj)Q(x)=\prod_{j=0}^{n-1}(x-x_j): one factor at a time, O(n)O(n) each, O(n2)O(n^2) total;
  2. for each kk, Qk=Q/(x−xk)Q_k=Q/(x-x_k) by Ruffini's rule (O(n)O(n));
  3. Qk(xk)Q_k(x_k) by Horner (O(n)O(n)) and add ykQk(xk)Qk\frac{y_k}{Q_k(x_k)}Q_k to the coefficients of pp (O(n)O(n)). Steps 2 and 3 repeat nn times: O(n2)O(n^2).

Example. Points (0,0),(1,1),(2,4)(0,0),(1,1),(2,4): Q=x3−3x2+2xQ=x^3-3x^2+2x; Q0=x2−3x+2Q_0=x^2-3x+2 with Q0(0)=2Q_0(0)=2; Q1=x2−2xQ_1=x^2-2x with Q1(1)=−1Q_1(1)=-1; Q2=x2−xQ_2=x^2-x with Q2(2)=2Q_2(2)=2. p=0+Q1−1+4Q22=−(x2−2x)+2(x2−x)=x2p=0+\frac{Q_1}{-1}+4\frac{Q_2}2=-(x^2-2x)+2(x^2-x)=x^2.

Why a special base is needed

The generic conversions cost Θ(n2)\Theta(n^2) or more, so the path "coefficients →\to values on an extended base →\to ⊙\odot →\to coefficients" costs Θ(n2)\Theta(n^2), like the direct convolution. But if a base existed where evaluation and interpolation cost Θ(nlog⁡n)\Theta(n\log n), the polynomial product would cost Θ(nlog⁡n)\Theta(n\log n). It exists: the roots of unity. The transformation is 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 →), computed by the FFT (Fast Fourier TransformThe FFT computes the DFT_n (n a power of two) by divide and conquer: split the coefficients by index parity, p(x) = p0(x^2) + x p1(x^2), and by the halving lemma two DFTs of order n/2 suffice; y_i = y0_i + w^i y1_i and y_{i+n/2} = y0_i - w^i y1_i. The recurrence T(n) = 2T(n/2) + 2n with T(1) = 0 has exact solution 2 n log2 n. The inverse has matrix F^{-1}_{ij} = w^{-ij}/n and is obtained with the FFT on negative powers (then dividing by n outside the recursion) or with a reversed DFT_n(y). LIN_CONV multiplies two polynomials with n coefficients in 12 n log2(2n) + 4n operations.Fast Fourier Transform →). In signal terms, the linear convolution of two finite sequences is the response of an FIR system and equals the product of the polynomials in z−1z^{-1}.

Versione ripasso

Teoria collegata