Esempio n. 1
0
 def svd_unitary_test(self):
     """Test if matrices u and v are unitary."""
     a = [[22., 10.,   2.,   3.,  7.],
          [14.,  7.,  10.,   0.,  8.],
          [-1., 13., - 1., -11.,  3.],
          [-3., -2.,  13.,  -2.,  4.],
          [ 9.,  8.,   1.,  -2.,  4.],
          [ 9.,  1.,  -7.,   5., -1.],
          [ 2., -6.,   6.,   5.,  1.],
          [ 4.,  5.,   0.,  -2.,  2.]]
     matrix = Matrix(5, 8)
     matrix.initialize(a, rowBased=True)
     u, diag, v = matrix.svd()
     # u and v should be unitary matrices. Matrixmultiplication withs its
     # transformation should be the identity Matrix.
     res = u.transform() * u
     res1 = v * v.transform()
     for row in range(res.get_height()):
         for col in range(res.get_width()):
             if row == col:
                 # value should be 1 at diagonal
                 self.assertAlmostEqual(res.get_value(col, row), 1, PRECISION)
                 self.assertAlmostEqual(res1.get_value(col, row), 1, PRECISION)
             else:
                 # value should be 0 otherwise.
                 self.assertAlmostEqual(res.get_value(col, row), 0, PRECISION)
                 self.assertAlmostEqual(res1.get_value(col, row), 0, PRECISION)
Esempio n. 2
0
 def svd_unitary_test(self):
     """Test if matrices u and v are unitary."""
     a = [[22., 10., 2., 3., 7.], [14., 7., 10., 0., 8.],
          [-1., 13., -1., -11., 3.], [-3., -2., 13., -2., 4.],
          [9., 8., 1., -2., 4.], [9., 1., -7., 5., -1.],
          [2., -6., 6., 5., 1.], [4., 5., 0., -2., 2.]]
     matrix = Matrix(5, 8)
     matrix.initialize(a, rowBased=True)
     u, diag, v = matrix.svd()
     # u and v should be unitary matrices. Matrixmultiplication withs its
     # transformation should be the identity Matrix.
     res = u.transform() * u
     res1 = v * v.transform()
     for row in range(res.get_height()):
         for col in range(res.get_width()):
             if row == col:
                 # value should be 1 at diagonal
                 self.assertAlmostEqual(res.get_value(col, row), 1,
                                        PRECISION)
                 self.assertAlmostEqual(res1.get_value(col, row), 1,
                                        PRECISION)
             else:
                 # value should be 0 otherwise.
                 self.assertAlmostEqual(res.get_value(col, row), 0,
                                        PRECISION)
                 self.assertAlmostEqual(res1.get_value(col, row), 0,
                                        PRECISION)
Esempio n. 3
0
    def svd_diagional_test(self):
        """Test if the one Matrix of svd() is in diagonal form."""
        a = [[22., 10., 2., 3., 7.], [14., 7., 10., 0., 8.],
             [-1., 13., -1., -11., 3.], [-3., -2., 13., -2., 4.],
             [9., 8., 1., -2., 4.], [9., 1., -7., 5., -1.],
             [2., -6., 6., 5., 1.], [4., 5., 0., -2., 2.]]

        matrix = Matrix(5, 8)
        matrix.initialize(a, rowBased=True)
        u, diag, v = matrix.svd()
        # test if Matrix is in diagonal form
        for row in range(diag.get_height()):
            for col in range(diag.get_width()):
                if row != col:
                    self.assertEqual(diag.get_value(col, row), 0.0)
Esempio n. 4
0
 def svd_test(self):
     """Test the Singular Value Decomposition."""
     a = [[22., 10., 2., 3., 7.], [14., 7., 10., 0., 8.],
          [-1., 13., -1., -11., 3.], [-3., -2., 13., -2., 4.],
          [9., 8., 1., -2., 4.], [9., 1., -7., 5., -1.],
          [2., -6., 6., 5., 1.], [4., 5., 0., -2., 2.]]
     matrix = Matrix(5, 8)
     matrix.initialize(a, rowBased=True)
     u, diag, v = matrix.svd()
     # multiply result matrices should get the original matrix
     res = u * diag * v.transform()
     for row in range(res.get_height()):
         for col in range(res.get_width()):
             self.assertAlmostEqual(res.get_value(col, row), a[row][col],
                                    PRECISION)
Esempio n. 5
0
 def svd_test(self):
     """Test the Singular Value Decomposition."""
     a = [[22., 10.,   2.,   3.,  7.],
          [14.,  7.,  10.,   0.,  8.],
          [-1., 13., - 1., -11.,  3.],
          [-3., -2.,  13.,  -2.,  4.],
          [ 9.,  8.,   1.,  -2.,  4.],
          [ 9.,  1.,  -7.,   5., -1.],
          [ 2., -6.,   6.,   5.,  1.],
          [ 4.,  5.,   0.,  -2.,  2.]]
     matrix = Matrix(5, 8)
     matrix.initialize(a, rowBased=True)
     u, diag, v = matrix.svd()
     # multiply result matrices should get the original matrix
     res = u * diag * v.transform()
     for row in range(res.get_height()):
         for col in range(res.get_width()):
             self.assertAlmostEqual(res.get_value(col, row), a[row][col], PRECISION)
Esempio n. 6
0
    def svd_diagional_test(self):
        """Test if the one Matrix of svd() is in diagonal form."""
        a = [[22., 10.,  2.,   3.,  7.],
             [14.,  7., 10.,   0.,  8.],
             [-1., 13., -1., -11.,  3.],
             [-3., -2., 13.,  -2.,  4.],
             [ 9.,  8.,  1.,  -2.,  4.],
             [ 9.,  1., -7.,   5., -1.],
             [ 2., -6.,  6.,   5.,  1.],
             [ 4.,  5.,  0.,  -2.,  2.]]

        matrix = Matrix(5, 8)
        matrix.initialize(a, rowBased=True)
        u, diag, v = matrix.svd()
        # test if Matrix is in diagonal form
        for row in range(diag.get_height()):
            for col in range(diag.get_width()):
                if row != col:
                    self.assertEqual(diag.get_value(col, row), 0.0)