[英]Find SVD of a symmetric matrix in Python
I know np.linalg.svd(A)
would return the SVD of matrix A.我知道
np.linalg.svd(A)
会返回矩阵 A 的 SVD。
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?在 R 中,我们可以使用
La.svd(A,nu=0)
但是否有任何函数可以加速 Python 中对称矩阵的 SVD 过程?
In SVD of symmetric matrices, U=V would be valid only if the eigenvalues of A are all positive.在对称矩阵的 SVD 中,只有当 A 的特征值都为正时,U=V 才有效。 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.否则 V 将几乎等于 U 但不完全等于 U,因为对应于负特征值的某些 V 列将是 U 对应列的负数。因此,
A=vT * np.diag(s) * v
仅当 A 的所有特征值都为正时才有效。
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.假设对称矩阵是实矩阵,奇异值分解的U矩阵就是特征向量矩阵本身,奇异值就是矩阵特征值的绝对值,V矩阵和U矩阵一样与负特征值对应的列是 U 列的负值。
And so, by finding out the eigenvalues and the eigenvectors, SVD can be computed.因此,通过找出特征值和特征向量,可以计算 SVD。
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.这将打印与 A 相同的矩阵。
(Note: I don't know whether it works for complex matrices or not.) (注意:我不知道它是否适用于复杂矩阵。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.