Tutorial: A simple O(n log n) polynomial multiplication algorithm

Правка en53, от pajenegod, 2023-07-21 20:29:32

Hi Codeforces!

I have something exciting to tell you guys about today! I have recently come up with a really neat and simple recursive algorithm for multiplying polynomials in $$$O(n \log n)$$$ time. It is so neat and simple that I think it might possibly revolutionize the way that fast polynomial multiplication is taught and coded. You don't need to know anything about FFT to understand and implement this algorithm.

Big thanks to nor, c1729 and Spheniscine for discussing the contents of the blog with me and comming up with ideas for how to improve the blog =).

I've split this blog up into two parts. The first part is intended for anyone to be able to read and understand. The second part is advanced and goes into a ton of interesting ideas and concepts related to this algorithm.

Prerequisite: Polynomial quotient and remainder, see Wiki article and this Stackexchange example.

Task:

Given two polynomials $$$P$$$ and $$$Q$$$, an integer $$$n$$$ and a non-zero complex number $$$c$$$, where degree $$$P < n$$$ and degree $$$Q < n$$$. Your task is to calculate the polynomial $$$P(x) \, Q(x) \% (x^n - c)$$$ in $$$O(n \log n)$$$ time. You may assume that $$$n$$$ is a power of two.

Solution:

We can create a divide and conquer algorithm for $$$P(x) \, Q(x) \% (x^n - c)$$$ based on the difference of squares formula. Assuming $$$n$$$ is even, then $$$(x^n - c) = (x^{n/2} - \sqrt{c}) (x^{n/2} + \sqrt{c})$$$. The idea behind the algorithm is to calculate $$$P(x) \, Q(x) \% (x^{n/2} - \sqrt{c})$$$ and $$$P(x) \, Q(x) \% (x^{n/2} + \sqrt{c})$$$ using 2 recursive calls, and then use that result to calculate $$$P(x) \, Q(x) \% (x^n - c)$$$.

So how do we actually calculate $$$P(x) \, Q(x) \% (x^n - c)$$$ using $$$P(x) \, Q(x) \% (x^{n/2} - \sqrt{c})$$$ and $$$P(x) \, Q(x) \% (x^{n/2} + \sqrt{c})$$$?

Well, we can use the following formula:

$$$ \begin{aligned} A(x) \% (x^n - c) = &\frac{1}{2} \left(1 + \frac{x^{n/2}}{\sqrt{c}}\right) \left(A(x) \% (x^{n/2} - \sqrt{c})\right) \, + \\ &\frac{1}{2} \left(1 - \frac{x^{n/2}}{\sqrt{c}}\right) \left(A(x) \% (x^{n/2} + \sqrt{c})\right). \end{aligned} $$$
Proof of the formula

This formula is very useful. If we substitute $$$A(x)$$$ by $$$P(x) Q(x)$$$, then the formula tells us how to calculate $$$P(x) \, Q(x) \% (x^n - c)$$$ using $$$P(x) \, Q(x) \% (x^{n/2} - \sqrt{c})$$$ and $$$P(x) \, Q(x) \% (x^{n/2} + \sqrt{c})$$$ in linear time. With this we have the recipie for implementing a $$$O(n \log n)$$$ divide and conquer algorithm:

Input:

  • Integer $$$n$$$ (power of 2),
  • Non-zero complex number $$$c$$$,
  • Two polynomials $$$P(x) \% (x^n - c)$$$ and $$$Q(x) \% (x^n - c)$$$.

Output:

  • The polynomial $$$P(x) \, Q(x) \% (x^n - c)$$$.

Algorithm:

Step 1. (Base case) If $$$n = 1$$$, then return $$$P(0) \cdot Q(0)$$$. Otherwise:

Step 2. Starting from $$$P(x) \% (x^n - c)$$$ and $$$Q(x) \% (x^n - c)$$$, in $$$O(n)$$$ time calculate

$$$ \begin{align} & P(x) \% (x^{n/2} - \sqrt{c}), \\ & Q(x) \% (x^{n/2} - \sqrt{c}), \\ & P(x) \% (x^{n/2} + \sqrt{c}) \text{ and} \\ & Q(x) \% (x^{n/2} + \sqrt{c}). \end{align} $$$

Step 3. Make two recursive calls to calculate $$$P(x) \, Q(x) \% (x^{n/2} - \sqrt{c})$$$ and $$$P(x) \, Q(x) \% (x^{n/2} + \sqrt{c})$$$.

Step 4. Using the formula, calculate $$$P(x) \, Q(x) \% (x^n - c)$$$ in $$$O(n)$$$ time. Return the result.

Here is a Python implementation following this recipie:

Python solution to the task

One final thing that I want to mention before going into the advanced section is that this algorithm can also be used to do fast unmodded polynomial multiplication, i.e. given polynomials $$$P(x)$$$ and $$$Q(x)$$$ calculate $$$P(x) \, Q(x)$$$. The trick is simply to pick $$$n$$$ large enough such that $$$P(x) \, Q(x) = P(x) \, Q(x) \% (x^n - c)$$$, and then use the exact same algorithm as before. $$$c$$$ can be arbitrarily picked (any non-zero complex number works).

Python implementation for general Fast polynomial multiplication

If you want to try out implementing this algorithm yourself, then here is a very simple problem to test out your implementation on: SPOJ:POLYMUL.

(Advanced) Speeding up the algorithm

This section will be about tricks that can be used to speed up the algorithm. The first two tricks will speed up the algorithm by a factor of 2 each. The last trick is advanced, and it has the potential to both speed up the algorithm and also make it more numerically stable.

