Example #1
0
    def test_generate_matrix(self):
        dtype = self.dtype
        x_shape, s_shape = self.x_s_shapes
        sv = self.xp.random.uniform(0.5, 1.5,
                                    s_shape).astype(dtype().real.dtype)
        x = testing.generate_matrix(x_shape,
                                    xp=self.xp,
                                    dtype=dtype,
                                    singular_values=sv)
        assert x.shape == x_shape

        if 0 in x_shape:
            return

        s = self.xp.linalg.svd(
            x.astype(numpy.complex128),
            full_matrices=False,
            compute_uv=False,
        )
        sv = self.xp.broadcast_to(sv, s.shape)
        sv_sorted = self.xp.sort(sv, axis=-1)[..., ::-1]

        rtol = 1e-3 if dtype == numpy.float16 else 1e-7
        self.xp.testing.assert_allclose(s, sv_sorted, rtol=rtol)
Example #2
0
 def test_shape_mismatch_2(self):
     with self.assertRaises(numpy.linalg.LinAlgError):
         testing.generate_matrix((0, 2, 2), singular_values=numpy.ones(3))
Example #3
0
 def test_shape_mismatch(self):
     with self.assertRaises(ValueError):
         testing.generate_matrix((2, 2), singular_values=numpy.ones(3))
Example #4
0
 def test_negative_singular_values(self):
     with self.assertRaises(ValueError):
         testing.generate_matrix((2, 2), singular_values=[1, -1])
Example #5
0
 def test_invalid_dtype(self):
     with self.assertRaises(TypeError):
         testing.generate_matrix((2, 2),
                                 dtype=numpy.int32,
                                 singular_values=1)
Example #6
0
 def test_invalid_dtype_singular_values(self):
     with self.assertRaises(TypeError):
         testing.generate_matrix((2, 2), singular_values=1 + 0j)
Example #7
0
 def test_invalid_shape(self):
     with self.assertRaises(ValueError):
         testing.generate_matrix((2, ), singular_values=1)
Example #8
0
 def test_no_singular_values(self):
     with self.assertRaises(TypeError):
         testing.generate_matrix((2, 2))