NCBI C++ ToolKit
Classes | Public Member Functions | Static Private Member Functions | Static Private Attributes | List of all members
mu::Parser Class Reference

Search Toolkit Book for mu::Parser

Mathematical expressions parser. More...

#include <gui/utils/muparser/muParser.h>

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

Classes

class  change_dec_sep
 A facet class used to change decimal and thousands separator. More...
 

Public Member Functions

 Parser ()
 Constructor. More...
 
virtual void InitCharSets ()
 Define the character sets. More...
 
virtual void InitFun ()
 Initialize the default functions. More...
 
virtual void InitConst ()
 Initialize constants. More...
 
virtual void InitOprt ()
 Initialize operators. More...
 
void SetDecSep (char_type cDecSep)
 Set the decimal separator. More...
 
void SetThousandsSep (char_type cThousandsSep)
 Sets the thousands operator. More...
 
value_type Diff (value_type *a_Var, value_type a_fPos, value_type a_fEpsilon=0.00074) const
 Numerically differentiate with regard to a variable. More...
 
- Public Member Functions inherited from mu::ParserBase
 ParserBase ()
 Constructor. More...
 
 ParserBase (const ParserBase &a_Parser)
 
ParserBaseoperator= (const ParserBase &a_Parser)
 Assignement operator. More...
 
virtual ~ParserBase ()
 
value_type Eval () const
 Calculate the result. More...
 
void SetExpr (const string_type &a_sExpr)
 Set the formula. More...
 
void SetVarFactory (facfun_type a_pFactory, void *pUserData=NULL)
 
void EnableOptimizer (bool a_bIsOn=true)
 Enable or disable the formula optimization feature. More...
 
void EnableByteCode (bool a_bIsOn=true)
 Enable or disable parsing from Bytecode. 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)
 
void DefineFun (const string_type &a_strName, fun_type0 a_pFun, bool a_bAllowOpt=true)
 
void DefineFun (const string_type &a_strName, fun_type1 a_pFun, bool a_bAllowOpt=true)
 
void DefineFun (const string_type &a_strName, fun_type2 a_pFun, bool a_bAllowOpt=true)
 
void DefineFun (const string_type &a_strName, fun_type3 a_pFun, bool a_bAllowOpt=true)
 
void DefineFun (const string_type &a_strName, fun_type4 a_pFun, bool a_bAllowOpt=true)
 
void DefineFun (const string_type &a_strName, fun_type5 a_pFun, bool a_bAllowOpt=true)
 
void DefineFun (const string_type &a_strName, multfun_type a_pFun, bool a_bAllowOpt=true)
 
void DefineFun (const string_type &a_strName, strfun_type1 a_pFun, bool a_bAllowOpt=true)
 
void DefineFun (const string_type &a_strName, strfun_type2 a_pFun, bool a_bAllowOpt=true)
 
void DefineFun (const string_type &a_strName, strfun_type3 a_pFun, bool a_bAllowOpt=true)
 
