Loading...
Searching...
No Matches
parsedFunction.h
Go to the documentation of this file.
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
#ifndef BALL_MATHS_PARSEDFUNCTION_H
#define BALL_MATHS_PARSEDFUNCTION_H
#ifndef BALL_DATATYPE_STRINGHASHMAP_H
# include <BALL/DATATYPE/stringHashMap.h>
#endif
#include <numeric>
using std::unary_function;
extern double ParsedFunctionResult;
extern int ParsedFunctionparse();
extern void ParsedFunction_initBuffer(const char*);
extern void ParsedFunction_delBuffer();
namespace BALL
{
extern StringHashMap<double*> *ParsedFunctionConstants;
extern StringHashMap<double (*)(double)> *ParsedFunctionFunctions;
template <typename arg>
class ParsedFunction
: public unary_function<arg, double>
{
public:
ParsedFunction();
ParsedFunction(const String& expression);
ParsedFunction(const ParsedFunction& func);
~ParsedFunction();
double operator () (arg p);
void initTable();
StringHashMap<double*> constants_;
StringHashMap<double (*)(double)> functions_;
protected:
String expression_;
};
template <typename arg>
ParsedFunction<arg>::ParsedFunction()
: constants_(),
functions_(),
expression_("")
{
initTable();
}
template <typename arg>
ParsedFunction<arg>::ParsedFunction(const String& expression)
: constants_(),
functions_(),
expression_(expression)
{
initTable();
}
template <typename arg>
ParsedFunction<arg>::ParsedFunction(const ParsedFunction& func)
{
constants_ = func.constants_;
functions_ = func.functions_;
expression_ = func.expression_;
initTable();
}
template <typename arg>
ParsedFunction<arg>::~ParsedFunction()
{
}
template <typename arg>
double ParsedFunction<arg>::operator () (arg argument)
{
constants_["X"] = (double*)&argument;
ParsedFunctionConstants = &constants_;
ParsedFunctionFunctions = &functions_;
ParsedFunction_initBuffer(expression_.c_str());
ParsedFunctionparse();
ParsedFunction_delBuffer();
return ParsedFunctionResult;
}
template <>
BALL_EXPORT double ParsedFunction<float>::operator () (float argument);
template <>
BALL_EXPORT double ParsedFunction<double>::operator () (double argument);
template <typename arg>
void ParsedFunction<arg>::initTable()
{
// initialize the functions table
functions_["sin"] = (double(*)(double))&sin;
functions_["cos"] = (double(*)(double))&cos;
functions_["asin"] = (double(*)(double))&asin;
functions_["acos"] = (double(*)(double))&acos;
functions_["tan"] = (double(*)(double))&tan;
functions_["atan"] = (double(*)(double))&atan;
functions_["ln"] = (double(*)(double))&log;
functions_["exp"] = (double(*)(double))&exp;
functions_[""] = 0;
}
}
#endif // BALL_MATHS_PARSEDFUNCTION_H
BALL_EXPORT
#define BALL_EXPORT
Definition COMMON/global.h:50
BALL::ParsedFunction::expression_
String expression_
Definition parsedFunction.h:85
BALL::ParsedFunction::functions_
StringHashMap< double(*)(double)> functions_
Definition parsedFunction.h:82
BALL::ParsedFunction::constants_
StringHashMap< double * > constants_
Definition parsedFunction.h:78
BALL::StringHashMap
Definition stringHashMap.h:42
BALL::String
Definition string.h:57
double
BALL::ParsedFunction::initTable
void initTable()
Definition parsedFunction.h:140
BALL::ParsedFunction::ParsedFunction
ParsedFunction()
Default constructor.
Definition parsedFunction.h:89
BALL::ParsedFunction::~ParsedFunction
~ParsedFunction()
Destructor.
Definition parsedFunction.h:116
BALL::ParsedFunctionFunctions
StringHashMap< double(*)(double)> * ParsedFunctionFunctions
BALL::ParsedFunctionConstants
StringHashMap< double * > * ParsedFunctionConstants
BALL::ParsedFunction::operator()
double operator()(arg p)
Definition parsedFunction.h:121
BALL
Definition constants.h:13
ParsedFunctionResult
double ParsedFunctionResult
ParsedFunction_delBuffer
void ParsedFunction_delBuffer()
ParsedFunctionparse
int ParsedFunctionparse()
ParsedFunction_initBuffer
void ParsedFunction_initBuffer(const char *)
stringHashMap.h