PssMathParser  1.0.0
Parser of mathemathical expressions
 All Classes Namespaces Files Functions Variables Enumerations Macros Pages
PssMathParser::MathExpression Class Reference

Main functionality of this library. More...

#include <pssmathparser.h>

Inheritance diagram for PssMathParser::MathExpression:
[legend]
Collaboration diagram for PssMathParser::MathExpression:
[legend]

Public Member Functions

 MathExpression ()
 Constructor, constructor list only. More...
 
 ~MathExpression ()
 Destructor.
 
const string expression () const
 Getter of the loaded expression. More...
 
void setExpression (const string &a_expression)
 Load the infix expression to the object. More...
 
void setMath (const string &a_expression)
 Does all the steps needed to produce a result. More...
 
const string reversePolish () const
 Getter of the expression in reverse Polish notation. More...
 
bool setArgumentMap (const string &a_reversePolish)
 Setter of the Argument Map from user input of reverse polish. More...
 
bool setArgumentMap ()
 Populates the m_argumentMap. More...
 
bool expressionToReversePolish ()
 Creates the Reverse polish notation of the expression. More...
 
double calculateExpression ()
 Calculates the expression. More...
 
uint32_t reversePolishErrorNum ()
 Getter of m_reversePolishErrorNum. More...
 
const string reversePolishErrorString ()
 Getter of m_reversePolishErrorString. More...
 
uint32_t expressionErrorNum ()
 Getter of m_expressionErrorNum. More...
 
const string expressionErrorString ()
 Getter of m_expressionErrorString. More...
 
const string mathToString () const
 Gets the vector m_math into string. More...
 
const string mathToStringFull () const
 Gets the vector m_math into string, with explanation of elements. More...
 
bool expandMathExpression ()
 Expands the math expression with generated arguments. More...
 
void clear ()
 Clears all containters from data.
 
void setMathPrintPrecision (const uint16_t &mathPrintPrecision)
 Sets the precission (digits after comma) when printing numbers. More...
 
uint16_t getMathPrintPrecision () const
 Get the math precision for printing. More...
 
void setVariableDouble (const string &a_name, const double a_value)
 Sets the value of the variable from the argument map. More...
 
uint16_t getVariableSize () const
 Return the number of variables in the argumentMap.
 
- Public Member Functions inherited from PssMathParser::MathParser
virtual ~MathParser ()=0
 Virtual destructor because this class is pure virtual.
 

Static Public Member Functions

static double add (const double a_arg1, const double a_arg2)
 Overloaded addition of two doubles. More...
 
static double subtract (const double a_arg1, const double a_arg2)
 Overloaded subtraction of two doubles. More...
 
static double multiply (const double a_arg1, const double a_arg2)
 Overloaded multiplication of two doubles. More...
 
static double divide (const double a_arg1, const double a_arg2)
 Overloaded division of two doubles. More...
 
static bool isNumber (const string &a_str)
 Check if string is number. More...
 
static bool isSpecialCharacter (const char &a_char)
 Checks if the character exists in the vector of special chars. More...
 
static bool isSpecialNoParenthesis (const char &a_char)
 isSpecialCharacter but exclude parenthesis More...
 
static double convertStringToDouble (const string &a_str)
 Convert string to double using atof() More...
 
static double getResultdddOperator (double a_arg1, double a_arg2, const string &a_operatorKey)
 Returns the value of the function that operates on two args. More...
 
- Static Public Member Functions inherited from PssMathParser::MathParser
static MathParsermakeMathParser ()
 Factory constructor. More...
 

Static Public Attributes

static vector< char > specialChars
 Special characters found in the infix notation. More...
 
static unordered_map< string,
Operator
operatorMap
 Available operators in a map. More...
 
static unordered_map< string,
Argument
constantMap
 Available constants in a map. More...
 

Private Member Functions

size_t operatorMapSize ()
 Getter of the operatorMap number of elements (size) More...
 
bool hasOperatorMap (const string &a_operatorName) const
 Tests if the operatorMap has an operator with the given name. More...
 
bool hasConstantMap (const string &a_constantName) const
 Tests if the constantMap has a constant with the given name. More...
 
bool hasArgumentMap (const string &a_argumentName) const
 Tests if the m_argumentMap has an argument with the given name. More...
 
bool hasGeneratorMap (const string &a_generatorName) const
 Tests if the m_generatorVec has a generator with the given name. More...
 
bool isOperator (const string a_entityName) const
 Tests if the given name is an Operator. More...
 
bool isArgument (const string a_entityName) const
 Tests if the given name is an Argument. More...
 
bool isGenerator (const string a_entityName) const
 Tests if the given name is an Generator. More...
 
