Skip to content

Commit ca1a118

Browse files
Improve code coverage
1 parent f561ec4 commit ca1a118

File tree

2 files changed

+20
-27
lines changed

2 files changed

+20
-27
lines changed

dpnp/linalg/dpnp_iface_linalg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def cond(x, p=None):
200200
This function will raise :class:`dpnp.linalg.LinAlgError` on singular input
201201
when using any of the norm: ``1``, ``-1``, ``inf``, ``-inf``, or ``'fro'``.
202202
In contrast, :obj:`numpy.linalg.cond` will fill the result array with
203-
``inf`` values for every 2D batch in the input array that is singular
203+
``inf`` values for each 2D batch in the input array that is singular
204204
when using these norms.
205205
206206
Examples

dpnp/tests/test_linalg.py

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -327,28 +327,25 @@ def test_bool(self, p):
327327
"p", [None, -dpnp.inf, -2, -1, 1, 2, dpnp.inf, "fro"]
328328
)
329329
def test_singular_2D(self, p):
330-
a = numpy.ones((2, 2))
330+
a = numpy.array([[1, 2], [0, 0]])
331331
ia = dpnp.array(a)
332332

333333
# NumPy returns `inf` for most norms on singular matrices,
334-
# and large (often meaningless) values for [None, 2, -2].
334+
# and zero for norm -2.
335335
# DPNP raises LinAlgError for 1, -1, inf, -inf, and 'fro'
336336
# due to use of gesv in 2D case.
337337
# DPNP matches NumPy behavior for [None, 2, -2].
338338
if p in [None, 2, -2]:
339-
# Only ensure the function runs and returns non-infinite values.
340339
result = dpnp.linalg.cond(ia, p=p)
341340
expected = numpy.linalg.cond(a, p=p)
342-
assert not dpnp.any(dpnp.isinf(result))
343-
assert not numpy.any(numpy.isinf(expected))
341+
assert_dtype_allclose(result, expected)
344342
else:
345343
assert_raises(dpnp.linalg.LinAlgError, dpnp.linalg.cond, ia, p=p)
346344

347-
@pytest.mark.parametrize("shape", [(2, 2, 2), (2, 2, 2, 2)])
348345
@pytest.mark.parametrize(
349346
"p", [None, -dpnp.inf, -2, -1, 1, 2, dpnp.inf, "fro"]
350347
)
351-
def test_singular_ND(self, shape, p):
348+
def test_singular_ND(self, p):
352349
# dpnp.linalg.cond uses dpnp.linalg.inv()
353350
# for the case when p is not None or p != -2 or p != 2
354351
# For singular matrices cuSolver raises an error
@@ -360,32 +357,28 @@ def test_singular_ND(self, shape, p):
360357
and p in [-dpnp.inf, -1, 1, dpnp.inf, "fro"]
361358
):
362359
pytest.skip("Different behavior on CUDA")
363-
a = numpy.ones((shape))
360+
a = generate_random_numpy_array((2, 2, 2, 2))
361+
a[0, 0] = 0
362+
a[1, 1] = 1
364363
ia = dpnp.array(a)
365364

366365
# NumPy returns `inf` for most norms on singular matrices,
367-
# and large (often meaningless) values for [None, 2, -2].
366+
# and zeros for norm -2.
368367
# DPNP raises LinAlgError for 1, -1, inf, -inf, and 'fro'
369368
# due to use of dpnp.linalg.inv() with oneMKL >= 2025.2.
370369
# DPNP matches NumPy behavior for [None, 2, -2].
371-
if requires_intel_mkl_version("2025.2"):
372-
if p in [None, 2, -2]:
373-
# Only ensure the function runs and
374-
# returns non-infinite values.
375-
result = dpnp.linalg.cond(ia, p=p)
376-
expected = numpy.linalg.cond(a, p=p)
377-
assert not dpnp.any(dpnp.isinf(result))
378-
assert not numpy.any(numpy.isinf(expected))
379-
else:
380-
assert_raises(
381-
dpnp.linalg.LinAlgError, dpnp.linalg.cond, ia, p=p
382-
)
370+
if p in [None, 2, -2]:
371+
result = dpnp.linalg.cond(ia, p=p)
372+
expected = numpy.linalg.cond(a, p=p)
373+
assert_dtype_allclose(result, expected)
374+
elif requires_intel_mkl_version("2025.2"):
375+
assert_raises(dpnp.linalg.LinAlgError, dpnp.linalg.cond, ia, p=p)
376+
# With oneMKL < 2025.2 and norms: 1, -1, inf, -inf, 'fro',
377+
# dpnp.linalg.inv() uses getrf_batch + getri_batch
378+
# which do not raise LinAlgError.
379+
# Instead, the result contains `inf` for each 2D batch
380+
# in the input array that is singular
383381
else:
384-
# For OneMKL < 2025.2:
385-
# dpnp.linalg.inv() uses getrf_batch + getri_batch
386-
# which do not raise LinAlgError.
387-
# Instead, the result may contain `inf` or `nan`
388-
# depending on singularity.
389382
result = dpnp.linalg.cond(ia, p=p)
390383
expected = numpy.linalg.cond(a, p=p)
391384
assert_dtype_allclose(result, expected)

0 commit comments

Comments
 (0)