Skip to content

Commit

Permalink
Merge pull request qutip#2349 from Ericgig/misc.matmul_shapecheck
Browse files Browse the repository at this point in the history
Fix logic error in shape check
  • Loading branch information
Ericgig authored Mar 12, 2024
2 parents c5ec829 + 71a9bb3 commit 9f04c73
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
6 changes: 4 additions & 2 deletions qutip/core/data/matmul.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ cdef int _check_shape(Data left, Data right, Data out=None) except -1 nogil:
)
if (
out is not None
and out.shape[0] != left.shape[0]
and out.shape[1] != right.shape[1]
and (
out.shape[0] != left.shape[0]
or out.shape[1] != right.shape[1]
)
):
raise ValueError(
"incompatible output shape, got "
Expand Down
9 changes: 9 additions & 0 deletions qutip/tests/core/data/test_dense.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,12 @@ def test_like_keep_order(func, fortran):
old = dense.zeros(3, 3, fortran=fortran)
new = func(old)
assert new.fortran == old.fortran


@pytest.mark.parametrize("shape", [(4, 3), (3, 3), (2, 8)])
def test_inplace_matmul_error(shape):
op = dense.zeros(4, 4)
out = dense.zeros(*shape)
with pytest.raises(ValueError) as err:
data.matmul_dense(op, op, 1., out=out)
assert str(err.value).startswith("incompatible output shape")

0 comments on commit 9f04c73

Please sign in to comment.