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

BUG: Fix typecasting problem in scipy.sparse.lil_matrix truediv #19408

Merged
merged 5 commits into from
Nov 8, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions scipy/sparse/_lil.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ def _mul_scalar(self, other):
def __truediv__(self, other): # self / other
if isscalarlike(other):
new = self.copy()
new.dtype = np.result_type(self, other)
# Divide every element by this scalar
for j, rowvals in enumerate(new.data):
new.data[j] = [val/other for val in rowvals]
Expand Down
8 changes: 8 additions & 0 deletions scipy/sparse/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4201,6 +4201,14 @@ def test_scalar_mul(self):
x = x*0
assert_equal(x[0, 0], 0)

def test_truediv_scalar(self):
A = self.spcreator((3, 2))
A[0, 1] = -10
A[2, 0] = 20

assert_array_equal((A / 1j).toarray(), A.toarray() / 1j)
assert_array_equal((A / 9).toarray(), A.toarray() / 9)

def test_inplace_ops(self):
A = lil_matrix([[0, 2, 3], [4, 0, 6]])
B = lil_matrix([[0, 1, 0], [0, 2, 3]])
Expand Down