Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

python - how do I check that two slices of numpy arrays are the same (or overlapping)?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...