from-python-to-numpy icon indicating copy to clipboard operation
from-python-to-numpy copied to clipboard

memory layout: offset explanation has a small flaw in code

Open LesyaHoy opened this issue 6 months ago • 1 comments

Z = np.arange(9).reshape(3,3).astype(np.int16) index = 1,1 print(Z[index].tobytes()) b'\x04\x00' offset = 0 for i in range(Z.ndim): ... offset + = Z.strides[i]*index[i] print(Z.tobytes()[offset_start:offset_end] b'\x04\x00'

Should be

Z = np.arange(9).reshape(3,3).astype(np.int16) index = 1,1 print(Z[index].tobytes()) b'\x04\x00' offset_start= 0 for i in range(Z.ndim): offset_start+ = Z.strides[i]*index[i] offset_end = offset_start + Z.itemsize print(Z.tobytes()[offset_start:offset_end] b'\x04\x00'

LesyaHoy avatar Jun 22 '25 10:06 LesyaHoy

Thanks. Can you give a bit more context?

rougier avatar Jun 23 '25 05:06 rougier