6
6
7
7
# array-api-strict#6
8
8
import array_api_strict as xp # type: ignore[import-untyped]
9
+ import numpy as np
9
10
import pytest
10
11
from numpy .testing import assert_allclose , assert_array_equal , assert_equal
11
12
@@ -114,6 +115,7 @@ def test_combination(self):
114
115
115
116
class TestCreateDiagonal :
116
117
def test_1d (self ):
118
+ # from np.diag tests
117
119
vals = 100 * xp .arange (5 , dtype = xp .float64 )
118
120
b = xp .zeros ((5 , 5 ))
119
121
for k in range (5 ):
@@ -127,13 +129,24 @@ def test_1d(self):
127
129
assert_array_equal (create_diagonal (vals , offset = 2 , xp = xp ), b )
128
130
assert_array_equal (create_diagonal (vals , offset = - 2 , xp = xp ), c )
129
131
132
+ @pytest .mark .parametrize ("n" , range (1 , 10 ))
133
+ @pytest .mark .parametrize ("offset" , range (1 , 10 ))
134
+ def test_create_diagonal (self , n , offset ):
135
+ # from scipy._lib tests
136
+ rng = np .random .default_rng (2347823 )
137
+ one = xp .asarray (1.0 )
138
+ x = rng .random (n )
139
+ A = create_diagonal (xp .asarray (x , dtype = one .dtype ), offset = offset , xp = xp )
140
+ B = xp .asarray (np .diag (x , offset ), dtype = one .dtype )
141
+ assert_array_equal (A , B )
142
+
130
143
def test_0d (self ):
131
144
with pytest .raises (ValueError , match = "1-dimensional" ):
132
- print ( create_diagonal (xp .asarray (1 ), xp = xp ) )
145
+ create_diagonal (xp .asarray (1 ), xp = xp )
133
146
134
147
def test_2d (self ):
135
148
with pytest .raises (ValueError , match = "1-dimensional" ):
136
- print ( create_diagonal (xp .asarray ([[1 ]]), xp = xp ) )
149
+ create_diagonal (xp .asarray ([[1 ]]), xp = xp )
137
150
138
151
139
152
class TestKron :
0 commit comments