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 |
Gael Guennebaud | 138aad0 | 2008-03-06 11:36:27 +0000 | [diff] [blame^] | 6 | struct CwiseMinOp EIGEN_EMPTY_STRUCT { |
Gael Guennebaud | 2556892 | 2008-03-03 10:52:44 +0000 | [diff] [blame] | 7 | template<typename Scalar> |
Gael Guennebaud | 138aad0 | 2008-03-06 11:36:27 +0000 | [diff] [blame^] | 8 | Scalar operator()(const Scalar& a, const Scalar& b) const { return std::min(a,b); } |
Gael Guennebaud | 2556892 | 2008-03-03 10:52:44 +0000 | [diff] [blame] | 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 | } |
| 18 | |
| 19 | int main(int, char**) |
| 20 | { |
| 21 | Matrix4d m1 = Matrix4d::random(), m2 = Matrix4d::random(); |
Gael Guennebaud | 138aad0 | 2008-03-06 11:36:27 +0000 | [diff] [blame^] | 22 | cout << cwiseMin(m1,m2) << endl; // use our new global operator |
| 23 | cout << m1.cwise<CwiseMinOp>(m2) << endl; // directly use the generic expression member |
| 24 | cout << m1.cwise(m2, CwiseMinOp()) << endl; // directly use the generic expression member (variant) |
Gael Guennebaud | 2556892 | 2008-03-03 10:52:44 +0000 | [diff] [blame] | 25 | return 0; |
| 26 | } |