# Welcome to Part 2

Firstly, I am assuming that you have been through the Part 1 of this blog.

Okay so in hindsight I now see the drawbacks there were in my explanation of the roots of unity and how the divide and conquer works in FFT.

**So in this blog I would be aiming to give you a visual intuition of what really FFT exploits over the trivial classical DFT. Also I would be covering up NTT and sharing a nice trick that I got to know about while learning NTT.**

## Visual Intuition of FFT

The part of converting the polynomial from coefficient form to point value form in instead of the trivial *N*^{2} would be elaborated upon in this section as I find the dry mathematical explanation to be rigorous but not intuitive. In the notation below I have referred to *n* complex *n*^{th} roots of unity as powers of *w*_{n}^{1} = *e*^{2π·i / n}

Assume that you have a 8 terms (7 degree) polynomial you need to convert from coefficient form to point value form. So firstly you choose the powers of the 8^{th} complex root of unity as the *x*_{i}'s that you will be plugging in the polynomial. Initially this might seem random but hopefully by the end of this topic it would be clear to you.

Okay so now we can reduce our polynomial *A*(*x*) as *A*_{even}(*x*^{2}) + *x*·*A*_{odd}(*x*^{2}) where *A*_{even}(*y*) is a 4 terms (3 degree) polynomial in *y* and *A*_{odd}(*y*) is also a 4 terms (3 degree) polynomial in *y*.

Now solving the original problem of converting *A*(*x*) from coefficient form to point value form for each of the power of the 8^{th} root of unity is exactly equivalent to solving this new problem of converting *A*_{even}(*x*^{2}) and *A*_{odd}(*x*^{2}) for powers the 8^{th} root of unity.

So really this doesn't help us much.* But the trick *is that when we square (because *x*^{2}) these powers of the 8^{th} root of unity, they come out to be same pairwise, i.e. the 0^{th} and 4^{th} powers of the 8^{th} root of unity when squared give the same result, similarly, the 1^{st} and 5^{th} powers of 8^{th} root of unity when squared give the same result and so on.

Also note that these results actually turn out to be the powers of the 4^{th} root of unity.

Okay, so I gave the dry mathematical proof for this in the previous part, but that time I did not have a visual understanding of it. Well now I do so refer to the animations and diagrams below to get the idea but first read through the below subtopic of Rotation in Imaginary Plane.

** Rotation in Imaginary Plane **

Okay so as a rule of thumb remember that if you have a complex number lets say *Z* and you multiply it with *e*^{iθ} then it basically rotates it by θ angle in anti clockwise direction with respect to the origin.

So lets say you have 3^{rd} power of 8^{th} root of unity, i.e. *w*_{n}^{3} = *e*^{i·2π·(3 / 8)} and you wish to exponentiate it. Then currently the θ is .

So if you square it you are actually multiplying the θ by 2 so you are basically rotating it by θ degrees in anti clockwise direction with respect to the origin, i.e. it will now be make an angle of with the positive x axis in the anti clockwise direction.

Raising it by power *x* is (*w*_{n}^{3})^{x} = *e*^{i·2π·(3 / 8)·x} = *e*^{i·2π·(3 / 8)}·*e*^{i·2π·(3 / 8)·(x - 1)} which is equivalent to rotating *e*^{i·2π·(3 / 8)} by in anti clockwise direction with respect to positive x axis.

So given this understanding we can now easily exponentiate roots of unity. So here is the key, when you mark the 8th roots of unity on a graph it is regular octagon whose vertices are making angle degree with respect to positive x axis.

Now when you square each of these, the angles double as shown above, so they become 0 * 2, 45 * 2, 90 * 2, 135 * 2, 180 * 2, 225 * 2, 270 * 2, 315 * 2 = 0, 90, 180, 270, 360, 450, 540, 630 = which are basically the points of the 4^{th} root of unity. Below is a beautiful animation showing the same (You would need to open the link below and play the animation from the top left hand side)

Now you can clearly grasp the complexity of *T*(*n*) = *T*(*n* / 2) (For *A*_{even}) + *T*(*n* / 2) (For *A*_{odd}) + *O*(2·*n*)(For combining the results as *A*_{even}(*x*^{2}) + *x*·*A*_{odd}(*x*^{2})). So

## NTT (Number Theoretic Transform)

The objective of NTT is to multiply 2 polynomials such that the coefficient of the resultant polynomials are calculated under a particular modulo. The benefit of NTT is that there are no precision errors as all the calculations are done in integers. A major drawback of NTT is that generally we are only able to do NTT with a prime modulo of the form 2^{k}·*c* + 1, where *k* and *c* are arbitrary constants. So for doing it for a random mod we would need to use CRT (Chinese Remainder Theorem).

Firstly, *n*^{th} roots of unity under a primitive field, i.e. are defined as , where *P* is only considered as prime for simplicity.

Here we are assuming that *P* = 2^{k}·*c* + 1, where *c*, *k* are positive integers and *P* is prime.

