[Numpy-discussion] Generalized inner?

Jaime Fernández del Río jaime.frio at gmail.com
Sun Mar 24 12:50:59 EDT 2013

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)),
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


( 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.python.org/pipermail/numpy-discussion/attachments/20130324/f9bca852/attachment.html>

More information about the NumPy-Discussion mailing list