How to check that two segments of numpy arrays are the same (or overlap)?

I would like to check if two ndarrays are overlapping representations of the same base ndarray.

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

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

Buffer comparisons seemed to work in one simple case - can someone tell me if it works at all?

Unfortunately, this does not work for overlapping fragments, and I did not understand how to extract from the buffer exactly what its offset is in the base data - maybe someone can help me?

Also, say a and b are slices x , and c is slices b . Since the underlying data is the same, I would also like to detect overlaps between c and a . It would seem that I should get away from comparing only the buffer and the form ... if someone could tell me exactly how I would appreciate it.

+7
python numpy
May 25 '12 at 2:30
source share
2 answers

numpy.may_share_memory() is the best heuristic we have at the moment. It is conservatively heuristic; he may give you false positives, but he will not give you false negatives. I think there may be ways to improve heuristics in order to be 100% correct. If they expand, they will be collapsed into this function, so that the best way forward.

+9
May 25 '12 at 10:17
source share
— -

Perhaps you can compare where the indices live in memory using the ctypes property of arrays. This may take some work, so you may need to step back and see if there is another way to solve your problem.

+1
May 25 '12 at 6:48
source share



All Articles