bool isOperatorOneArg (const string a_entityName) const
 Checks if the operator operates on one arg. More...
 
bool isOperatorTwoArg (const string a_entityName) const
 Checks if the operator operates on two args. More...
 
bool generateArgArgOp (const string &a_key1, const string &a_key2, const string &a_key3)
 Generates an Argument that is connected to a generator. More...
 
bool generateArgOp (const string &a_key1, const string &a_key2)
 Generates an Argument that is connected to a generator. More...
 
double getArgumentDoubleValue (const string &a_key) const
 Getter of the Argument m_dvalue. More...
 
double getConstantDoubleValue (const string &a_key) const
 Getter of the constant double value. More...
 
const ArgumentgetConstant (const string &a_key) const
 Gets pointer to the constant. More...
 
const OperatorgetOperator (const string &a_key) const
 Gets pointer to the operator. More...
 
GeneratorgetGenerator (const string &a_key)
 Gets pointer to the generator. More...
 
const ArgumentgetArgument (const string &a_key) const
 Gets pointer to the argument. More...
 
uint16_t getEntitySize (EntityType a_entityType) const
 Gets the number of specific entities. More...
 
EntityType entityType (const string &a_key) const
 Getter of the EntityType. More...
 
void setDoubleValueToArgument (const string &a_key, const double a_value)
 Setter of the m_dvalue of Argument. More...
 
const string createNewUserConstantName () const
 Creates new names for internaly defined arguments. More...
 
bool pushToReversePolish (const string &a_str, const char &a_char)
 Converts infix notation to Reverse Polish. More...
 
void appendToReversePolishString (const string &a_str)
 Appends elements to the Reverse Polish separated by space. More...
 

Private Attributes

uint32_t m_reversePolishError
 
string m_reversePolishErrorString
 
uint32_t m_expressionError
 
string m_expressionErrorString
 
string m_expression
 
string m_reversePolish
 
uint16_t m_mathPrintPrecision
 
unordered_map< string, Argumentm_argumentMap
 
vector< Generatorm_generatorVec
 
vector< string > m_math
 
vector< string > m_RPstack
 

Detailed Description

Main functionality of this library.

Everything from parsing expressions, converting to Reverse Polish notation and contruction of maps of generator for fast calculation is done here.

Constructor & Destructor Documentation

MathExpression::MathExpression ( )

Constructor, constructor list only.

Set the errors to zero

Member Function Documentation

double MathExpression::add ( const double  a_arg1,
const double  a_arg2 
)
static

Overloaded addition of two doubles.

Parameters
a_arg1
a_arg2
Returns
a_arg1+a_arg2
void MathExpression::appendToReversePolishString ( const string &  a_str)
private

Appends elements to the Reverse Polish separated by space.

Parameters
a_strEntity to be apended
double MathExpression::calculateExpression ( )
virtual

Calculates the expression.

This function calculates all the generators from the generator map going successively which yields in the end the result of the calculation of the whole expression.

Returns
double The result of the calculation, i.e. the value of the last argument in the map

Implements PssMathParser::MathParser.

double MathExpression::convertStringToDouble ( const string &  a_str)
static

Convert string to double using atof()

Parameters
a_str
Returns
double Value of the conversion
const string MathExpression::createNewUserConstantName ( ) const
private

Creates new names for internaly defined arguments.

Counts how many arguments are there and creates one from the new available name. Available names are of the form #AA, #AB, #AC, ..., #BA, #BB, ...

Returns
string The new name
double MathExpression::divide ( const double  a_arg1,
const double  a_arg2 
)
static

Overloaded division of two doubles.

Parameters
a_arg1
a_arg2
Returns
a_arg1/a_arg2
EntityType MathExpression::entityType ( const string &  a_key) const
private

Getter of the EntityType.

Parameters
a_keyName of the entity
Returns
EntityType Return of the entityType()
bool MathExpression::expandMathExpression ( )
virtual

Expands the math expression with generated arguments.

This function extracts the expression to calculation steps. It expands the m_math expression to a one-step calculations arguments (generated arguments) and unused operators. These arguments don't exist in the original expression but represent in-between steps of calculation (temporary varables). The extended expression ends with an argument which is the result of the calculation. This way we form a structure that can later be used as an arbitrary function for any user input variables.

Example 1:

infix expression: (10 + 2) - 3 + 5
reverse polish: 10 2 + 3 - 5 +
expanded math: 10 2 + 3 - 5 + a 3 - 5 + c 5 + d (where: a=10+2; c=a-3; d=c+5;)

Example 2:

