nemo/ScalarMappings.h
Go to the documentation of this file.00001 #ifndef _NEMO_SCALAR_MAPPINGS_H_ 00002 #define _NEMO_SCALAR_MAPPINGS_H_ 00003 00004 #include <cmath> 00005 00006 #include "Mapping.h" 00007 #include "detail/nemo-cpp11.h" 00008 00009 00010 namespace nemo { 00011 00012 00017 #ifdef _NEMO_HAS_FUNCTION_TEMPLATE_DEFAULTS_ 00018 template <typename T=double> 00019 #else 00020 template <typename T> 00021 #endif 00022 static inline Mapping<T,T> Cos(){ 00023 // compiler expects a function pointer T(*func)(T), it will pick out the correct 'overload' 00024 return Mapping<T,T>(std::cos, std::acos); 00025 } 00026 00031 #ifdef _NEMO_HAS_FUNCTION_TEMPLATE_DEFAULTS_ 00032 template <typename T=double> 00033 #else 00034 template <typename T> 00035 #endif 00036 static inline Mapping<T,T> Sin(){ 00037 return Mapping<T,T>(std::sin, std::asin); 00038 } 00039 00044 #ifdef _NEMO_HAS_FUNCTION_TEMPLATE_DEFAULTS_ 00045 template <typename T=double> 00046 #else 00047 template <typename T> 00048 #endif 00049 static inline Mapping<T,T> Tan(){ 00050 return Mapping<T,T>(std::tan, std::atan); 00051 } 00052 00057 #ifdef _NEMO_HAS_FUNCTION_TEMPLATE_DEFAULTS_ 00058 template <typename T=double> 00059 #else 00060 template <typename T> 00061 #endif 00062 static inline Mapping<T,T> Exp(){ 00063 return Mapping<T,T>(std::exp, std::log); 00064 } 00065 00070 #ifdef _NEMO_HAS_FUNCTION_TEMPLATE_DEFAULTS_ 00071 template <typename T=double> 00072 #else 00073 template <typename T> 00074 #endif 00075 static inline Mapping<T,T> Log(){ 00076 return Mapping<T,T>(std::log, std::exp); 00077 } 00078 00079 00080 namespace detail { 00085 template <typename ValueType, typename ExponentType> 00086 class PowMappingDefinition : public MappingDefinition<ValueType,ValueType> { 00087 public: 00088 ValueType evaluate(ValueType value) const { 00089 return pow(value, exponent); 00090 } 00091 00092 MappingDefinition<ValueType,ValueType>* getInverse() const { 00093 if(expIsIntegral){ 00094 // ExponentType needs to be changed!!! 00095 return new PowMappingDefinition<ValueType,double>(1.0/(double)exponent); 00096 } 00097 return new PowMappingDefinition<ValueType,ExponentType>(1.0/exponent); 00098 } 00099 00100 PowMappingDefinition(ExponentType exponentP) : exponent(exponentP) {} 00101 00102 private: 00103 ExponentType exponent; 00104 static bool expIsIntegral; 00105 }; 00106 template <typename ValueType, typename ExponentType> 00107 bool PowMappingDefinition<ValueType,ExponentType>::expIsIntegral = ( (ExponentType)(ExponentType()+0.5) == ExponentType() ); 00108 } // end namespace 00109 00114 #ifdef _NEMO_HAS_FUNCTION_TEMPLATE_DEFAULTS_ 00115 template <typename T=double,typename ExpType=double> 00116 #else 00117 template <typename T,typename ExpType> 00118 #endif 00119 static inline Mapping<T,T> Pow(ExpType exponent){ 00120 return Mapping<T,T>(new detail::PowMappingDefinition<T,ExpType>(exponent)); 00121 } 00122 00123 00124 00129 #ifdef _NEMO_HAS_FUNCTION_TEMPLATE_DEFAULTS_ 00130 template <typename T=double> 00131 #else 00132 template <typename T> 00133 #endif 00134 static inline Mapping<T,T> Sqrt(){ 00135 return Pow<T>(0.5); 00136 } 00137 00138 00139 00140 namespace detail { 00141 // MSVC even tries to compile the 'fabs(value)' below when T is integral 00142 // consequence: ambiguous overload. Provide dummy implementations that let it compile: 00143 //static inline short fabs(short){_NEMO_LOGIC_ERROR("");} 00144 //static inline int fabs(int){_NEMO_LOGIC_ERROR("");} 00145 //static inline long int fabs(long int){_NEMO_LOGIC_ERROR("");} 00146 00147 static inline long int nemo_abs(long int x){ return abs(x); } 00148 static inline int nemo_abs(int x){ return abs(x); } 00149 static inline short nemo_abs(short x){ return abs(x); } 00150 00151 static inline float nemo_abs(float x){ return fabs(x); } 00152 static inline double nemo_abs(double x){ return fabs(x); } 00153 static inline long double nemo_abs(long double x){ return fabs(x); } 00154 00159 template <typename T> 00160 class AbsMappingDefinition : public MappingDefinition<T,T> { 00161 public: 00162 T evaluate(T value) const { 00163 return nemo_abs(value); 00164 /*if(isIntegral){ 00165 return abs(value); 00166 } 00167 else{ 00168 using std::fabs; // use stdlib version if correct type is available 00169 return fabs(value); 00170 }*/ 00171 } 00172 private: 00173 //static bool isIntegral; 00174 00175 00176 }; 00177 //template <typename T> 00178 //bool AbsMappingDefinition<T>::isIntegral = ( (T)(T()+0.5) == T() ); 00179 } // end namespace 00180 00185 #ifdef _NEMO_HAS_FUNCTION_TEMPLATE_DEFAULTS_ 00186 template <typename T=double> 00187 #else 00188 template <typename T> 00189 #endif 00190 static inline Mapping<T,T> Abs(){ 00191 return Mapping<T,T>(new detail::AbsMappingDefinition<T>()); 00192 } 00193 00194 00195 00196 namespace detail { 00197 // these function shall be used by ModMappingDefinition 00198 static inline long int nemo_mod(long int x, long int y){ return x%y; } 00199 static inline int nemo_mod(int x, int y){ return x%y; } 00200 static inline short nemo_mod(short x, short y){ return x%y; } 00201 00202 static inline float nemo_mod(float x, float y){ return fmod(x,y); } 00203 static inline double nemo_mod(double x, double y){ return fmod(x,y); } 00204 static inline long double nemo_mod(long double x, long double y){ return fmod(x,y); } 00205 00210 template <typename T> 00211 class ModMappingDefinition : public MappingDefinition<T,T> { 00212 public: 00213 T evaluate(T value) const { 00214 return nemo_mod(value, modValue); 00215 /*if(isIntegral){ 00216 // note: direct use of % would give compiler error for floats 00217 return lmod(value, modValue); 00218 } 00219 else{ 00220 using std::fmod; 00221 return fmod(value, modValue); 00222 }*/ 00223 } 00224 00225 ModMappingDefinition(T modValueP) : modValue(modValueP) {} 00226 private: 00227 T modValue; 00228 //static bool isIntegral; 00229 }; 00230 //template <typename T> 00231 //bool ModMappingDefinition<T>::isIntegral = ( (T)(T()+0.5) == T() ); 00232 } // end namespace 00233 00238 #ifdef _NEMO_HAS_FUNCTION_TEMPLATE_DEFAULTS_ 00239 template <typename T=double> 00240 #else 00241 template <typename T> 00242 #endif 00243 static inline Mapping<T,T> Mod(T modValue){ 00244 return Mapping<T,T>(new detail::ModMappingDefinition<T>(modValue)); 00245 } 00246 00247 00248 } // end namespace 00249 00250 #endif 00251
Generated on Mon Feb 25 12:49:59 2013 for NemoMath by 1.6.3