11 #ifndef __utlFunctors_h 12 #define __utlFunctors_h 21 #define utlUnaryFunctorBaseMacro(name, realFunc) \ 23 template<typename T> \ 26 UTL_ALWAYS_INLINE T operator()(const T a) const \ 27 { return realFunc(a); } \ 28 UTL_ALWAYS_INLINE bool operator!=(const name &) const \ 30 UTL_ALWAYS_INLINE bool operator==(const name & other) const \ 31 { return !( *this != other ); } \ 37 #define utlUnaryFunctorMacro(name, realFunc) \ 38 utlUnaryFunctorBaseMacro(name, realFunc) \ 40 template<class ExprT> \ 41 auto name(const ExprT& expr) -> decltype(utl::F<utl::Functor::name<typename ExprT::ValueType> >(expr)) \ 43 return utl::F<utl::Functor::name<typename ExprT::ValueType> >(expr); \ 47 template<typename T, unsigned Dim> \ 48 struct name<utl::NDArray<T,Dim> > : public VectorFunctorBase<utl::NDArray<T,Dim>, utl::NDArray<T,Dim> > \ 51 bool operator!=(const name & other) const \ 53 bool operator==(const name & other) const \ 54 { return !( *this != other ); } \ 55 void operator=(const name & other) {} \ 56 inline utl::NDArray<T,Dim> operator()( const utl::NDArray<T,Dim> & A ) const \ 58 return utl::name(A); \ 61 int GetOutputDimension(const int inSize) const \ 69 #define utlBinaryFunctorBaseMacro(name, realFunc) \ 71 template<typename T> \ 74 UTL_ALWAYS_INLINE T operator()(const T a, const T b) const \ 75 { return realFunc(a, b); } \ 76 UTL_ALWAYS_INLINE bool operator!=(const name &) const \ 78 UTL_ALWAYS_INLINE bool operator==(const name & other) const \ 79 { return !( *this != other ); } \ 85 #define utlBinaryFunctorMacro(name, realFunc) \ 86 utlBinaryFunctorBaseMacro(name, realFunc) \ 88 template<class TLeft, class TRight> \ 89 auto name(const TLeft& lhs, const TRight& rhs) -> decltype(utl::F<utl::Functor::name<Expr2ValueType<TLeft,TRight>> >(lhs, rhs)) \ 91 return utl::F<utl::Functor::name<Expr2ValueType<TLeft,TRight>> >(lhs, rhs); \ 93 template<class TLeft> \ 94 auto name(const TLeft& lhs, const ScalarExpr& rhs) -> decltype(utl::F<utl::Functor::name<Expr2ValueType<TLeft,ScalarExpr>> >(lhs, rhs)) \ 96 return utl::F<utl::Functor::name<Expr2ValueType<TLeft,ScalarExpr>> >(lhs, rhs); \ 98 template<class TRight> \ 99 auto name(const ScalarExpr& lhs, const TRight& rhs) -> decltype(utl::F<utl::Functor::name<Expr2ValueType<ScalarExpr,TRight>> >(lhs, rhs)) \ 101 return utl::F<utl::Functor::name<Expr2ValueType<ScalarExpr,TRight>> >(lhs, rhs); \ 106 #define utlVectorToScalarFunctorMacro(name, funcName) \ 108 template< class TVector, class TOutput=double > \ 109 class name : public VectorFunctorBase<TVector,TOutput> \ 112 bool operator!=(const name & other) const \ 114 bool operator==(const name & other) const \ 115 { return !( *this != other ); } \ 116 void operator=(const name & other) {} \ 117 inline TOutput operator()( const TVector & A ) const \ 119 return A.funcName(); \ 122 int GetOutputDimension(const int) const \ 130 template <
class T,
unsigned int Dim >
142 template<
class TVector,
class TOutput=TVector >
150 {
return ! (*
this==other); }
171 void Print(std::ostream & os=std::cout)
const 181 template<
class TFunctor,
class TVector=utl::NDArray<
double,1>,
class TOutput=TVector >
189 {
return ! (*
this==other); }
191 {m_Functor = other.m_Functor;}
209 int inputSize = A.Size();
210 TOutput out(inputSize);
211 for (
int i = 0; i < inputSize; ++i )
213 out[i]= m_Functor(A[i]);
226 template<
class TFunctor,
class TVector=utl::NDArray<
double,1>,
class TOutput=TVector >
234 {
return ! (*
this==other); }
236 {m_Functor = other.m_Functor;}
254 int inputSize = A.Size();
255 TOutput out(inputSize);
268 template<
class TFunction=std::function<
double(
double)> ,
class TVector=utl::NDArray<
double,1>,
class TOutput=TVector >
276 {
return ! (*
this==other); }
278 {m_Function = other.m_Function;}
287 int inputSize = A.Size();
288 TOutput out(inputSize);
289 for (
int i = 0; i < inputSize; ++i )
291 out[i]= m_Function(A[i]);
304 template<
class TFunction=std::function<
double(std::vector<
double>)> ,
class TVector=utl::NDArray<
double,1>,
class TOutput=TVector >
312 {
return ! (*
this==other); }
314 {m_Function = other.m_Function;}
321 inline TOutput
operator()(
const std::vector<TVector>& vec)
const 325 int inputSize = vec[0].Size();
326 TOutput out(inputSize);
327 std::vector<double> values;
328 for (
int i = 0; i < inputSize; ++i )
331 for (
int j = 0; j < N; ++j )
333 values.push_back(vec[j][i]);
335 out[i]= m_Function(values);
411 template<
class TVector,
class TOutput=TVector >
419 {
return m_Offset==other.m_Offset && m_ChunkSize==other.m_ChunkSize && m_Space==other.m_Space; }
421 {
return ! (*
this==other); }
425 Superclass::operator=(other);
428 m_Offset = other.m_Offset;
429 m_ChunkSize = other.m_ChunkSize;
430 m_Space = other.m_Space;
437 int inputSize = A.Size();
440 for (
int t = m_Offset; t < inputSize; t++)
442 if (t >= m_Offset && (t - m_Offset) % (m_ChunkSize + m_Space) < m_ChunkSize)
453 int outputSize = (inputSize - m_Offset) / (m_ChunkSize + m_Space) * m_ChunkSize;
454 int residual = (inputSize - m_Offset) % (m_ChunkSize + m_Space);
455 outputSize +=
std::min(m_ChunkSize, residual);
463 m_ChunkSize = vec[1];
469 std::vector<int> vec(3);
471 vec[1] = m_ChunkSize;
480 void Print(std::ostream & os=std::cout)
const 493 template<
class TVector,
class TOutput=TVector >
501 {
return ! (*
this==other); }
504 inline TOutput
operator()(
const TVector & A,
const TVector & B )
const 506 int inputSize = A.Size();
509 for (
int i = 0; i < inputSize; ++i )
514 inline TOutput
operator()(
const std::vector<TVector>& vec)
const 516 utlException(vec.size()!=2,
"need to set only 2 vectors");
519 int inputSize = A.Size();
520 utlSAException(B.Size()!=inputSize)(inputSize)(B.Size()).msg(
"two vectors have different sizes");
523 for (
int i = 0; i < inputSize; ++i )
538 template<
class TVector,
class TOutput=TVector >
546 {
return ! (*
this==other); }
550 inline TOutput
operator()(
const std::vector<TVector>& vec)
const 552 std::vector<int> sizeVec;
553 for (
int i = 0; i < vec.size(); ++i )
554 sizeVec.push_back(vec[i].Size());
559 for (
int i = 0; i < vec.size(); ++i )
561 for (
int j = 0; j < sizeVec[i]; ++j )
572 return std::accumulate(sizeVec.begin(), sizeVec.end(), 0);
NDArray is a N-Dimensional array class (row-major, c version)
VectorFunctorWrapper Self
bool operator==(const Self &other) const
bool operator!=(const Self &other) const
VectorFunctorWrapper(const TFunctor &func=TFunctor())
Self & operator=(const Self &other)
auto Round(const ExprT &expr) -> decltype(utl::F< utl::Functor::Round< typename ExprT::ValueType > >(expr))
auto Atan(const ExprT &expr) -> decltype(utl::F< utl::Functor::Atan< typename ExprT::ValueType > >(expr))
TOutput operator()(const TVector &A) const
void VerifyInputParameters(const int inputSize=-1) const
bool operator==(const Self &other) const
TOutput operator()(const std::vector< TVector > &vec) const
void operator=(const Self &other)
int GetOutputDimension(const std::vector< int > &sizeVec) const
auto Conj(const ExprT &expr) -> decltype(utl::F< utl::Functor::Conj< typename ExprT::ValueType > >(expr))
VectorFunctorBase< TVector, TOutput > Superclass
void operator=(const Self &other)
VectorUnaryFunctionWrapper Self
auto Pow(const TLeft &lhs, const TRight &rhs) -> decltype(utl::F< utl::Functor::Pow< Expr2ValueType< TLeft, TRight >> >(lhs, rhs))
auto Exp(const ExprT &expr) -> decltype(utl::F< utl::Functor::Exp< typename ExprT::ValueType > >(expr))
int GetOutputDimension(const int inputSize) const
bool operator==(const Self &other) const
bool operator==(const Self &other) const
auto Exp2(const ExprT &expr) -> decltype(utl::F< utl::Functor::Exp2< typename ExprT::ValueType > >(expr))
#define utlLogOSVar(os,...)
TOutput operator()(const std::vector< TVector > &vec) const
auto Sign(const ExprT &expr) -> decltype(utl::F< utl::Functor::Sign< typename ExprT::ValueType > >(expr))
bool operator!=(const Self &other) const
int GetOutputDimension(const int inputSize) const
int GetOutputDimension(const std::vector< int > &sizeVec) const
void operator=(const Self &other)
auto Log(const ExprT &expr) -> decltype(utl::F< utl::Functor::Log< typename ExprT::ValueType > >(expr))
#define utlException(cond, expout)
auto Log2(const ExprT &expr) -> decltype(utl::F< utl::Functor::Log2< typename ExprT::ValueType > >(expr))
TOutput operator()(const std::vector< TVector > &vec) const
ScalarFunctorWrapper Self
bool operator!=(const Self &other) const
void Print(std::ostream &os=std::cout) const
auto Acos(const ExprT &expr) -> decltype(utl::F< utl::Functor::Acos< typename ExprT::ValueType > >(expr))
int GetOutputDimension(const int inputSize) const
Self & operator=(const Self &other)
TOutput operator()(const TVector &A) const
auto LRound(const ExprT &expr) -> decltype(utl::F< utl::Functor::LRound< typename ExprT::ValueType > >(expr))
void operator=(const Self &other)
bool operator==(const Self &other) const
auto Neg(const ExprT &expr) -> decltype(utl::F< utl::Functor::Neg< typename ExprT::ValueType > >(expr))
const T & max(const T &a, const T &b)
Return the maximum between a and b.
bool operator!=(const Self &other) const
auto Asin(const ExprT &expr) -> decltype(utl::F< utl::Functor::Asin< typename ExprT::ValueType > >(expr))
VectorUnaryFunctionWrapper(TFunction func=nullptr)
bool operator!=(const Self &other) const
auto Sqrt(const ExprT &expr) -> decltype(utl::F< utl::Functor::Sqrt< typename ExprT::ValueType > >(expr))
bool operator!=(const Self &other) const
const T & min(const T &a, const T &b)
Return the minimum between a and b.
int GetOutputDimension(const std::vector< int > &sizeVec) const
#define utlSetGetMacro(name, type)
T abs(const T x)
template version of the fabs function
TOutput operator()(const TVector &A) const
auto Square(const ExprT &expr) -> decltype(utl::F< utl::Functor::Square< typename ExprT::ValueType > >(expr))
TOutput operator()(const TVector &A, const TVector &B) const
auto Min(const TLeft &lhs, const TRight &rhs) -> decltype(utl::F< utl::Functor::Min< Expr2ValueType< TLeft, TRight >> >(lhs, rhs))
VectorMultiVariableFunctionWrapper(TFunction func=nullptr)
auto Floor(const ExprT &expr) -> decltype(utl::F< utl::Functor::Floor< typename ExprT::ValueType > >(expr))
int GetOutputDimension(const int inputSize) const
int GetOutputDimension(const int inputSize) const
int GetOutputDimension(const int inputSize) const
std::vector< int > GetArguments()
auto Tan(const ExprT &expr) -> decltype(utl::F< utl::Functor::Tan< typename ExprT::ValueType > >(expr))
auto Cube(const ExprT &expr) -> decltype(utl::F< utl::Functor::Cube< typename ExprT::ValueType > >(expr))
auto Atan2(const TLeft &lhs, const TRight &rhs) -> decltype(utl::F< utl::Functor::Atan2< Expr2ValueType< TLeft, TRight >> >(lhs, rhs))
int GetOutputDimension(const int inputSize) const
#define utlSAException(expr)
void operator=(const Self &other)
void Print(std::ostream &os=std::cout) const
bool operator==(const Self &other) const
TFunctor GetFunctor() const
TOutput operator()(const TVector &A) const
void SetFunctor(const TFunctor &func)
ScalarFunctorWrapper(const TFunctor &func=TFunctor())
TFunctor GetFunctor() const
bool operator!=(const Self &other) const
#define utlVectorToScalarFunctorMacro(name, funcName)
VectorMultiVariableFunctionWrapper Self
#define utlUnaryFunctorMacro(name, realFunc)
#define utlBinaryFunctorMacro(name, realFunc)
auto Abs(const ExprT &expr) -> decltype(utl::F< utl::Functor::Abs< typename ExprT::ValueType > >(expr))
void SetFunctor(const TFunctor &func)
int GetOutputDimension(const std::vector< int > &sizeVec) const
obtain a part of a vector.
void operator=(const Self &other)
bool operator==(const Self &other) const
bool operator==(const Self &other) const
bool operator!=(const Self &other) const
int sign(const T &x)
Return the sign of x.
auto Log10(const ExprT &expr) -> decltype(utl::F< utl::Functor::Log10< typename ExprT::ValueType > >(expr))
void SetArguments(const std::vector< int > &vec)
auto Sin(const ExprT &expr) -> decltype(utl::F< utl::Functor::Sin< typename ExprT::ValueType > >(expr))
auto Cos(const ExprT &expr) -> decltype(utl::F< utl::Functor::Cos< typename ExprT::ValueType > >(expr))
auto Max(const TLeft &lhs, const TRight &rhs) -> decltype(utl::F< utl::Functor::Max< Expr2ValueType< TLeft, TRight >> >(lhs, rhs))