Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

utils.hpp

Go to the documentation of this file.
00001 #ifndef _UTILS_HPP_
00002 #define _UTILS_HPP_
00003 
00004 /* --------------------------------------------------------------- *\
00005 
00006    author:
00007 
00008       steven bergner
00009       mathias gumz
00010 
00011 \* --------------------------------------------------------------- */
00012 
00013 
00014 namespace math
00015 {
00016 
00019 struct Utility { };
00020 
00023 template<typename Out, typename In, typename U1, typename U2>
00024 struct Chain : public Utility
00025 {
00026   Out operator()(In x, U1 u1, U2 u2) const { return u1(u2(x)); };
00027 }; // struct Chain
00028 
00029 
00032 template<typename T>
00033 struct Clamp2Upper : public Utility
00034 {
00035   Clamp2Upper(const T& _ub= Traits<T>::zero()) : ub(_ub) { };
00036   T operator() (const T& x) const { return std::min(x, ub); }
00037   T operator() (const T& x, const T& _ub) { return std::min(x, _ub); }
00038   private:
00039     T ub;
00040 };
00041 
00044 template<typename T>
00045 struct Clamp2Lower : public Utility
00046 {
00047   Clamp2Lower(const T& _lb= Traits<T>::zero()) : lb(_lb) { };
00048   T operator() (const T& x) const { return std::max(x, lb); }
00049   T operator() (const T& x, const T& _lb) { return std::max(x, _lb); }
00050 
00051   private:
00052     T lb;
00053 
00054 };
00055 
00058 template<typename T>
00059 struct Clamp : public Utility
00060 {
00061   Clamp(const T& lb= Traits<T>::zero(), const T& ub= Traits<T>::zero()) : clamp2lower(lb), clamp2upper(ub) { };
00062   T operator() (const T& x) const { return clamp2upper(clamp2lower(x)); };
00063   T operator() (const T& x, const T& lb, const T& ub)
00064   {
00065     Chain<T, T, Clamp2Lower<T>, Clamp2Upper<T> > clamp;
00066     return clamp(x, Clamp2Lower<T>(lb), Clamp2Upper<T>(ub));
00067   }
00068 
00069   private:
00070     Clamp2Lower<T> clamp2lower;
00071     Clamp2Upper<T> clamp2upper;
00072 
00073 }; // struct Clamp
00074 
00075 
00079 template<typename T>
00080 struct Sum : public Utility
00081 {
00082   typename T::ValType operator() (const T& x) const
00083   {
00084     return std::accumulate(x.begin(), x.end(), T::ValTrait::zero());
00085   }
00086 };
00087 
00090 template<typename T>
00091 struct Average : public Utility
00092 {
00093   typename T::ValType operator() (const T& x) const
00094   {
00095     return typename T::ValType(sum(x) / x.size());
00096   }
00097 
00098   private:
00099     Sum<T> sum;
00100 };
00101 
00102 
00105 template<typename T>
00106 struct Variance : public Utility
00107 {
00108   typename T::ValType operator() (const T& x) const
00109   { return T(a(x)).norm2(); }
00110 
00111   private:
00112     Average<T> a;
00113 };
00114 
00117 template<typename T>
00118 struct StdDeviation : public Utility
00119 {
00120   typename T::ValType operator() (const T& x) const
00121   {
00122     return sqrt(v(x));
00123   }
00124   private:
00125     Variance<T> v;
00126 }; // struct StdDeviation
00127 
00128 }; // namespace math
00129 
00130 #endif // _UTILS_HPP_
00131 

Generated on Tue Sep 2 11:33:27 2003 for math by doxygen 1.3.2