Search code examples
c++linear-algebraeigen

How to use Eigen's unaryExpr with custom scalar type?


I have the following code:

Matrix<Vector3f, 3, 3> m;
m(0,0) = Vector3f(0,0,0);
m(1,0) = Vector3f(1,1,1);
m(2,0) = Vector3f(2,2,2);
m(0,1) = Vector3f(3,3,3);
m(1,1) = Vector3f(4,4,4);
m(2,1) = Vector3f(5,5,5);
m(0,2) = Vector3f(6,6,6);
m(1,2) = Vector3f(7,7,7);
m(2,2) = Vector3f(8,8,8);

auto lambda = [&](const Ref<const Vector3f> & element)->Vector3f{ return element * -1;};
auto b = m.unaryExpr(lambda);
cout << b << endl;

But this code won't compile. So I am wondering if that is possible to use unaryExpr with custom scalar type? If yes, is there an example?

The compiling error:

/path/Eigen/src/Core/IO.h:132:95: error: no matching function for call to ‘log(Eigen::internal::significant_decimals_default_impl, false>::RealScalar)’ return cast(ceil(-log(NumTraits::epsilon())/log(RealScalar(10))));


Solution

  • The unaryExpr works fine (though it would have been simpler to use a const Vector3f & element as argument).

    What is not working is cout << b << endl; because Eigen tries to call log and ceil on a Vector3f to format the output.