Add tests for cupy.cusolver.gels
@@ -230,3 +230,50 @@ class TestGesv(unittest.TestCase):
x = cusolver.gesv(self.a, self.b)
cupy.testing.assert_allclose(x, self.x_ref,
rtol=self.tol, atol=self.tol)
+
+
+@testing.parameterize(*testing.product({
+ 'dtype': [numpy.float32, numpy.float64, numpy.complex64, numpy.complex128],
+ 'shape': [(4, 4), (5, 4), (4, 5)],
+ 'nrhs': [None, 1, 4],
+}))
+@attr.gpu
+class TestGels(unittest.TestCase):
+ _tol = {'f': 1e-5, 'd': 1e-12}
+
+ def _make_array(self, shape):
+ a = testing.shaped_random(shape, cupy, dtype=self.r_dtype, scale=1)
+ return a
+
+ def _make_matrix(self, shape):
+ a = self._make_array(shape)
+ if self.dtype.char in 'FD':
+ a = a + 1j * self._make_array(shape)
+ return a
+
+ def setUp(self):
+ self.dtype = numpy.dtype(self.dtype)
+ if self.dtype.char in 'fF':
+ self.r_dtype = numpy.float32
+ else:
+ self.r_dtype = numpy.float64
+ m, n = self.shape
+ nrhs = 1 if self.nrhs is None else self.nrhs
+ a = self._make_matrix((m, n))
+ x = self._make_matrix((n, nrhs))
+ b = cupy.matmul(a, x)
+ b_shape = [m]
+ if self.nrhs is not None:
+ b_shape.append(nrhs)
+ self.a = a
+ self.b = b.reshape(b_shape)
+ if self.r_dtype == numpy.float32:
+ self.tol = self._tol['f']
+ elif self.r_dtype == numpy.float64:
+ self.tol = self._tol['d']
+
+ def test_gels(self):
+ x = cusolver.gels(self.a, self.b)
+ b = cupy.matmul(self.a, x)
+ cupy.testing.assert_allclose(b, self.b,
+ rtol=self.tol, atol=self.tol)
