2008-04-03 19:10:17 +08:00
|
|
|
// FIXME - this example is not too good as that functionality is provided in the Eigen API
|
|
|
|
|
// additionally it's quite heavy. the CwiseUnaryOp example is better.
|
|
|
|
|
|
2008-03-03 18:52:44 +08:00
|
|
|
#include <Eigen/Core>
|
|
|
|
|
USING_PART_OF_NAMESPACE_EIGEN
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
// define a custom template binary functor
|
2008-04-03 19:10:17 +08:00
|
|
|
template<typename Scalar> struct CwiseMinOp EIGEN_EMPTY_STRUCT {
|
2008-03-06 19:36:27 +08:00
|
|
|
Scalar operator()(const Scalar& a, const Scalar& b) const { return std::min(a,b); }
|
2008-04-03 19:10:17 +08:00
|
|
|
enum { Cost = Eigen::ConditionalJumpCost + Eigen::NumTraits<Scalar>::AddCost };
|
2008-03-03 18:52:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// define a custom binary operator between two matrices
|
2008-03-11 01:23:11 +08:00
|
|
|
template<typename Derived1, typename Derived2>
|
2008-04-03 19:10:17 +08:00
|
|
|
const Eigen::CwiseBinaryOp<CwiseMinOp<typename Derived1::Scalar>, Derived1, Derived2>
|
2008-03-11 01:23:11 +08:00
|
|
|
cwiseMin(const MatrixBase<Derived1> &mat1, const MatrixBase<Derived2> &mat2)
|
2008-03-03 18:52:44 +08:00
|
|
|
{
|
2008-04-03 19:10:17 +08:00
|
|
|
return Eigen::CwiseBinaryOp<CwiseMinOp<typename Derived1::Scalar>, Derived1, Derived2>(mat1, mat2);
|
2008-03-03 18:52:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int, char**)
|
|
|
|
|
{
|
|
|
|
|
Matrix4d m1 = Matrix4d::random(), m2 = Matrix4d::random();
|
2008-03-06 19:36:27 +08:00
|
|
|
cout << cwiseMin(m1,m2) << endl; // use our new global operator
|
2008-04-03 19:10:17 +08:00
|
|
|
cout << m1.cwise<CwiseMinOp<double> >(m2) << endl; // directly use the generic expression member
|
|
|
|
|
cout << m1.cwise(m2, CwiseMinOp<double>()) << endl; // directly use the generic expression member (variant)
|
2008-03-03 18:52:44 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|