As an example, suppose I want to write a function that checks whether each floating point number in a dense eigen object is normal
, and returns 1.0
for the corresponding position if it is normal, or 0.0
if it is not. The output must not be evaluated immediately, and would preferably be in the form of an expression.
I've read through the documentation on passing eigen objects as parameters, which has led me to attempt the following:
#include <functional>
#include <Eigen/Dense>
template <typename Derived>
auto Fun(const Eigen::DenseBase<Derived>& matrix) -> decltype(matrix.unaryExpr(std::ptr_fun<typename Derived::Scalar, bool>(std::isnormal)).template cast<typename Derived::Scalar>())
{
return matrix.unaryExpr(std::ptr_fun<typename Derived::Scalar, bool>(std::isnormal)).template cast<typename Derived::Scalar>();
}
int main()
{
Eigen::Matrix<double, -1, -1> mat;
mat.resize(100,100);
mat.fill(100);
auto mat2 = Fun(mat);
return 0;
}
This fails with the error that unaryExpr
is not defined for an object of type Eigen::DenseBase<Derived>
, and sure enough, if I look at the documentation for DenseBase
I see that there is no such function.
So then, short of forcing evaluation every time I want to call this function and casting the eigen object to an evaluated Matrix
, how can I achieve this?