Note All the calculation done below are done under modulo *P* including the operations in the exponents.

Example *r*^{P + 5} = *r*^{5}. The modulo has been intentionally skipped sometimes as it makes the notation way too ugly.

So we first find a *r* such that goes through all the numbers from 1 to *P* - 1 when *x* goes from 1 to *P* - 1. Note that is already a fact using Fermat's Little Theorem which states that if *gcd*(*a*, *P*) = 1

After finding one such *r*, the (2^{k})^{th} root of unity under the modulo field of *P* will be *r*^{c}.

The powers will be as

Notice that as *w*_{n}^{n} = 1 for complex roots similarly here

Lemma 1 Here when 1 ≤ *x* < 2^{k}. this is because *r* is itself defined as the (*P* - 1)^{th} root of unity, i.e. any positive power of *r* less than *P* - 1 will not be equal to , and as *P* - 1 = 2^{k}·*c*, therefore any power of *r* where *c* is multiplied by anything less than 2^{k} would not give .

Lemma 2 Another key observation is that (*r*^{c})^{α} = (*r*^{c})^{β} where α ≠ β and both lie between [0, 2^{k}) can never be true, this observation can be proven by contradiction, assuming that *r*^{c·α} = *r*^{c·β} under then which can be generalised to (*r*^{c})^{α + γ} = (*r*^{c})^{β + γ} where γ is an integer. But we already know for a fact that , so lets put γ = 2^{k} - α, so now where β + γ ≠ 2^{k} because α ≠ β. This is contradictory to the result mentioned in Lemma 1, hence proved.

So, *r*^{c} is now the (2^{k})^{th} root of unity under the modulo field of *P*. Now the only difference between NTT and FFT will be that the *n*^{th} root of unity changes from *w* to *r*^{c}, where *r* is found by hit and trial method.

## Trick to handle more prime modulos

Instead of breaking the sub-problems as powers of 2 (into two halves), we can generalise it to powers of any number.

Then we can basically solve NTT for a prime of the form *B*^{k}·*c* + 1, obviously if *B* = 2, then it gives the best time complexity and it would be slower when we opt for different and bigger B's. But it would still work which is the key.

Okay so how ?

Let's take an example for 3

So we have a polynomial of lets say 3^{3} = 27 terms, i.e. it is a 26 degree polynomial.

Now *A*(*x*) = *a*_{0} + *a*_{1}·*x*^{1} + *a*_{2}·*x*^{2} + ... + *a*_{26}·*x*^{26}

*A*(*x*) = (*a*_{0} + *a*_{3}·*x*^{3} + ... + *a*_{24}·*x*^{24}) + (*a*_{1}·*x* + *a*_{4}·*x*^{4} + ... + *a*_{25}·*x*^{25}) + (*a*_{2}·*x*^{2} + *a*_{5}·*x*^{5} + ... + *a*_{26}·*x*^{26}) = *A*_{0mod3}(*x*^{3}) + *x*·*A*_{1mod3}(*x*^{3}) + *x*^{2}·*A*_{2mod3}(*x*^{3})

Now the neat trick is that the complex roots not only coincide in one another when squared but also when exponentiated to any other power, in this case when cubed.

Below is an animation showing a regular 9-gon denoting the 9^{th} roots of unity and when cubed, forming an equilateral triangle denoting the 3^{rd} roots of unity.

So basically

Here it is *O*(2·3·*N*) as on each step of conquer for the N terms we need to do 3 multiplications i.e. *A*_{0mod3}(*x*^{3})·1, *A*_{1mod3}(*x*^{3})·*x*, *A*_{2mod3}(*x*^{3})·*x*^{2} and 3 additions, i.e. adding all the terms together.

For a general *B* the time complexity will be

* Note The underlying idea behind using roots of unity in any kind of transform is that the roots of unity under any field (complex numbers or under modulo P) form in themselves a cyclic group over multiplication, i.e if any 2 elements of these roots of unity are operated using multiplication then it is guaranteed that their product will be element of this set as well. Also this set has an identity element. So the property of these roots of unity of coinciding into one another when exponentiated is satisfied because they are forming a cyclic group over multiplication and also because the size of this group is of the form B^{k}*

## Problems for Practice

To be added.

Thanks to tanujkhattar and polingy for assisting me in understanding the concepts mentioned in this blog and to NibNalin for proof reading the blog.

References and resources used Introduction to Algorithms (aka CLRS), Better Explained, Apfloat blog on NTT, Desmos Graphing Calculator

Any feedback would be appreciated. Please notify me about any typos or errors in the comments or in PM.

Auto comment: topic has been updated by sidhant (previous revision, new revision, compare).Auto comment: topic has been updated by sidhant (previous revision, new revision, compare).while inverting the product form to co-efficient form , what will be the value of the roots associated?

Very nice contributions! Anxiously waiting for the "Problems for Practice" section :)

Still waiting for the "Problems for Practice" section :(

Thanks.....great blog ....