NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
mu::Parser Class Reference

Mathematical expressions parser. More...

#include <muParser.h>

Inheritance diagram for mu::Parser:
Collaboration diagram for mu::Parser:

Public Member Functions

 Parser ()
 Constructor. More...
 
virtual void InitCharSets () override
 Define the character sets. More...
 
virtual void InitFun () override
 Initialize the default functions. More...
 
virtual void InitConst () override
 Initialize constants. More...
 
virtual void InitOprt () override
 Initialize operators. More...
 
virtual void OnDetectVar (string_type *pExpr, int &nStart, int &nEnd) override
 
value_type Diff (value_type *a_Var, value_type a_fPos, value_type a_fEpsilon=0, size_t order=1)
 Numerically differentiate with regard to a variable. More...
 
- Public Member Functions inherited from mu::ParserBase
void ActivateLoopMode (unsigned int _nLoopLength)
 Activates the loop mode and prepares the internal arrays for storing the necessary data. More...
 
void DeactivateLoopMode ()
 Deactivates the loop mode and resets the internal arrays. More...
 
void SetIndex (unsigned int _nLoopElement)
 Activates the selected position in the internally stored bytecode. More...
 
void SetCompiling (bool _bCompiling=true)
 Activate the compiling step for the parser. More...
 
bool IsCompiling ()
 Returns true, if the parser is currently in compiling step. More...
 
void CacheCurrentAccess (const CachedDataAccess &_access)
 Store the passed data access for this position internally. More...
 
size_t HasCachedAccess ()
 Evaluate, whether there are any cached data accesses for this position. More...
 
void DisableAccessCaching ()
 Disable the data access caching for this position. More...
 
bool CanCacheAccess ()
 Check, whether the current position can cache any data accesses. More...
 
const CachedDataAccessGetCachedAccess (size_t nthAccess)
 Returns the cached data access for the passed position. More...
 
void CacheCurrentEquation (const std::string &sEquation)
 Caches the passed equation for this position. More...
 
const std::string & GetCachedEquation () const
 Returns the stored equation for this position. More...
 
void CacheCurrentTarget (const std::string &sEquation)
 Caches the passed target equation for this position. More...
 
const std::string & GetCachedTarget () const
 Returns the stored target equation for this position. More...
 
int IsValidByteCode (unsigned int _nthLoopElement=-1, unsigned int _nthPartEquation=0)
 This member function returns, whether the current equation is already parsed and there's a valid bytecode for it. More...
 
bool ActiveLoopMode () const
 Check, whether the loop mode is active. This function returns true even if the loop mode is paused. More...
 
bool IsLockedPause () const
 Check, whether the pause mode is locked. More...
 
void LockPause (bool _bLock=true)
 This member function locks the pause mode so that it cannot be accidentally activated. More...
 
void PauseLoopMode (bool _bPause=true)
 This member function pauses the loop mode, so that the new assigned equation does not invalidate already parsed equations. More...
 
bool IsAlreadyParsed (StringView sNewEquation)
 This member function checks, whether the passed expression is already parsed, so that the parsing step may be omitted. More...
 
bool IsNotLastStackItem () const
 Check, whether there are more elements on the parsing stack remaining. More...
 
 ParserBase ()
 Constructor. More...
 
 ParserBase (const ParserBase &a_Parser)
 Copy constructor. More...
 
ParserBaseoperator= (const ParserBase &a_Parser)
 Assignement operator. More...
 
virtual ~ParserBase ()
 
value_type Eval ()
 Single-value wrapper around the vectorized overload of this member function. More...
 
value_typeEval (int &nStackSize)
 Evaluate an expression containing comma seperated subexpressions. More...
 
void Eval (value_type *results, int nBulkSize)
 This member function evaluates the expression in parallel. Is not used at the moment. More...
 
void SetExpr (StringView a_sExpr)
 Set the expression. Triggers first time calculation thus the creation of the bytecode and scanning of used variables. More...
 
void SetVarFactory (facfun_type a_pFactory, void *pUserData=NULL)
 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 optimizeAway=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 optimizeAway=true)
 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 optimizeAway=true)
 Add a user defined operator. More...
 
