[Numpy-discussion] Learning "strides"
ndarray at mac.com
Thu Feb 2 15:54:16 CST 2006
I don't know if this came from numarray or not, but for me as someone
who transitions from Numeric, the "strides" attribute to an ndarray is
a a new feature. I've spend some time playing with it and there are
some properties that I dislike. Some of these undesired properties
are probably bugs and easy to fix, but others require some discussion.
1. Negative strides:
>>> x = zeros(5)
>>> x.strides= (-4,)
array([ 0, 25, 0, -136009696, -136009536])
Looks like a bug. PyArray_CheckStrides only checks for one end of the
buffer. It is easy to fix that by disallowing negative strides, but I
think that would be wrong. In my view. the right sollution is to pass
offset to PyArray_CheckStrides and check for both ends of the buffer.
The later will change C-API.
2. Zero strides:
>>> x = arange(5)
>>> x.strides = 0
array([0, 0, 0, 0, 0])
>>> x += 1
array([5, 5, 5, 5, 5])
These are somewhat puzzling properties unless you know the internals.
I believe ndarray with 0s in strides are quite useful and will follow
up with the description of the properties I would expect from them.
3. "Fractional" strides:
I call "fractional" strides that are not a multiple of "itemsize".
>>> x = arange(5)
>>> x.strides = 3
array([ 0, 256, 131072, 50331648, 3])
I think these should be disallowed. It is just too easy to forget that
strides are given in bytes, not in elements. Ideally rather than
checking for strides[i] % itemsize, I would just make strides[i] to
be expressed in number of elements, not in bytes. This can be done
without changing the way strides are stored internally - just multiply
by itemsize in
set_strides and divide in get_strides. If strides attribute was not
introduced before numpy, this change should not cause any
compatibility problems. If it has some history
of use, it may be possible to depricate "strides" (with a deprecation
introduce a different attribute, say "steps", that will be expressed
in number of elements.
More information about the Numpy-discussion