binaryExpr


字面意思就是“二元表达式”,主要的作用是实现”两个矩阵/数组的元素之间的运算“,如计算 atan2,实现按元素相加减的运算。

MatrixBase::binaryExpr(const MatrixBase &,const CustomBinaryOp &) const

CustomBinaryOp 可以是 lambda 表示式 或者是 ptr_fun 的仿函数。

代码

#include 
#include 

double myatan2(double a, double b){ return std::atan2(a,b); }

int main()
{
    Eigen::Matrix2d A, B, C, D;
    A << 1, 1, -1, -1;
    B << 1, -1, -1, 1;
    // 使用 lambda 表达式
    C = A.binaryExpr(B, [](double a, double b) {return std::atan2(a,b);});
    // 使用 ptr_fun 仿函数
    D = A.binaryExpr(B, std::ptr_fun(myatan2));
    std::cout << "A=\n" << A << std::endl;
    std::cout << "B=\n" << B << std::endl;
    std::cout << "C=\n" << C << std::endl;
    std::cout << "D=\n" << D << std::endl;
    return 0;
}

参考

  • Eigen: Eigen::CwiseBinaryOp< BinaryOp, LhsType, RhsType > Class Template Reference
  • Eigen arctan2_月亮不知道的博客-CSDN博客
  • C++11 Lambda表达式(匿名函数)详解 (biancheng.net)
  • STL: bind1st, bind2nd 的使用(C++)_zhangpiu的专栏-CSDN博客
  • ptr_fun_SAYA_的博客-CSDN博客