iLand
Classes | Public Member Functions | Static Public Member Functions | List of all members
Expression Class Reference

An expression engine for mathematical expressions provided as strings. More...

#include <expression.h>

Public Member Functions

 ~Expression ()
 
 Expression ()
 
 Expression (const QString &aExpression)
 
 Expression (const QString &expression, ExpressionWrapper *wrapper)
 
void setExpression (const QString &aExpression)
 set expression More...
 
void setAndParse (const QString &expr)
 set expression and parse instantly More...
 
void setModelObject (ExpressionWrapper *wrapper)
 
const QString & expression () const
 
void parse (ExpressionWrapper *wrapper=nullptr)
 force a parsing of the expression More...
 
void linearize (const double low_value, const double high_value, const int steps=1000)
 call linearize() to 'linarize' an expression, i.e. approximate the function by linear interpolation. More...
 
void linearize2d (const double low_x, const double high_x, const double low_y, const double high_y, const int stepsx=50, const int stepsy=50)
 lineraize2d works with two variables More...
 
double execute (double *varlist=nullptr, ExpressionWrapper *object=nullptr) const
 calculate formula and return result. variable values need to be set using "setVar()" More...
 
bool executeBool (double *varlist=nullptr, ExpressionWrapper *object=nullptr) const
 
double executeLocked ()
 thread safe version More...
 
double calculate (const double Val1=0., const double Val2=0., const bool forceExecution=false) const
 calculate formula. More...
 
bool calculateBool (const double Val1=0., const double Val2=0., const bool forceExecution=false) const
 
double calculate (ExpressionWrapper &object, const double variable_value1=0., const double variable_value2=0.) const
 calculate formula with object More...
 
double calculateBool (ExpressionWrapper &object, const double variable_value1=0., const double variable_value2=0.) const
 
void setVar (const QString &Var, double Value)
 set the value of the variable named "Var". Note: using addVar to obtain a pointer may be more efficient for multiple executions. More...
 
double * addVar (const QString &VarName)
 adds variable "VarName" and returns a double pointer to the variable. Use *ptr to set the value (before calling execute()) More...
 
double * getVarAdress (const QString &VarName)
 retrieve again the value pointer of a variable. More...
 
bool isConstExpression () const
 returns true if current expression is a constant. More...
 
bool isEmpty () const
 returns true if expression is empty More...
 
const QString & lastError () const
 
bool isStrict ()
 strict property: if true, variables must be named before execution. More...
 
void setStrict (bool str)
 
void setCatchExceptions (bool docatch=true)
 
void setExternalVarSpace (const QStringList &ExternSpaceNames, double *ExternSpace)
 
void enableIncSum ()
 

Static Public Member Functions

static void setLinearizationEnabled (const bool enable)
 global switch for linerization. If set to false, subsequent calls to linearize are ignored. More...
 
static void addConstant (const QString const_name, const double const_value)
 

Detailed Description

An expression engine for mathematical expressions provided as strings.

The main purpose is fast execution speed. notes regarding the syntax: +,-,*,/ as expected, additionally "^" for power. mod(x,y): modulo division, gets remainder of x/y functions:

Constructor & Destructor Documentation

◆ ~Expression()

Expression::~Expression ( )

◆ Expression() [1/3]

Expression::Expression ( )

◆ Expression() [2/3]

Expression::Expression ( const QString &  aExpression)
inline

◆ Expression() [3/3]

Expression::Expression ( const QString &  expression,
ExpressionWrapper wrapper 
)
inline

Member Function Documentation

◆ addConstant()

void Expression::addConstant ( const QString  const_name,
const double  const_value 
)
static

◆ addVar()

double * Expression::addVar ( const QString &  VarName)

adds variable "VarName" and returns a double pointer to the variable. Use *ptr to set the value (before calling execute())

◆ calculate() [1/2]

double Expression::calculate ( const double  Val1 = 0.,
const double  Val2 = 0.,
const bool  forceExecution = false 
) const

calculate formula.

