Skip to content

Commit 14ba899

Browse files
committed
Add tests coverage with empty arrays
1 parent a206503 commit 14ba899

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

dpnp/tests/test_linalg.py

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3104,7 +3104,7 @@ def test_matrix_power_errors(self):
31043104

31053105

31063106
class TestMatrixRank:
3107-
@pytest.mark.parametrize("dtype", get_all_dtypes())
3107+
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
31083108
@pytest.mark.parametrize(
31093109
"data",
31103110
[
@@ -3116,15 +3116,15 @@ class TestMatrixRank:
31163116
numpy.array(1),
31173117
],
31183118
)
3119-
def test_matrix_rank(self, data, dtype):
3119+
def test_basic(self, data, dtype):
31203120
a = data.astype(dtype)
31213121
a_dp = dpnp.array(a)
31223122

31233123
np_rank = numpy.linalg.matrix_rank(a)
31243124
dp_rank = dpnp.linalg.matrix_rank(a_dp)
31253125
assert dp_rank.asnumpy() == np_rank
31263126

3127-
@pytest.mark.parametrize("dtype", get_all_dtypes())
3127+
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
31283128
@pytest.mark.parametrize(
31293129
"data",
31303130
[
@@ -3134,7 +3134,7 @@ def test_matrix_rank(self, data, dtype):
31343134
numpy.diag([1, 1, 1, 0]),
31353135
],
31363136
)
3137-
def test_matrix_rank_hermitian(self, data, dtype):
3137+
def test_hermitian(self, data, dtype):
31383138
a = data.astype(dtype)
31393139
a_dp = dpnp.array(a)
31403140

@@ -3151,7 +3151,7 @@ def test_matrix_rank_hermitian(self, data, dtype):
31513151
],
31523152
ids=["float", "0-D array", "1-D array"],
31533153
)
3154-
def test_matrix_rank_tolerance(self, high_tol, low_tol):
3154+
def test_tolerance(self, high_tol, low_tol):
31553155
a = numpy.eye(4)
31563156
a[-1, -1] = 1e-6
31573157
a_dp = dpnp.array(a)
@@ -3190,7 +3190,7 @@ def test_matrix_rank_tolerance(self, high_tol, low_tol):
31903190
[0.99e-6, numpy.array(1.01e-6), numpy.ones(4) * [0.99e-6]],
31913191
ids=["float", "0-D array", "1-D array"],
31923192
)
3193-
def test_matrix_rank_tol(self, tol):
3193+
def test_tol(self, tol):
31943194
a = numpy.zeros((4, 3, 2))
31953195
a_dp = dpnp.array(a)
31963196

@@ -3209,7 +3209,7 @@ def test_matrix_rank_tol(self, tol):
32093209
result = dpnp.linalg.matrix_rank(a_dp, tol=dp_tol)
32103210
assert_dtype_allclose(result, expected)
32113211

3212-
def test_matrix_rank_errors(self):
3212+
def test_errors(self):
32133213
a_dp = dpnp.array([[1, 2], [3, 4]], dtype="float32")
32143214

32153215
# unsupported type `a`
@@ -3238,6 +3238,41 @@ def test_matrix_rank_errors(self):
32383238
ValueError, dpnp.linalg.matrix_rank, a_dp, tol=1e-06, rtol=1e-04
32393239
)
32403240

3241+
# TODO: use below fixture when NumPy 2.5 is released
3242+
# @testing.with_requires("numpy>=2.5")
3243+
@pytest.mark.parametrize(
3244+
"shape",
3245+
[
3246+
(0, 0),
3247+
(0, 5),
3248+
(5, 0),
3249+
(0, 5, 5),
3250+
(3, 0, 5),
3251+
(2, 0, 0),
3252+
(2, 5, 0),
3253+
(2, 3, 0, 4),
3254+
],
3255+
)
3256+
def test_empty(self, shape):
3257+
a = numpy.zeros(shape)
3258+
ia = dpnp.array(a)
3259+
3260+
result = dpnp.linalg.matrix_rank(ia)
3261+
if numpy_version() < "2.5.0": # TODO: remove
3262+
# Expected behavior: rank of empty matrix is 0
3263+
# For stacked matrices, return array of zeros
3264+
expected = numpy.zeros(shape[:-2], dtype=numpy.intp)
3265+
if expected.ndim == 0:
3266+
expected = numpy.array(0)
3267+
else:
3268+
result = numpy.linalg.matrix_rank(a)
3269+
assert_array_equal(result, expected, strict=True)
3270+
3271+
# Also test with hermitian=True
3272+
if len(shape) >= 2 and shape[-2] == shape[-1]:
3273+
result = dpnp.linalg.matrix_rank(ia, hermitian=True)
3274+
assert_array_equal(result, expected, strict=True)
3275+
32413276

32423277
# numpy.linalg.matrix_transpose() is available since numpy >= 2.0
32433278
@testing.with_requires("numpy>=2.0")

0 commit comments

Comments
 (0)