$n$ doesn't actually need to be a power of 2
Imaginary-cyclic convolution
Calculating fast_polymult_mod(P, Q, n, c) using using fast_polymult_mod(P, Q, n, 1) (reweight technique)

(Advanced) -is-this-fft-?

This algorithm is actually FFT in disguise. But it is also different compared to any other FFT algorithm that I've seen in the past (for example the Cooley–Tukey FFT algorithm).

Using this algorithm to calculate FFT
This algorithm is not the same algorithm as Cooley–Tukey
FFT implementation in Python based on this algorithm
FFT implementation in C++ based on this algorithm

(Advanced) Connection between this algorithm and NTT

Just like how there is FFT and NTT, there are two variants of this algorithm too. One using complex floating point numbers, and the other using modulo a prime (or more generally modulo an odd composite number).

Using modulo integers instead of complex numbers
Calculating fast_polymult_mod(P, Q, n, c) using fast_polymult_mod(P, Q, 2*n, 1)
This algorithm works to some degree even for bad NTT primes
NTT implementation in Python based on this algorithm
NTT implementation in C++ based on this algorithm
Blazingly fast NTT C++ implementation

(Advanced) Shorter implementations ("Codegolfed version")

It is possible to make really short but slightly less natural implementations of this algorithm. Originally I was thinking of using this shorter version in the blog, but in the end I didn't do it. So here they are. If you want a short implemention of this algorithm to use in practice, then I would recommend taking one of these implementations and porting it to C++.

Short Python implementation without any speedup tricks
Short Python implementation supporting odd and even $n$ (making it up to 2 times faster)
Short Python implementation supporting odd and even $n$ and imaginary cyclic convolution (making it up to 4 times faster)
Теги fft, convolution, polynomials, recursion

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en53 Английский pajenegod 2023-07-21 20:29:32 631
en52 Английский pajenegod 2023-07-21 19:29:42 0 (published)
en51 Английский pajenegod 2023-07-21 19:29:23 120 (saved to drafts)
en50 Английский pajenegod 2023-07-10 22:53:22 1131
en49 Английский pajenegod 2023-07-10 20:27:39 204
en48 Английский pajenegod 2023-07-10 20:18:13 0 (published)
en47 Английский pajenegod 2023-07-10 20:14:48 435
en46 Английский pajenegod 2023-07-10 19:55:32 288
en45 Английский pajenegod 2023-07-10 19:51:08 13
en44 Английский pajenegod 2023-07-10 19:48:33 7
en43 Английский pajenegod 2023-07-10 19:41:29 1385
en42 Английский pajenegod 2023-07-10 03:42:37 2274
en41 Английский pajenegod 2023-07-09 22:40:43 571
en40 Английский pajenegod 2023-07-09 22:00:53 39
en39 Английский pajenegod 2023-07-09 21:57:38 1100
en38 Английский pajenegod 2023-07-09 20:46:05 384
en37 Английский pajenegod 2023-07-09 20:03:42 39
en36 Английский pajenegod 2023-07-09 19:36:59 3745
en35 Английский pajenegod 2023-07-09 17:00:26 121
en34 Английский pajenegod 2023-07-09 16:53:59 2297
en33 Английский pajenegod 2023-07-08 22:04:53 23
en32 Английский pajenegod 2023-07-08 15:24:23 3
en31 Английский pajenegod 2023-07-08 15:22:29 24
en30 Английский pajenegod 2023-07-08 15:19:05 192
en29 Английский pajenegod 2023-07-08 15:03:43 438
en28 Английский pajenegod 2023-07-08 14:50:40 16
en27 Английский pajenegod 2023-07-08 14:48:51 278
en26 Английский pajenegod 2023-07-08 14:41:17 5
en25 Английский pajenegod 2023-07-08 14:40:38 7540
en24 Английский pajenegod 2023-07-08 13:38:20 1
en23 Английский pajenegod 2023-07-08 13:27:45 24
en22 Английский pajenegod 2023-07-08 13:26:39 844
en21 Английский pajenegod 2023-07-08 13:08:19 1427
en20 Английский pajenegod 2023-07-08 12:36:17 492
en19 Английский pajenegod 2023-07-08 04:13:26 5
en18 Английский pajenegod 2023-07-08 04:10:18 30
en17 Английский pajenegod 2023-07-08 03:59:49 999
en16 Английский pajenegod 2023-07-08 03:42:36 977
en15 Английский pajenegod 2023-07-08 03:27:23 4195
en14 Английский pajenegod 2023-07-07 20:07:54 1204
en13 Английский pajenegod 2023-07-07 18:13:08 2
en12 Английский pajenegod 2023-07-07 18:08:18 39
en11 Английский pajenegod 2023-07-07 13:51:58 89
en10 Английский pajenegod 2023-07-07 13:41:19 789
en9 Английский pajenegod 2023-07-07 06:29:01 11
en8 Английский pajenegod 2023-07-07 02:22:13 4
en7 Английский pajenegod 2023-07-07 02:02:33 122 ffao,-is-this-fft-,meooow,ToxicPie9,algmyr,aryanc403,kostia244,nor,Spheniscine,magnus.hegdahl,jeroenodb
en6 Английский pajenegod 2023-07-07 01:52:20 119
en5 Английский pajenegod 2023-07-07 01:46:09 448
en4 Английский pajenegod 2023-07-07 01:35:15 2157
en3 Английский pajenegod 2023-07-07 00:53:13 1957
en2 Английский pajenegod 2023-07-07 00:15:55 2037
en1 Английский pajenegod 2023-07-06 23:46:15 3121 Initial revision (saved to drafts)