infix expression: 162 / (2 + 1 ) ^4
reverse polish: 162 2 1 + 4 ^ /
expanded math: 162 2 1 + 4 ^ / 162 a 4 ^ / 162 b / c (where: a=2+1; b=a^4; c=162/b;)

Returns
true If successful expression extraction
false If some error happened. This sets the members m_expressionError and m_expressionErrorString

Implements PssMathParser::MathParser.

const string MathExpression::expression ( ) const
virtual

Getter of the loaded expression.

Returns
string The loaded infix m_expression of the object

Implements PssMathParser::MathParser.

uint32_t MathExpression::expressionErrorNum ( )

Getter of m_expressionErrorNum.

Returns
uint32_t The m_expressionError
const string MathExpression::expressionErrorString ( )

Getter of m_expressionErrorString.

Returns
string The m_expressionErrorString
bool MathExpression::expressionToReversePolish ( )
virtual

Creates the Reverse polish notation of the expression.

Start parsing the infix notation. On every full argument or operator (entity) that is found, a call to the pushToReversePolish function is performed to reorder the entities in a reverse polish notation. This function can change the setted expression. One change is adding 0 in front of signed variables thus making a sign into an operator. Other change is adding parenthesis arround the ^ operator because it is right to left operator (meaning 2^3^4 = 2^(3^4)). Main functionality of this algorithm is to recognize separate entities from the infix notation. Then it sends this data to the pushToReversePolish which only rearanges the entries by preceedence and parenthesis.

Returns
true Successful creation of the reverse polish
false Some error occured

Implements PssMathParser::MathParser.

bool MathExpression::generateArgArgOp ( const string &  a_key1,
const string &  a_key2,
const string &  a_key3 
)
private

Generates an Argument that is connected to a generator.

Creates new argument and ads it to the m_argumentMap. Ads the generator to he m_generattorMap. Ads entity to the m_math.

Parameters
a_key1Argument 1 name
a_key2Argument 2 name
a_key3Operator name
Returns
true All went ok
false Some error happened
bool MathExpression::generateArgOp ( const string &  a_key1,
const string &  a_key2 
)
private

Generates an Argument that is connected to a generator.

Creates new argument and ads it to the m_argumentMap. Ads the generator to the m_generattorMap. Ads entity to the m_math.

Parameters
a_key1Argument name
a_key2Operator name
Returns
true All went ok
false Some error happened
const Argument * MathExpression::getArgument ( const string &  a_key) const
private

Gets pointer to the argument.

Parameters
a_keyName of the argument
Returns
*Argument If the argument exists in m_argumentMap
nullptr If the argument doesn't exist
double MathExpression::getArgumentDoubleValue ( const string &  a_key) const
private

Getter of the Argument m_dvalue.

Parameters
a_keyName of the argument
Returns
double Value of m_dvalue
const Argument * MathExpression::getConstant ( const string &  a_key) const
private

Gets pointer to the constant.

Parameters
a_keyName of the constant
Returns
*Argument If the constant exists in constantMap
nullptr If the constant doesn't exist
double MathExpression::getConstantDoubleValue ( const string &  a_key) const
private

Getter of the constant double value.

Parameters
a_keyName of the constant
Returns
double Value of the constant
uint16_t MathExpression::getEntitySize ( EntityType  a_entityType) const
private

Gets the number of specific entities.

This is used to count the Arguments for creating a new argument.

Parameters
a_entityTypeThis is one of the entities
Returns
uint16_t Number of entities
Generator * MathExpression::getGenerator ( const string &  a_key)
private

Gets pointer to the generator.

Parameters
a_keyName of the generator
Returns
*Generator If the generator exists in m_generatorVec
nullptr If the generator doesn't exist
uint16_t MathExpression::getMathPrintPrecision ( ) const
virtual

Get the math precision for printing.

Returns
uint16_t The precision when printing

Implements PssMathParser::MathParser.

const Operator * MathExpression::getOperator ( const string &  a_key) const
private

Gets pointer to the operator.

Parameters
a_keyName of the operator
Returns
*Operator If the operator exists in operatorMap
nullptr If the operator doesn't exist
double MathExpression::getResultdddOperator ( double  a_arg1,
double  a_arg2,
const string &  a_operatorKey 
)
static

Returns the value of the function that operates on two args.

Parameters
a_arg1First argument
a_arg2Secont Argument
a_operatorKeyName of the operator or function
Returns
double Result of the function(a_arg1, a_arg2)
bool MathExpression::hasArgumentMap ( const string &  a_argumentName) const
private

Tests if the m_argumentMap has an argument with the given name.

