访问eigen3中的特征值

时间:2013-03-21 17:05:07

标签: c++ matrix linear-algebra eigen

我正在尝试设置eigen3特征值求解器,看起来我遇到了一些复杂问题。代码编译得很好,但在某些时候失败了。失败的代码是

EigenSolver<MatrixXd> eigensolver(A);
cout << "tell me something" << endl;
Matrix<complex<double>, -1, 1, 0, -1, 1> E = eigensolver.eigenvalues();
cout << "tell me something more" << endl;
cout << E;

并输出错误消息:

tell me something
tell me something more
(3.5,1.93649)
class_out: /usr/include/eigen3/Eigen/src/Core/DenseCoeffsBase.h:407: Eigen::internal::traits<Derived>::Scalar &Eigen::DenseCoeffsBase<Derived, 1>::operator()(Eigen::internal::traits<Derived>::Index) [with Derived = Eigen::Matrix<std::complex<double>, -1, 1, 0, -1, 1>]: Assertion `index >= 0 && index < size()' failed.
(3.5,-1.93649)Aborted

这看起来有点奇怪,但看起来它不像<<运算符那样普通

cout << eigensolver.eigenvalues();
对于普通矩阵,

cout一样正常。另外,我用Mathematica检查了A的特征值,我得到了(3.5,1.93649)和(3.5,-1.93649)。

有谁知道为什么会这样,或者有人可能建议其他方式来访问特征值?

1 个答案:

答案 0 :(得分:6)

解决方案似乎非常简单:

EigenSolver<MatrixXd> eigensolver(A);
cout << "tell me something" << endl;
complex<double> E;
cout << "tell me something more" << endl;
for(int i = 0; i < A.rows(); ++i){
E = eigensolver.eigenvalues().col(0)[i];
cout << E << endl;
}