1#include <Eigen/Core>
2#include <iostream>
3using namespace Eigen;
4using namespace std;
5
6// define a custom template unary functor
7template<typename Scalar>
8struct CwiseClampOp {
9  CwiseClampOp(const Scalar& inf, const Scalar& sup) : m_inf(inf), m_sup(sup) {}
10  const Scalar operator()(const Scalar& x) const { return x<m_inf ? m_inf : (x>m_sup ? m_sup : x); }
11  Scalar m_inf, m_sup;
12};
13
14int main(int, char**)
15{
16  Matrix4d m1 = Matrix4d::Random();
17  cout << m1 << endl << "becomes: " << endl << m1.unaryExpr(CwiseClampOp<double>(-0.5,0.5)) << endl;
18  return 0;
19}
20