blob: 9bb10cdcbe2e7955cc1505f04b2b96ed5519c58d [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
6struct 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
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 // Note that the above is equivalent to:
18 // return mat1.template cwise<CwiseMinOp>(mat2);
19}
20
21int 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}