void DefineOprt (const string_type &a_strName, fun_type2 a_pFun, unsigned a_iPri=0, 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. 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 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...
 

Static Private Member Functions

static value_type Sin (value_type)
 
static value_type Cos (value_type)
 
static value_type Tan (value_type)
 
static value_type ASin (value_type)
 
static value_type ACos (value_type)
 
static value_type ATan (value_type)
 
static value_type Sinh (value_type)
 
static value_type Cosh (value_type)
 
static value_type Tanh (value_type)
 
static value_type ASinh (value_type)
 
static value_type ACosh (value_type)
 
static value_type ATanh (value_type)
 
static value_type Log2 (value_type)
 
static value_type Log10 (value_type)
 
static value_type Ln (value_type)
 
static value_type Exp (value_type)
 
static value_type Abs (value_type)
 
static value_type Sqrt (value_type)
 
static value_type Rint (value_type)
 
static value_type Sign (value_type)
 
static value_type Ite (value_type, value_type, value_type)
 Conditional (if then else). More...
 
static value_type UnaryMinus (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...
 

Static Private Attributes

static std::locale s_locale = std::locale("C")
 The locale used by the parser. More...
 

Additional Inherited Members

- Public Types inherited from mu::ParserBase
typedef ParserError exception_type
 Type of the error class. More...
 
- Protected Member Functions inherited from mu::ParserBase
void Init ()
 Initialize user defined functions. More...
 
- Static Protected Attributes inherited from mu::ParserBase
static char_typec_DefaultOprt []
 Identifiers for built in binary operators. More...
 

Detailed Description

Mathematical expressions parser.

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

(C) 2004-2008 Ingo Berg
ingo_berg(at)gmx.de

Definition at line 52 of file muParser.h.

Constructor & Destructor Documentation

◆ Parser()

Parser::Parser ( )

Constructor.

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

Definition at line 216 of file muParser.cpp.

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

Member Function Documentation

◆ Abs()

value_type Parser::Abs ( value_type  v)
staticprivate

Definition at line 79 of file muParser.cpp.

References fabs.

Referenced by InitFun().

◆ ACos()

value_type Parser::ACos ( value_type  v)
staticprivate

Definition at line 61 of file muParser.cpp.

Referenced by InitFun().

◆ ACosh()

value_type Parser::ACosh ( value_type  v)
staticprivate

Definition at line 67 of file muParser.cpp.

References log.

Referenced by InitFun().

◆ ASin()

value_type Parser::ASin ( value_type  v)
staticprivate

Definition at line 60 of file muParser.cpp.

Referenced by InitFun().

◆ ASinh()

value_type Parser::ASinh ( value_type  v)
staticprivate

Definition at line 66 of file muParser.cpp.

References log.

Referenced by InitFun().

◆ ATan()

value_type Parser::ATan ( value_type  v)
staticprivate

Definition at line 62 of file muParser.cpp.

Referenced by InitFun().

◆ ATanh()

value_type Parser::ATanh ( value_type  v)
staticprivate

Definition at line 68 of file muParser.cpp.

References log.

Referenced by InitFun().

◆ Avg()

value_type Parser::Avg ( const value_type a_afArg,
int  a_iArgc 
)
staticprivate

Callback for averaging multiple values.

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

Definition at line 126 of file muParser.cpp.

References i, and wxT.

Referenced by InitFun().

◆ Cos()

value_type Parser::Cos ( value_type  v)
staticprivate

Definition at line 58 of file muParser.cpp.

Referenced by InitFun().

◆ Cosh()

value_type Parser::Cosh ( value_type  v)
staticprivate

Definition at line 64 of file muParser.cpp.

Referenced by InitFun().

◆ Diff()

value_type Parser::Diff ( value_type a_Var,
value_type  a_fPos,
value_type  a_fEpsilon = 0.00074 
) 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 numerical_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 344 of file muParser.cpp.

References mu::ParserBase::Eval(), and f().

◆ Exp()

value_type Parser::Exp ( value_type  v)
staticprivate

Definition at line 78 of file muParser.cpp.

Referenced by InitFun().

◆ InitCharSets()

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

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

Referenced by Parser().

◆ InitConst()

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

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

Referenced by Parser().

◆ InitFun()

void Parser::InitFun ( )
virtual

Initialize the default functions.

Implements mu::ParserBase.

Definition at line 243 of file muParser.cpp.

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

Referenced by Parser().

◆ InitOprt()

void Parser::InitOprt ( )
virtual

Initialize operators.

By default only the unary minus operator is added.

Implements mu::ParserBase.

Definition at line 325 of file muParser.cpp.

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

Referenced by Parser().

◆ IsVal()

int Parser::IsVal ( const char_type a_szExpr,
int a_iPos,
value_type a_fVal 
)
staticprivate

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

Referenced by Parser().

◆ Ite()

value_type Parser::Ite ( value_type  v1,
value_type  v2,
value_type  v3 
)
staticprivate

Conditional (if then else).

Parameters
v1Condition
v2First value
v3Second value
Returns
v2 if v1!=0 v3 otherwise.

Definition at line 91 of file muParser.cpp.

References v2.

Referenced by InitFun().

◆ Ln()

value_type Parser::Ln ( value_type  v)
staticprivate

Definition at line 74 of file muParser.cpp.

References log.

Referenced by InitFun().

◆ Log10()

value_type Parser::Log10 ( value_type  v)
staticprivate

Definition at line 73 of file muParser.cpp.

References log10().

Referenced by InitFun().

◆ Log2()

value_type Parser::Log2 ( value_type  v)
staticprivate

Definition at line 72 of file muParser.cpp.

References log.

Referenced by InitFun().

◆ Max()

value_type Parser::Max ( const value_type a_afArg,
int  a_iArgc 
)
staticprivate

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

References i, max(), and wxT.

Referenced by InitFun().

◆ Min()

value_type Parser::Min ( const value_type a_afArg,
int  a_iArgc 
)
staticprivate

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

References i, min(), and wxT.

Referenced by InitFun().

◆ Rint()

value_type Parser::Rint ( value_type  v)
staticprivate

Definition at line 81 of file muParser.cpp.

Referenced by InitFun().

◆ SetDecSep()

void Parser::SetDecSep ( char_type  cDecSep)

Set the decimal separator.

Parameters
cDecSepDecimal separator as a character value.
See also
SetThousandsSep

By default muparser uses the "C" locale. The decimal separator of this locale is overwritten by the one provided here.

Definition at line 300 of file muParser.cpp.

References locale, MUP_USE_FACET, and s_locale.

◆ SetThousandsSep()

void Parser::SetThousandsSep ( char_type  cThousandsSep)

Sets the thousands operator.

Parameters
cThousandsSepThe thousands separator as a character
See also
SetDecSep

By default muparser uses the "C" locale. The thousands separator of this locale is overwritten by the one provided here.

Definition at line 314 of file muParser.cpp.

References locale, MUP_USE_FACET, and s_locale.

◆ Sign()

value_type Parser::Sign ( value_type  v)
staticprivate

Definition at line 82 of file muParser.cpp.

Referenced by InitFun().

◆ Sin()

value_type Parser::Sin ( value_type  v)
staticprivate

Definition at line 57 of file muParser.cpp.

Referenced by InitFun().

◆ Sinh()

value_type Parser::Sinh ( value_type  v)
staticprivate

Definition at line 63 of file muParser.cpp.

Referenced by InitFun().

◆ Sqrt()

value_type Parser::Sqrt ( value_type  v)
staticprivate

Definition at line 80 of file muParser.cpp.

Referenced by InitFun().

◆ Sum()

value_type Parser::Sum ( const value_type a_afArg,
int  a_iArgc 
)
staticprivate

Callback for adding multiple values.

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

Definition at line 111 of file muParser.cpp.

References i, and wxT.

Referenced by InitFun().

◆ Tan()

value_type Parser::Tan ( value_type  v)
staticprivate

Definition at line 59 of file muParser.cpp.

Referenced by InitFun().

◆ Tanh()

value_type Parser::Tanh ( value_type  v)
staticprivate

Definition at line 65 of file muParser.cpp.

Referenced by InitFun().

◆ UnaryMinus()

value_type Parser::UnaryMinus ( value_type  v)
staticprivate

Callback for the unary minus operator.

Parameters
vThe value to negate
Returns
-v

Definition at line 101 of file muParser.cpp.

Referenced by InitOprt().

Member Data Documentation

◆ s_locale

std::locale Parser::s_locale = std::locale("C")
staticprivate

The locale used by the parser.

Definition at line 149 of file muParser.h.

Referenced by SetDecSep(), and SetThousandsSep().


The documentation for this class was generated from the following files:
Modified on Sat Feb 24 07:47:53 2024 by modify_doxy.py rev. 669887