Parameters
a_argumentNameName of the tested argument
Returns
true Exists argument with the given name
false Doesn't exist argument with the given name
bool MathExpression::hasConstantMap ( const string &  a_constantName) const
private

Tests if the constantMap has a constant with the given name.

Parameters
a_constantNameName of the tested constant
Returns
true Exists constant with the given name
false Doesn't exist constant with the given name
bool MathExpression::hasGeneratorMap ( const string &  a_generatorName) const
private

Tests if the m_generatorVec has a generator with the given name.

Parameters
a_generatorNameName of the tested generator
Returns
true Exists generator with the given name
false Doesn't exist generator with the given name
bool MathExpression::hasOperatorMap ( const string &  a_operatorName) const
private

Tests if the operatorMap has an operator with the given name.

Parameters
a_operatorNameName of the tested operator
Returns
true Exists operator with the given name
false Doesn't exist operator with the given name
bool MathExpression::isArgument ( const string  a_entityName) const
private

Tests if the given name is an Argument.

Arguments are either in the m_argumentMap or in the constantMap. We check if in one of these maps.

Parameters
a_entityNameThe name of the entity to be checked
Returns
true It is an argument
false It is not an argument
bool MathExpression::isGenerator ( const string  a_entityName) const
private

Tests if the given name is an Generator.

This is same as hasGeneratorMap function, but is here for completness with other functions.

Parameters
a_entityNameThe name of the entity to be checked
Returns
true It is a generator
false It is not a generator
bool MathExpression::isNumber ( const string &  a_str)
static

Check if string is number.

Parameters
a_strString to test if it is number
Returns
true Is number
false Is not number
bool MathExpression::isOperator ( const string  a_entityName) const
private

Tests if the given name is an Operator.

This is same as hasOperatorMap function, but is here for completness with other functions.

Parameters
a_entityNameThe name of the entity to be checked
Returns
true It is an operator
false It is not an operator
bool MathExpression::isOperatorOneArg ( const string  a_entityName) const
private

Checks if the operator operates on one arg.

Parameters
a_entityNameThe operator name
Returns
true Operates on one arg
false Doesn't operate on one arg
bool MathExpression::isOperatorTwoArg ( const string  a_entityName) const
private

Checks if the operator operates on two args.

Parameters
a_entityNameThe operator name
Returns
true Operates on two args
false Doesn't operate on two args
bool MathExpression::isSpecialCharacter ( const char &  a_char)
static

Checks if the character exists in the vector of special chars.

Parameters
a_char
Returns
true Character is special
false Character doesn't exist in the vector of special chars
bool MathExpression::isSpecialNoParenthesis ( const char &  a_char)
static

isSpecialCharacter but exclude parenthesis

Parameters
a_char
Returns
true Character is special
false Character doesn't exist in the vector of special chars
const string MathExpression::mathToString ( ) const
virtual

Gets the vector m_math into string.

Returns
string String of elements of m_math separated by coma

Implements PssMathParser::MathParser.

const string MathExpression::mathToStringFull ( ) const
virtual

Gets the vector m_math into string, with explanation of elements.

Returns
string String of elements of m_math separated by coma with explanation

Implements PssMathParser::MathParser.

double MathExpression::multiply ( const double  a_arg1,
const double  a_arg2 
)
static

Overloaded multiplication of two doubles.

Parameters
a_arg1
a_arg2
Returns
a_arg1*a_arg2
size_t MathExpression::operatorMapSize ( )
private

Getter of the operatorMap number of elements (size)

Returns
size_t Unsigned int, the operatorMap size
bool MathExpression::pushToReversePolish ( const string &  a_str,
const char &  a_char 
)
private

Converts infix notation to Reverse Polish.

Uses the Shunting-yard algorithm to reorder infix notation coming successively as it is written. The algorithm needs first to recognize the type of the argument (variable, constant, operator...). Then it parses the operator precednce and pushes to stack. The m_RPstack acts as containter while the infix is being read. The RP notation is build in the m_reversePolish as a string.

The algorithm shorlty goes as follows:

``` if a_str is a number or variable then push it to the front of the output; else if a_str is an operator, then: find the first '(' and make this position iend or if there is no '(' make end of the stack position the iend; loop from i=0 to iend: if the i operator from the stack has greater than or equal precedence than a_str: pop from front of the operators stack onto the output queue; reset i and iend because of poping the operator stack; push to front the operator a_str; else if a_str is '(' push to front of stack; else if a_str is ')': pop from front of stack to output until the first '('; if there is no '(' then raise error;

if it is the last entry from the infix push all stack entries to the output; ```

