diff options
author | Mark Wiebe <mwwiebe@gmail.com> | 2011-03-11 12:03:11 -0800 |
---|---|---|
committer | Mark Wiebe <mwwiebe@gmail.com> | 2011-03-11 12:03:11 -0800 |
commit | 5289230a0008e27c89c2427873928ceb1a54264f (patch) | |
tree | 28f29f23a8f04289ef00d37f232f54211ccc7070 /numpy | |
parent | f8c38cb088c9478416f5c99ade68342dcb3db985 (diff) | |
download | numpy-5289230a0008e27c89c2427873928ceb1a54264f.tar.gz |
BUG: For compatibility with 1.5, revert to permitting limited broadcasting of the assignment output
This change got Travis's -10 veto for 1.6.
An unfortunate consequence of reverting this is that some of the broadcasting
error messages get worse, but they're still no worse than in 1.5.
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/src/multiarray/ctors.c | 8 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 24 |
2 files changed, 31 insertions, 1 deletions
diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index 93a26c1e9..08909819d 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -2702,7 +2702,13 @@ PyArray_CopyInto(PyArrayObject *dst, PyArrayObject *src) op[0] = dst; op[1] = src; - op_flags[0] = NPY_ITER_WRITEONLY|NPY_ITER_NO_BROADCAST; + /* + * TODO: In NumPy 2.0, renable NPY_ITER_NO_BROADCAST. This + * was removed during NumPy 1.6 testing for compatibility + * with NumPy 1.5, as per Travis's -10 veto power. + */ + /*op_flags[0] = NPY_ITER_WRITEONLY|NPY_ITER_NO_BROADCAST;*/ + op_flags[0] = NPY_ITER_WRITEONLY; op_flags[1] = NPY_ITER_READONLY; iter = NpyIter_MultiNew(2, op, diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 2f3575649..811fb33ab 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -112,6 +112,30 @@ class TestAttributes(TestCase): x.fill(x[0]) assert_equal(x['f1'][1], x['f1'][0]) +class TestAssignment(TestCase): + def test_assignment_broadcasting(self): + a = np.arange(6).reshape(2,3) + + # Broadcasting the input to the output + a[...] = np.arange(3) + assert_equal(a, [[0,1,2],[0,1,2]]) + a[...] = np.arange(2).reshape(2,1) + assert_equal(a, [[0,0,0],[1,1,1]]) + + # For compatibility with <= 1.5, a limited version of broadcasting + # the output to the input. + # + # This behavior is inconsistent with NumPy broadcasting + # in general, because it only uses one of the two broadcasting + # rules (adding a new "1" dimension to the left of the shape), + # applied to the output instead of an input. In NumPy 2.0, this kind + # of broadcasting assignment will likely be disallowed. + a[...] = np.arange(6)[::-1].reshape(1,2,3) + assert_equal(a, [[5,4,3],[2,1,0]]) + # The other type of broadcasting would require a reduction operation. + def assign(a,b): + a[...] = b + assert_raises(ValueError, assign, a, np.arange(12).reshape(2,2,3)) class TestDtypedescr(TestCase): def test_construction(self): |