Apply function to all Eigen matrix element

2019-01-09 09:50发布

问题:

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?

回答1:

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;
}


回答2:

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;
}


回答3:

@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



标签: c++ matrix eigen