我比较了计算对称矩阵的逆的各种方法:
- 解决(来自 LAPACK 包)
- 解决(但使用更高的机器精度)
- qr.solve(据说更快)
- ginv(MASS 包,Moore-Penrose 算法的实现)
- chol2inv(使用 Cholesky 分解)
通过特征值比较逆矩阵:
R
library(MASS)
## Create the matrix
m = replicate(10, runif(n=10))
m[lower.tri(m)] = t(m)[lower.tri(m)]
## Inverse the matrix
inv1 = solve(m)
inv2 = solve(m, tol = .Machine$double.eps)
inv3 = qr.solve(m)
inv4 = ginv(m)
inv5 = chol2inv(m)
## Eigenvalues of the inverse
em1=eigen(inv1)
em2=eigen(inv2)
em3=eigen(inv3)
em4=eigen(inv4)
em5=eigen(inv5)
## Plot the abs of the eigenvalues (may be complex)
myPch=c( 20, 15, 17, 25, 3 )
plot(abs(em1$values),pch=myPch[1],cex=1.5)
points(abs(em2$values),pch=myPch[2], cex=1.5)
points(abs(em3$values),pch=myPch[3], cex=1.5)
points(abs(em4$values),pch=myPch[4], cex=1.5)
points(abs(em5$values),pch=myPch[5], cex=1.5)
legend( "topright", c("solve","solve-double","solve-fast","Moore-Penrose","Cholesky"), pch=myPch )
正如您所看到的,Cholesky 方法给出的逆函数与其他方法明显不同。
根据这篇文章,如果矩阵是对称的(在我们的例子中是),则首选 Cholesky 方法:矩阵求逆还是 Cholesky?
但solve()是“官方-wellspread”R方法来反转方法,我可能会误解一些东西......
有什么好的建议吗?
提前致谢,