I'm trying to make the compiler tail-call optimize eigen-expressions that are passed recursively. The following will optimize using GCC -O2:
inline double recursive(const double &A,
const double &b,
const int &N)
{
if (N <= 0)
return b;
return recursive(A, b/A, N - 1);
}
int main()
{
double A = 1;
double b = 2;
double res2 = recursive(A, b, 10);
}
The following does not:
#include <iostream>
#include <Eigen/Dense>
using Mat = Eigen::MatrixXd;
using Vec = Eigen::VectorXd;
using Eigen::MatrixBase;
inline Vec recursive(const Mat &A,
const Vec &b,
const int &N)
{
if (N <= 0)
return b;
return recursive(A, A.ldlt().solve(b), N - 1);
}
int main()
{
Mat A(2, 2);
A(0, 0) = 10.0;
A(1, 1) = 10.0;
Vec b(2);
b(0) = 1.0;
b(1) = 5.0;
Vec res(2);
Vec res2 = recursive(A, b, 10);
}
Even when I pass the computation as sol = A.ldlt().solve(b).Eval(); it is unable to perform optimization. Why?