void DefineInfixOprt (const string_type &a_strName, fun_type1 a_pOprt, int a_iPrec=prINFIX, bool optimizeAway=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 ()
 Return a map containing the used variables only. More...
 
const varmap_typeGetVar () const
 Return a map containing the used variables only. More...
 
const valmap_typeGetConst () const
 Return a map containing all parser constants. More...
 
const string_typeGetExpr () const
 Retrieve the formula. More...
 
const funmap_typeGetFunDef () const
 Return prototypes of all parser functions. More...
 
const std::map< std::string, std::vector< mu::value_type > > & GetVectors () const
 
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. More...
 
void DefineOprtChars (const char_type *a_szCharset)
 Define the set of valid characters to be used in names of binary operators and postfix operators. More...
 
void DefineInfixOprtChars (const char_type *a_szCharset)
 Define the set of valid characters to be used in names of infix operators. More...
 
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. 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...
 
void Error (EErrorCodes a_iErrc, const string_type &a_Expr, 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...
 
string_type CreateTempVectorVar (const std::vector< mu::value_type > &vVar)
 This member function copies the passed vector into the internal storage referencing it with a auto-generated variable name. More...
 
void SetVectorVar (const std::string &sVarName, const std::vector< mu::value_type > &vVar, bool bAddVectorType=false)
 This member function copies the passed vector into the internal storage referencing it with the passed name. More...
 
std::vector< mu::value_type > * GetVectorVar (const std::string &sVarName)
 This member function returns a pointer to the vector stored internally. More...
 
void UpdateVectorVar (const std::string &sVarName)
 This member function updates the corresponding variable of a vector with the previously newly assigned value. More...
 
void ClearVectorVars (bool bIgnoreProcedureVects=false)
 This member function cleares the internal vector storage. More...
 
bool ContainsVectorVars (StringView sExpr, bool ignoreSingletons)
 This member function checks, whether the passed expression contains a vector. More...
 

Static Protected Member Functions

static value_type Sin (const value_type &)
 
static value_type Cos (const value_type &)
 
static value_type Tan (const value_type &)
 
static value_type Tan2 (const value_type &, const value_type &)
 
static value_type ASin (const value_type &)
 
static value_type ACos (const value_type &)
 
static value_type ATan (const value_type &)
 
static value_type ATan2 (const value_type &, const value_type &)
 
static value_type Sinh (const value_type &)
 
static value_type Cosh (const value_type &)
 
static value_type Tanh (const value_type &)
 
static value_type ASinh (const value_type &)
 
static value_type ACosh (const value_type &)
 
static value_type ATanh (const value_type &)
 
static value_type Log2 (const value_type &)
 
static value_type Log10 (const value_type &)
 
static value_type Ln (const value_type &)
 
static value_type Exp (const value_type &)
 
static value_type Abs (const value_type &)
 
static value_type Sqrt (const value_type &)
 
static value_type Rint (const value_type &)
 
static value_type Sign (const value_type &)
 
static value_type UnaryMinus (const value_type &)
 Callback for the unary minus operator. More...
 
static value_type Sum (const value_type *, int)
 Callback for adding multiple values. More...
 
static value_type Avg (const value_type *, int)
 Callback for averaging multiple values. More...
 
static value_type Min (const value_type *, int)
 Callback for determining the minimum value out of a vector. More...
 
static value_type Max (const value_type *, int)
 Callback for determining the maximum value out of a vector. More...
 
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 amd stack content on the console. More...
 
- Public Attributes inherited from mu::ParserBase
std::map< std::string, std::string > * mVarMapPntr
 
std::list< mu::value_type * > m_lDataStorage
 
- Protected Member Functions inherited from mu::ParserBase
void Init ()
 Initialize user defined functions. More...
 
virtual void InitCharSets ()=0
 
virtual void InitFun ()=0
 
virtual void InitConst ()=0
 
virtual void InitOprt ()=0
 
virtual void OnDetectVar (string_type *pExpr, int &nStart, int &nEnd)
 
- 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. More...
 
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.

(C) 2011 Ingo Berg
muparser(at)gmx.de

Definition at line 50 of file muParser.h.

Constructor & Destructor Documentation

◆ Parser()

mu::Parser::Parser ( )

Constructor.

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

Definition at line 241 of file muParser.cpp.

References mu::ParserBase::AddValIdent(), InitCharSets(), InitConst(), InitFun(), InitOprt(), and IsVal().

Here is the call graph for this function:

Member Function Documentation

◆ Abs()

value_type mu::Parser::Abs ( const value_type v)
staticprotected

Definition at line 132 of file muParser.cpp.

References date::abs().

Referenced by InitFun().

Here is the call graph for this function:

◆ ACos()

value_type mu::Parser::ACos ( const value_type v)
staticprotected

Definition at line 74 of file muParser.cpp.

References mu::MathImpl< T >::ACos().

Referenced by InitFun().

Here is the call graph for this function:

◆ ACosh()

value_type mu::Parser::ACosh ( const value_type v)
staticprotected

Definition at line 102 of file muParser.cpp.

References mu::MathImpl< T >::ACosh().

Referenced by InitFun().

Here is the call graph for this function:

◆ ASin()

value_type mu::Parser::ASin ( const value_type v)
staticprotected

Definition at line 70 of file muParser.cpp.

References mu::MathImpl< T >::ASin().

Referenced by InitFun().

Here is the call graph for this function:

◆ ASinh()

value_type mu::Parser::ASinh ( const value_type v)
staticprotected

Definition at line 98 of file muParser.cpp.

References mu::MathImpl< T >::ASinh().

Referenced by InitFun().

Here is the call graph for this function:

◆ ATan()

value_type mu::Parser::ATan ( const value_type v)
staticprotected

Definition at line 78 of file muParser.cpp.

References mu::MathImpl< T >::ATan().

Referenced by InitFun().

Here is the call graph for this function:

◆ ATan2()

value_type mu::Parser::ATan2 ( const value_type v1,
const value_type v2 
)
staticprotected

Definition at line 82 of file muParser.cpp.

References mu::MathImpl< T >::ATan2().

Referenced by InitFun().

Here is the call graph for this function:

◆ ATanh()

value_type mu::Parser::ATanh ( const value_type v)
staticprotected

Definition at line 106 of file muParser.cpp.

References mu::MathImpl< T >::ATanh().

Referenced by InitFun().

Here is the call graph for this function:

◆ Avg()

value_type mu::Parser::Avg ( const value_type a_afArg,
int  a_iArgc 
)
staticprotected

Callback for averaging multiple values.

Parameters
[in]a_afArgVector with the function arguments
[in]a_iArgcThe size of a_afArg

Definition at line 176 of file muParser.cpp.

References _nrT, and parser_Avg().

Referenced by InitFun().

Here is the call graph for this function:

◆ Cos()

value_type mu::Parser::Cos ( const value_type v)
staticprotected

Definition at line 62 of file muParser.cpp.

References mu::MathImpl< T >::Cos().

Referenced by InitFun().

Here is the call graph for this function:

◆ Cosh()

value_type mu::Parser::Cosh ( const value_type v)
staticprotected

Definition at line 90 of file muParser.cpp.

References mu::MathImpl< T >::Cosh().

Referenced by InitFun().

Here is the call graph for this function:

◆ Diff()

value_type mu::Parser::Diff ( value_type a_Var,
value_type  a_fPos,
value_type  a_fEpsilon = 0,
size_t  order = 1 
)

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.
[in]orderGives the order of 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) as suggested in the muparser forum:

http://sourceforge.net/forum/forum.php?thread_id=1994611&forum_id=462843

Definition at line 383 of file muParser.cpp.

References date::abs(), mu::ParserBase::Eval(), and intPower().

Referenced by differentiate(), findExtrema(), and localizeExtremum().

Here is the call graph for this function:

◆ Exp()

value_type mu::Parser::Exp ( const value_type v)
staticprotected

Definition at line 128 of file muParser.cpp.

References mu::MathImpl< T >::Exp().

Referenced by InitFun().

Here is the call graph for this function:

◆ InitCharSets()

void mu::Parser::InitCharSets ( )
overridevirtual

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 259 of file muParser.cpp.

References _nrT, mu::ParserBase::DefineInfixOprtChars(), mu::ParserBase::DefineNameChars(), and mu::ParserBase::DefineOprtChars().

Referenced by Parser().

Here is the call graph for this function:

◆ InitConst()

void mu::Parser::InitConst ( )
overridevirtual

Initialize constants.

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

Implements mu::ParserBase.

Definition at line 328 of file muParser.cpp.

References mu::ParserBase::DefineConst(), and PARSER_CONST_PI.

Referenced by Parser().

Here is the call graph for this function:

◆ InitFun()

void mu::Parser::InitFun ( )
overridevirtual

Initialize the default functions.

Implements mu::ParserBase.

Definition at line 268 of file muParser.cpp.

References Abs(), ACos(), ACosh(), ASin(), ASinh(), ATan(), ATan2(), ATanh(), Avg(), Cos(), Cosh(), mu::ParserBase::DefineFun(), Exp(), Ln(), Log10(), Log2(), Max(), Min(), Rint(), Sign(), Sin(), Sinh(), Sqrt(), Sum(), Tan(), and Tanh().

Referenced by Parser().

Here is the call graph for this function:

◆ InitOprt()

void mu::Parser::InitOprt ( )
overridevirtual

Initialize operators.

By default only the unary minus operator is added.

Implements mu::ParserBase.

Definition at line 338 of file muParser.cpp.

References mu::ParserBase::DefineInfixOprt(), and UnaryMinus().

Referenced by Parser().

Here is the call graph for this function:

◆ IsVal()

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 217 of file muParser.cpp.

References mu::ParserBase::s_locale.

Referenced by Parser().

◆ Ln()

value_type mu::Parser::Ln ( const value_type v)
staticprotected

Definition at line 121 of file muParser.cpp.

References mu::MathImpl< T >::Log().

Referenced by InitFun().

Here is the call graph for this function:

◆ Log10()

value_type mu::Parser::Log10 ( const value_type v)
staticprotected

Definition at line 117 of file muParser.cpp.

References mu::MathImpl< T >::Log10().

Referenced by InitFun().

Here is the call graph for this function:

◆ Log2()

value_type mu::Parser::Log2 ( const value_type v)
staticprotected

Definition at line 113 of file muParser.cpp.

References mu::MathImpl< T >::Log2().

Referenced by InitFun().

Here is the call graph for this function:

◆ Max()

value_type mu::Parser::Max ( const value_type a_afArg,
int  a_iArgc 
)
staticprotected

Callback for determining the maximum value out of a vector.

Parameters
[in]a_afArgVector with the function arguments
[in]a_iArgcThe size of a_afArg

Definition at line 202 of file muParser.cpp.

References _nrT, and parser_Max().

Referenced by InitFun().

Here is the call graph for this function:

◆ Min()

value_type mu::Parser::Min ( const value_type a_afArg,
int  a_iArgc 
)
staticprotected

Callback for determining the minimum value out of a vector.

Parameters
[in]a_afArgVector with the function arguments
[in]a_iArgcThe size of a_afArg

Definition at line 189 of file muParser.cpp.

References _nrT, and parser_Min().

Referenced by InitFun().

Here is the call graph for this function:

◆ OnDetectVar()

void mu::Parser::OnDetectVar ( string_type pExpr,
int &  nStart,
int &  nEnd 
)
overridevirtual

Reimplemented from mu::ParserBase.

Definition at line 344 of file muParser.cpp.

◆ Rint()

value_type mu::Parser::Rint ( const value_type v)
staticprotected

Definition at line 140 of file muParser.cpp.

Referenced by InitFun().

◆ Sign()

value_type mu::Parser::Sign ( const value_type v)
staticprotected

Definition at line 144 of file muParser.cpp.

Referenced by InitFun().

◆ Sin()

value_type mu::Parser::Sin ( const value_type v)
staticprotected

Definition at line 58 of file muParser.cpp.

References mu::MathImpl< T >::Sin().

Referenced by InitFun().

Here is the call graph for this function:

◆ Sinh()

value_type mu::Parser::Sinh ( const value_type v)
staticprotected

Definition at line 86 of file muParser.cpp.

References mu::MathImpl< T >::Sinh().

Referenced by InitFun().

Here is the call graph for this function:

◆ Sqrt()

value_type mu::Parser::Sqrt ( const value_type v)
staticprotected

Definition at line 136 of file muParser.cpp.

References mu::MathImpl< T >::Sqrt().

Referenced by InitFun().

Here is the call graph for this function:

◆ Sum()

value_type mu::Parser::Sum ( const value_type a_afArg,
int  a_iArgc 
)
staticprotected

Callback for adding multiple values.

Parameters
[in]a_afArgVector with the function arguments
[in]a_iArgcThe size of a_afArg

Definition at line 164 of file muParser.cpp.

References _nrT, and parser_Sum().

Referenced by InitFun().

Here is the call graph for this function:

◆ Tan()

value_type mu::Parser::Tan ( const value_type v)
staticprotected

Definition at line 66 of file muParser.cpp.

References mu::MathImpl< T >::Tan().

Referenced by InitFun().

Here is the call graph for this function:

◆ Tan2()

static value_type mu::Parser::Tan2 ( const value_type ,
const value_type  
)
staticprotected

◆ Tanh()

value_type mu::Parser::Tanh ( const value_type v)
staticprotected

Definition at line 94 of file muParser.cpp.

References mu::MathImpl< T >::Tanh().

Referenced by InitFun().

Here is the call graph for this function:

◆ UnaryMinus()

value_type mu::Parser::UnaryMinus ( const value_type v)
staticprotected

Callback for the unary minus operator.

Parameters
vThe value to negate
Returns
-v

Definition at line 154 of file muParser.cpp.

Referenced by InitOprt().


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