Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For the particular case of sums, you could use <a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.filters.convolve.html" rel="nofollow">ndimage.convolve</a>:</p> <pre><code>In [42]: import numpy as np In [43]: a = np.zeros((6,6), dtype=np.int) a[1:5, 1:5] = 1; a[3,3] = 0; a[2,2] = 2 In [48]: s = ndimage.generate_binary_structure(2,2) # Binary structure In [49]: ndimage.convolve(a,s) Out[49]: array([[1, 2, 3, 3, 2, 1], [2, 5, 7, 7, 4, 2], [3, 7, 9, 9, 5, 3], [3, 7, 9, 9, 5, 3], [2, 4, 5, 5, 3, 2], [1, 2, 3, 3, 2, 1]]) </code></pre> <p>For the particular case of products, you could use the fact that <code>log(a*b) = log(a)+log(b)</code> to convert the problem back to one involving sums. For example, if we wanted to "product-convolve" <code>b</code>:</p> <pre><code>b = a[1:-1, 1:-1] print(b) # [[1 1 1 1] # [1 2 1 1] # [1 1 0 1] # [1 1 1 1]] </code></pre> <p>we could compute:</p> <pre><code>print(np.exp(ndimage.convolve(np.log(b), s, mode = 'constant'))) # [[ 2. 2. 2. 1.] # [ 2. 0. 0. 0.] # [ 2. 0. 0. 0.] # [ 1. 0. 0. 0.]] </code></pre> <p>The situation becomes more complicated if <code>b</code> includes negative values:</p> <pre><code>b[0,1] = -1 print(b) # [[ 1 -1 1 1] # [ 1 2 1 1] # [ 1 1 0 1] # [ 1 1 1 1]] </code></pre> <p>but not impossible:</p> <pre><code>logb = np.log(b.astype('complex')) real, imag = logb.real, logb.imag print(np.real_if_close( np.exp( sum(j * ndimage.convolve(x, s, mode = 'constant') for x,j in zip((real, imag),(1,1j)))))) # [[-2. -2. -2. 1.] # [-2. -0. -0. 0.] # [ 2. 0. 0. 0.] # [ 1. 0. 0. 0.]] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload