Add tests for cupy.cusparse.csrilu02
@@ -757,3 +757,64 @@ class TestCsrsm2(unittest.TestCase):
transa=self.transa, blocking=self.blocking,
level_info=self.level_info)
testing.assert_allclose(x, self.ref_x, atol=self.tol, rtol=self.tol)
+
+
+@testing.parameterize(*testing.product({
+ 'n': [5, 8],
+ 'level_info': [True, False],
+}))
+@testing.with_requires('scipy')
+class TestCsrilu02(unittest.TestCase):
+
+ _tol = {'f': 1e-5, 'd': 1e-12}
+
+ def _make_matrix(self, dtype):
+ if not cusparse.check_availability('csrilu02'):
+ unittest.SkipTest('csrilu02 is not available')
+ a = testing.shaped_random((self.n, self.n), cupy, dtype=dtype,
+ scale=0.9) + 0.1
+ a = a + cupy.diag(cupy.ones((self.n,), dtype=dtype.char.lower()))
+ return a
+
+ @testing.for_dtypes('fdFD')
+ def test_csrilu02(self, dtype):
+ dtype = numpy.dtype(dtype)
+ a_ref = self._make_matrix(dtype)
+ a = sparse.csr_matrix(a_ref)
+ cusparse.csrilu02(a, level_info=self.level_info)
+ a = a.todense()
+ al = cupy.tril(a, k=-1)
+ al = al + cupy.diag(cupy.ones((self.n,), dtype=dtype.char.lower()))
+ au = cupy.triu(a)
+ a = al @ au
+ tol = self._tol[dtype.char.lower()]
+ cupy.testing.assert_allclose(a, a_ref, atol=tol, rtol=tol)
+
+ def test_invalid_cases(self):
+ dtype = numpy.dtype('d')
+ a_ref = self._make_matrix(dtype)
+
+ # invalid format
+ a = sparse.csc_matrix(a_ref)
+ with self.assertRaises(TypeError):
+ cusparse.csrilu02(a, level_info=self.level_info)
+
+ # invalid shape
+ a = cupy.ones((self.n, self.n + 1), dtype=dtype)
+ a = sparse.csr_matrix(a)
+ with self.assertRaises(ValueError):
+ cusparse.csrilu02(a, level_info=self.level_info)
+
+ # matrix with zero diagonal element
+ a = a_ref
+ a[-1, -1] = 0
+ a = sparse.csr_matrix(a)
+ with self.assertRaises(ValueError):
+ cusparse.csrilu02(a, level_info=self.level_info)
+
+ # singular matrix
+ a = a_ref
+ a[1:] = a[0]
+ a = sparse.csr_matrix(a)
+ with self.assertRaises(ValueError):
+ cusparse.csrilu02(a, level_info=self.level_info)
