Esempio n. 1
0
 def test_streaming_gram_schmidt(self):
     X0 = numpy.array([[1, 0.5, 10., 5., -2.], [0, 0.4, 20, 4., 2.],
                       [0, 0.4, 19, 4.2, 2.], [0, 0.7, 18, 4.1, 1.4]],
                      dtype=float).T
     for dim in (3, 2, 4):
         X = X0[:, :dim]
         Xt = X.T
         algo1 = []
         algo1_t = []
         idd = numpy.identity(dim)
         for i in range(dim, Xt.shape[1] + 1):
             t, p = gram_schmidt(Xt[:, :i], change=True)
             algo1_t.append(t)
             algo1.append(p)
             t_ = X[:i] @ p.T
             self.assertEqualArray(t @ t.T, idd)
             self.assertEqualArray(t_.T @ t_, idd)
         algo2 = []
         self.assertRaise(
             lambda: list(streaming_gram_schmidt(X)),  # pylint: disable=W0640
             RuntimeError)
         for i, p in enumerate(streaming_gram_schmidt(Xt)):
             p2 = p.copy()
             t2 = X[:i + dim] @ p2.T
             algo2.append(p2)
             self.assertNotEmpty(t2)
             self.assertNotEmpty(p2)
             self.assertEqualArray(t2.T @ t2, idd)
         self.assertEqual(len(algo1), len(algo2))
Esempio n. 2
0
    def test_gram_schmidt(self):
        mat1 = numpy.array([[1, 0], [0, 1]], dtype=float)
        res = gram_schmidt(mat1)
        self.assertEqual(res, mat1)

        mat = numpy.array([[1, 0.5], [0.5, 1]], dtype=float)
        res = gram_schmidt(mat)
        self.assertEqualArray(mat1, res @ res.T)
        res2 = gram_schmidt(mat.T).T

        res2, change2 = gram_schmidt(mat, change=True)
        self.assertEqual(res, res2)
        res3 = change2 @ mat
        self.assertEqual(res3, res2)

        mat1 = numpy.array([[1, 0, 0], [0, 0, 1]], dtype=float)
        res = gram_schmidt(mat1)
        self.assertEqual(res, mat1)

        mat1 = numpy.array([[1, 0.5, 0], [0, 0.5, 1]], dtype=float)
        res = gram_schmidt(mat1)
        self.assertEqual(res[0, 2], 0)

        mat1 = numpy.array([[1, 0.5, 0], [0, 0.5, 1], [1, 0.5, 1]],
                           dtype=float)
        res = gram_schmidt(mat1)
        self.assertEqualArray(numpy.identity(3), res @ res.T)
Esempio n. 3
0
    def test_gram_schmidt(self):
        mat1 = numpy.array([[1, 0], [0, 1]], dtype=float)
        res = gram_schmidt(mat1)
        self.assertEqual(res, mat1)

        mat = numpy.array([[1, 0.5], [0.5, 1]], dtype=float)
        res = gram_schmidt(mat)
        self.assertEqualArray(mat1, res @ res.T)
        res2 = gram_schmidt(mat.T).T

        res2, change2 = gram_schmidt(mat, change=True)
        self.assertEqual(res, res2)
        res3 = change2 @ mat
        self.assertEqual(res3, res2)

        mat1 = numpy.array([[1, 0, 0], [0, 0, 1]], dtype=float)
        res = gram_schmidt(mat1)
        self.assertEqual(res, mat1)

        mat1 = numpy.array([[1, 0.5, 0], [0, 0.5, 1]], dtype=float)
        res = gram_schmidt(mat1)
        self.assertEqual(res[0, 2], 0)

        mat1 = numpy.array(
            [[1, 0.5, 0], [0, 0.5, 1], [1, 0.5, 1]], dtype=float)
        res = gram_schmidt(mat1)
        self.assertEqualArray(numpy.identity(3), res @ res.T)
Esempio n. 4
0
 def test_gram_schmidt_xx(self):
     X = numpy.array([[1, 0.5, 0], [0, 0.4, 2]], dtype=float).T
     T, P = gram_schmidt(X.T, change=True)
     P = P.T
     T = T.T
     m = P.T @ X.T
     z = m @ m.T
     self.assertEqual(z, numpy.identity(2))
     m = X @ P
     self.assertEqual(m, T)
     z2 = m.T @ m
     self.assertEqual(z2, numpy.identity(2))
Esempio n. 5
0
 def test_gram_schmidt_xx(self):
     X = numpy.array([[1, 0.5, 0], [0, 0.4, 2]], dtype=float).T
     U, P = gram_schmidt(X.T, change=True)
     P = P.T
     U = U.T
     m = P.T @ X.T
     z = m @ m.T
     self.assertEqual(z, numpy.identity(2))
     m = X @ P
     self.assertEqual(m, U)
     z2 = m.T @ m
     self.assertEqual(z2, numpy.identity(2))