SVD example

759 days ago by jhlee2chn

A=matrix([[sqrt(3), 2], [0, sqrt(3)]]) B=A.transpose()*A eig=B.eigenvalues() sv=[sqrt(i) for i in eig] # Computing singular values print sv print B.eigenvectors_right() # Computing eigenvectors of 
       
[3, 1]
[(9, [(1, sqrt(3))], 1), (1, [(1, -1/3*sqrt(3))], 1)]
[3, 1]
[(9, [(1, sqrt(3))], 1), (1, [(1, -1/3*sqrt(3))], 1)]
G=matrix([[1, sqrt(3)], [1, -1/3*sqrt(3)]]) Vh=matrix([1/G.row(j).norm()*G.row(j) for j in range(0,2)]) Vh=Vh.simplify() # Transpose of V print Vh 
       
[        1/2 1/2*sqrt(3)]
[1/2*sqrt(3)        -1/2]
[        1/2 1/2*sqrt(3)]
[1/2*sqrt(3)        -1/2]
C=A*A.transpose() print C.eigenvectors_right() # Computing eigenvectors of 
       
[(9, [(1, 1/3*sqrt(3))], 1), (1, [(1, -sqrt(3))], 1)]
[(9, [(1, 1/3*sqrt(3))], 1), (1, [(1, -sqrt(3))], 1)]
F=matrix([[1, 1/3*sqrt(3)], [1, -sqrt(3)]]) U=matrix([1/F.row(j).norm()*F.row(j) for j in range(0,2)]) U=U.simplify().transpose() # U print U 
       
[ 1/2*sqrt(3)          1/2]
[         1/2 -1/2*sqrt(3)]
[ 1/2*sqrt(3)          1/2]
[         1/2 -1/2*sqrt(3)]
S=diagonal_matrix(sv); S 
       
[3 0]
[0 1]
[3 0]
[0 1]
U*S*Vh 
       
[sqrt(3)       2]
[      0 sqrt(3)]
[sqrt(3)       2]
[      0 sqrt(3)]