numpy 关于 array index的描述

An instance of class ndarray consists of a contiguous one-dimensional segment of computer memory (owned by the array, or by some other object), combined with an indexing scheme that maps N integers into the location of an item in the block. The ranges in which the indices can vary is specified by the shape of the array. How many bytes each item takes and how the bytes are interpreted is defined by the data-type object associated with the array.

A segment of memory is inherently 1-dimensional, and there are many different schemes for arranging the items of an N-dimensional array in a 1-dimensional block. NumPy is flexible, and ndarray objects can accommodate any strided indexing scheme. In a strided scheme, the N-dimensional index ( n 0 , n 1 , . . . , n N 1 ) corresponds to the offset (in bytes):

n o f f s e t = k = 0 N 1 s k n k

from the beginning of the memory block associated with the array. Here, s k are integers which specify the strides of the array. The column-major order (used, for example, in the Fortran language and in Matlab) and row-major order (used in C) schemes are just specific kinds of strided scheme, and correspond to memory that can be addressed by the strides:

s k c o l u m n = i t e m s i z e j = 0 k 1 d j , s k r o w = i t e m s i z e j = k + 1 N 1 d j .

w h e r e d j = s e l f . s h a p e [ j ] .

Both the C and Fortran orders are contiguous, i.e., single-segment, memory layouts, in which every part of the memory block can be accessed by some combination of the indices.

While a C-style and Fortran-style contiguous array, which has the corresponding flags set, can be addressed with the above strides, the actual strides may be different. This can happen in two cases:

If self.shape[k] == 1 then for any legal index index[k] == 0. This means that in the formula for the offset n k = 0 and thus s k n k = 0 and the value of s k = s e l f . s t r i d e s [ k ] is arbitrary.
If an array has no elements (self.size == 0) there is no legal index and the strides are never used. Any array with no elements may be considered C-style and Fortran-style contiguous.
Point 1. means that self and self.squeeze() always have the same contiguity and aligned flags value. This also means that even a high dimensional array could be C-style and Fortran-style contiguous at the same time.

An array is considered aligned if the memory offsets for all elements and the base offset itself is a multiple of self.itemsize.

猜你喜欢

转载自blog.csdn.net/u014134138/article/details/81458828