# Python | Number Theoretic Transformation

• Last Updated : 09 Sep, 2019

Number Theoretic Transform is a Fast Fourier transform theorem generalization. It is obtained by the replacement of e^(-2piik/N) with an nth primitive unity root. So this means, instead of the complex numbers C, use transform over the quotient ring Z/pZ. The theory is based on and uses the concepts of finite fields and number theory.

Number Theoretic Transform modulus need to be prime necessarily. But if it is prime, it makes things simpler. One can perform the NTT with a composite modulus. For modulus :

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

• nth root of unity existence
• multiplicative inverse of n

A number-theoretic transform is basically a Fourier transform. Also, suppose that a Normal Discrete Fourier Transform is given and it can be done in matrix form by multiplying the data with a Fourier Matrix. Let us suppose N = 4. Then, the matrix can be –

```[ 1   1    1    1  ]
[ 1   w   w^2  w^3 ]
[ 1  w^2  w^4  w^6 ]
[ 1  w^3  w^6  w^9 ]
```

## sympy.discrete.transforms.ntt( ) :

It can Number Theoretic Transform (NTT) of the sequence.
It specializes over the Discrete Fourier Transform (DFT) quotient ring with Z/pZ for the prime na complexstead of acomplex numbers.

Automatically the sequence is padded with zero to the right because the radix-2 FFT requires the sample point number as a power of 2.

```
Parameters :
-> seq       : [iterable] sequence on which DFT is to be applied.
-> prime no. : [Integer] prime modulus for NTT to perform on.

Returns :
Number Theoretic Transform
```

Example 1 :

 `# import sympy ``from` `sympy ``import` `ntt`` ` `# sequence ``seq ``=` `[``15``, ``21``, ``13``, ``44``]`` ` `prime_no ``=` `3` `*` `2``*``*``8` `+` `1`` ` `# ntt``transform ``=` `ntt(seq, prime_no)``print` `(``"NTT : "``, transform)`

Output :

`NTT :  [93, 114, 732, 659]`

Example 2 :

 `# import sympy ``from` `sympy ``import` `ntt`` ` `# sequence ``seq ``=` `[``153``, ``321``, ``133``, ``44``, ]`` ` `# Prime modulus of the form (m * 2**k + 1)``prime_no ``=` `3` `*` `2``*``*``8` `+` `1`` ` `# ntt``transform ``=` `ntt(seq, prime_no)``print` `(``"NTT : "``, transform)`

Output :

```NTT :  [651, 276, 690, 533]
```

My Personal Notes arrow_drop_up
{"ft_lang_label":"__label__en","ft_lang_prob":0.77333146,"math_prob":0.98084307,"size":2258,"snap":"2021-43-2021-49","text_gpt3_token_len":631,"char_repetition_ratio":0.1313221,"word_repetition_ratio":0.14285715,"special_character_ratio":0.2887511,"punctuation_ratio":0.13824885,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9976879,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-26T00:10:21Z\",\"WARC-Record-ID\":\"<urn:uuid:8de6b9a3-6226-438d-bd7c-00aa6008bbe4>\",\"Content-Length\":\"114975\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:1afcf032-cb64-40d4-8675-2ad20b20e1b9>\",\"WARC-Concurrent-To\":\"<urn:uuid:06c28048-215d-407c-98df-f916c6d62c1e>\",\"WARC-IP-Address\":\"23.12.145.61\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/python-number-theoretic-transformation/?ref=rp\",\"WARC-Payload-Digest\":\"sha1:N2WETLQ25SLGYNLQGYEEFYDBSMLD4L37\",\"WARC-Block-Digest\":\"sha1:AOOIRSZDYTLUV5YWPPEGBLZH5D3B6IXB\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323587770.37_warc_CC-MAIN-20211025220214-20211026010214-00047.warc.gz\"}"}