Add tests for gesv
@@ -177,3 +177,51 @@ class TestSyevj(unittest.TestCase):
self.assertEqual(v.shape, a.shape)
self.assertEqual(w.shape, a.shape[:-1])
+
+
+@testing.parameterize(*testing.product({
+ 'dtype': [numpy.float32, numpy.float64],
+ 'is_complex': [False, True],
+ 'n': [10, 100],
+ 'nrhs': [None, 1, 10],
+}))
+@attr.gpu
+class TestGesv(unittest.TestCase):
+ _tol = {'f': 1e-6, 'd': 1e-12}
+
+ def _make_array(self, shape, alpha, beta):
+ a = testing.shaped_random(shape, numpy, dtype=self.dtype, scale=alpha)
+ a += beta
+ return a
+
+ def _make_matrix(self, shape, alpha, beta):
+ a = self._make_array(shape, alpha, beta)
+ if self.is_complex:
+ a = a + 1j * self._make_array(shape, alpha, beta)
+ return a
+
+ def setUp(self):
+ if not cusolver.check_availability('gesv'):
+ pytest.skip('gesv is not available')
+
+ # Diagonally dominant matrix is used as it is stable
+ alpha = 2.0 / self.n
+ a = self._make_matrix((self.n, self.n), alpha, -alpha / 2)
+ a += numpy.diag(numpy.ones((self.n), dtype=self.dtype))
+ if self.nrhs is None:
+ x_shape = (self.n, )
+ else:
+ x_shape = (self.n, self.nrhs)
+ self.x_ref = self._make_matrix(x_shape, 0.9, 0.1)
+ b = numpy.dot(a, self.x_ref)
+ if self.dtype == numpy.float32:
+ self.tol = self._tol['f']
+ elif self.dtype == numpy.float64:
+ self.tol = self._tol['d']
+ self.a = cupy.array(a)
+ self.b = cupy.array(b)
+
+ def test_gesv(self):
+ x = cusolver.gesv(self.a, self.b)
+ cupy.testing.assert_allclose(x, self.x_ref,
+ rtol=self.tol, atol=self.tol)
