Fast Fourier Transform (FFT) for polynomial multiplication

When I still have FFT, write a blog and keep it for later. . . . . .

FFT is used to solve polynomial multiplication, then first we must know what polynomial is.

\[A(x) = a_0+a_1x^1+a_2x^2+···+a_{n-1}x^{n-1} \]

This is an n-1 degree polynomial (the highest term is \ (x ^ {n-1} \) ), \ (a_0, a_1, ··· a_ {n-1} \) is the coefficient of each item, the polynomial Can be written as:

\[A(x) = \sum_{j=0}^{n-1}a_jx^j \]

A polynomial can be determined by a set of coefficients, and the vector formed by this set of coefficients is also called the coefficient vector (such as \ (A (x) \) . The coefficient vector is: [ \ (a_0 \ \ \ a_1 \ ··· \ a_ {n-1} \) ]), the way of expressing a polynomial through the coefficient vector is called coefficient notation

Polynomial multiplication is that we know two polynomials: \ (A (x), B (x) \) , which are polynomials of degree n-1 and m-1, respectively.

\[A(x) = \sum_{j = 0}^{n - 1}a_jx^j\\ B(x) = \sum_{j = 0}^{m-1}b_jx^j \]

Now talk about multiplying these two polynomials to get a polynomial of degree n + m-1 highest:

\[C(x) = \sum_{j = 0}^{n+m-1}c_jx^j \]

Then solving \ (C (x) \) is to find its coefficient vector : [ \ (c_0 \ \ c1 \ \ ··· \ \ c_j \ \ ··· \ \ c_n + m-1 \) ]

If we traverse each item of \ (A (x), B (x) \) directly is \ (O (n * m) \) , we need a faster algorithm, which is what we are discussing: fast Fourier Transform (FFT)

Front cheese:

Point value notation

For a known polynomial \ (A (x) \) , substitute \ (x_0 \) to get a definite \ (y_0 \) , and you can think of \ ((x_0, y_0) \) as coordinates A point on the tie. Further, any number of unequal independent variables can be substituted into \ ((x_1, x_2, ···) \) to get more points: \ ((x_1, y_1), (x_2, y_2), ·· · \)

We know that through two (different) points, the first-degree polynomial can be resolved \ ((ax + b) \) , and through three different points the second-order polynomial can be resolved \ ((ax ^ 2 + bx + c) \) , So it can be further deduced that n + 1 polynomials can be solved through n + 1 different points

Through n + 1 different points \ (\ left \ {(x_1, y_1), (x_2, y_2), ··· \ right \} \) , uniquely determine a polynomial of degree n, this method is also called polynomial point Value notation

We were surprised to find that the multiplication of two polynomials represented by point values ​​has a complexity of \ (O (n) \) ! Specific method: \ (C (x_i) = A (x_i) × B (x_i) \) , so O \ ((n) \) enumeration \ (x_i \) is enough.

If we convert two polynomials into point value representations, then multiply, and then convert the new point value representations into polynomials, wouldn't it be possible to \ (O (n) \) solve polynomial multiplication!

... Unfortunately, obviously, the naive algorithm that converts polynomials into point-value representations is \ (O (n ^ 2) \) . In addition, even if you might not—the plain “interpolation algorithm” that converts point-value representations to polynomials is also \ (O (n ^ 2) \) .

But you can find bottlenecks large integer multiplication complexity may be "converted into a polynomial point value represents" This step (and the reverse operation), as long as this is done you can \ (O (n) \) seeking answers. If only this step can be optimized.

So I introduced two other front cheeses

Discrete Fourier Transform (DFT) and Inverse Discrete Fourier Transform (IDFT), the formula is as follows:

\[X_k = \sum_{j = 0}^{n-1}x_je^{\frac{2\pi i}{n}kj} (DFT)\\ X_j = \frac{1}{n}\sum_{k = 0}^{n-1}X_ke^{\frac{-2\pi i}{n}jk}(IDFT) \]

In the above two formulas, \ (i \) is the imaginary root ( \ (i ^ 2 = -1 \) ), and the proof of these two formulas will be given at the end of the article.

We set some variables and substitute these two formulas into our previous problem.

Suppose: \ (w = e ^ {\ frac {2 \ pi i} {N}} \) , where \ (N = n + m \) , the nature of the variable \ (w \) will not be discussed first, and will be discussed later Yes again

\[c_j = f(j),C_k = C(w^k) \]

Substituting these variables into the DFT formula gives:

