Skip to content
Advertisement

Calculate the cumulative sum of multiplying each element of one array by all the elements of a second array

I need to efficiently calculate the running sum of multiplying the elements of one array by all the elements of a second array. It is probably easiest to explain what I am trying to do with code:

JavaScript

My ‘a’ array will typically be big so I would like this to run as fast as possible, but right now it is too slow (~5s on my computer). Is there a more efficient way of doing this in Python?

Advertisement

Answer

In general multiplying two sliding windows is called a convolution, implemented in numpy. Your definition is subtly different at the end, however this can be fixed.

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement