C ++线性代数库armadillo:如何使用eig_pair获得与Matlab中的eig函数相同的结果? [英] C++ linear algebra library armadillo : how to use eig_pair to get the same result as eig function in Matlab?

查看:340
本文介绍了C ++线性代数库armadillo:如何使用eig_pair获得与Matlab中的eig函数相同的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用eig_pair对具有相同大小的一对一般密集方矩阵A和B进行特征分解,使得A * eigvec = B * eigvec * diagmat(eigval),但结果与Matlab函数图.例如:

I try to use eig_pair to get Eigen decomposition for pair of general dense square matrices A and B of the same size, such that A*eigvec = B*eigvec*diagmat(eigval), but the result doesn't match the Matlab function eig. for example:

A= [1,2;3,4]     B=[2,4;5,8]

在Matlab中:

[u,v] = eig(A,B)

结果:

u =

 -1.0000   -0.0000

 0.5000   -1.0000

v =

    1.0000         0
         0    0.5000

犰狳:

eig_pair(v,u,A,B)

结果: 你:

9.9301e-016     -1.0000e+000

1.0000e+000      5.0000e-001

v:

0.5000

1.0000

我的问题是:如何获取与Matlab中的结果相匹配的u和v的值? 期待您的回复!

My question is: how to get the values of u and v that match the results in Matlab? Looking forwards to your reply!!!

推荐答案

特征向量不是唯一的.如果 u 是一个特征向量,则所有 m!= 0的 m * u 也是本征向量em>.此外,在Matlab中eig返回特征向量的顺序是任意的. (我不知道Armadillo返回特征向量的顺序.)您可以尝试通过对特征值进行排序来为特征向量创建规范的顺序,但是如果您具有复杂的特征值,这将是一个问题. (回想一下,真实矩阵可以具有复杂的特征值.)

Eigenvectors are not unique. If u is an eigenvector, so is m * u for all m != 0. Furthermore, the order that eig returns eigenvectors in Matlab is arbitrary. (I don't know what order Armadillo returns eigenvectors.) You could try and create a canonical order for the eigenvectors by sorting the eigenvalues, but that is problematic if you have complex eigenvalues. (Recall that real matrices can have complex eigenvalues.)

因此,(-1.0000,0.5000)(Matlab中 u 的第一列)与(-1.0000e + 000,5.0000e -001)(Armadillo中 u 的第二列).同样,当您按-1缩放并考虑浮点错误时,(-0.0000,-1.0000)等效于(9.9301e-016,1.0000e + 000).请注意,可能存在数值精度错误,即使在数学上数字 相等,也会导致浮点值的比较不相等.

Thus, (-1.0000, 0.5000) (first column of u in Matlab) is the same eigenvector as ( -1.0000e+000, 5.0000e-001) (second column of u in Armadillo). Similarly, (-0.0000, -1.0000) is equivalent to (9.9301e-016, 1.0000e+000) when you scale by -1 and account for floating point errors. Note that there may be numerical precision errors which would cause the floating point values to compare not equal even if mathematically the numbers are equal.

如果想要特征向量的规范表示,则可以将其重新缩放为范数为1,如果第一个元素的符号为负,则还可以乘以-1.当然,如果特征向量中的第一个元素接近于0,这又是有问题的,因为由于数值原因,该值可能刚好落在零的错误一侧.因此,考虑一下,可能最好确保最大的元素(归一化之后)而不是第一个元素是正的.

If you want a canonical representation of eigenvectors, you could rescale them to have norm 1, and also multiply by -1 if the sign of the first element is negative. Of course, if the first element in the eigenvector is close to 0, this is again problematic since the value might have ended up just barely on the wrong side of zero due to numerical reasons. So come to think of it, it might be better to ensure that the largest element (after normalization)--rather than the first--is positive.

这篇关于C ++线性代数库armadillo:如何使用eig_pair获得与Matlab中的eig函数相同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