(In fact, this article should be written as "debugging techniques with FFT discussion + code problem method", the language is not good to write articles on tricky qaq)
There are suggestions to welcome, there are omissions Welcome to add!
FFT (Fast Fourier transform)/ntt (number theory transformation) is a common and practical optimization of convolution operations
But the FFT/NTT process is not as visually reflective of the real-time changes in convolution results as violent operations (almost polynomial multiplication).
Therefore, the use of the FFT will be more or less difficult to debug.
If you are directly tracking variables when you debug a program, the results of each hand calculation will not only delay a lot of time, but the effect may not be ideal.
The direct visual inspection of errors may not be too high.
But also because of the characteristics of FFT, in fact there are some very convenient and practical debugging methods, can significantly improve the efficiency of debugging, reduce the difficulty of debugging.
First, the violent convolution determination algorithm
Write a procedure to solve the problem , but the process of solving the convolution uses violence to achieve
This may seem like a waste of time, but this step is also useful.
In doing a step, we can focus our thinking on modeling and model transformation . Modeling is also the process of completing the FFT template, and doing a good job of modeling at the outset will not only reduce the complexity and tension of thinking after the FFT template, but also make it easy to visually detect and correct the error of the model .
If the convolution algorithm is not available at this step, then it is no longer necessary to write the FFT multi-detours
If the convolution algorithm determined in this step is correct, then after the program error, we can boldly exclude the error of the algorithm, the focus of debugging on the FFT template.
This step of the program can also be used in the back to shoot
In the game, if it took a short time to write the FFT template, the results found that the convolution algorithm is not as simple as imagined, and even convolution can not solve the problem. Then the whole person must be very uncomfortable ...
In order to play a stable, it is best to write a convolution violence.
If you do not want to write the violent convolutional code, we can actually directly violent hand-count convolution, see if we could get the results of the sample
How is it convenient to hand-count?
Mechanically perform convolutional codes ...
Directly imitate the vertical multiplication, the two input array is shifted and added .
Second, the sub-fault fast positioning
In normal commissioning, time is sure to be wasted on the correct code test.
And if the program is long, we may also delay a lot of time in locating the wrong location
So for the efficient and fast commissioning process
reduce the scope of debugging when you are debugging
Detect errors in each of the full-featured subroutines directly in turn
Iii. Output Debug Validation template
We can even write the code every time we write the results of the output, further improve the efficiency of the writing problem
And the FFT feature is also convenient for us to write the FFT template when the segmented output debugging
0 Check the code that was written incorrectly, whether the key code is wrong to write the array size is sufficient
1 Check the number of digits N, modulus p=kn+1, number of CI, the inverse of the original root g,n (multiply n is 1) is correct
3 then check if n unit root W is correct
How to check it? Just check to see if W[1]^n is 1.
4 Check that binary reverse processing is correct
A good template, the above key parts are OK, but this does not guarantee that the FFT is completely error-free
The next step is simple and useful , and passing this validation indicates that the FFT is likely to be error-free.
5 The DfT of the polynomial multiplication array A will be performed first, then the inverse DFT of the result is obtained, and the observation is equal to a itself
And then you can do the same with the previously written violence program, to ensure the correctness of the program
Iv. Supplementary
The FFT uses the relatively advanced mathematics knowledge to the middle school student
If you can't understand the principle, rote memorization should be quite troublesome.
So what is the way to remember the template?
1 The main thing is to understand the principle, understanding the algorithm. can try to supplement learning some math knowledge and read some related papers
2 record the key points of the template
Roughly figuring out how several sections have several layers of loops.
More than 3 write templates, and summarize easy to write the wrong place
4 some interesting mnemonic memories.
A common method of doing a problem:
1 The answer to the small prime number modulo, and n the order of magnitude at 10^5, you can directly perform the FFT, and then add the results of EPS after the rounding operation
2 The answer to the large prime number modulo, if the large prime (about 10^9 order of magnitude) is two of the power of several times +1 (kn+1), then the NTT can be directly
If a large prime number is not a form of kn+1
Then you can select 3 suitable primes as kn+1, and then solve the answers separately.
Using the Chinese remainder theorem to obtain the answer of the large prime number of modulo primitive
Fft/ntt method and debugging skill (+ some ideas to improve the efficiency of code problem)