On the FFT

Fast Fourier Transportation(FFT)

· Polynomial expression

Factor expression

For a polynomial number of boundaries n \ (A (x) = \ sum_ {j = 0} ^ {n-1} {a_jx ^ j} \) , the coefficients of the expression vector is composed of a coefficient \ (A = (a_0, A_1, ...,. 1-n-A_ {}) \) .

Point value expression

A sector number of the polynomial A (x) n is a point value expression that a value of n points of the set consisting of
\ [{(x_0, y_0) , (x_1, y_1), ..., (x_ {n -1}, y_ {n-1
})} \] such that k = 0,1, ..., n- 1, all \ (x_k \) varies,
\ [Y_k = A (x_k) \ ]
simply find the point value calculation we can not arbitrarily substituted into the same number of n, and then draw point, time complexity \ (\ Theta (2 ^ n) \) . Behind you can see, if we use a little clever value, you can make the time complexity of optimization to \ (\ Theta (the n-\ lg_2n) \) .

Inverse evaluation (expressed from the expression of a polynomial coefficient point value determined form) referred calculated interpolation .

· Polynomial operation

\[ C_j=\sum_{k=0}^{j}{A_kB_{j-k}} \]

The above is a polynomial multiplication, we referred to as A and B, C convolution (Convolution), expressed as \ (C = A \ B bigotimes \) .

The main idea of ​​the FFT is first converted into the points A and B values ​​expressed, and the expression values ​​obtained point C, and then do it again against obtain coefficients C expression.

· DFT and FFT

Above slow approach, we use \ (\ Theta (n ^ 2 ) \) time to turn into a point value for each polynomial expression, and then use the \ (\ Theta (n ^ 2 ) \) to turn back time, obviously very slow, not as violent.

We think, is it possible to use some special number, so that each operation can be done once you can get a few more of it? The answer is yes: unit root complex roots

n - th complex root satisfies \ (\ omega ^ n = 1 \) complex \ (\ Omega \) . Unit exactly the n-th complex root of n, for k = 0,1, ..., n- 1, which is the root \ (^ E {\ IK PI / n} \) . To explain this expression, we use complex exponential form is defined:
\ [^ {E} = COS IU (U) + ISIN (U) \]

That is, given a unit circle, evenly distributed above the n vectors as:
Here Insert Picture Description

• For the n-th unit complex roots

Above figure as an example we can see that every n (here, 8) subunit complex roots are a vector, they form a group under multiplication sense.

Lemma 1: (Lemma erasing)
\ [for any integer n \ geqslant0, k \ geqslant0, and d> 0, \]

\ [\ Omega ^ {dk} _ {dk} = \ omega ^ {k} _ {n} \]

证明
\[ \omega^{dk}_{dk}=(e^{2\pi i/dn})^{dk}=(e^{2\pi i/n})^k=\omega^{k}_{n} \]
引理2:(折半引理)
\[ 如果n>0为偶数,那么n个n次单位根的平方集合就是n/2个n/2次单位根的集合 \]
证明
\[ (\omega^{k+n/2}_{n})^2=\omega^{2k+n}_n=\omega^{2k}_n\omega^n_n=(\omega^k_n)^2 \]
因此\(\omega^k_n\)\(\omega^{k+n/2}_n\)平方相等。

引理3:(求和引理)
\[ 对于任意整数n\geqslant0和不能被n整除的非负整数k,有 \]

\[ \sum_{j=0}^{n-1}(\omega^k_n)^j=0 \]

证明
\[ \sum_{j=0}^{n-1}(\omega^k_n)^j=\frac{(\omega^k_n)^0(1-(\omega^k_n)^n)}{1-\omega^{k}_{n}}=\frac{(\omega^n_n)^k-1}{\omega^{k}_{n}-1}=\frac{(1)^k-1}{\omega^{k}_{n}-1}=0 \]
因为要求k不能被n整除,而且仅当k被n整除时\[\omega^k_n=1\]成立,同时保证分母不为0。

DFT

回顾一下,我们希望计算次数界为n的多项式
\[ A(x)=\sum_{j=0}^{n-1}{a_jx^j} \]
\[\omega^0_n,\omega^1_n,...,\omega^{n-1}_n\]处的值。假设A以系数形式给出,接下来定义结果\(y_k\):
\[ y_k=A(\omega^k_n)=\sum_{j=0}^{n-1}a_j\omega^{kj}_n \]
向量\(y=(y_0,y_1,...,y_{n-1})\)就是系数向量\(a=(a_0,a_1,...,a_{n-1})\)离散傅里叶变换(DFT)。我们也记作\(y=DFT_n(a)\)

FFT

通过使用一种称为快速傅里叶变换(FFT)的方法,利用复根的特殊性质,我们就可以在\[\Theta(n\lg n)\]的时间内计算出\(DFT_n(a)\)

