I have two arrays, a and b both of length N. I want to do, for example j = 4
import numpy as np
N = 1000
c = np.zeros(N)
j = 40
c[j] = np.sum([a[i] * b[(j-i)%(N-1)] for i in range(N)])
But if directly using convolution theorem, I would get
c[j] = np.sum([a[i] * b[j-i] for i in range(N)])
which is the same as
c = np.fft.ifft(np.fft(a) * np.fft(b))
Is there a clever way to pad zeros such that one index misalignment can be treated using np.fft, which is very fast.
The solution is to first truncate the arrays so that the rolling back in
numpy.arrays, e.g., index-1in the truncated arrays is the second last in the original one. For example,And finally add the tail element manually.
The total complexity for this is still
O(Nlog(N))plus someO(N)operations to complement tails.