简体   繁体   中英

Find SVD of a symmetric matrix in Python

I know np.linalg.svd(A) would return the SVD of matrix A.

A=u * np.diag(s) * v

However if it is a symmetric matrix you only need one unitary matrix:

A=v.T * np.diag(s) * v

In R we can use La.svd(A,nu=0) but is there any functions to accelerate the SVD process in Python for a symmetric matrix?

In SVD of symmetric matrices, U=V would be valid only if the eigenvalues of A are all positive. Otherwise V would be almost equal to U but not exactly equal to U, as some the columns of V corresponding to negative eigenvalues would be negatives of the corresponding columns of U. And so, A=vT * np.diag(s) * v is valid only if A has all its eigenvalues positive.

Assuming the symmetric matrix is a real matrix , the U matrix of singular value decomposition is the eigenvector matrix itself, and the singular values would be absolute values of the eigenvalues of the matrix, and the V matrix would be the same as U matrix except that the columns corresponding to negative eigenvalues are the negative values of the columns of U.

And so, by finding out the eigenvalues and the eigenvectors, SVD can be computed.

Python code:

import numpy as np

def svd_symmetric(A):
    [s,u] = np.linalg.eig(A)  #eigenvalues and eigenvectors
    v = u.copy()
    v[:,s<0] = -u[:,s<0] #replacing the corresponding columns with negative sign
    s = abs(s)
    return [u, s, v.T]


n = 5
A = np.matrix(np.random.rand(n,n)) # a matrix of random numbers
A = A.T+A #making it a symmetric matrix
[u, s, vT] = svd_symmetric(A)
print(u * np.diag(s) * vT)

This prints the same matrix as A.

(Note: I don't know whether it works for complex matrices or not.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM