Gael Guennebaud | 2556892 | 2008-03-03 10:52:44 +0000 | [diff] [blame] | 1 | #include <Eigen/Core> |
| 2 | USING_PART_OF_NAMESPACE_EIGEN |
| 3 | using namespace std; |
| 4 | |
| 5 | // define a custom template binary functor |
| 6 | struct CwiseMinOp { |
| 7 | template<typename Scalar> |
| 8 | static Scalar op(const Scalar& a, const Scalar& b) { return std::min(a,b); } |
| 9 | }; |
| 10 | |
| 11 | // define a custom binary operator between two matrices |
| 12 | template<typename Scalar, typename Derived1, typename Derived2> |
| 13 | const Eigen::CwiseBinaryOp<CwiseMinOp, Derived1, Derived2> |
| 14 | cwiseMin(const MatrixBase<Scalar, Derived1> &mat1, const MatrixBase<Scalar, Derived2> &mat2) |
| 15 | { |
Benoit Jacob | 861c6f4 | 2008-03-04 17:08:23 +0000 | [diff] [blame^] | 16 | return Eigen::CwiseBinaryOp<CwiseMinOp, Derived1, Derived2>(mat1.asArg(), mat2.asArg()); |
Gael Guennebaud | 2556892 | 2008-03-03 10:52:44 +0000 | [diff] [blame] | 17 | // Note that the above is equivalent to: |
| 18 | // return mat1.template cwise<CwiseMinOp>(mat2); |
| 19 | } |
| 20 | |
| 21 | int main(int, char**) |
| 22 | { |
| 23 | Matrix4d m1 = Matrix4d::random(), m2 = Matrix4d::random(); |
| 24 | cout << cwiseMin(m1,m2) << endl; // use our new global operator |
| 25 | cout << m1.cwise<CwiseMinOp>(m2) << endl; // directly use the generic expression member |
| 26 | return 0; |
| 27 | } |