注意:通篇的n我们假设是2的整数次幂。

FFT利用分治策略,采用A(x)中偶数下标的系数与奇数下标的系数,分别定义两个新的次数界为n/2的多项式

\[ A^{[0]}(x)=a_0+a_2x+a_4x^2+...+a_{n-2}x^{n/2-1} \]

\[ A^{[1]}(x)=a_1+a_3x+a_5x^2+...+a_{n-1}x^{n/2-1} \]

可以很容易发现
\[ A(x)=A^{[0]}(x^2)+xA^{[1]}(x^2) \]
所以原问题转化为求两个次数界为n/2的多项式\[A^{[0]}(x)\]\[A^{[1]}(x)\]在点\[(\omega^0_n)^2,(\omega^1_n)^2,...,(\omega^{n-1}_n)^2\]的取值。

所以我们可以发现在求出\[A^{[0]}(x^2)\]\[A^{[1]}(x^2)\]以后,可以算出两个复根的结果:
\[ y_k=y^{[0]}_k+\omega^k_ny^{[1]}_k =A^{[0]}(\omega^{2k}_n)+\omega^k_nA^{[1]}(\omega^{2k}_n) =A(\omega^k_n) \]
还有
\[ y_{k+(n/2)}=y^{[0]}_k-\omega^{k}_ny^{[1]}_k =y^{[0]}_k+\omega^{k+(n/2)}_ny^{[1]}_k =A^{[0]}(\omega^{2k}_n)+\omega^{k+(n/2)}A^{[1]}(\omega^{2k}_n) \]
\[ =A^{[0]}(\omega^{2k+n}_n)+\omega^{k+(n/2)}A^{[1]}(\omega^{2k+n}_n) =A(\omega^{k+(n/2)}_n) \]

所以就有代码:

void FFT(comp *a,int n,int inv){
    if(n==1) return;
    int mid=n/2;
    for (int i=0;i<mid;++i) c[i]=a[i*2],c[i+mid]=a[i*2+1];
    for (int i=0;i<n;++i) a[i]=c[i];
    FFT(a,mid,inv);
    FFT(a+mid,mid,inv);
    comp wn={cos(2.0*pi/n),inv*sin(2.0*pi/n)},w={1,0};
    for (int i=0;i<mid;++i,w=w*wn){
        c[i]=a[i]+w*a[i+mid];
        c[i+mid]=a[i]-w*a[i+mid];
    } 
    for (int i=0;i<n;++i) a[i]=c[i];
}

·在单位复数根的插值

现在我们展示如何在单位复数根处插值来完成多项式乘法方案,使得我们把一个多项式从点值表达转换回系数表达。
我们可以把DFT写成矩阵乘积
\[ \left[ \begin{matrix} y_0\\ y_1\\ y_2\\ \vdots\\ y_{n-1} \end{matrix} \right]= \left[ \begin{matrix} 1 & 1 & 1 & 1 &\cdots& 1\\ 1 & \omega_n & \omega^2_n & \omega^3_n &\cdots& \omega^{n-1}_n\\ 1 & \omega^2_n & \omega^4_n & \omega^6_n &\cdots& \omega^{2(n-1)}_n\\ 1 & \omega^3_n & \omega^6_n & \omega^9_n &\cdots& \omega^{3(n-1)}_n\\ \vdots & \vdots& \vdots& \vdots& \ddots& \vdots\\ 1 & \omega^{n-1}_n & \omega^{2(n-1)}_n & \omega^{3(n-1)}_n &\cdots& \omega^{(n-1)(n-1)}_n \end{matrix} \right] \left[ \begin{matrix} a_0\\ a_1\\ a_2\\ \vdots\\ a_{n-1} \end{matrix} \right] \]
尴尬的是跑得贼慢:
Here Insert Picture Description
随便卡卡就爆了....
分治难免递归,递归常数大。
于是,考虑改进。

· Butterfly Transformation

Here Insert Picture Description
Pirates one
can be found, each in turn, is under the underlying binary form their final position in the sequence.
So there will be an iterative style of play.

void FFT(Moon *a,int inv){
    int i,j,len;
    for (i=0;i<n;++i)
        if(i<R[i])swap(a[i],a[R[i]]);
    for (len=2;len<=n;len<<=1){
        int half=len/2;
        Moon w,wn={cos(Pi/half),inv*sin(Pi/half)};
        for (j=0;j<n-i;j+=len,w={1,0}){
            for (i=0;i<half;++i,w=w*wn){
                Moon q=w*a[j+half+i],Q=a[j+i];
                a[j+half+i]=Q-q;
                a[j+i]=Q+q;
            }
        }
    }
}
int main(){
    for (i=0;i<n;++i) R[i]=(R[i>>1]>>1)|((i&1)<<(p-1));
} 

Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/Chandery/p/11332777.html
FFT