15
15
)
16
16
17
17
import dpnp
18
- import dpnp .linalg
19
18
20
19
from .helper import (
21
20
assert_dtype_allclose ,
@@ -279,15 +278,12 @@ def test_cholesky_errors(self):
279
278
280
279
281
280
class TestCond :
282
- def setup_method (self ):
283
- numpy .random .seed (70 )
281
+ _norms = [None , - dpnp .inf , - 2 , - 1 , 1 , 2 , dpnp .inf , "fro" ]
284
282
285
283
@pytest .mark .parametrize (
286
- "shape" , [(0 , 4 , 4 ), (4 , 0 , 3 , 3 )], ids = ["(0, 5, 3)" , "(4, 0, 2, 3)" ]
287
- )
288
- @pytest .mark .parametrize (
289
- "p" , [None , - dpnp .inf , - 2 , - 1 , 1 , 2 , dpnp .inf , "fro" ]
284
+ "shape" , [(0 , 4 , 4 ), (4 , 0 , 3 , 3 )], ids = ["(0, 4, 4)" , "(4, 0, 3, 3)" ]
290
285
)
286
+ @pytest .mark .parametrize ("p" , _norms )
291
287
def test_empty (self , shape , p ):
292
288
a = numpy .empty (shape )
293
289
ia = dpnp .array (a )
@@ -296,26 +292,27 @@ def test_empty(self, shape, p):
296
292
expected = numpy .linalg .cond (a , p = p )
297
293
assert_dtype_allclose (result , expected )
298
294
295
+ # TODO: uncomment once numpy 2.3.3 release is published
296
+ # @testing.with_requires("numpy>=2.3.3")
299
297
@pytest .mark .parametrize (
300
298
"dtype" , get_all_dtypes (no_none = True , no_bool = True )
301
299
)
302
300
@pytest .mark .parametrize (
303
301
"shape" , [(4 , 4 ), (2 , 4 , 3 , 3 )], ids = ["(4, 4)" , "(2, 4, 3, 3)" ]
304
302
)
305
- @pytest .mark .parametrize (
306
- "p" , [None , - dpnp .inf , - 2 , - 1 , 1 , 2 , dpnp .inf , "fro" ]
307
- )
303
+ @pytest .mark .parametrize ("p" , _norms )
308
304
def test_basic (self , dtype , shape , p ):
309
305
a = generate_random_numpy_array (shape , dtype )
310
306
ia = dpnp .array (a )
311
307
312
308
result = dpnp .linalg .cond (ia , p = p )
313
309
expected = numpy .linalg .cond (a , p = p )
310
+ # TODO: remove when numpy#29333 is released
311
+ if numpy_version () < "2.3.3" :
312
+ expected = expected .real
314
313
assert_dtype_allclose (result , expected , factor = 16 )
315
314
316
- @pytest .mark .parametrize (
317
- "p" , [None , - dpnp .inf , - 2 , - 1 , 1 , 2 , dpnp .inf , "fro" ]
318
- )
315
+ @pytest .mark .parametrize ("p" , _norms )
319
316
def test_bool (self , p ):
320
317
a = numpy .array ([[True , True ], [True , False ]])
321
318
ia = dpnp .array (a )
@@ -324,9 +321,7 @@ def test_bool(self, p):
324
321
expected = numpy .linalg .cond (a , p = p )
325
322
assert_dtype_allclose (result , expected )
326
323
327
- @pytest .mark .parametrize (
328
- "p" , [None , - dpnp .inf , - 2 , - 1 , 1 , 2 , dpnp .inf , "fro" ]
329
- )
324
+ @pytest .mark .parametrize ("p" , _norms )
330
325
def test_nan_to_inf (self , p ):
331
326
a = numpy .zeros ((2 , 2 ))
332
327
ia = dpnp .array (a )
@@ -344,9 +339,7 @@ def test_nan_to_inf(self, p):
344
339
else :
345
340
assert_raises (dpnp .linalg .LinAlgError , dpnp .linalg .cond , ia , p = p )
346
341
347
- @pytest .mark .parametrize (
348
- "p" , [None , - dpnp .inf , - 2 , - 1 , 1 , 2 , dpnp .inf , "fro" ]
349
- )
342
+ @pytest .mark .parametrize ("p" , _norms )
350
343
@pytest .mark .parametrize (
351
344
"stride" ,
352
345
[(- 2 , - 3 , 2 , - 2 ), (- 2 , 4 , - 4 , - 4 ), (2 , 3 , 4 , 4 ), (- 1 , 3 , 3 , - 3 )],
@@ -358,21 +351,23 @@ def test_nan_to_inf(self, p):
358
351
],
359
352
)
360
353
def test_strided (self , p , stride ):
361
- A = numpy .random .rand (6 , 8 , 10 , 10 )
362
- B = dpnp .asarray (A )
354
+ A = generate_random_numpy_array (
355
+ (6 , 8 , 10 , 10 ), seed_value = 70 , low = 0 , high = 1
356
+ )
357
+ iA = dpnp .array (A )
363
358
slices = tuple (slice (None , None , stride [i ]) for i in range (A .ndim ))
364
- a = A [slices ]
365
- b = B [slices ]
359
+ a , ia = A [slices ], iA [slices ]
366
360
367
- result = dpnp .linalg .cond (b , p = p )
361
+ result = dpnp .linalg .cond (ia , p = p )
368
362
expected = numpy .linalg .cond (a , p = p )
369
363
assert_dtype_allclose (result , expected , factor = 24 )
370
364
371
- def test_error (self ):
365
+ @pytest .mark .parametrize ("xp" , [dpnp , numpy ])
366
+ def test_error (self , xp ):
372
367
# cond is not defined on empty arrays
373
- ia = dpnp .empty ((2 , 0 ))
368
+ a = xp .empty ((2 , 0 ))
374
369
with pytest .raises (ValueError ):
375
- dpnp .linalg .cond (ia , p = 1 )
370
+ xp .linalg .cond (a , p = 1 )
376
371
377
372
378
373
class TestDet :
0 commit comments