BALL  1.4.1
parsedFunction.h
Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 
00005 #ifndef BALL_MATHS_PARSEDFUNCTION_H
00006 #define BALL_MATHS_PARSEDFUNCTION_H
00007 
00008 #ifndef BALL_DATATYPE_STRINGHASHMAP_H
00009 # include <BALL/DATATYPE/stringHashMap.h>
00010 #endif
00011 
00012 #include <numeric>
00013 
00014 using std::unary_function;
00015 
00016 extern double ParsedFunctionResult;
00017 extern int ParsedFunctionparse();
00018 extern void ParsedFunction_initBuffer(const char*);
00019 extern void ParsedFunction_delBuffer();
00020 
00021 namespace BALL
00022 {
00026   extern StringHashMap<double*> *ParsedFunctionConstants;
00027   extern StringHashMap<double (*)(double)> *ParsedFunctionFunctions;
00028   
00036   template <typename arg> 
00037   class ParsedFunction 
00038     : public unary_function<arg, double> 
00039   {
00040     public:
00044   
00046       ParsedFunction();
00047   
00049       ParsedFunction(const String& expression);
00050       
00052       ParsedFunction(const ParsedFunction& func);
00053       
00055       ~ParsedFunction();
00056       
00058       
00065       double operator () (arg p);
00066     
00068 
00073       void initTable();
00075   
00078       StringHashMap<double*> constants_;
00079   
00082       StringHashMap<double (*)(double)> functions_;
00083     
00084     protected:
00085       String expression_;
00086   };
00087 
00088   template <typename arg>
00089   ParsedFunction<arg>::ParsedFunction()
00090     : constants_(),
00091       functions_(),
00092       expression_("")
00093   {
00094     initTable();
00095   }
00096 
00097   template <typename arg>
00098   ParsedFunction<arg>::ParsedFunction(const String& expression)
00099     : constants_(),
00100       functions_(),
00101       expression_(expression)
00102   {
00103     initTable();
00104   }
00105   
00106   template <typename arg>
00107   ParsedFunction<arg>::ParsedFunction(const ParsedFunction& func) 
00108   {
00109     constants_ = func.constants_;
00110     functions_ = func.functions_;
00111     expression_ = func.expression_;
00112     initTable();
00113   }
00114 
00115   template <typename arg>
00116   ParsedFunction<arg>::~ParsedFunction()
00117   {
00118   }
00119 
00120   template <typename arg>
00121   double ParsedFunction<arg>::operator () (arg argument)
00122   {
00123     constants_["X"] = (double*)&argument;
00124     ParsedFunctionConstants = &constants_;
00125     ParsedFunctionFunctions = &functions_;
00126     ParsedFunction_initBuffer(expression_.c_str());
00127     ParsedFunctionparse();
00128     ParsedFunction_delBuffer();
00129     
00130     return ParsedFunctionResult;
00131   }
00132 
00133   template <>
00134   BALL_EXPORT double ParsedFunction<float>::operator () (float argument);
00135 
00136   template <>
00137   BALL_EXPORT double ParsedFunction<double>::operator () (double argument);
00138  
00139   template <typename arg>
00140   void ParsedFunction<arg>::initTable()
00141   {
00142     // initialize the functions table
00143     functions_["sin"] = (double(*)(double))&sin;
00144     functions_["cos"] = (double(*)(double))&cos;
00145     functions_["asin"] = (double(*)(double))&asin;
00146     functions_["acos"] = (double(*)(double))&acos;
00147     functions_["tan"] = (double(*)(double))&tan;
00148     functions_["atan"] = (double(*)(double))&atan;
00149     functions_["ln"] = (double(*)(double))&log;
00150     functions_["exp"] = (double(*)(double))&exp;
00151     functions_[""] = 0;
00152   }
00153 
00155 }
00156 
00157 #endif // BALL_MATHS_PARSEDFUNCTION_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines