Search code examples
eigen3

Create EigenSolver from MatrixWrapper


How to construct an EigenSolver from a MaxtrixWrapper?

test (also at godbolt.org)

#include <Eigen/Eigen>
using namespace Eigen;

template<typename D>
void f(const Eigen::DenseBase<D>& a) {
    const Eigen::MatrixWrapper<const D> a2(a.derived());
    Eigen::EigenSolver<typename Eigen::MatrixWrapper<const D>> 
    es(a2);
}

int main() {
    ArrayXXf a(3, 3);
    a = 1.0f;
    f(a);
}

1st Error:

<...>/eigen3/Eigen/src/Eigenvalues/EigenSolver.h:71:10: error: 
‘Options’ is not a member of 
‘Eigen::EigenSolver<
    Eigen::MatrixWrapper<
        const Eigen::Array<float, -1, -1> 
    > 
 >::MatrixType {
 aka Eigen::MatrixWrapper<const Eigen::Array<float, -1, -1> >}’
     enum {

Solution

  • You don't. The solvers all want a plain Matrix<...> (or a Ref<Matrix<...> >) as template parameter. You can get the correct Matrix type using:

    template<typename D>
    void f(const Eigen::DenseBase<D>& a) {
        Eigen::EigenSolver<typename D::PlainMatrix> es(a.derived().matrix());
    }
    

    The .derived().matrix() is actually optional here, since ArrayXXf gets converted to MatrixXf implicitly. (godbolt times out on this -- the EigenSolver is quite heavy for the compiler).