Skip to content

Commit

Permalink
BUG: sparse: Fix summing duplicates for CSR/CSC creation from (data,c…
Browse files Browse the repository at this point in the history
…oords) (scipy#20687)

* test and then fix duplicates for CSR/CSC creation from (data,coords)

* remove has_canonical_format check when summing duplicates.
  • Loading branch information
dschult authored and tylerjereddy committed May 15, 2024
1 parent d08c8d8 commit 198a73b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions scipy/sparse/_compressed.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def __init__(self, arg1, shape=None, dtype=None, copy=False):
coo = self._coo_container(arg1, shape=shape, dtype=dtype)
arrays = coo._coo_to_compressed(self._swap)
self.indptr, self.indices, self.data, self._shape = arrays
self.sum_duplicates()
elif len(arg1) == 3:
# (data, indices, indptr) format
(data, indices, indptr) = arg1
Expand Down
15 changes: 15 additions & 0 deletions scipy/sparse/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3851,6 +3851,10 @@ def test_constructor4(self):
dense = array([[2**63 + 1, 0], [0, 1]], dtype=np.uint64)
assert_array_equal(dense, csr.toarray())

# with duplicates (should sum the duplicates)
csr = csr_matrix(([1,1,1,1], ([0,2,2,0], [0,1,1,0])))
assert csr.nnz == 2

def test_constructor5(self):
# infer dimensions from arrays
indptr = array([0,1,3,3])
Expand Down Expand Up @@ -4092,6 +4096,10 @@ def test_constructor4(self):
csc = csc_matrix((data,ij),(4,3))
assert_array_equal(arange(12).reshape(4, 3), csc.toarray())

# with duplicates (should sum the duplicates)
csc = csc_matrix(([1,1,1,1], ([0,2,2,0], [0,1,1,0])))
assert csc.nnz == 2

def test_constructor5(self):
# infer dimensions from arrays
indptr = array([0,1,3,3])
Expand Down Expand Up @@ -4506,6 +4514,13 @@ def test_todok_duplicates(self):
dok = coo.todok()
assert_array_equal(dok.toarray(), coo.toarray())

def test_tocompressed_duplicates(self):
coo = coo_matrix(([1,1,1,1], ([0,2,2,0], [0,1,1,0])))
csr = coo.tocsr()
assert_equal(csr.nnz + 2, coo.nnz)
csc = coo.tocsc()
assert_equal(csc.nnz + 2, coo.nnz)

def test_eliminate_zeros(self):
data = array([1, 0, 0, 0, 2, 0, 3, 0])
row = array([0, 0, 0, 1, 1, 1, 1, 1])
Expand Down

0 comments on commit 198a73b

Please sign in to comment.