functional.h

Go to the documentation of this file.
00001 
00015 #include <cmath>
00016 #include <functional>
00017 #include <dlrNumeric/numericTraits.h>
00018 
00019 #ifndef _DLR_NUMERIC_FUNCTIONAL_H_
00020 #define _DLR_NUMERIC_FUNCTIONAL_H_
00021 
00022 namespace dlr {
00023 
00024   namespace numeric {
00025     
00030     template <class Type>
00031     struct Gaussian1DFunctor
00032       : public std::unary_function<Type, Type>
00033     {
00041       Gaussian1DFunctor(Type sigma = 1.0)
00042         : m_k0(1.0 / (std::sqrt(2.0 * M_PI) * sigma)),
00043           m_k1(-1.0 / (2.0 * sigma * sigma)) {}
00044 
00045     
00052       inline Type
00053       operator()(const Type& input) {
00054         return m_k0 * std::exp(input * input * m_k1);
00055       }
00056 
00057     private:
00058       Type m_k0;
00059       Type m_k1;
00060     };
00061 
00062   
00067     template <class Type>
00068     struct LogFunctor
00069       : public std::unary_function<Type, Type>
00070     {
00077       inline Type
00078       operator()(const Type& input) {
00079         return std::log(input);
00080       }
00081     };
00082 
00083   
00090     template <class TypeIn, class TypeOut>
00091     struct NumericTypeConversionFunctor
00092       : public std::unary_function<TypeIn, TypeOut>
00093     {
00102       inline TypeOut
00103       operator()(const TypeIn& input) {
00104         // This "if" should optimize away, since traits are known at compile
00105         // time.
00106         if(!NumericTraits<TypeIn>().isIntegral()
00107            && NumericTraits<TypeOut>().isIntegral()) {
00108           return static_cast<TypeOut>(input + 0.5);
00109         }
00110         return static_cast<TypeOut>(input);
00111       }
00112     };
00113 
00114   
00119     template <class Type>
00120     struct SquareRootFunctor
00121       : public std::unary_function<Type, Type>
00122     {
00129       inline Type
00130       operator()(const Type& input) {
00131         return std::sqrt(input);
00132       }
00133     };
00134 
00135   } // namespace numeric
00136 
00137 }  // namespace dlr
00138 
00139 
00140 /* ======= Declarations to maintain compatibility with legacy code. ======= */
00141 
00142 namespace dlr {
00143 
00144   using numeric::Gaussian1DFunctor;
00145   using numeric::LogFunctor;
00146   using numeric::NumericTypeConversionFunctor;
00147   using numeric::SquareRootFunctor;
00148 
00149 }  // namespace dlr
00150 
00151 
00152 /* ================= Specializations =================== */
00153 
00154 namespace dlr {
00155 
00156   namespace numeric {
00157     
00158   // None
00159 
00160   } // namespace numeric
00161   
00162 }  // namespace dlr
00163 
00164 #endif // #ifndef _DLR_NUMERIC_FUNCTIONAL_H_

Generated on Wed Nov 25 00:42:42 2009 for dlrUtilities Utility Library by  doxygen 1.5.8