Skip to content

Commit 6711bbd

Browse files
Avoid generating singular matrices in TestLuFactor/TestLuFactorBatched (#2599)
This PR suggests adding `_make_nonsingular_np` and ``_make_nonsingular_nd_np functions to `TestLuFactor` and `TestLuFactorBatched` to ensure the generation of non-singular matrices and avoid warnings in test log
1 parent 9e9c446 commit 6711bbd

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

dpnp/tests/test_linalg.py

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,6 +1875,16 @@ def _apply_pivots_rows(A_dp, piv_dp):
18751875
rows = dpnp.asarray(rows)
18761876
return A_dp[rows]
18771877

1878+
@staticmethod
1879+
def _make_nonsingular_np(shape, dtype, order):
1880+
A = generate_random_numpy_array(shape, dtype, order)
1881+
m, n = shape
1882+
k = min(m, n)
1883+
for i in range(k):
1884+
off = numpy.sum(numpy.abs(A[i, :n])) - numpy.abs(A[i, i])
1885+
A[i, i] = A.dtype.type(off + 1.0)
1886+
return A
1887+
18781888
@staticmethod
18791889
def _split_lu(lu, m, n):
18801890
L = dpnp.tril(lu, k=-1)
@@ -1889,7 +1899,7 @@ def _split_lu(lu, m, n):
18891899
@pytest.mark.parametrize("order", ["C", "F"])
18901900
@pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True))
18911901
def test_lu_factor(self, shape, order, dtype):
1892-
a_np = generate_random_numpy_array(shape, dtype, order)
1902+
a_np = self._make_nonsingular_np(shape, dtype, order)
18931903
a_dp = dpnp.array(a_np, order=order)
18941904

18951905
lu, piv = dpnp.linalg.lu_factor(
@@ -1991,12 +2001,7 @@ def test_empty_inputs(self, shape):
19912001
],
19922002
)
19932003
def test_strided(self, sl):
1994-
base = (
1995-
numpy.arange(7 * 7, dtype=dpnp.default_float_type()).reshape(
1996-
7, 7, order="F"
1997-
)
1998-
+ 0.1
1999-
)
2004+
base = self._make_nonsingular_np((7, 7), dpnp.default_float_type(), "F")
20002005
a_np = base[sl]
20012006
a_dp = dpnp.array(a_np)
20022007

@@ -2037,6 +2042,22 @@ def _apply_pivots_rows(A_dp, piv_dp):
20372042
rows = dpnp.asarray(rows)
20382043
return A_dp[rows]
20392044

2045+
@staticmethod
2046+
def _make_nonsingular_nd_np(shape, dtype, order):
2047+
A = generate_random_numpy_array(shape, dtype, order)
2048+
m, n = shape[-2], shape[-1]
2049+
k = min(m, n)
2050+
A3 = A.reshape((-1, m, n))
2051+
for B in A3:
2052+
for i in range(k):
2053+
off = numpy.sum(numpy.abs(B[i, :n])) - numpy.abs(B[i, i])
2054+
B[i, i] = A.dtype.type(off + 1.0)
2055+
2056+
A = A3.reshape(shape)
2057+
# Ensure reshapes did not break memory order
2058+
A = numpy.array(A, order=order)
2059+
return A
2060+
20402061
@staticmethod
20412062
def _split_lu(lu, m, n):
20422063
L = dpnp.tril(lu, k=-1)
@@ -2053,7 +2074,7 @@ def _split_lu(lu, m, n):
20532074
@pytest.mark.parametrize("order", ["C", "F"])
20542075
@pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True))
20552076
def test_lu_factor_batched(self, shape, order, dtype):
2056-
a_np = generate_random_numpy_array(shape, dtype, order)
2077+
a_np = self._make_nonsingular_nd_np(shape, dtype, order)
20572078
a_dp = dpnp.array(a_np, order=order)
20582079

20592080
lu, piv = dpnp.linalg.lu_factor(
@@ -2077,7 +2098,8 @@ def test_lu_factor_batched(self, shape, order, dtype):
20772098
@pytest.mark.parametrize("dtype", get_float_complex_dtypes())
20782099
@pytest.mark.parametrize("order", ["C", "F"])
20792100
def test_overwrite(self, dtype, order):
2080-
a_dp = dpnp.arange(2 * 2 * 3, dtype=dtype).reshape(3, 2, 2, order=order)
2101+
a_np = self._make_nonsingular_nd_np((3, 2, 2), dtype, order)
2102+
a_dp = dpnp.array(a_np, order=order)
20812103
a_dp_orig = a_dp.copy()
20822104
lu, piv = dpnp.linalg.lu_factor(
20832105
a_dp, overwrite_a=True, check_finite=False
@@ -2108,13 +2130,11 @@ def test_empty_inputs(self, shape):
21082130
assert piv.shape == (*shape[:-2], min(m, n))
21092131

21102132
def test_strided(self):
2111-
a = (
2112-
dpnp.arange(5 * 3 * 3, dtype=dpnp.default_float_type()).reshape(
2113-
5, 3, 3, order="F"
2114-
)
2115-
+ 0.1
2133+
a_np = self._make_nonsingular_nd_np(
2134+
(5, 3, 3), dpnp.default_float_type(), "F"
21162135
)
2117-
a_stride = a[::2]
2136+
a_dp = dpnp.array(a_np, order="F")
2137+
a_stride = a_dp[::2]
21182138
lu, piv = dpnp.linalg.lu_factor(a_stride, check_finite=False)
21192139
for i in range(a_stride.shape[0]):
21202140
L, U = self._split_lu(lu[i], 3, 3)

0 commit comments

Comments
 (0)