def test_rank_square(self): #Ranks of square matrices x = [[1.0, 2.0, 3.0, 4.0], [-5.0, -2.0, -1.0, 0.0]] self.assertEqual(svd.rank(mmul(transpose(x), x)), 2) x = [[1.0, 2.0, 3.0, 4.0]] self.assertEqual(svd.rank(mmul(transpose(x), x)), 1) x = [[1.0, 2.0, 3.0, 4.0], [1.0, 0.0, 0.0, 0.0], [-5.0, -2.0, -1.0, 0.0]] self.assertEqual(svd.rank(mmul(transpose(x), x)), 3)
def test_rank_square(self): #Ranks of square matrices x = [[1.0, 2.0, 3.0, 4.0], [-5.0, -2.0, -1.0, 0.0]] self.assertEqual( svd.rank( mmul( transpose(x), x) ), 2 ) x = [[1.0, 2.0, 3.0, 4.0] ] self.assertEqual( svd.rank( mmul( transpose(x), x) ), 1 ) x = [[1.0, 2.0, 3.0, 4.0], [1.0, 0.0,0.0,0.0], [-5.0, -2.0, -1.0, 0.0]] self.assertEqual( svd.rank( mmul( transpose(x), x) ), 3 )
def test_pinv_full_rank(self): """First check pinv on the matrices of full rank, when PINV is the same as INV""" 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]] self.assertEqual(svd.rank(M), 4) #Rank is full #print (svd.svd(M)[1]) IM = svd.pinv(M) #print (svd.svd(IM)[1]) IIM = svd.pinv(IM) #print ("----") #print (show_mat( mat_diff( M, IIM ))) #print (show_mat( mmul(M, IM) ) ) self.assertTrue(mat_eq(mmul(M, IM), eye(4), 1e-5)) #Inverse is right self.assertTrue(mat_eq(M, IIM, 1e-5)) #double pinv is the same as original
def test_pinv_full_rank(self): """First check pinv on the matrices of full rank, when PINV is the same as INV""" 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]] self.assertEqual( svd.rank(M), 4 ) #Rank is full #print (svd.svd(M)[1]) IM = svd.pinv(M) #print (svd.svd(IM)[1]) IIM = svd.pinv(IM) #print ("----") #print (show_mat( mat_diff( M, IIM ))) #print (show_mat( mmul(M, IM) ) ) self.assertTrue( mat_eq( mmul(M, IM), eye(4), 1e-5 ) ) #Inverse is right self.assertTrue( mat_eq( M, IIM, 1e-5 ) ) #double pinv is the same as original