[Numpy-discussion] Funny business with 'is' operator?

Robert Kern robert.kern at gmail.com
Fri Sep 6 13:27:51 EDT 2013

On Fri, Sep 6, 2013 at 5:58 PM, James Bergstra <bergstrj at iro.umontreal.ca>
> I'm stumped. I can't figure out how to extract from e.g.
> view = A[:, 3]
> that the view starts at element 3 of A. I was planning to make a
may_share_memory implementation based on the idea of swapping in a buffer
of 0s, and using the shapes, strides, itemsize etc. to increment just the
parts of the 0s buffer touched by the two ndarrays. If there  are any 2s in
the incremented buffer, it's because the two views overlapped. It's not the
best algorithm for comparing tiny views of huge arrays, I was wondering if
in my case it would have been quicker than the built-in method (I don't
know how it works).

It certainly won't be faster. may_share_memory() is very simplistic. It
just checks if the outermost bounds of the each array overlap. Thus, it can
give false positives for arrays that are interleaved but do not actually
intersect. This is why it is named may_share_memory() instead of

> I actually have another data structure around to pull out that shape and
stride info, but it's a shame to use it, because then I can't use the
algorithm to compare ndarrays in general (or at least ones that have been
created by typical construction methods and slicing).

All of the information you need is stored in the __array_interface__
attribute of an ndarray.

The right way to solve this is to solve a bounded, linear Diophantine
equation. That's where you should be looking if you want to crack this

Robert Kern
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20130906/a6142c0e/attachment.html>

More information about the NumPy-Discussion mailing list