Skip to content

Commit

Permalink
BUG: For compatibility with 1.5, revert to permitting limited broadca…
Browse files Browse the repository at this point in the history
…sting 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.
  • Loading branch information
mwiebe committed Mar 11, 2011
1 parent f8c38cb commit 5289230
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doc/source/reference/c-api.array.rst
Expand Up @@ -1901,7 +1901,7 @@ Array Functions

.. cfunction:: PyObject* PyArray_EinsteinSum(char* subscripts, npy_intp nop, PyArrayObject** op_in, PyArray_Descr* dtype, NPY_ORDER order, NPY_CASTING casting, PyArrayObject* out)

.. versionadded:: 1.6
.. versionadded:: 1.6

Applies the einstein summation convention to the array operands
provided, returning a new array or placing the result in *out*.
Expand Down
8 changes: 7 additions & 1 deletion numpy/core/src/multiarray/ctors.c
Expand Up @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions numpy/core/tests/test_multiarray.py
Expand Up @@ -112,6 +112,30 @@ def test_fill(self):
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):
Expand Down

0 comments on commit 5289230

Please sign in to comment.