muParser API -  1.35
Public Member Functions | Static Protected Member Functions | List of all members
mu::Parser Class Reference

Mathematical expressions parser. More...

#include <muParser.h>

Inheritance diagram for mu::Parser:
Inheritance graph
[legend]

Public Member Functions

 Parser ()
 Constructor. More...
 
virtual void InitCharSets ()
 Define the character sets. More...
 
virtual void InitFun ()
 Initialize the default functions.
 
virtual void InitConst ()
 Initialize constants. More...
 
virtual void InitOprt ()
 Initialize operators. More...
 
virtual void OnDetectVar (string_type *pExpr, int &nStart, int &nEnd)
 
value_type Diff (value_type *a_Var, value_type a_fPos, value_type a_fEpsilon=0) const
 Numerically differentiate with regard to a variable. More...
 
- Public Member Functions inherited from mu::ParserBase
 ParserBase ()
 Constructor. More...
 
 ParserBase (const ParserBase &a_Parser)
 Copy constructor. More...
 
ParserBaseoperator= (const ParserBase &a_Parser)
 Assignment operator. More...
 
value_type Eval () const
 Calculate the result. More...
 
value_typeEval (int &nStackSize) const
 Evaluate an expression containing comma separated subexpressions. More...
 
void Eval (value_type *results, int nBulkSize)
 
int GetNumResults () const
 Return the number of results on the calculation stack. More...
 
void SetExpr (const string_type &a_sExpr)
 Set the formula. More...
 
void SetVarFactory (facfun_type a_pFactory, void *pUserData=nullptr)
 Set a function that can create variable pointer for unknown expression variables. More...
 
void SetDecSep (char_type cDecSep)
 Set the decimal separator. More...
 
void SetThousandsSep (char_type cThousandsSep=0)
 Sets the thousands operator. More...
 
void ResetLocale ()
 Resets the locale. More...
 
void EnableOptimizer (bool a_bIsOn=true)
 Enable or disable the formula optimization feature. More...
 
void EnableBuiltInOprt (bool a_bIsOn=true)
 Enable or disable the built in binary operators. More...
 
bool HasBuiltInOprt () const
 Query status of built in variables. More...
 
void AddValIdent (identfun_type a_pCallback)
 Add a value parsing function. More...
 
template<typename T >
void DefineFun (const string_type &a_strName, T a_pFun, bool a_bAllowOpt=true)
 Define a parser function without arguments. More...
 
void DefineOprt (const string_type &a_strName, fun_type2 a_pFun, unsigned a_iPri=0, EOprtAssociativity a_eAssociativity=oaLEFT, bool a_bAllowOpt=false)
 Define a binary operator. More...
 
void DefineConst (const string_type &a_sName, value_type a_fVal)
 Add a user defined constant. More...
 
void DefineStrConst (const string_type &a_sName, const string_type &a_strVal)
 Define a new string constant. More...
 
void DefineVar (const string_type &a_sName, value_type *a_fVar)
 Add a user defined variable. More...
 
void DefinePostfixOprt (const string_type &a_strFun, fun_type1 a_pOprt, bool a_bAllowOpt=true)
 Add a user defined operator. More...
 
void DefineInfixOprt (const string_type &a_strName, fun_type1 a_pOprt, int a_iPrec=prINFIX, bool a_bAllowOpt=true)
 Add a user defined operator. More...
 
void ClearVar ()
 Clear all user defined variables. More...
 
void ClearFun ()
 Clear all functions. More...
 
void ClearConst ()
 Clear all user defined constants. More...
 
void ClearInfixOprt ()
 Clear the user defined Prefix operators. More...
 
void ClearPostfixOprt ()
 Clear all user defined postfix operators. More...
 
void ClearOprt ()
 Clear all user defined binary operators. More...
 
void RemoveVar (const string_type &a_strVarName)
 Remove a variable from internal storage. More...
 
const varmap_typeGetUsedVar () const
 Return a map containing the used variables only.
 
const varmap_typeGetVar () const
 Return a map containing the used variables only.
 
const valmap_typeGetConst () const
 Return a map containing all parser constants.
 
const string_typeGetExpr () const
 Retrieve the formula.
 
const funmap_typeGetFunDef () const
 Return prototypes of all parser functions. More...
 
string_type GetVersion (EParserVersionInfo eInfo=pviFULL) const
 Returns the version of muparser. More...
 
