[Numpy-discussion] how is y += x computed when y.strides = (0, 8) and x.strides=(16, 8) ?

Sebastian Walter sebastian.walter at gmail.com
Fri Aug 31 05:31:16 EDT 2012


Hi,

I'm using numpy 1.6.1 on Ubuntu 12.04.1 LTS.
A code that used to work with an older version of numpy now fails with an error.

Were there any changes in the way inplace operations like +=, *=, etc.
work on arrays with non-standard strides?

For the script:

------- start of code -------

import numpy

x = numpy.arange(6).reshape((3,2))
y = numpy.arange(2)

print 'x=\n', x
print 'y=\n', y

u,v = numpy.broadcast_arrays(x, y)

print 'u=\n', u
print 'v=\n', v
print 'v.strides=\n', v.strides

v += u

print 'v=\n', v  # expectation: v = [[6,12], [6,12], [6,12]]
print 'u=\n', u
print 'y=\n', y  # expectation: y = [6,12]

------- end of code -------

I get the output

-------- start of output ---------
x=
[[0 1]
 [2 3]
 [4 5]]
y=
[0 1]
u=
[[0 1]
 [2 3]
 [4 5]]
v=
[[0 1]
 [0 1]
 [0 1]]
v.strides=
(0, 8)
v=
[[4 6]
 [4 6]
 [4 6]]
u=
[[0 1]
 [2 3]
 [4 5]]
y=
[4 6]

-------- end of output --------

I would have expected that

v += u

performs an element-by-element +=

v[0,0] += u[0,0]  # increments y[0]
v[0,1] += u[0,1]  # increments y[1]
v[1,0] += u[1,0]  # increments y[0]
v[1,1] += u[1,1]  # increments y[1]
v[2,0] += u[2,0]  # increments y[0]
v[2,1] += u[2,1]  # increments y[1]

 yielding the result

y = [6,12]

but instead one obtains

y = [4, 6]

which could be the result of

v[2,0] += u[2,0]  # increments y[0]
v[2,1] += u[2,1]  # increments y[1]


Is this the intended behavior?

regards,
Sebastian



More information about the NumPy-Discussion mailing list