Salta al contenuto
Note per Studenti Notable Transforms and Polynomial Exponentiation Report

Notable Transforms and Polynomial Exponentiation

In questa pagina 3

Two applications of the FFT. First, the structure of the vector is exploited to compute the DFT with fewer operations than the general 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 →); second, the representation by values is used to compute the coefficients of the power p(x)kp(x)^k. The method is the same as in the whole chapter: modify the substructure property of the FFT with the extra information on the instance and derive the new recurrence.

Transforms known a priori

Component ii of y=Fna\mathbf y=F_n\mathbf a is the dot product of row ii of FnF_n with a\mathbf a; the first row is all ones.

Constant vector. If a=(a,…,a)∈Cn\mathbf a=(a,\dots,a)\in\mathbb C^n then DFTn(a)=(na,0,…,0)\mathrm{DFT}_n(\mathbf a)=(na,0,\dots,0).

Proof. y0=∑ja=nay_0=\sum_ja=na. For 0<i≤n−10<i\le n-1, yi=a∑j=0n−1(ωni)j=0y_i=a\sum_{j=0}^{n-1}(\omega_n^i)^j=0 by the summation lemma (ii is not a multiple of nn). □\square

Only the first component. If a=(a,0,…,0)\mathbf a=(a,0,\dots,0) then DFTn(a)=(a,a,…,a)\mathrm{DFT}_n(\mathbf a)=(a,a,\dots,a).

Proof. a=a e0\mathbf a=a\,\mathbf e_0 and Fne0F_n\mathbf e_0 is the first column of FnF_n, all ones. Equivalently p(x)=ap(x)=a takes the value aa everywhere. □\square

More generally DFT(ej)\mathrm{DFT}(\mathbf e_j) is column jj of FnF_n, (ωnij)i(\omega_n^{ij})_i. Example n=4n=4: (7,7,7,7)→(28,0,0,0)(7,7,7,7)\to(28,0,0,0) and (7,0,0,0)→(7,7,7,7)(7,0,0,0)\to(7,7,7,7).

(n,k)(n,k)-sparse vectors

Definition. For n,kn,k powers of two, 1≤k≤n1\le k\le n, x∈Cn\mathbf x\in\mathbb C^n is (n,k)(n,k)-sparse if xi=0x_i=0 whenever i mod k≠0i\bmod k\ne0.

Only the n/kn/k components at multiples of kk may be nonzero (separated by k−1k-1 zeros). An (8,4)(8,4)-sparse vector looks like (x0,0,0,0,x4,0,0,0)(x_0,0,0,0,x_4,0,0,0). Extremes: every vector is (n,1)(n,1)-sparse; an (n,n)(n,n)-sparse vector has only x0x_0 (known DFT).

Goal. A D&C algorithm computing DFTn(x)\mathrm{DFT}_n(\mathbf x) for (n,k)(n,k)-sparse x\mathbf x with T(n,k)=O(nklog⁡nk)T(n,k)=O(\frac nk\log\frac nk): for k=1k=1 the usual O(nlog⁡n)O(n\log n), for k=nk=n no operations.

Strengthened substructure. With x[0],x[1]\mathbf x^{[0]},\mathbf x^{[1]} the even and odd vectors, for 1<k<n1<k<n (k≥2k\ge2 even):

  • x[0]\mathbf x^{[0]} is (n/2,k/2)(n/2,k/2)-sparse: xj[0]=x2jx^{[0]}_j=x_{2j} is zero if 2j mod k≠02j\bmod k\ne0, i.e. j mod (k/2)≠0j\bmod(k/2)\ne0;
  • x[1]\mathbf x^{[1]} is the zero vector: xj[1]=x2j+1x^{[1]}_j=x_{2j+1} has odd index, never a multiple of the power of two k≥2k\ge2.

So y[1]=0\mathbf y^{[1]}=\mathbf 0 and the FFT recombination becomes yi=yi[0]y_i=y^{[0]}_i, yi+n/2=yi[0]y_{i+n/2}=y^{[0]}_i: the DFT is y[0]\mathbf y^{[0]} concatenated with itself, with no arithmetic.

