blob: f5439a434804804b41384d6f3dff6fa39ba15b21 [file] [log] [blame]
Gael Guennebaud25568922008-03-03 10:52:44 +00001#include <Eigen/Core>
2USING_PART_OF_NAMESPACE_EIGEN
3using namespace std;
4
5// define a custom template binary functor
Gael Guennebaud138aad02008-03-06 11:36:27 +00006struct CwiseMinOp EIGEN_EMPTY_STRUCT {
Gael Guennebaud25568922008-03-03 10:52:44 +00007 template<typename Scalar>
Gael Guennebaud138aad02008-03-06 11:36:27 +00008 Scalar operator()(const Scalar& a, const Scalar& b) const { return std::min(a,b); }
Gael Guennebaud25568922008-03-03 10:52:44 +00009};
10
11// define a custom binary operator between two matrices
12template<typename Scalar, typename Derived1, typename Derived2>
13const Eigen::CwiseBinaryOp<CwiseMinOp, Derived1, Derived2>
14cwiseMin(const MatrixBase<Scalar, Derived1> &mat1, const MatrixBase<Scalar, Derived2> &mat2)
15{
Benoit Jacob861c6f42008-03-04 17:08:23 +000016 return Eigen::CwiseBinaryOp<CwiseMinOp, Derived1, Derived2>(mat1.asArg(), mat2.asArg());
Gael Guennebaud25568922008-03-03 10:52:44 +000017}
18
19int main(int, char**)
20{
21 Matrix4d m1 = Matrix4d::random(), m2 = Matrix4d::random();
Gael Guennebaud138aad02008-03-06 11:36:27 +000022 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 Guennebaud25568922008-03-03 10:52:44 +000025 return 0;
26}