10

I would like to check if two ndarrays are overlapping views of the same underlying ndarray.

To check that two slices are exactly the same, I can do something like:

a.base is b.base and a.shape == b.shape and a.data == b.data

The comparison of buffers seemed to work in one simple case -- can anyone tell me if it works in general?

Unfortunately, this wont work for overlapping slices, and I haven't figured out how to extract from the buffer exactly what its offset is in the underlying data -- perhaps someone can help me with this?

Also, say a and b are slices of x, and c is a slice of b. As the underlying data is the same, I would also like to detect overlaps between c and a. It would seem that I should be able to get away with comparing just buffer and shape... if anyone could tell me exactly how, I would be grateful.

shaunc
  • 5,317
  • 4
  • 43
  • 58

2 Answers2

11

numpy.may_share_memory() is the best heuristic that we have at the moment. It is conservatively heuristic; it may give you false positives, but it will not give you false negatives. I think there might be ways to improve the heuristic to be 100% correct. If they pan out, they will be folded into that function, so that's the best way forward.

CharlesB
  • 86,532
  • 28
  • 194
  • 218
Robert Kern
  • 13,118
  • 3
  • 35
  • 32
  • What sort of cases might I expect to fail? If its just staggered slices with non-unity step that generate false positive, I can live with that.... – shaunc May 29 '12 at 04:25
  • 2
    `x[0::2]` / `x[1::2]`. `x[:, 0:5]`, `x[:, 5:10]`. `x = np.dstack(*args); np.may_share_memory(x[0], x[1])`. – Robert Kern Jun 06 '12 at 17:01
1

It might be possible to compare where the indices live in memory using the ctypes property of the arrays. It might take some work, so you might want to step back and see if there is a different way of solving your problem.

Ken
  • 1,778
  • 11
  • 10