3

Let's say I have separate two-dimensional arrays with different numbers of rows:

Array One:

1 2 3
4 5 6
7 8 9

Array Two:

10 11 12
13 14 15

I'd like to perform a convolution of these two arrays together along the zero axis, with the output size being the same as array 1, similar to what scipy's convolve method will output when using mode='same'.

So column 1 of both arrays are convoluted together, etc, column 2 of both arrays are convoluted together, etc. I'd like to do this in a way which is computationally viable for larger arrays (upwards of 100,000 rows), so I'd like to avoid the use of for loops, if at all possible. Do numpy/scipy or any other libraries have any methods of performing this?

Desired Output should look like:

10 22 36
53 83 117
122 158 198

with the same shape as the first input array. Each column is the output obtained from the convolution of the corresponding columns in arrays 1 and 2.

Gmattei
  • 83
  • 1
  • 8
  • Can you post your desired output for this small sample? – user3483203 Aug 13 '18 at 15:39
  • Sure thing - I updated the opening post to include what the output should look like. – Gmattei Aug 13 '18 at 16:05
  • If you have 100,000 rows the overhead for the interpreted loop is almost zero. A interpreted loop would only be a problem if the convolutions itself are small (eg. 20rows), but 100,000 colums. – max9111 Aug 14 '18 at 12:05
  • I don't think I have anything that large, but the total number of columns for convolution can range anywhere from 4 to upwards of 30,000 depending on the situation. – Gmattei Aug 14 '18 at 12:52

1 Answers1

0

The implementation of np.convolve simply calls np.core.multiarray.correlate (since a convolution is the same as computing the correlation after flipping one vector). It seems that many people desire a Numpy method which computes the correlation along a certain axis, but it doesn't exist (yet):

Apply numpy's correlation to specific axis (autocorrelation)

Compact way of simulating 'axes' parameter in scipy.signal.fftconvolve?

You could, however, use a loop and see if scipy.signal.fftconvolve provides enough speed-up for your problem.

cheersmate
  • 2,385
  • 4
  • 19
  • 32