Keep going and be optimistic!|

Wreng

园龄:4年8个月粉丝:34关注:5

📂eigen
🔖eigen
2022-02-21 11:49阅读: 118评论: 0推荐: 0

binaryExpr

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

MatrixBase::binaryExpr(const MatrixBase<OtherDerived> &,const CustomBinaryOp &) const

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

代码

#include <iostream>
#include <Eigen/Dense>

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

参考

本文作者:Wreng

本文链接:https://www.cnblogs.com/wreng/p/15918230.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Wreng  阅读(118)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起