@@ -2030,3 +2030,43 @@ def test_unstack_invalid_type():
20302030 a = np .arange (6 ).reshape (2 , 3 ) # not a sparse array
20312031 with pytest .raises (TypeError , match = "must be a SparseArray" ):
20322032 sparse .unstack (a , axis = 0 )
2033+
2034+
2035+ @pytest .mark .parametrize ("ndim" , range (1 , 4 ))
2036+ @pytest .mark .parametrize ("shape_range" , [3 ])
2037+ @pytest .mark .parametrize ("n" , [1 , 2 ])
2038+ @pytest .mark .parametrize ("use_prepend, use_append" , [(False , False ), (True , False ), (False , True ), (True , True )])
2039+ def test_diff_matches_numpy (ndim , shape_range , n , use_prepend , use_append ):
2040+ rng = np .random .default_rng (42 )
2041+ shape = tuple (rng .integers (2 , shape_range + 2 ) for _ in range (ndim ))
2042+ x = rng .integers (0 , 10 , size = shape )
2043+ sparse_x = COO .from_numpy (x )
2044+
2045+ for axis in range (- ndim , ndim ):
2046+ prepend = rng .integers (0 , 10 , size = x .shape ).astype (x .dtype ) if use_prepend else None
2047+ append = rng .integers (0 , 10 , size = x .shape ).astype (x .dtype ) if use_append else None
2048+
2049+ sparse_prepend = COO .from_numpy (prepend ) if prepend is not None else None
2050+ sparse_append = COO .from_numpy (append ) if append is not None else None
2051+
2052+ sparse_result = sparse .diff (sparse_x , axis = axis , n = n , prepend = sparse_prepend , append = sparse_append )
2053+
2054+ kwargs = {}
2055+ if prepend is not None :
2056+ kwargs ["prepend" ] = prepend
2057+ if append is not None :
2058+ kwargs ["append" ] = append
2059+
2060+ dense_result = np .diff (x , axis = axis , n = n , ** kwargs )
2061+
2062+ np .testing .assert_array_equal (
2063+ sparse_result .todense (),
2064+ dense_result ,
2065+ err_msg = f"Mismatch at axis={ axis } , n={ n } , prepend={ use_prepend } , append={ use_append } " ,
2066+ )
2067+
2068+
2069+ def test_diff_invalid_type ():
2070+ a = np .arange (6 ).reshape (2 , 3 )
2071+ with pytest .raises (TypeError , match = "must be a SparseArray" ):
2072+ sparse .diff (a )
0 commit comments