-2
a = [1,2,3]
b = [2,4,6]

c = [1*2, 1*4 + 2*2, 1*6 + 2*4 + 3*2]

I have done using dot product by creating Toeplitz matrix but I am looking for a much faster version since I am dealing with a huge dataset. Using loop will also slow down computation.

user2357112
  • 260,549
  • 28
  • 431
  • 505
Pradeep Tummala
  • 308
  • 1
  • 5
  • 15

1 Answers1

4

This output is the first half of a convolution of a and b:

numpy.convolve(a, b)[:len(a)]

If your arrays are big enough, you may be able to save time with an FFT-based convolution:

scipy.signal.fftconvolve(a, b)[:len(a)]

An FFT convolution has runtime O(n log n) where n is the length of the input arrays, in contrast to the O(n^2) runtime of a straightforward nested-loop convolution. However, it has a worse constant factor, and it requires floating-point arithmetic, so results may have a small amount of numerical error.

On a sufficiently recent version of SciPy (at least 0.19.0, released March 09, 2017), you can use scipy.signal.convolve to automatically pick between a direct or FFT convolution based on an estimate of which would be faster. Prior to 0.19.0, scipy.signal.convolve would always use a direct computation.

user2357112
  • 260,549
  • 28
  • 431
  • 505