Skip to content

Commit 9d7411b

Browse files
Update tests for dpnp.linalg.cholesky
1 parent 8565346 commit 9d7411b

File tree

2 files changed

+81
-37
lines changed

2 files changed

+81
-37
lines changed

tests/test_linalg.py

Lines changed: 80 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -44,44 +44,88 @@ def vvsort(val, vec, size, xp):
4444
vec[:, imax] = temp
4545

4646

47-
@pytest.mark.parametrize(
48-
"array",
49-
[
50-
[[[1, -2], [2, 5]]],
51-
[[[1.0, -2.0], [2.0, 5.0]]],
52-
[[[1.0, -2.0], [2.0, 5.0]], [[1.0, -2.0], [2.0, 5.0]]],
53-
],
54-
ids=[
55-
"[[[1, -2], [2, 5]]]",
56-
"[[[1., -2.], [2., 5.]]]",
57-
"[[[1., -2.], [2., 5.]], [[1., -2.], [2., 5.]]]",
58-
],
59-
)
60-
def test_cholesky(array):
61-
a = numpy.array(array)
62-
ia = inp.array(a)
63-
result = inp.linalg.cholesky(ia)
64-
expected = numpy.linalg.cholesky(a)
65-
assert_array_equal(expected, result)
47+
class TestCholesky:
48+
@pytest.mark.parametrize(
49+
"array",
50+
[
51+
[[1, 2], [2, 5]],
52+
[[[5, 2], [2, 6]], [[7, 3], [3, 8]], [[3, 1], [1, 4]]],
53+
[
54+
[[[5, 2], [2, 5]], [[6, 3], [3, 6]]],
55+
[[[7, 2], [2, 7]], [[8, 3], [3, 8]]],
56+
],
57+
],
58+
ids=[
59+
"2D_array",
60+
"3D_array",
61+
"4D_array",
62+
],
63+
)
64+
@pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True))
65+
def test_cholesky_3d_4d(self, array, dtype):
66+
a = numpy.array(array, dtype=dtype)
67+
ia = inp.array(a)
68+
result = inp.linalg.cholesky(ia)
69+
expected = numpy.linalg.cholesky(a)
70+
assert_dtype_allclose(result, expected)
6671

72+
def test_cholesky_strides(self):
73+
a_np = numpy.array(
74+
[
75+
[5, 2, 0, 0, 1],
76+
[2, 6, 0, 0, 2],
77+
[0, 0, 7, 0, 0],
78+
[0, 0, 0, 4, 0],
79+
[1, 2, 0, 0, 5],
80+
]
81+
)
6782

68-
@pytest.mark.parametrize(
69-
"shape",
70-
[
71-
(0, 0),
72-
(3, 0, 0),
73-
],
74-
ids=[
75-
"(0, 0)",
76-
"(3, 0, 0)",
77-
],
78-
)
79-
def test_cholesky_0D(shape):
80-
a = numpy.empty(shape)
81-
ia = inp.array(a)
82-
result = inp.linalg.cholesky(ia)
83-
expected = numpy.linalg.cholesky(a)
84-
assert_array_equal(expected, result)
83+
a_dp = inp.array(a_np)
84+
85+
# positive strides
86+
expected = numpy.linalg.cholesky(a_np[::2, ::2])
87+
result = inp.linalg.cholesky(a_dp[::2, ::2])
88+
assert_allclose(expected, result, rtol=1e-3, atol=1e-4)
89+
90+
# negative strides
91+
expected = numpy.linalg.cholesky(a_np[::-2, ::-2])
92+
result = inp.linalg.cholesky(a_dp[::-2, ::-2])
93+
assert_allclose(expected, result, rtol=1e-3, atol=1e-4)
94+
95+
@pytest.mark.parametrize(
96+
"shape",
97+
[
98+
(0, 0),
99+
(3, 0, 0),
100+
(0, 2, 2),
101+
],
102+
ids=[
103+
"(0, 0)",
104+
"(3, 0, 0)",
105+
"(0, 2, 2)",
106+
],
107+
)
108+
def test_cholesky_empty(self, shape):
109+
a = numpy.empty(shape)
110+
ia = inp.array(a)
111+
result = inp.linalg.cholesky(ia)
112+
expected = numpy.linalg.cholesky(a)
113+
assert_array_equal(expected, result)
114+
115+
def test_cholesky_errors(self):
116+
a_dp = inp.array([[1, 2], [2, 5]], dtype="float32")
117+
118+
# unsupported type
119+
a_np = inp.asnumpy(a_dp)
120+
assert_raises(TypeError, inp.linalg.cholesky, a_np)
121+
122+
# a.ndim < 2
123+
a_dp_ndim_1 = a_dp.flatten()
124+
assert_raises(inp.linalg.LinAlgError, inp.linalg.cholesky, a_dp_ndim_1)
125+
126+
# a is not square
127+
a_dp = inp.ones((2, 3))
128+
assert_raises(inp.linalg.LinAlgError, inp.linalg.cholesky, a_dp)
85129

86130

87131
@pytest.mark.parametrize(

tests/third_party/cupy/linalg_tests/test_decomposition.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class TestCholeskyInvalid(unittest.TestCase):
117117
def check_L(self, array):
118118
for xp in (numpy, cupy):
119119
a = xp.asarray(array)
120-
with pytest.raises((numpy.linalg.LinAlgError, ValueError)):
120+
with pytest.raises(xp.linalg.LinAlgError):
121121
xp.linalg.cholesky(a)
122122

123123
# TODO: remove skipif when MKLD-16626 is resolved

0 commit comments

Comments
 (0)