@@ -231,6 +231,26 @@ def test07_mutate_arg():
231231 assert_array_equal (A , 2 * A2 )
232232
233233
234+ def create_spmat_unsorted ():
235+ import scipy .sparse as sparse
236+ # Create a small matrix with explicit indices and indptr
237+ data = np .array ([1.0 , 2.0 , 3.0 , 4.0 , 5.0 ])
238+
239+ # Deliberately unsorted indices within columns
240+ # For a properly sorted CSC matrix, indices should be sorted within each column
241+ indices = np .array ([0 , 2 , 1 , 4 , 3 ]) # Unsorted (should be [0, 1, 2, 3, 4])
242+
243+ # indptr points to where each column starts in the indices/data arrays
244+ indptr = np .array ([0 , 2 , 3 , 5 ])
245+
246+ # Create a 5x3 matrix with unsorted indices
247+ unsorted_csc = sparse .csc_matrix ((data , indices , indptr ), shape = (5 , 3 ))
248+
249+ # Verify that indices are unsorted
250+ assert not unsorted_csc .has_sorted_indices
251+ return unsorted_csc
252+
253+
234254@needs_numpy_and_eigen
235255def test08_sparse ():
236256 pytest .importorskip ("scipy" )
@@ -244,6 +264,10 @@ def test08_sparse():
244264 assert type (t .sparse_copy_r (t .sparse_c ())) is scipy .sparse .csr_matrix
245265 assert type (t .sparse_copy_c (t .sparse_r ())) is scipy .sparse .csc_matrix
246266
267+ # construct scipy matrix with unsorted indices
268+ assert type (t .sparse_copy_c (create_spmat_unsorted ())) is scipy .sparse .csc_matrix
269+
270+
247271 def assert_sparse_equal_ref (sparse_mat ):
248272 ref = np .array (
249273 [
@@ -262,6 +286,7 @@ def assert_sparse_equal_ref(sparse_mat):
262286 assert_sparse_equal_ref (t .sparse_copy_c (t .sparse_c ()))
263287 assert_sparse_equal_ref (t .sparse_copy_r (t .sparse_c ()))
264288 assert_sparse_equal_ref (t .sparse_copy_c (t .sparse_r ()))
289+ assert_sparse_equal_ref (t .sparse_copy_c (create_spmat_unsorted ()))
265290
266291
267292@needs_numpy_and_eigen
0 commit comments