Esempio n. 1
0
 def test_svd(self):
     #Test that SVD works
     #Take a sample matrix:
     M = [[1.0, 2.0, 3.0, 4.0], [3.0, 5.0, -1.0, 0.0],
          [2.0, -2.0, 1.0, 1.0], [-5.0, -2.0, -1.0, 0.0]]
     #And calculate it's bidiagonal transform:
     U, s, V = svd.svd(M)
     #ENsure that decomposition is correct.
     M1 = reduce(mmul, [U, from_diag(s), V])
     self.assertTrue(mat_eq(M, M1, 1e-5))
     #Ensure that matrices are orthogonal
     self.assertOrthogonal(U)
     self.assertOrthogonal(V)
Esempio n. 2
0
 def test_svd(self):
     #Test that SVD works
     #Take a sample matrix:
     M = [[1.0, 2.0, 3.0, 4.0],
          [3.0, 5.0, -1.0, 0.0],
          [2.0, -2.0, 1.0, 1.0],
          [-5.0, -2.0, -1.0, 0.0]]
     #And calculate it's bidiagonal transform:
     U,s,V = svd.svd( M )
     #ENsure that decomposition is correct.
     M1 = reduce( mmul, [U, from_diag(s), V] )
     self.assertTrue( mat_eq( M, M1, 1e-5 ) )
     #Ensure that matrices are orthogonal
     self.assertOrthogonal( U )
     self.assertOrthogonal( V )
Esempio n. 3
0
 def test_svd_singular(self):
     #Let's make a singular matrix M:
     x = [[1.0, 2.0, 3.0, 4.0], [-5.0, -2.0, -1.0, 0.0]]
     M = mmul(transpose(x), x)
     #And see, what it's SVD looks like:
     U, s, V = svd.svd(M)
     #ensure that rank is correct
     self.assertTrue(sum(1 if abs(si) > 1e-5 else 0
                         for si in s) == 2)  #rank must be 2
     #ENsure that decomposition is correct.
     M1 = reduce(mmul, [U, from_diag(s), V])
     self.assertTrue(mat_eq(M, M1, 1e-5))
     #Ensure that matrices are orthogonal
     self.assertOrthogonal(U)
     self.assertOrthogonal(V)
Esempio n. 4
0
 def test_svd_singular(self):
     #Let's make a singular matrix M:
     x = [[1.0, 2.0, 3.0, 4.0],
          [-5.0, -2.0, -1.0, 0.0]]
     M = mmul(transpose(x), x)
     #And see, what it's SVD looks like:
     U,s,V = svd.svd(M)
     #ensure that rank is correct
     self.assertTrue( sum( 1 if abs(si) > 1e-5 else 0
                           for si in s ) == 2 ) #rank must be 2
     #ENsure that decomposition is correct.
     M1 = reduce( mmul, [U, from_diag(s), V] )
     self.assertTrue( mat_eq( M, M1, 1e-5 ) )
     #Ensure that matrices are orthogonal
     self.assertOrthogonal( U )
     self.assertOrthogonal( V )
Esempio n. 5
0
    """
    eps = gmpy.mpf(2, prec) ** (-prec + 3)
    return NumericContext(
        one=gmpy.mpf(1,prec),
        zero=gmpy.mpf(0, prec),
        fabs=abs,
        sqrt=gmpy.fsqrt,
        from_int = lambda x: gmpy.mpf(x, prec),
        eps = eps
        )


if __name__=="__main__":
    import pyla.core as pylinalg
    from pyla import svd

    context = GMPYContext(300)
    
    m = pylinalg.to_context_mat(pylinalg.rand_mat(4,4),
                                context=context)

    u,s,v = svd.svd( m, context=context, tol = context.from_int(1e-150) )
    print (pylinalg.show_mat(u))
    print (pylinalg.show_mat(v))
    print (s)

    m1 = reduce( pylinalg.mmul, [u, pylinalg.from_diag(s,context=context),
                                 v] )
    print ("-------------------------------------")
    print (pylinalg.show_mat( pylinalg.mat_diff( m, m1 ) ))