简体   繁体   English

在R编程中,关于逆矩阵及其乘法

[英]in R programming, concerning on inverse matrix and its multiplication

this is my solving process from the exercise of [a beginner's guide to R] 这是我从[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. 我不知道为什么我不能就身份矩阵应该得出正确的答案。

Use the zapsmall function on the final result. 在最终结果上使用zapsmall函数。 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. 在这种情况下,您看到的值在第一个非零数字之前的小数点后有16 0。 The zapsmall function will convert these small, essentially 0, values to 0 so that what you see matches what you expect. zapsmall函数会将这些较小的值(基本上为0)转换为0,以便您看到的与您期望的匹配。

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 . 要了解有关浮点算法的更多信息, 请转到此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM