Inverse of matrix and multiplication

耗尽温柔 提交于 2019-12-06 16:46:08

I'm guessing that Nick and Ben are both teachers and have even greater scruples than I do about doing other peoples' homework, but the path to a complete solution was really so glaringly obvious that it didn't make a lot of sense not to tae the next step:

B = solve(X) %*% G %*% A 
> B
             [,1]
[1,] -2.622000509
[2,]  7.566857261
[3,] 17.691911600
[4,]  2.318762273

The QR method of inversion can be invoked by supplying an identity matrix as the second argument:

> qr.solve(G, diag(1,4))
                [,1]             [,2]          [,3]             [,4]
[1,]  0.098084556856 -0.0087200426695 -0.3027373205 -0.0336789016478
[2,] -0.008720042669  4.4473233701790  1.7395207242 -0.0007717410073
[3,] -0.302737320546  1.7395207241703 13.9161591761  0.1483895429511
[4,] -0.033678901648 -0.0007717410073  0.1483895430  0.0166129089935

A more computationally stable solution is to use qr rather than solve.

method1 <- solve(X) %*% G %*% A
method2 <- qr.coef(qr(X), G) %*% A
stopifnot(isTRUE(all.equal(method1, method2)))

See the examples in ?qr.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!