const char_type ** GetOprtDef () const
 Get the default symbols used for the built in operators. More...
 
void DefineNameChars (const char_type *a_szCharset)
 Define the set of valid characters to be used in names of functions, variables, constants.
 
void DefineOprtChars (const char_type *a_szCharset)
 Define the set of valid characters to be used in names of binary operators and postfix operators.
 
void DefineInfixOprtChars (const char_type *a_szCharset)
 Define the set of valid characters to be used in names of infix operators.
 
const char_typeValidNameChars () const
 Virtual function that defines the characters allowed in name identifiers. More...
 
const char_typeValidOprtChars () const
 Virtual function that defines the characters allowed in operator definitions. More...
 
const char_typeValidInfixOprtChars () const
 Virtual function that defines the characters allowed in infix operator definitions. More...
 
void SetArgSep (char_type cArgSep)
 Set argument separator. More...
 
char_type GetArgSep () const
 Get the argument separator character.
 

Static Protected Member Functions

static int IsVal (const char_type *a_szExpr, int *a_iPos, value_type *a_fVal)
 Default value recognition callback. More...
 

Additional Inherited Members

- Public Types inherited from mu::ParserBase
typedef ParserError exception_type
 Type of the error class. More...
 
- Static Public Member Functions inherited from mu::ParserBase
static void EnableDebugDump (bool bDumpCmd, bool bDumpStack)
 Enable the dumping of bytecode and stack content on the console. More...
 
- Protected Member Functions inherited from mu::ParserBase
void Init ()
 Initialize user defined functions. More...
 
void Error (EErrorCodes a_iErrc, int a_iPos=(int) mu::string_type::npos, const string_type &a_strTok=string_type()) const
 Create an error containing the parse error position. More...
 
- Static Protected Attributes inherited from mu::ParserBase
static const char_typec_DefaultOprt []
 Identifiers for built in binary operators. More...
 
static std::locale s_locale = std::locale(std::locale::classic(), new change_dec_sep<char_type>('.'))
 The locale used by the parser.
 
static bool g_DbgDumpCmdCode = false
 
static bool g_DbgDumpStack = false
 

Detailed Description

Mathematical expressions parser.

Standard implementation of the mathematical expressions parser. Can be used as a reference implementation for subclassing the parser.

Definition at line 50 of file muParser.h.

Constructor & Destructor Documentation

mu::Parser::Parser ( )

Constructor.

Call ParserBase class constructor and trigger Function, Operator and Constant initialization.

Definition at line 79 of file muParser.cpp.

80  :ParserBase()
81  {
83 
84  InitCharSets();
85  InitFun();
86  InitConst();
87  InitOprt();
88  }
virtual void InitFun()
Initialize the default functions.
Definition: muParser.cpp:106
void AddValIdent(identfun_type a_pCallback)
Add a value parsing function.
virtual void InitCharSets()
Define the character sets.
Definition: muParser.cpp:97
static int IsVal(const char_type *a_szExpr, int *a_iPos, value_type *a_fVal)
Default value recognition callback.
Definition: muParser.cpp:55
virtual void InitConst()
Initialize constants.
Definition: muParser.cpp:160
ParserBase()
Constructor.
virtual void InitOprt()
Initialize operators.
Definition: muParser.cpp:171

Member Function Documentation

value_type mu::Parser::Diff ( value_type a_Var,
value_type  a_fPos,
value_type  a_fEpsilon = 0 
) const

Numerically differentiate with regard to a variable.

Parameters
[in]a_VarPointer to the differentiation variable.
[in]a_fPosPosition at which the differentiation should take place.
[in]a_fEpsilonEpsilon used for the numerical differentiation.

Numerical differentiation uses a 5 point operator yielding a 4th order formula. The default value for epsilon is 0.00074 which is numeric_limits<double>::epsilon() ^ (1/5).

Definition at line 212 of file muParser.cpp.

