Apply function to all Eigen matrix element
Asked Answered
E

4

29

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?

Exteroceptor answered 18/11, 2015 at 17:35 Comment(0)
A
35

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;
}
Alcina answered 18/11, 2015 at 17:42 Comment(4)
@XingShi No, because std::exp has overloads, and unaryExpr is unable to deduce the functor signature from std::exp alone (i.e., there are more than 1 candidates). If you "help" the compiler by specifying the functor type, like m.unaryExpr<double(*)(double)>(&std::exp), it will work.Alcina
According to the API reference std::ptr_func must be used to convert a pointer to a function in a functor, which is what is passed as argument to unaryExpr. m.unaryExp( std::ptr_func(&Exp) );Berberine
@Alex Thanks for the comment. However it's not a must, the API says that 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
Does anyone knows how I can assign the matrix returned from m.unaryExpr(&Expr) to a new object?Prolusion
L
29

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 arrays 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;
}
Liaotung answered 18/11, 2015 at 18:18 Comment(2)
This is also of note as it would appear that this approach results in more efficient (read vectorised) machine code godbolt.org/z/YE_RJrOutgo
For future users who want to know what other coefficient-wise functions Eigen supports, see here: Catalog of Eigen coefficient-wise math functionsScalise
B
11

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;
}
Biyearly answered 10/10, 2019 at 22:39 Comment(0)
J
2

@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

Junna answered 27/10, 2018 at 10:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.