matrix inversion R

微笑、不失礼 提交于 2019-12-04 15:49:39

For solve you need to give your original matrix, but for chol2inv you use precomputed cholesky decomposition:

set.seed(1)
a<-crossprod(matrix(rnorm(9),3,3))
a_chol<-chol(a)
solve(a)
            [,1]        [,2]       [,3]
[1,]  1.34638151 -0.02957435  0.8010735
[2,] -0.02957435  0.32780020 -0.1786295
[3,]  0.80107345 -0.17862950  1.4533671
chol2inv(a_chol)
            [,1]        [,2]       [,3]
[1,]  1.34638151 -0.02957435  0.8010735
[2,] -0.02957435  0.32780020 -0.1786295
[3,]  0.80107345 -0.17862950  1.4533671

Here are several ways to compute matrix inverse, including solve() and chol2inv():

> A <- matrix(c(2, -1, 0, -1, 2, -1, 0, -1, 2), 3)

> solve(A)
     [,1] [,2] [,3]
[1,] 0.75  0.5 0.25
[2,] 0.50  1.0 0.50
[3,] 0.25  0.5 0.75

> chol2inv(chol(A))
     [,1] [,2] [,3]
[1,] 0.75  0.5 0.25
[2,] 0.50  1.0 0.50
[3,] 0.25  0.5 0.75

> library(MASS)
> ginv(A)
     [,1] [,2] [,3]
[1,] 0.75  0.5 0.25
[2,] 0.50  1.0 0.50
[3,] 0.25  0.5 0.75
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!