Skip to content

BUG: wrong output of inplace operators x += y, x *= y, ... for nonstandard strides #2705

@b45ch1

Description

@b45ch1

The following script works for numpy 1.4.1 but not for numpy > 1.6.

See also the discussion on
http://thread.gmane.org/gmane.comp.python.numeric.general/51379

import numpy
print('numpy version =', numpy.__version__)

x = numpy.array([1,2])
y = numpy.array([1,2,3,4]).reshape((2,2))

x_expected = x.copy()
x_expected[0] += y[0,0]
x_expected[1] += y[0,1]
x_expected[0] += y[1,0]
x_expected[1] += y[1,1]

u,v = numpy.broadcast_arrays(x, y)
# u[0,0] += v[0,0]    <===> x[0] += y[0,0]
# u[0,1] += v[0,1]    <===> x[1] += y[0,1]
# u[1,0] += v[1,0]    <===> x[0] += y[1,0]
# u[1,1] += v[1,1]    <===> x[1] += y[1,1]
u += v

print('x=\n', x)
print('x_expected\n', x_expected)

numpy.testing.assert_array_almost_equal(x_expected, x)

NumPy 1.4.1 output

$ python inplace_op_bug.py 
numpy version = 1.4.1
x=
[5 8]
x_expected
[5 8]

NumPy 1.8.0.dev-9597b1f output

$ python inplace_op_bug.py 
numpy version = 1.8.0.dev-9597b1f
x=
[4 6]
x_expected
[5 8]
Traceback (most recent call last):
  File "inplace_op_bug.py", line 24, in <module>
    numpy.testing.assert_array_almost_equal(x_expected, x)
  File "/Users/b45ch1/.virtualenvs/sci/lib/python2.7/site-packages/numpy-1.8.0.dev_9597b1f_20120920-py2.7-macosx-10.8-x86_64.egg/numpy/testing/utils.py", line 811, in assert_array_almost_equal
    header=('Arrays are not almost equal to %d decimals' % decimal))
  File "/Users/b45ch1/.virtualenvs/sci/lib/python2.7/site-packages/numpy-1.8.0.dev_9597b1f_20120920-py2.7-macosx-10.8-x86_64.egg/numpy/testing/utils.py", line 644, in assert_array_compare
    raise AssertionError(msg)
AssertionError: 
Arrays are not almost equal to 6 decimals

(mismatch 100.0%)
 x: array([5, 8])
 y: array([4, 6])

Edit: updated for python3 printing

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions