# [AstroPy] Convolution of NumPy arrays of arbitrary dimension

Jacob Vanderplas jakevdp at cs.washington.edu
Sat Aug 11 16:11:46 EDT 2018

```Hi Foad

> Thanks a lot for the reply. I have indeed seen scipy.ndimage.convolve and
> have mentioned it in the OP
> <https://stackoverflow.com/questions/51794274/convolution-of-numpy-arrays-of-arbitrary-dimension-for-cauchy-product-of-multiva>.
> but some questions:
>

I saw that, but you seemed to imply there that it was limited to 1 and 2
dimensions, when it works for arbitrary dimensions.

>
>
>    1. although there is nothing about the dimension limit of the ndarrays
>    in its official page
>    <https://docs.scipy.org/doc/scipy-0.16.1/reference/generated/scipy.ndimage.filters.convolve.html>,
>    but I haven't seen any examples showing it works with higher dimensions.
>
> I showed an example of a simple four-dimensional convolution in my
original response.

>
>    1. what is the difference between astropy.convolve_fft and
>    scipy.signal.convolve? It seems to me they are for function analysis
>    <https://en.wikipedia.org/wiki/Convolution> not array arithmetics.
>
> By design, scipy.signal.convolve and scipy.signal.fftconvolve produce the
same results. The difference is that the latter uses an FFT to compute the
results, which is generally faster than direct convolution for large
inputs. astropy.convolve_fft is similar to scipy.signal.fftconvolve, with a
few differences listed in the documentation
<http://docs.astropy.org/en/stable/api/astropy.convolution.convolve_fft.html>
.

>
>    1. As I can see the term convolution, even for array arithmetics is
>    used for different purposes. For example there is
>    also scipy.ndimage.filters.convolve which apparently calculates
>    different things. My final goal is to do finite multivariate formal power
>    series multiplication (Cauchy product). I think I have figured the formula
>    out here
>    <https://math.stackexchange.com/questions/2877478/cauchy-product-of-multivariate-formal-power-series>,
>    but I'm not sure if it is correct completely. questions are:
>       1. is my formula correct?
>          - if not what is the correct one?
>          2. if yes has this been done before?
>       - if yes where? does any of the above functions do the job?
>          3. regardless of the correctness of the formula and existence of
>       other implementations, is my implementation correct so far?
>       4. how to finish the final step to populate the ndarray using the
>       conv function?
>
> At a glance, I'm not certain if your formula or implementation is correct,
and I don't have the time to dig-in at the moment.

Perhaps someone else can help with that part.

Best of luck,
Jake

> Thanks a gain and looking forwards to hearing back.
>
> Best,
> Foad
>
> On Sat, Aug 11, 2018 at 1:56 AM Adam Ginsburg <adam.g.ginsburg at gmail.com>
> wrote:
>
>>
>>
>> On Fri, Aug 10, 2018 at 5:51 PM, Jacob Vanderplas <
>> jakevdp at cs.washington.edu> wrote:
>>
>>> Hi Foad,
>>> I'm sorry if I'm misunderstanding something, but does
>>> scipy.ndimage.convolve not address your use case? It implements
>>> N-dimensional convolution:
>>>
>>> from scipy.ndimage import convolveimport numpy as np
>>>
>>> x = np.random.rand(10, 10, 10, 10)
>>> w = np.ones((3, 3, 3, 3))
>>>
>>> result = convolve(x, w)
>>>
>>> For completeness, astropy's convolve_fft supports this same operation
>> since it's doing an nd fft under the hood, but the direct convolution
>> (astropy.convolution.convolve) does not, since we had to hard-code the
>> direct convolution operations in each dimension and so far there has been
>> no demand for an n-dimensional convolution with n>3.
>> _______________________________________________
>> AstroPy mailing list
>> AstroPy at python.org
>> https://mail.python.org/mailman/listinfo/astropy
>>
>
> _______________________________________________
> AstroPy mailing list
> AstroPy at python.org
> https://mail.python.org/mailman/listinfo/astropy
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/astropy/attachments/20180811/5ff68b55/attachment.html>
```

More information about the AstroPy mailing list