the first two variables are assigned the values Val1 and Val2. This function is for convenience. the return is the result of the calculation. e.g.: x+3*y --> Val1->x, Val2->y forceExecution: do not apply linearization

◆ calculate() [2/2]

double Expression::calculate ( ExpressionWrapper object,
const double  variable_value1 = 0.,
const double  variable_value2 = 0. 
) const

calculate formula with object

◆ calculateBool() [1/2]

bool Expression::calculateBool ( const double  Val1 = 0.,
const double  Val2 = 0.,
const bool  forceExecution = false 
) const
inline

◆ calculateBool() [2/2]

double Expression::calculateBool ( ExpressionWrapper object,
const double  variable_value1 = 0.,
const double  variable_value2 = 0. 
) const
inline

◆ enableIncSum()

void Expression::enableIncSum ( )

◆ execute()

double Expression::execute ( double *  varlist = nullptr,
ExpressionWrapper object = nullptr 
) const

calculate formula and return result. variable values need to be set using "setVar()"

◆ executeBool()

bool Expression::executeBool ( double *  varlist = nullptr,
ExpressionWrapper object = nullptr 
) const
inline

◆ executeLocked()

double Expression::executeLocked ( )
inline

thread safe version

◆ expression()

const QString & Expression::expression ( ) const
inline

◆ getVarAdress()

double * Expression::getVarAdress ( const QString &  VarName)

retrieve again the value pointer of a variable.

◆ isConstExpression()

bool Expression::isConstExpression ( ) const
inline

returns true if current expression is a constant.

◆ isEmpty()

bool Expression::isEmpty ( ) const
inline

returns true if expression is empty

◆ isStrict()

bool Expression::isStrict ( )
inline

strict property: if true, variables must be named before execution.

When strict=true, all variables in the expression must be added by setVar or addVar. if false, variable values are assigned depending on occurence. strict is false by default for calls to "calculate()".

◆ lastError()

const QString & Expression::lastError ( ) const
inline

◆ linearize()

void Expression::linearize ( const double  low_value,
const double  high_value,
const int  steps = 1000 
)

call linearize() to 'linarize' an expression, i.e. approximate the function by linear interpolation.

Linarize an expression, i.e.

approximate the function by linear interpolation. This is an option for performance critical calculations that include time consuming mathematic functions (e.g. exp()) low_value: linearization start at this value. values below produce an error high_value: upper limit steps: number of steps the function is split into

◆ linearize2d()

void Expression::linearize2d ( const double  low_x,
const double  high_x,
const double  low_y,
const double  high_y,
const int  stepsx = 50,
const int  stepsy = 50 
)

lineraize2d works with two variables

like 'linearize()' but for 2d-matrices

◆ parse()

void Expression::parse ( ExpressionWrapper wrapper = nullptr)

force a parsing of the expression

◆ setAndParse()

void Expression::setAndParse ( const QString &  expr)

set expression and parse instantly

sets expression expr and checks the syntax (parse).

Expressions are setup with strict = false, i.e. no fixed binding of variable names.

◆ setCatchExceptions()

void Expression::setCatchExceptions ( bool  docatch = true)
inline

◆ setExpression()

void Expression::setExpression ( const QString &  aExpression)

set expression

set the current expression.

do some preprocessing (e.g. handle the different use of ",", ".", ";")

◆ setExternalVarSpace()

void Expression::setExternalVarSpace ( const QStringList &  ExternSpaceNames,
double *  ExternSpace 
)

◆ setLinearizationEnabled()

static void Expression::setLinearizationEnabled ( const bool  enable)
inlinestatic

global switch for linerization. If set to false, subsequent calls to linearize are ignored.

◆ setModelObject()

void Expression::setModelObject ( ExpressionWrapper wrapper)
inline

◆ setStrict()

void Expression::setStrict ( bool  str)
inline

◆ setVar()

void Expression::setVar ( const QString &  Var,
double  Value 
)

set the value of the variable named "Var". Note: using addVar to obtain a pointer may be more efficient for multiple executions.


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