|
From: Sasha <nd...@ma...> - 2006-02-03 01:02:21
|
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=3D0 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=3D0 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=3D[0], buffer=3Darray([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=3D0, 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=3D1. 2. Use strides provided to the ndarray even when buffer is not provided. Currently they are silently ignored: >>> ndarray([5], strides=3D[0]).strides (4,) 3. Fix augmented assignment operators. Currently: >>> x =3D zeros(5) >>> x.strides=3D0 >>> x +=3D 1 >>> x array([5, 5, 5, 5, 5]) >>> x +=3D arange(5) >>> x array([15, 15, 15, 15, 15]) Desired: >>> x =3D zeros(5) >>> x.strides=3D0 >>> x +=3D 1 >>> x array([1, 1, 1, 1, 1]) >>> x +=3D arange(5) >>> x array([1, 2, 3, 4, 5]) This will probably require proper handling of stride=3D0 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=3D0 arrays as a super-fast alternative to zeros and ones. |