Skip to content

Commit

Permalink
Merge pull request scipy#15232 from stefanv/sparse-array-rmul
Browse files Browse the repository at this point in the history
BUG: Add rmul for sparse arrays
  • Loading branch information
tylerjereddy authored Dec 17, 2021
2 parents d11a3d9 + 5bae3a4 commit d85cdca
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions scipy/sparse/_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ def _lil_container(self):
def __mul__(self, *args, **kwargs):
return self.multiply(*args, **kwargs)

def __rmul__(self, *args, **kwargs):
return self.multiply(*args, **kwargs)



def _matrix_doc_to_array(docstr):
# For opimized builds with stripped docstrings
Expand Down
13 changes: 13 additions & 0 deletions scipy/sparse/tests/test_array_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,19 @@ def test_elementwise_mul(A):
assert np.all((A * A).todense() == A.power(2).todense())


@parametrize_sparrays
def test_elementwise_rmul(A):
with pytest.raises(TypeError):
None * A

with pytest.raises(ValueError):
np.eye(3) * scipy.sparse.csr_array(np.arange(6).reshape(2, 3))

assert np.all((2 * A) == (A.todense() * 2))

assert np.all((A.todense() * A) == (A.todense() ** 2))


@parametrize_sparrays
def test_matmul(A):
assert np.all((A @ A.T).todense() == A.dot(A.T).todense())
Expand Down

0 comments on commit d85cdca

Please sign in to comment.