特征库中的本征解算器

时间:2015-06-11 08:20:04

标签: c++ matlab eigen

我想使用Eigen库将[vec,val] = eig(A)从MATLAB翻译成c ++,但我无法达到相同的结果! 我尝试了eigensolverComplexEigenSolverSelfAdjointEigenSolver.他们都没有在MATLAB中给我eig(A)这样的结果。

Sample matrices:
Tv(:,:,223) =

    0.8648   -1.9658   -0.2785
   -1.9658    4.9142    0.8646
   -0.2785    0.8646    0.3447


Tv(:,:,224) =

    1.9735   -0.4218    1.0790
   -0.4218    3.3012    0.1855
    1.0790    0.1855    3.7751


Tv(:,:,225) =

    2.4948    1.0185    1.1633
    1.0185    1.1732   -0.4479
    1.1633   -0.4479    4.3289


Tv(:,:,226) =

    0.3321    0.0317    0.1617
    0.0317    0.0020   -0.0139
    0.1617   -0.0139    0.5834

征:

MatrixXcd vec(3 * n, 3);
VectorXcd val(3);
for (int k = 0; k < n; k++){
        EigenSolver<Matrix3d> eig(Tv.block<3, 3>(3 * k, 0));
        vec.block<3, 3>(3 * k, 0) = eig.eigenvectors();
        cout <<endl << vec.block<3, 3>(3 * k, 0) << endl;
        val = eig.eigenvalues();
        cout << "val= " << endl << val << endl;

    }

//结果

  (0.369152,0)   (-0.830627,0)   (-0.416876,0)
  (-0.915125,0)   (-0.403106,0) (-0.00717218,0)
  (-0.162088,0)    (0.384142,0)   (-0.908935,0)
val=
  (5.86031,0)
(0.0396418,0)
 (0.223765,0)

 (0.881678,0)  (0.204005,0)  (0.425472,0)
  (0.23084,0)  (-0.97292,0) (-0.011858,0)
(-0.411531,0) (-0.108671,0)  (0.904894,0)
val=
(1.35945,0)
(3.41031,0)
(4.27996,0)

 (0.526896,0) (-0.726801,0)  (0.440613,0)
(-0.813164,0) (-0.581899,0) (0.0125466,0)
(-0.247274,0)  (0.364902,0)  (0.897609,0)
val=
(0.377083,0)
 (2.72623,0)
 (4.89367,0)

    (0.88992,0)    (-0.43968,0)    (0.121341,0)
    (0.13406,0) (-0.00214387,0)   (-0.990971,0)
   (-0.43597,0)   (-0.898152,0)  (-0.0570358,0)
val=
   (0.257629,0)
   (0.662467,0)
(-0.00267575,0)

MATLAB:

for k=1:n
    [u,d] = eig(Tv(:,:,k))
end

%结果

u =

    0.8306   -0.4169   -0.3692
    0.4031   -0.0072    0.9151
   -0.3841   -0.9089    0.1621


d =

    0.0396         0         0
         0    0.2238         0
         0         0    5.8603


u =

    0.8817    0.2040    0.4255
    0.2308   -0.9729   -0.0119
   -0.4115   -0.1087    0.9049


d =

    1.3594         0         0
         0    3.4103         0
         0         0    4.2800


u =

   -0.5269    0.7268    0.4406
    0.8132    0.5819    0.0125
    0.2473   -0.3649    0.8976


d =

    0.3771         0         0
         0    2.7262         0
         0         0    4.8937


u =

   -0.1213   -0.8899    0.4397
    0.9910   -0.1341    0.0021
    0.0570    0.4360    0.8982


d =

   -0.0027         0         0
         0    0.2576         0
         0         0    0.6625

你的建议是什么?

2 个答案:

答案 0 :(得分:3)

我没有得到你的问题,因为看着你的结果他们都会返回相同的结果。回想一下矩阵的特征分解并不是完全唯一的:

  • 特征值/向量可以任意重新排序
  • 如果v是特征向量,则-v也是有效的特征向量

由于您的矩阵是对称的,您应该使用SelfAdjointEigenSolver将它们自动排序为MatLab。那么特征向量只会与它们的符号不同,但你必须忍受它。

答案 1 :(得分:1)

嗯......结果是一样的......

结果特征:

  (0.369152,0)   (-0.830627,0)   (-0.416876,0)
  (-0.915125,0)   (-0.403106,0) (-0.00717218,0)
  (-0.162088,0)    (0.384142,0)   (-0.908935,0)
val=
  (5.86031,0)
(0.0396418,0)
 (0.223765,0)

结果matlab:

u =

    0.8306   -0.4169   -0.3692
    0.4031   -0.0072    0.9151
   -0.3841   -0.9089    0.1621


d =

    0.0396         0         0
         0    0.2238         0
         0         0    5.8603

我有个好消息......

载体是相同的,但无序.....

来自eigen的eigV1是来自Matlab的-eigV3,

来自eigen的eigV2是来自Matlab的-eigV1,

来自eigen的eigV3是来自Matlab的-eigV2,

特征值被重新排序......