[Numpy-discussion] Zeros in strides

Sasha ndarray at mac.com
Thu Feb 2 17:03:07 EST 2006

As I explained in my previous post, numpy allows zeros in the
"strides" tuple, but the arrays with such strides have unexpected
properties. In this post I will try to explain why arrays with zeros
in strides are desireable and what properties they should have.

A rank-1 array with strides=0 behaves almost like a scalar, in fact
scalar arithmetics is currently implemented by setting stride to 0 is
generic umath loops.  Like scalar, rank-1 array with stride=0 only
needs a buffer of size 1*itemsize, but currently numpy does not allow
creation of rank-1 arrays with buffer smaller than size*itemsize:

>>> ndarray([5], strides=[0], buffer=array([1]))
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: buffer is too small for requested array

An array with 0 stride is a better alternative to x + zeros(n) than a
scalar or rank-0 x because  an array with zero stride knows its size.
(With the current umath implementation, adding two arrays with
stride=0, would still require n operations, but this would probably
not be the case if BLAS is used instead of a generic loop).

I propose to make a few changes to the way zeros in strides are
handled.  This looks like undocumented territory,  so I don't think
there are any  compatibility issues.

1. Change the buffer size requirements so that dimentions with zero
stride count as size=1.

2. Use strides provided to the ndarray even when buffer is not
provided. Currently they are silently ignored:

>>> ndarray([5], strides=[0]).strides

3. Fix augmented assignment operators.

>>> x = zeros(5)
>>> x.strides=0
>>> x += 1
>>> x
array([5, 5, 5, 5, 5])
>>> x += arange(5)
>>> x
array([15, 15, 15, 15, 15])

>>> x = zeros(5)
>>> x.strides=0
>>> x += 1
>>> x
array([1, 1, 1, 1, 1])
>>> x += arange(5)
>>> x
array([1, 2, 3, 4, 5])

This will probably require proper handling of stride=0 case in the
output arguments of ufuncs in general, so this may be harder to get
right than the first two proposals.

4. Introduce xzeros and xones functions that will create stride=0
arrays as a super-fast alternative to zeros and ones.

More information about the NumPy-Discussion mailing list