为什么 C++ Armadillo 的某些特征向量符号与 Python 和 R 不同
Why some eigen vector signs from C++ Armadillo are different from Python and R
我想知道为什么 Armadillo 的特征向量中元素的符号与 Python(即 numpy)和 R.
等其他语言相反
例如:
C++
using namespace arma;
vec eigval;
mat eigvec;
// C++11 initialization
mat A = { 1, -1, 0, -1, 2, -1, 0, -1, 1};
eig_sym(eigval, eigvec, A);
eigvec.print("Eigen Vectors");
输出
Eigen Vectors
-5.7735e-01 -7.071068e-01 0.4082483
-5.7735e-01 9.714451e-e17 -0.8164966
-5.7735e-01 7.017068e-01 0.4082483
Python
import numpy as np
w,v = np.linalg.eig(np.array([[1,-1,0],[-1,2,-1],[0,-1,1]]))
v
输出
array([[ -4.08248290e-01, -7.07106781e-01, 5.77350269e-01],
[ 8.16496581e-01, 2.61214948e-16, 5.77350269e-01],
[ -4.08248290e-01, 7.07106781e-01, 5.77350269e-01]])
R
eigen(matrix(c(1,-1,0,-1,2,-1,0,-1,1), 3, byrow=TRUE)$vectors
输出
-4.082483e-01 -7.071068e-01 5.773503e-01
8.164966e-01 9.420555e-16 5.773503e-01
-4.082483e-01 7.071068e-01 5.773503e-01
可以看到Python和R提供了相同的特征向量(不包括舍入误差)。犰狳结果确实提供了相同的数字(顺序是一个简单的修复),但第一列和第三列的符号与 Python 和 R 中的相应列相反。我在这里忽略了什么吗?
help(eigen)
在 R 中回答:
Value:
The spectral decomposition of ‘x’ is returned as components of a
list with components
values: a vector containing the p eigenvalues of ‘x’, sorted in
_decreasing_ order, according to ‘Mod(values)’ in the
asymmetric case when they might be complex (even for real
matrices). For real asymmetric matrices the vector will be
complex only if complex conjugate pairs of eigenvalues are
detected.
vectors: either a p * p matrix whose columns contain the eigenvectors
of ‘x’, or ‘NULL’ if ‘only.values’ is ‘TRUE’. The vectors
are normalized to unit length.
Recall that the eigenvectors are only defined up to a
constant: even when the length is specified they are still
only defined up to a scalar of modulus one (the sign for real
matrices).
所以符号是一个'free'参数,结果是真正等价的。如果是我,我会跟随 R 和 Python 但康拉德通常知道他在做什么。
你没有使用好函数(你的函数是针对对称矩阵的)
A << 1. << -1. << 0. <<endr
<< -1. << 2. << -1. <<endr
<< 0. << -1. << 1. <<endr;
A.print("A :");
eig_gen(eigval, eigvec, A);
eigval.print("eigval");
eigvec.print("eigvec");
和输出:
A :
1.0000 -1.0000 0
-1.0000 2.0000 -1.0000
0 -1.0000 1.0000
eigval
(+3.000e+00,+0.000e+00)
(+1.000e+00,+0.000e+00)
(-3.368e-17,+0.000e+00)
eigvec
(-4.082e-01,+0.000e+00) (-7.071e-01,+0.000e+00) (+5.774e-01,+0.000e+00)
(+8.165e-01,+0.000e+00) (+2.612e-16,+0.000e+00) (+5.774e-01,+0.000e+00)
(-4.082e-01,+0.000e+00) (+7.071e-01,+0.000e+00) (+5.774e-01,+0.000e+00)
我想知道为什么 Armadillo 的特征向量中元素的符号与 Python(即 numpy)和 R.
等其他语言相反例如:
C++
using namespace arma;
vec eigval;
mat eigvec;
// C++11 initialization
mat A = { 1, -1, 0, -1, 2, -1, 0, -1, 1};
eig_sym(eigval, eigvec, A);
eigvec.print("Eigen Vectors");
输出
Eigen Vectors
-5.7735e-01 -7.071068e-01 0.4082483
-5.7735e-01 9.714451e-e17 -0.8164966
-5.7735e-01 7.017068e-01 0.4082483
Python
import numpy as np
w,v = np.linalg.eig(np.array([[1,-1,0],[-1,2,-1],[0,-1,1]]))
v
输出
array([[ -4.08248290e-01, -7.07106781e-01, 5.77350269e-01],
[ 8.16496581e-01, 2.61214948e-16, 5.77350269e-01],
[ -4.08248290e-01, 7.07106781e-01, 5.77350269e-01]])
R
eigen(matrix(c(1,-1,0,-1,2,-1,0,-1,1), 3, byrow=TRUE)$vectors
输出
-4.082483e-01 -7.071068e-01 5.773503e-01
8.164966e-01 9.420555e-16 5.773503e-01
-4.082483e-01 7.071068e-01 5.773503e-01
可以看到Python和R提供了相同的特征向量(不包括舍入误差)。犰狳结果确实提供了相同的数字(顺序是一个简单的修复),但第一列和第三列的符号与 Python 和 R 中的相应列相反。我在这里忽略了什么吗?
help(eigen)
在 R 中回答:
Value:
The spectral decomposition of ‘x’ is returned as components of a
list with components
values: a vector containing the p eigenvalues of ‘x’, sorted in
_decreasing_ order, according to ‘Mod(values)’ in the
asymmetric case when they might be complex (even for real
matrices). For real asymmetric matrices the vector will be
complex only if complex conjugate pairs of eigenvalues are
detected.
vectors: either a p * p matrix whose columns contain the eigenvectors
of ‘x’, or ‘NULL’ if ‘only.values’ is ‘TRUE’. The vectors
are normalized to unit length.
Recall that the eigenvectors are only defined up to a
constant: even when the length is specified they are still
only defined up to a scalar of modulus one (the sign for real
matrices).
所以符号是一个'free'参数,结果是真正等价的。如果是我,我会跟随 R 和 Python 但康拉德通常知道他在做什么。
你没有使用好函数(你的函数是针对对称矩阵的)
A << 1. << -1. << 0. <<endr
<< -1. << 2. << -1. <<endr
<< 0. << -1. << 1. <<endr;
A.print("A :");
eig_gen(eigval, eigvec, A);
eigval.print("eigval");
eigvec.print("eigvec");
和输出:
A :
1.0000 -1.0000 0
-1.0000 2.0000 -1.0000
0 -1.0000 1.0000
eigval
(+3.000e+00,+0.000e+00)
(+1.000e+00,+0.000e+00)
(-3.368e-17,+0.000e+00)
eigvec
(-4.082e-01,+0.000e+00) (-7.071e-01,+0.000e+00) (+5.774e-01,+0.000e+00)
(+8.165e-01,+0.000e+00) (+2.612e-16,+0.000e+00) (+5.774e-01,+0.000e+00)
(-4.082e-01,+0.000e+00) (+7.071e-01,+0.000e+00) (+5.774e-01,+0.000e+00)