\[C_k = C(w^k)=\sum_{j = 0}^{N-1}c_j(w^k)^j= \sum_{j=0}^{N-1}c_je_{\frac{2\pi i}{N}kj} \]

According to the above formula, DFT is related to our target polynomial \ (C (x) \) , and naturally the coefficient of the polynomial can be calculated by IDFT \ (c_j \) :

\[c_j = \frac{1}{N}\sum_{k=0}^{N-1}C_k(w^{-j})^k=\frac{1}{N}\sum_{k=0}^{N-1}C_ke^{-\frac{2\pi i}{N}jk} \]

However, since I do n’t know the specific value of \ (c_j \) (if I know it, I ’ll finish the flowering), I ca n’t use DFT to calculate \ (C (w ^ k) \)

However, we know: \ (C (x) = A (x) * B (x) \) , and the coefficients of the polynomial \ (A (x) B (x) \) are known, so we can “curve to save the country ":

\[C(w^k)= A(w^k)*B(w^k) \]

Use this formula to calculate \ (C (w ^ k) \) , and then use IDFT to calculate \ (c_j \) .

The above is a summary of FFT, the following discussion

One cheese: N times unit root

Discussion In order to better \ (^ W = E {\ FRAC {2 \ I PI {N}}} \) , we from Euler's formula Speaking

\ [e ^ {i \ theta} = \ cos \ theta + i \ sin \ theta \]

The proof of the formula is also given at the end of the article.

\ (e ^ {i \ theta} \) is a complex number, the value of its real part is \ (\ cos \ theta \) and the value of the imaginary part is \ (\ sin \ theta \) , so, \ (e ^ {i \ theta} \) corresponding to the complex plane of a distance between the point of origin is a constant, as shown in

Because \ (\ sin \ theta, \ cos \ theta \) is a periodic function (period is \ (2 \ pi \) ), it can be concluded that:

\ [e ^ {i (\ theta + 2 \ pi) = e ^ {i \ theta}} \]

And no matter what value \ (\ theta \) takes, \ (e ^ {i \ theta} \) is distributed on the unit circle of the complex plane. Since it is a unit element, \ (\ theta \) is equal to It is the arc length on the circle from 1 (starting point) to \ (e ^ {i \ theta} \) .

The Euler formula is briefly introduced above, and the following returns to the variable we defined: \ (w = e ^ {\ frac {2 \ pi i} {n}} \) , for convenience in the previous overview, \ (w \) No subscripts are added, a clearer representation will be added to \ (w \) here : \ (w_n = e ^ {\ frac {2 \ pi i} {n}} \)

It can be guided from the discussion of the surface Euler formula, \ (e ^ {2 \ pi i} = 1 \) , which is equivalent to going around the unit circle and returning to the starting point, and \ (\ frac {2 \ pi } {n} \) is equivalent to this unit circle divided into \ (n \) equal divisions, and \ (w_n ^ k \) corresponds to the \ (k \) equal division point on the unit circle ( \ (k = 0 , 1,2, ···, n-1 \) )

This is an example of \ (n = 18 \) , and two values ​​are marked \ (\ left \ {w_n ^ k | k = 2,10 \ right \} \) . Through the previous discussion and this chestnut, it should be I have a more intuitive understanding of \ (w_n ^ k \). Having said so much, I should be able to guess the Nth unit root refers to it. About the Nth unit root, I introduce several properties:

\ [1. \ \ w_n ^ {k + \ frac {n} {2}} = e ^ {\ frac {2 \ pi i} {n} (k + \ frac {n} {w})} = e ^ {\ frac {2 \ pi i} {n} k} e ^ {i \ pi} = w_n ^ k (\ cos \ pi + i \ sin \ pi) = - w_n ^ k \\ 2. \ \ w_ {2n} ^ {2k} = w_n ^ k \\ 3. \ \ w_n ^ {- k} = 1 · e ^ {\ frac {i2 \ pi} {n} (- k)} = e ^ {2 \ pi i} e ^ {\ frac {2 \ pi i} { n} (- k)} = e ^ {\ frac {2 \ pi i} {n} (nk)} = w_n ^ {nk} \]

Property 1 is the elimination lemma , property 2 is the halving lemma , property 3 I forgot to call it. . .

Calculation \ (C (w_N ^ k) \)

First, define \ (N = m + n \) (n, m are the number of coefficients of polynomial \ (A (x), B (x) \) respectively). As mentioned earlier, DFT cannot be used directly to calculate \ (C ( w_n ^ k) \) , you need to calculate \ (A (w_N ^ k), B (w_N ^ k) \)

