in R programming, concerning on inverse matrix and its multiplication

戏子无情 提交于 2019-12-01 08:07:44

问题


this is my solving process from the exercise of [a beginner's guide to R]

> Q
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    2    1
[3,]    2    3    0
> solve(Q)
      [,1]  [,2]  [,3]
[1,] -0.12  0.36 -0.16
[2,]  0.08 -0.24  0.44
[3,]  0.32  0.04 -0.24
> solve(Q)%*%Q
     [,1]          [,2] [,3]
[1,]    1 -2.775558e-17    0
[2,]    0  1.000000e+00    0
[3,]    0  0.000000e+00    1

I wonder why I cannot get to the right answer that the identity matrix should come out.


回答1:


Use the zapsmall function on the final result. Due to floating point representation and rounding errors anything more than simple arithmatic (and even that sometimes) will result in values that are very close, but not exactly the same as what is expected. In this case you are seeing a value that has 16 0's after the decimal place before the first non-zero digit. The zapsmall function will convert these small, essentially 0, values to 0 so that what you see matches what you expect.




回答2:


This is not a programming error, this is the result of what's called "floating point arithmetic." rounded to a reasonable length, you will get the identity matrix:

Q2 <- solve(Q)%*%Q
round(Q2, 4)

To learn more on floating point arithmetic go here.



来源:https://stackoverflow.com/questions/15575240/in-r-programming-concerning-on-inverse-matrix-and-its-multiplication

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