检查特征值是否等于实数的不同方法?
Different ways to check whether an eigenvalue equals a real number?
我有以下矩阵
M <- structure(c(0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 1, 0), .Dim = c(5L, 5L))
我想找到恰好为 1 的特征值。我认为这行得通:
Re(eigen(M)$values) == 1 & Im(eigen(M)$values) == 0
但 Re(eigen(M)$values) == 1
认为第 5 个特征值不等于 1,尽管它确实如此。我错过了什么?
编辑:一旦它被指出为浮点问题,我就会进行更多调查并找出几种解决方案,包括使用 abs(value) < tol
、all.equal
和 signif
.答案可以讨论这些选项吗?
您可能需要使用 abs
;这与浮点精度有关:
abs(Re(eigen(M)$values) - 1) < .001
在 R 中,在像 ==
这样的比较情况下处理浮点数的常用方法是使用函数 all.equal()
.
您可能想试试:
sapply(1:nrow(M), function(x) identical(all.equal(eigen(M)$values[x],1+0i),TRUE))
[1] FALSE FALSE TRUE FALSE TRUE
通常有导致相同结果的解决方法(round
、signif
...),但最好使用 all.equal()
,因为它是一个旨在处理浮点数的比较,无需调整精度阈值或指定有效位数。
如?Comparison
所述:
For numerical and complex values, remember == and != do not allow for the finite representation of fractions, nor for rounding error. Using all.equal with identical is almost always preferable.
我有以下矩阵
M <- structure(c(0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 1, 0), .Dim = c(5L, 5L))
我想找到恰好为 1 的特征值。我认为这行得通:
Re(eigen(M)$values) == 1 & Im(eigen(M)$values) == 0
但 Re(eigen(M)$values) == 1
认为第 5 个特征值不等于 1,尽管它确实如此。我错过了什么?
编辑:一旦它被指出为浮点问题,我就会进行更多调查并找出几种解决方案,包括使用 abs(value) < tol
、all.equal
和 signif
.答案可以讨论这些选项吗?
您可能需要使用 abs
;这与浮点精度有关:
abs(Re(eigen(M)$values) - 1) < .001
在 R 中,在像 ==
这样的比较情况下处理浮点数的常用方法是使用函数 all.equal()
.
您可能想试试:
sapply(1:nrow(M), function(x) identical(all.equal(eigen(M)$values[x],1+0i),TRUE))
[1] FALSE FALSE TRUE FALSE TRUE
通常有导致相同结果的解决方法(round
、signif
...),但最好使用 all.equal()
,因为它是一个旨在处理浮点数的比较,无需调整精度阈值或指定有效位数。
如?Comparison
所述:
For numerical and complex values, remember == and != do not allow for the finite representation of fractions, nor for rounding error. Using all.equal with identical is almost always preferable.