[Numpy-discussion] Generalized inner?

Nadav Horesh nadavh@visionsense....
Sun Mar 24 13:32:03 CDT 2013


This is what APL's . operator does, and  I found it useful from time to time (but I was much younger then).

  Nadav

Jaime Fernández del Río <jaime.frio@gmail.com> wrote:



The other day I found myself finding trailing edges in binary images doing something like this:

arr = np.random.randint(2, size=1000).astype(np.int8)
pattern = np.array([1, 1, 1, 1, 0, 0])
arr_match = 2*arr - 1
pat_match = 2*pattern - 1
from numpy.lib.stride_tricks import as_strided
arr_win = as_strided(arr_match, shape=arr.shape[:-1] + (arr.shape[-1]-len(pattern)+1, len(pattern)), strides=arr.strides+arr.strides[-1:])
matches = np.einsum('...i, i', arr_win, pat_match) == len(pattern)

While this works fine, this led me to thinking that all this functions (inner, dot, einsum, tensordot...) could be generalized to any other ufuncs apart from a pointwise np.multiply followed by an np.add reduction.

It would be great if there was a np.gen_inner that allowed something like:

np.gen_inner(arr_win, pattern, pointwise=np.equal, reduce=np.logical_and)

I would like to think that such a generalization would be useful in other settings (although I can't think of any right now), and that it could find it's place in numpy, rather than in scipy.ndimage or the like. Does this make any sense? Is there any already existing way of doing this that I'm overlooking?

Jaime

--
(\__/)
( O.o)
( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes de dominación mundial.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20130324/82ac2563/attachment.html 


More information about the NumPy-Discussion mailing list