I have an Eigen::MatrixXd
and I would like to modify all its elements by applying a function component-wise. For example:
MatrixXd m = ...;
for each m[i][j]:
m[i][j] = exp(m[i][j]);
Is there a way to achieve this result?
I have an Eigen::MatrixXd
and I would like to modify all its elements by applying a function component-wise. For example:
MatrixXd m = ...;
for each m[i][j]:
m[i][j] = exp(m[i][j]);
Is there a way to achieve this result?
Yes, use the Eigen::MatrixBase<>::unaryExpr()
member function. Example:
#include <cmath>
#include <iostream>
#include <Eigen/Core>
double Exp(double x) // the functor we want to apply
{
return std::exp(x);
}
int main()
{
Eigen::MatrixXd m(2, 2);
m << 0, 1, 2, 3;
std::cout << m << std::endl << "becomes: ";
std::cout << std::endl << m.unaryExpr(&Exp) << std::endl;
}
std::ptr_fun
can be used..., see e.g. eigen.tuxfamily.org/dox/classEigen_1_1MatrixBase.html#title108 In this case, the function name decays to a pointer automatically by the rules of C++, and a function pointer behaves like a functor. Moreover, std::ptr_fun
is deprecated in C++11, see en.cppreference.com/w/cpp/utility/functional/ptr_fun. –
Alcina m.unaryExpr(&Expr)
to a new object? –
Prolusion vsoftco's answer is very general and is good for custom functions. However, there is a simpler way for many of the commonly used functions. Adapting his example we can use array
s and it looks like this:
#include <iostream>
#include <Eigen/Core>
int main()
{
Eigen::MatrixXd m(2, 2);
m << 0, 1, 2, 3;
std::cout << m << "\nbecomes:\n";
std::cout << m.array().exp() << std::endl;
return 0;
}
FWIW, in C++11 and beyond, this also works with lambda functions.
#include <cmath>
#include <iostream>
#include <Eigen/Core>
int main()
{
Eigen::MatrixXd m(2, 2);
m << 0, 1, 2, 3;
std::cout << m << std::endl << " -> "
std::cout << m.unaryExpr([](double x){return x + 1}) << std::endl;
}
@vsoftco's answer got me 99% of the way on this problem, but for some reason passing &Exp
to .unaryExpr()
was giving me compilation errors (g++, c+11, Eigen 3.3.5 gave error relating to: base type ‘double (*)(double)’ fails to be a struct or class type
).
However, I found that creating a std::function
object and passing that instead fixed this. Copying @vsoftco's example:
#include <cmath>
#include <iostream>
#include <Eigen/Core>
double Exp(double x)
{
return std::exp(x);
}
int main()
{
Eigen::MatrixXd m(2, 2);
m << 0, 1, 2, 3;
std::function<double(double)> exp_wrap = Exp; //added to @vsoftco's answer
std::cout << m << std::endl << "becomes: ";
std::cout << std::endl << m.unaryExpr(exp_wrap) << std::endl; //and used here
}
I'm not sure how much overhead using a std::function
object (or std::ptr_fun
) gives compared to passing &Exp
, but I couldn't get it to work without these alternatives.
Cheers
© 2022 - 2024 — McMap. All rights reserved.
std::exp
has overloads, andunaryExpr
is unable to deduce the functor signature fromstd::exp
alone (i.e., there are more than 1 candidates). If you "help" the compiler by specifying the functor type, likem.unaryExpr<double(*)(double)>(&std::exp)
, it will work. – Alcina