C++ Compile Time Eigenvalue Evaluation

  c++, eigen, templates

I have a question I’m having trouble answering regarding Eigen. I would like to compute the eigen values of a matrix at compile time. My matrix sizes and values are known in advance. I don’t have much experience with Eigen so I’m not sure if this is possible. I know that Eigen doesn’t leverage any constexpr functions, but it does leverage template meta-programming to construct expression templates.

Is it possible to either:

  • wrap some facilities of Eigen in constexpr functions to achieve what I want
  • use template meta-programming to achieve the same thing

I’ve poked around google some and haven’t found anything which sufficiently answers my question.

I threw together a dumb example which obviously doesn’t do what I want it to. The function itself is still called at run-time.


#include <iostream>
#include "../eigen/Eigen/Dense"

template<typename T, size_t S>
constexpr Eigen::VectorXcd test()
    Eigen::Matrix<T,S,S> a = (Eigen::Matrix<double,S,S>() << 1, 2, 3.4, 5.4).finished();
    auto s = a.eigenvalues();
    return s;

int main()
    static const Eigen::VectorXcd s {test<double,2>()};
    std::cout << s << std::endl;

Source: Windows Questions C++