Eigen图书馆中的Eigensolver

2022-05-11 00:00:00 matlab c++ eigen

我想用本征库将[vec,val] = eig(A)从MatLab翻译成C++,但我达不到同样的结果! 我尝试了eigensolverComplexEigenSolverSelfAdjointEigenSolver.,但都没有给出像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

您的建议是什么?


解决方案

我不明白您的问题,因为从您的结果来看,它们返回的结果都是一样的。回想一下,矩阵的特征分解不是完全唯一的:

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

因为您的矩阵是对称的,所以您应该使用SelfAdjointEenerSolver将它们作为matlab自动排序。那么特征向量将只与它们的符号不同,但您将不得不接受这一点。

相关文章