If the singular number directly put \ (\ left \ {w_N ^ k | k = 0,1,2, ···, N-1 \ right \} \) into the simple and rough generation, the time complexity is still \ (O ( N ^ 2) \) , regardless the back calculation, can not reduce the complexity of the algorithm, because some methods require the use of, to reduce \ (a (w_N ^ k) , B (w_N ^ k) \) time

For example, we have a polynomial:

\[H(x) = h_0+h_1x^1+h_2x^2+h_3x^3 \]

We can separate its odd and even terms:

\[H(x)=(h_0+h_2x^2)+(h_1x^1+h_3x^3)=(h_0+h_2x^2)+x(h_1+h_3x^2)\\= H1(x^2)+xH2(x^2) \]

And \ (H1 (x), H2 (x) \) can also be calculated recursively, the method used in \ (A (x), B (x) \) is:

\[A(x) = A1(x^2)+xA2(x^2)B(x) = B1(x^2)+xB2(x^2) \]

The time complexity becomes:

\ [T (n) = 2T (\ frac {n} {2}) + O (n) = O (nlogn) \]

In addition, you can also use the properties of the unit root discussed earlier to reduce the amount of calculation, for example, \ (0 \ leq k <\ frac {n} {2} \) :

\[A(w_n^k) = A1(w_n^{2k})+w_n^kA2(w_n^{2k})=A1(w_{\frac{n}{2}}^k)+w_n^kA2(w_{\frac{n}{2}}^k)\\A(w_n^{k+\frac{n}{2}}) = A1(w_n^{2k+n})+w_n^{k+\frac{n}{2}}A2(w_n^{2k+n}) = A1(w_n^{2k})-w_n^kA2(w_n^{2k}) \\= A1(w_{\frac{n}{2}}^k)-w_n^kA2(w_{\frac{n}{2}}^k)) \]

In this way, the general value range of \ (k \) can be calculated to get the result of the entire value range, so after calculating \ (A (w_N ^ k), B (w_N ^ k) \) , you can pass Simple multiplication to calculate \ (C (w_N ^ k) \) :

\[C(w_N^k) = A(w_N^k)B(w_N^k),k=0,1,2,···,N-1 \]

Solve the \ (C (x) \) coefficient

This step of calculation is almost half done, just use the IDFT formula to solve the coefficients of \ (C (x) \) :

\[c_j = \frac{1}{N}\sum_{k=0}^{N-1}C_ke^{-\frac{2\pi i}{N}jk} = \frac{1}{N}\sum_{k=0}^{N-1}C_k(e^{-\frac{2\pi i}{N}j})^k=\frac{1}{N}\sum_{k=0}^{N-1}C_k(w_N^{-j})^k \]

In the above formula \ (C_k = C (w_N ^ k) \) , in addition, for the purpose of expression, usually an N-1 degree polynomial \ (D (x) \) is constructed , and its coefficients are each \ (C_k \ ) Value:

\[D(x) = C_0 + C_1x^1+C_2x^2+···+C_{N-1}x^{N-1} = \sum_{k=0}^{N-1}C_kx^k \]

Therefore, the final \ (c_j \) calculation formula can be written as:

\[c_j = \frac{1}{N}\sum_{k=0}^{N-1}C_k(w_N^{-j})^k=\frac{1}{N}\sum_{k=0}^{N-1}C_k(w_N^{N-j})^k=\frac{1}{N}D(w_N^{N-j}) \]

The second part of the above formula uses the property of unit root 3:

\[c_0 = \frac{1}{N}D(w_N^{N-0}) = \frac{1}{N}D(w_N^N)=\frac{1}{N}D(w_N^0) \]

to sum up

1. Take N unit roots: [ \ (w_N ^ 0 \ \ w_N ^ 1 \ \ w_N ^ 2 \ \ ··· \ \ w_N ^ {N-1} \) ]

2. They are substituted into the polynomial \ (A (x), B (x) \) in

3. Calculate \ (C (w_N ^ k) = A (w_N ^ k) B (w_N ^ k) \)

4. Construct a new polynomial \ (D (x) = C_0 + C_1x ^ 1 + C_2x ^ 2 + ·· + C_ {N-1} x ^ {N-1} \)

5. Calculation \ (D (w_N ^ k) \)

6. Calculate the polynomial \ (C (x) \) coefficients \ (c_j = \ frac {1 } {N} D (w_N ^ {Nj}), j = 0,1,2, ···, N-1 \)

at last (to be continued)

  • Proof of Euler's formula
  • DFT and IDTF certification

Guess you like

Origin www.cnblogs.com/excellent-zzy/p/12702388.html