Parameters
a_strVariable, constant, number or operator as a string
a_charExplains the a_str ('n'=number, 's'=special, 'a'=alpha, 'N'=last number, 'S'=last special, 'A'=last alpha)
Returns
true Construction of RP is ok
false Construction of the RP went wrong
const string MathExpression::reversePolish ( ) const
virtual

Getter of the expression in reverse Polish notation.

Returns
m_reversePolish The string that is the reverse polish of the m_expression

Implements PssMathParser::MathParser.

uint32_t MathExpression::reversePolishErrorNum ( )

Getter of m_reversePolishErrorNum.

Returns
uint32_t The m_reversePolishError
const string MathExpression::reversePolishErrorString ( )

Getter of m_reversePolishErrorString.

Returns
string The m_reversePolishErrorString
bool MathExpression::setArgumentMap ( const string &  a_reversePolish)
virtual

Setter of the Argument Map from user input of reverse polish.

This function exist if you want to load the m_argumentMap from polish notation from the user string input and not to be calculated from the infix. This also sets the m_reversePolish to the given input.

Parameters
a_reversePolishThe reverse polish with space as a separator
Returns
true Everything was ok
false Some error

Implements PssMathParser::MathParser.

bool MathExpression::setArgumentMap ( )
virtual

Populates the m_argumentMap.

Traverses the m_reversePolish notation and creates internally defined variables, constants and user defined constants. This new names are written in the map m_math.

Returns
true Everything was ok
false Some error

Implements PssMathParser::MathParser.

void MathExpression::setDoubleValueToArgument ( const string &  a_key,
const double  a_value 
)
private

Setter of the m_dvalue of Argument.

Parameters
a_keyName of the argument
a_valueValue to set
void MathExpression::setExpression ( const string &  a_expression)
virtual

Load the infix expression to the object.

Clear the m_expression. Set m_expression and remove spaces and tabs.

Parameters
a_expressionString with the infix expression

Implements PssMathParser::MathParser.

void MathExpression::setMath ( const string &  a_expression)
virtual

Does all the steps needed to produce a result.

These are the steps that produce full expression that can give result:

```c++ setExpression(a_expression); expressionToReversePolish(); setArgumentMap(); expandMathExpression(); ```

Parameters
a_expression

Implements PssMathParser::MathParser.

void MathExpression::setMathPrintPrecision ( const uint16_t &  mathPrintPrecision)
virtual

Sets the precission (digits after comma) when printing numbers.

Parameters
mathPrintPrecisionInteger of precission default is 7

Implements PssMathParser::MathParser.

void MathExpression::setVariableDouble ( const string &  a_name,
const double  a_value 
)
virtual

Sets the value of the variable from the argument map.

Parameters
a_nameName of the variable
a_valueValue of the variable (double)

Implements PssMathParser::MathParser.

double MathExpression::subtract ( const double  a_arg1,
const double  a_arg2 
)
static

Overloaded subtraction of two doubles.

Parameters
a_arg1
a_arg2
Returns
a_arg1-a_arg2

Member Data Documentation

unordered_map< string, Argument > MathExpression::constantMap
static

Available constants in a map.

Defined constants

The map hold the constant name as a key. The map values are preset here. These keys are also reserved words for the writing expressions.

unordered_map<string, Argument> PssMathParser::MathExpression::m_argumentMap
private

Map of Arguments

string PssMathParser::MathExpression::m_expression
private

The infix notation

uint32_t PssMathParser::MathExpression::m_expressionError
private

Error num in the infix notation

string PssMathParser::MathExpression::m_expressionErrorString
private

Error string in the infix notation

vector<Generator> PssMathParser::MathExpression::m_generatorVec
private

Vector of generators

vector<string> PssMathParser::MathExpression::m_math
private

The expression in entities

uint16_t PssMathParser::MathExpression::m_mathPrintPrecision
private

Precision when printing numbers

string PssMathParser::MathExpression::m_reversePolish
private

The RP notation

uint32_t PssMathParser::MathExpression::m_reversePolishError
private

Error num in the RP notation

string PssMathParser::MathExpression::m_reversePolishErrorString
private

Error string in the RP notation

vector<string> PssMathParser::MathExpression::m_RPstack
private

Stack for the Shunting-yard algorithm

unordered_map< string, Operator > MathExpression::operatorMap
static

Available operators in a map.

Defined operators

The map hold the operator name as a key. The map values are set to point to a function. These keys are also reserved words for the writing expressions.

vector< char > MathExpression::specialChars
static
Initial value:
=
{
'+',
'-',
'*',
'/',
'^',
'(',
')'
}

Special characters found in the infix notation.

Special characters

The vector holds the special characters in the infix notation. Operators and parenthesis.


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