Skip to content
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

Added cases for sparse arrays in __mul__ and __rmul__ #17014

Merged
merged 1 commit into from
Jun 12, 2019
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
10 changes: 10 additions & 0 deletions sympy/tensor/array/ndim_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,19 +377,29 @@ def __sub__(self, other):

def __mul__(self, other):
from sympy.matrices.matrices import MatrixBase
from sympy.tensor.array import SparseNDimArray

if isinstance(other, (Iterable, NDimArray, MatrixBase)):
raise ValueError("scalar expected, use tensorproduct(...) for tensorial product")

other = sympify(other)
if isinstance(self, SparseNDimArray):
return type(self)({k: other*v for (k, v) in self._sparse_array.items()}, self.shape)

result_list = [i*other for i in self]
return type(self)(result_list, self.shape)

def __rmul__(self, other):
from sympy.matrices.matrices import MatrixBase
from sympy.tensor.array import SparseNDimArray

if isinstance(other, (Iterable, NDimArray, MatrixBase)):
raise ValueError("scalar expected, use tensorproduct(...) for tensorial product")

other = sympify(other)
if isinstance(self, SparseNDimArray):
return type(self)({k: other*v for (k, v) in self._sparse_array.items()}, self.shape)

result_list = [other*i for i in self]
return type(self)(result_list, self.shape)

Expand Down
4 changes: 4 additions & 0 deletions sympy/tensor/array/tests/test_mutable_ndim_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ def test_sparse():
b[1, 1] = 2
assert a != b

# __mul__ and __rmul__
assert a * 3 == MutableSparseNDimArray({200001: 3}, (100000, 200000))
assert 3 * a == MutableSparseNDimArray({200001: 3}, (100000, 200000))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if you multiply by zero?



def test_calculation():

Expand Down