8000 BUG (I guess?): Make a @= b error out by njsmith · Pull Request #6000 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG (I guess?): Make a @= b error out #6000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
BUG: Make a @= b error out
Before this change, we defined a nb_matrix_multiply slot but not a
nb_inplace_matrix_multiply slot, which means that a statement like

  a @= b

would be silently expanded by the CPython interpreter to become

  a = a @ b

This is undesireable, because it produces unexpected memory
allocations, breaks view relationships, and so forth.

This commit adds a nb_inplace_matrix_multiply slot which simply errors
out, and suggests that users write 'a = a @ b' explicitly if that's
what they want.
  • Loading branch information
njsmith committed Jun 28, 2015
commit 1adcdf7aa5b20a9afd778290105ec327b705c93e
11 changes: 10 additions & 1 deletion numpy/core/src/multiarray/number.c
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,15 @@ array_matrix_multiply(PyArrayObject *m1, PyObject *m2)
0, nb_matrix_multiply);
return PyArray_GenericBinaryFunction(m1, m2, matmul);
}

static PyObject *
array_inplace_matrix_multiply(PyArrayObject *m1, PyObject *m2)
{
PyErr_SetString(PyExc_TypeError,
"In-place matrix multiplication is not (yet) supported. "
"Use 'a = a @ b' instead of 'a @= b'.");
return NULL;
}
#endif

/* Determine if object is a scalar and if so, convert the object
Expand Down Expand Up @@ -1092,6 +1101,6 @@ NPY_NO_EXPORT PyNumberMethods array_as_number = {
(unaryfunc)array_index, /*nb_index */
#if PY_VERSION_HEX >= 0x03050000
(binaryfunc)array_matrix_multiply, /*nb_matrix_multiply*/
(binaryfunc)NULL, /*nb_inplacematrix_multiply*/
(binaryfunc)array_inplace_matrix_multiply, /*nb_inplace_matrix_multiply*/
#endif
};
13 changes: 13 additions & 0 deletions numpy/core/tests/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -4322,6 +4322,19 @@ def __rmatmul__(self, other):
assert_equal(self.matmul(a, b), "A")
assert_equal(self.matmul(b, a), "A")

def test_matmul_inplace():
# It would be nice to support in-place matmul eventually, but for now
# we don't have a working implementation, so better just to error out
# and nudge people to writing "a = a @ b".
a = np.eye(3)
b = np.eye(3)
assert_raises(TypeError, a.__imatmul__, b)
import operator
assert_raises(TypeError, operator.imatmul, a, b)
# we avoid writing the token `exec` so as not to crash python 2's
# parser
exec_ = getattr(builtins, "exec")
assert_raises(TypeError, exec_, "a @= b", globals(), locals())

class TestInner(TestCase):

Expand Down
0