Search code examples
c++eigeneigen3

Compute Eigenvalue/vector of an Array instead of Matrix with Eigen 3


I want to compute the eigenvalue/vector of an array instead of matrix.

I tried EigenSolver<ArrayXf> but that gives compilation error.

I can copy the array to a matrix. But that is a waste of memory.

The follow code gives segmentation fault.

Test1:

#include <Eigen/Eigen>
using namespace Eigen;
int main() {
    ArrayXf A = ArrayXf::Ones(3,3);
    EigenSolver<MatrixXf> es(A);
}

Result:

<...>/Eigen/src/Core/util/XprHelper.h:130: 
Eigen::internal::variable_if_dynamic<T, Value>::variable_if_dynamic(T) 
[with T = long int; int Value = 1]: Assertion `v == T(Value)' failed.
Aborted (core dumped)

I also tried EigenSolver<MatrixXf> es(A.matrix()). But that doesn't work too.

Test2:

#include <Eigen/Eigen>
using namespace Eigen;
int main() {
    ArrayXf A = ArrayXf::Ones(3,3);
    EigenSolver<MatrixXf> es(A.matrix());
}

Result:

<...>/XprHelper.h:130: Eigen::internal::variable_if_dynamic<T, Value>::variable_if_dynamic(T) [with T = long int; int Value = 1]: Assertion `v == T(Value)' failed.
Aborted (core dumped)

Solution

  • The problem is that ArrayXf is a 1D array whereas you want a 2D one: ArrayXXf.

    Some history: we came up with the VectorXf/MatrixXf names before introducing Array for which there is no natural names to distinguish 1D and 2D, hence the single X versus the double XX...