SPARSE_FFT(x, k)
n <- x.len
if k = 1 then return FFT(x)
if k = n then return (x[0], x[0], ..., x[0])        { n copies }
x0 <- (x[0], x[2], ..., x[n-2])
y0 <- SPARSE_FFT(x0, k/2)
for i <- 0 to n/2 - 1
    y[i] <- y0[i];  y[i + n/2] <- y0[i]
return y

Correctness follows from that of the FFT and the strengthened substructure, by induction on kk.

Cost. Conquer contains only assignments (not counted as arithmetic operations): T(n,k)={2nlog⁡2nk=1 (FFT)0k=nT(n2,k2)1<k<n.T(n,k)=\begin{cases}2n\log_2n&k=1\ (\text{FFT})\\0&k=n\\T(\frac n2,\frac k2)&1<k<n.\end{cases} Unfolding: T(n,k)=T(n2,k2)=⋯=T(nk,1)=2nklog⁡2nk=Θ(nklog⁡nk)T(n,k)=T(\frac n2,\frac k2)=\dots=T(\frac nk,1)=2\frac nk\log_2\frac nk=\Theta(\frac nk\log\frac nk), also valid for k=1k=1 and k=nk=n (log⁡1=0\log1=0). If each assignment cost one unit, the work would be linear and the total O(nklog⁡nk+n)O(\frac nk\log\frac nk+n).

Consequence. The DFT of an (n,k)(n,k)-sparse vector is made of kk copies of DFTn/k(x0,xk,x2k,…,xn−k)\mathrm{DFT}_{n/k}(x_0,x_k,x_{2k},\dots,x_{n-k}). Directly: yi=∑m=0n/k−1xmkωnimk=∑mxmkωn/kimy_i=\sum_{m=0}^{n/k-1}x_{mk}\omega_n^{imk}=\sum_mx_{mk}\omega_{n/k}^{im} (cancellation), periodic in ii with period n/kn/k.

Example (n=8n=8, k=4k=4). x=(2,0,0,0,1,0,0,0)\mathbf x=(2,0,0,0,1,0,0,0): DFT2(2,1)=(3,1)\mathrm{DFT}_2(2,1)=(3,1) repeated 44 times: DFT8(x)=(3,1,3,1,3,1,3,1)\mathrm{DFT}_8(\mathbf x)=(3,1,3,1,3,1,3,1).

Example (decomposition by linearity). x=(5,3,3,3,4,3,3,3)=(3,…,3)+(2,0,0,0,1,0,0,0)\mathbf x=(5,3,3,3,4,3,3,3)=(3,\dots,3)+(2,0,0,0,1,0,0,0): the first is constant with DFT (24,0,…,0)(24,0,\dots,0), the second is (8,4)(8,4)-sparse with DFT (3,1,3,1,3,1,3,1)(3,1,3,1,3,1,3,1). Sum: y=(27,1,3,1,3,1,3,1)\mathbf y=(27,1,3,1,3,1,3,1).

Strategy (repeatable): look at the structure, decompose (also by linearity) into pieces with known transforms, or rewrite the FFT substructure using the structure and study the recurrence.

Exponentiation of polynomials

Given p(x)=∑i<naixi≡a∈Cnp(x)=\sum_{i<n}a_ix^i\equiv\mathbf a\in\mathbb C^n and an integer k≥1k\ge1, find the coefficients of q=pkq=p^k, i.e. a(k)=a⋆⋯⋆a\mathbf a^{(k)}=\mathbf a\star\dots\star\mathbf a (kk times). The degree bound of qq is (n−1)k+1(n-1)k+1.

Approach 1: repeated squaring with convolution

Convolution is associative, so apply Fast Exponentiation by Repeated SquaringPower x^n with an associative operator having an identity, cost = number of applications of the operator; STUPID_POWER costs n-1, the substructure x^n = (x^floor(n/2))^2 (times x for odd n) gives POWER with T(n) = floor(log2 n) + (number of 1 bits of n) - 1, Theta(log n) (worst case 2 floor(log2 n), n = 2^t - 1); iterative versions POWER2 and POWER3 from the binary representation of n; example with the Fibonacci matrix; unit-cost model valid only if the operator does not make operands grow.Fast Exponentiation by Repeated Squaring → with ⋆\star as the operator. Let d=⌊log⁡2k⌋+1d=\lfloor\log_2k\rfloor+1 and k=∑iki2ik=\sum_ik_i2^i: a(k)=a(k020)⋆⋯⋆a(kd−12d−1)\mathbf a^{(k)}=\mathbf a^{(k_02^0)}\star\dots\star\mathbf a^{(k_{d-1}2^{d-1})}.

EXPONENTIATE(a, k)
d <- floor(log2 k) + 1
h <- k                         { h = (k_{d-1} ... k_0) in base 2 }
y <- a                         { at iteration i: y = a^(2^i) }
z <- (1)                       { identity of the convolution; accumulates the product }
for i <- 0 to d - 1
    if h is odd then z <- LIN_CONV(z, y)       { digit k_i = 1 }
    y <- LIN_CONV(y, y)                         { y = a^(2^(i+1)) }
    h <- floor(h / 2)
return z

LIN_CONV is used in its arbitrary-length version (padding to a power of two). Analysis. At the start of iteration ii, y has length at most 2in2^in (length of a(2i)\mathbf a^{(2^i)} is 2i(n−1)+12^i(n-1)+1) and z has length less than 2in2^in. So Ti=O(2inlog⁡(2in))=O(2inlog⁡n+2in i)T_i=O(2^in\log(2^in))=O(2^in\log n+2^in\,i), and since 2d≤2k2^d\le2k: T(n,k)=∑i=0d−1Ti=O(n2dlog⁡n+n2dd)=O(nklog⁡(nk)).T(n,k)=\sum_{i=0}^{d-1}T_i=O\bigl(n2^d\log n+n2^dd\bigr)=O\bigl(nk\log(nk)\bigr).

Approach 2: power of the values

Given values of pp on an extended base with h≥(n−1)k+1h\ge(n-1)k+1 points, the values of q=pkq=p^k are the powers of the values: q(xi)=(p(xi))kq(x_i)=(p(x_i))^k. So evaluate, raise, interpolate.

EXPONENTIATE2(a, k)
n <- a.len
h <- 2^ceil(log2((n-1)*k + 1))              { first power of two >= (n-1)k+1 }
yp <- FFT((a | 0_{h-n}))
for i <- 0 to h - 1
    yq[i] <- POWER3(yp[i], k)                 { scalar power by squaring, O(log k) }
return INV_FFT(yq)

Correctness: qq has bound ≤h\le h, so yq\mathbf y_q determines it and INV_FFT returns its coefficients (followed by padding zeros). Cost: FFT and INV_FFT on size h<2((n−1)k+1)≤2nkh<2((n-1)k+1)\le2nk: Θ(nklog⁡(nk))\Theta(nk\log(nk)); plus hh scalar powers of Θ(log⁡k)\Theta(\log k) products each: Θ(nklog⁡k)\Theta(nk\log k). Total Θ(nklog⁡(nk))\Theta(nk\log(nk)): the same order as Approach 1, but simpler and with better constants (one forward transform, one inverse and hh powers, instead of O(log⁡k)O(\log k) convolutions of ever longer vectors).

Example (p=1+xp=1+x, k=4k=4). n=2n=2, bound of qq: (2−1)⋅4+1=5(2-1)\cdot4+1=5, so h=2⌈log⁡25⌉=8h=2^{\lceil\log_25\rceil}=8. (a∣0)=(1,1,0,0,0,0,0,0)(\mathbf a|0)=(1,1,0,0,0,0,0,0), yp,i=1+ω8iy_{p,i}=1+\omega_8^i:

ii 0 1 2 3 4 5 6 7
(1+ω8i)4(1+\omega_8^i)^4 1616 (6+42)i(6+4\sqrt2)i −4-4 −(6−42)i-(6-4\sqrt2)i 00 (6−42)i(6-4\sqrt2)i −4-4 −(6+42)i-(6+4\sqrt2)i

(e.g. (1+ω8)2=1+2ω8+i=(1+2)(1+i)(1+\omega_8)^2=1+2\omega_8+i=(1+\sqrt2)(1+i), so (1+ω8)4=(3+22)⋅2i=(6+42)i(1+\omega_8)^4=(3+2\sqrt2)\cdot2i=(6+4\sqrt2)i.) The inverse transform is (1,4,6,4,1,0,0,0)(1,4,6,4,1,0,0,0): (1+x)4=1+4x+6x2+4x3+x4(1+x)^4=1+4x+6x^2+4x^3+x^4, with three padding zeros.

Versione ripasso

Teoria collegata