diff options
-rw-r--r-- | doc/source/reference/c-api.array.rst | 2 | ||||
-rw-r--r-- | numpy/core/src/multiarray/ctors.c | 8 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 24 |
3 files changed, 32 insertions, 2 deletions
diff --git a/doc/source/reference/c-api.array.rst b/doc/source/reference/c-api.array.rst index 62746eb49..bc108eb8e 100644 --- a/doc/source/reference/c-api.array.rst +++ b/doc/source/reference/c-api.array.rst @@ -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*. 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): |