Skip to content

Commit

Permalink
BUG: Fix typecasting problem in scipy.sparse.lil_matrix truediv (#19408)
Browse files Browse the repository at this point in the history
* Fix typecasting problem in lil_matrix truedriv

* Added test

---------

Co-authored-by: Dan Schult <dschult@colgate.edu>
  • Loading branch information
vetschn and dschult committed Nov 8, 2023
1 parent 6ea14ff commit 6f16ecc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
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

0 comments on commit 6f16ecc

Please sign in to comment.