Reputation: 1
I used %*% to multiple a matrix and its inverse. I don't get the identity matrix. What am i missing?
D
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 2 1
[3,] 2 2 0
solve(D)
[,1] [,2] [,3]
[1,] -0.1428571 0.4285714 -0.2857143
[2,] 0.1428571 -0.4285714 0.7857143
[3,] 0.2857143 0.1428571 -0.4285714
D %*% solve(D)
[,1] [,2] [,3]
[1,] 1.000000e+00 0.000000e+00 -2.220446e-16
[2,] -5.551115e-17 1.000000e+00 0.000000e+00
[3,] -1.110223e-16 -1.110223e-16 1.000000e+00
Upvotes: 0
Views: 990
Reputation: 83
As Frank mentioned, it's precision errors. Usually e-16 ish numbers and smaller are a good indicator this happens. Also consider
> 10/3-3-1/3
[1] 1.665335e-16
Clearly, we'd consider this to be 0.
In addition to r2evans's answer, the answer to this question has a far more detail about any language. Why are these numbers not equal?
Upvotes: 0
Reputation: 7796
You're not getting back exactly zero for the off-diagonals because of floating-point precision errors.
You can see that this really is the identity matrix if you round:
round(D %*% solve(D))
Upvotes: 5