blob: 2a0f11a7577ff14270b017f375c3323304b9c3ec [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{
16 return Eigen::CwiseBinaryOp<CwiseMinOp, Derived1, Derived2>(mat1.ref(), mat2.ref());
17 // 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}