[Numpy-discussion] Learning "strides"

Travis Oliphant oliphant.travis at ieee.org
Thu Feb 2 19:03:08 EST 2006

Sasha wrote:

>On 2/2/06, Travis Oliphant <oliphant.travis at ieee.org> wrote:
>Please let me know if you plan to change PyArray_CheckStrides so that
>we don't duplicate effort.
I won't do anything with it in the near future. 

>Can you suggest a use-case?  I cannot think of anything that cannot be
>handled using a record-array view of the buffer.
Here's the issue.  With records it is quite easy to generate  strides 
that are not integer multiples of the data.  For example, a record 
[('field1', 'f8),('field2', 'i2')] data-type would have floating point 
data separated by 10 bytes.  When you get a view of field1 (but getting 
that attribute) you would get such a "misaligned" data.

Look at the following:

temp = array([(1.8,2),(1.7,3)],dtype='f8,i2')

How would you represent that in the element-based strides report?

So, fractional strides are actually fundamental to the ability to have 
record arrays.

>The problem is that many people (including myself) think that they
>know what strides are when they come to numpy because they used
>strides in other libraries (e.g. BLAS).
>Most people expect element-based strides.  A footnote in your book
>"Our definition of stride here is an element-based stride, while the
>strides attribute returns a byte-based stride." also suggests that
>element-based strides are more natural.
It's easier to explain striding when you have contiguous chunks of 
memory of the same data-type, but record-arrays change that and require 
byte-based striding.

>Assuming strides attribute is not used except for testing, would you
>object to renaming current byte-based strides to "byte_strides" and
>implementing element-based "strides"?
I wouldn't have a problem with that, necessarily (though there is 
already an __array_strides__ attribute that is byte-based for the array 
interface --- except it returns None for C-style contiguous so we really 
don't need another attribute).    The remaining issue is how will 
fractional strides be represented?


More information about the NumPy-Discussion mailing list