xref: /aosp_15_r20/external/eigen/doc/examples/class_CwiseBinaryOp.cpp (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1*bf2c3715SXin Li #include <Eigen/Core>
2*bf2c3715SXin Li #include <iostream>
3*bf2c3715SXin Li using namespace Eigen;
4*bf2c3715SXin Li using namespace std;
5*bf2c3715SXin Li 
6*bf2c3715SXin Li // define a custom template binary functor
7*bf2c3715SXin Li template<typename Scalar> struct MakeComplexOp {
8*bf2c3715SXin Li   EIGEN_EMPTY_STRUCT_CTOR(MakeComplexOp)
9*bf2c3715SXin Li   typedef complex<Scalar> result_type;
operator ()MakeComplexOp10*bf2c3715SXin Li   complex<Scalar> operator()(const Scalar& a, const Scalar& b) const { return complex<Scalar>(a,b); }
11*bf2c3715SXin Li };
12*bf2c3715SXin Li 
main(int,char **)13*bf2c3715SXin Li int main(int, char**)
14*bf2c3715SXin Li {
15*bf2c3715SXin Li   Matrix4d m1 = Matrix4d::Random(), m2 = Matrix4d::Random();
16*bf2c3715SXin Li   cout << m1.binaryExpr(m2, MakeComplexOp<double>()) << endl;
17*bf2c3715SXin Li   return 0;
18*bf2c3715SXin Li }
19