213  {
214  value_type fRes(0);
215  value_type fBuf(*a_Var);
216  value_type f[4] = { 0,0,0,0 };
217  value_type fEpsilon(a_fEpsilon);
218 
219  // Backwards compatible calculation of epsilon inc case the user doesn't provide
220  // his own epsilon
221  if (fEpsilon == 0)
222  fEpsilon = (a_fPos == 0) ? (value_type)1e-10 : (value_type)1e-7 * a_fPos;
223 
224  *a_Var = a_fPos + 2 * fEpsilon; f[0] = Eval();
225  *a_Var = a_fPos + 1 * fEpsilon; f[1] = Eval();
226  *a_Var = a_fPos - 1 * fEpsilon; f[2] = Eval();
227  *a_Var = a_fPos - 2 * fEpsilon; f[3] = Eval();
228  *a_Var = fBuf; // restore variable
229 
230  fRes = (-f[0] + 8 * f[1] - 8 * f[2] + f[3]) / (12 * fEpsilon);
231  return fRes;
232  }
value_type Eval() const
Calculate the result.
MUP_BASETYPE value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:294
void mu::Parser::InitCharSets ( )
virtual

Define the character sets.

See also
DefineNameChars, DefineOprtChars, DefineInfixOprtChars

This function is used for initializing the default character sets that define the characters to be useable in function and variable names and operators.

Implements mu::ParserBase.

Definition at line 97 of file muParser.cpp.

Referenced by Parser().

98  {
99  DefineNameChars(_T("0123456789_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"));
100  DefineOprtChars(_T("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+-*^/?<>=#!$%&|~'_{}"));
101  DefineInfixOprtChars(_T("/+-*^?<>=#!$%&|~'_"));
102  }
void DefineInfixOprtChars(const char_type *a_szCharset)
Define the set of valid characters to be used in names of infix operators.
#define _T(x)
Activate this option in order to compile with OpenMP support.
Definition: muParserDef.h:69
void DefineNameChars(const char_type *a_szCharset)
Define the set of valid characters to be used in names of functions, variables, constants.
void DefineOprtChars(const char_type *a_szCharset)
Define the set of valid characters to be used in names of binary operators and postfix operators...
void mu::Parser::InitConst ( )
virtual

Initialize constants.

By default the parser recognizes two constants. Pi ("pi") and the Eulerian number ("_e").

Implements mu::ParserBase.

Definition at line 160 of file muParser.cpp.

Referenced by Parser().

161  {
162  DefineConst(_T("_pi"), MathImpl<value_type>::CONST_PI);
163  DefineConst(_T("_e"), MathImpl<value_type>::CONST_E);
164  }
#define _T(x)
Activate this option in order to compile with OpenMP support.
Definition: muParserDef.h:69
void DefineConst(const string_type &a_sName, value_type a_fVal)
Add a user defined constant.
void mu::Parser::InitOprt ( )
virtual

Initialize operators.

By default only the unary minus operator is added.

Implements mu::ParserBase.

Definition at line 171 of file muParser.cpp.

Referenced by Parser().

172  {
173  DefineInfixOprt(_T("-"), MathImpl<value_type>::UnaryMinus);
174  DefineInfixOprt(_T("+"), MathImpl<value_type>::UnaryPlus);
175  }
#define _T(x)
Activate this option in order to compile with OpenMP support.
Definition: muParserDef.h:69
void DefineInfixOprt(const string_type &a_strName, fun_type1 a_pOprt, int a_iPrec=prINFIX, bool a_bAllowOpt=true)
Add a user defined operator.
int mu::Parser::IsVal ( const char_type a_szExpr,
int *  a_iPos,
value_type a_fVal 
)
staticprotected

Default value recognition callback.

Parameters
[in]a_szExprPointer to the expression
[in,out]a_iPosPointer to an index storing the current position within the expression
[out]a_fValPointer where the value should be stored in case one is found.
Returns
1 if a value was found 0 otherwise.

Definition at line 55 of file muParser.cpp.

Referenced by Parser().

56  {
57  value_type fVal(0);
58 
59  stringstream_type stream(a_szExpr);
60  stream.seekg(0); // todo: check if this really is necessary
61  stream.imbue(Parser::s_locale);
62  stream >> fVal;
63  stringstream_type::pos_type iEnd = stream.tellg(); // Position after reading
64 
65  if (iEnd == (stringstream_type::pos_type) - 1)
66  return 0;
67 
68  *a_iPos += (int)iEnd;
69  *a_fVal = fVal;
70  return 1;
71  }
static std::locale s_locale
The locale used by the parser.
Definition: muParserBase.h:187
std::basic_stringstream< char_type, std::char_traits< char_type >, std::allocator< char_type > > stringstream_type
Typedef for easily using stringstream that respect the parser stringtype.
Definition: muParserDef.h:309
MUP_BASETYPE value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:294

The documentation for this class was generated from the following files: