[AstroPy] Convolution of NumPy arrays of arbitrary dimension

Foad Sojoodi Farimani f.s.farimani at gmail.com
Mon Aug 13 04:31:53 EDT 2018


Dear Jake,

Thanks alot for your reply.
I just tried the scipy.ndimage.convolve and it doesn't seem to be what I'm
looking for. I use the term convolution as used in the context of the Cauchy
product <https://en.wikipedia.org/wiki/Cauchy_product> of multivariate
power series (polynomials). for example

    from scipy.ndimage import convolve
    import numpy as np

    D1=np.array([4,5])
    D2=np.array([2,3])
    A=np.random.randint(10,size=D1)
    B=np.random.randint(10,size=D2)

    print(A)
    print(B)

results:

[[9 5 2 0 1]
 [1 9 8 4 4]
 [7 9 2 9 6]
 [5 0 8 8 2]]
[[4 6 9]
 [9 0 8]]

I expect the first element of the conve(A,B) to be 36 but
the print(convolve(A, B)) results in:

[[168 185 185 137  85]
 [230 205 237 196 209]
 [212 151 233 198 218]
 [115 189 152 210 174]]

Also the conve(A,B) should be of the shape A.shape+B.shape-1
while scipy.ndimage.convolve result is of the A.shape. the conv I'm looking
for is commutative as the Cauchy product of two polynomial is.

Best,
Foad



On Sat, Aug 11, 2018 at 10:11 PM Jacob Vanderplas <jakevdp at cs.washington.edu>
wrote:

> 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
>>
>>
> _______________________________________________
> 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/20180813/94cd8a1c/attachment.html>


More information about the AstroPy mailing list