|
PssMathParser
1.0.0
Parser of mathemathical expressions
|
Main functionality of this library. More...
#include <pssmathparser.h>
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 MathParser * | makeMathParser () |
| 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 Argument * | getConstant (const string &a_key) const |
| Gets pointer to the constant. More... | |
| const Operator * | getOperator (const string &a_key) const |
| Gets pointer to the operator. More... | |
| Generator * | getGenerator (const string &a_key) |
| Gets pointer to the generator. More... | |
| const Argument * | getArgument (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, Argument > | m_argumentMap |
| vector< Generator > | m_generatorVec |
| vector< string > | m_math |
| vector< string > | m_RPstack |
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.
| MathExpression::MathExpression | ( | ) |
Constructor, constructor list only.
Set the errors to zero
|
static |
Overloaded addition of two doubles.
| a_arg1 | |
| a_arg2 |
|
private |
Appends elements to the Reverse Polish separated by space.
| a_str | Entity to be apended |
|
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.
Implements PssMathParser::MathParser.
|
static |
Convert string to double using atof()
| a_str |
|
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, ...
|
static |
Overloaded division of two doubles.
| a_arg1 | |
| a_arg2 |
|
private |
Getter of the EntityType.
| a_key | Name of the entity |
|
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;)
Implements PssMathParser::MathParser.
|
virtual |
Getter of the loaded expression.
Implements PssMathParser::MathParser.
| uint32_t MathExpression::expressionErrorNum | ( | ) |
Getter of m_expressionErrorNum.
| const string MathExpression::expressionErrorString | ( | ) |
Getter of m_expressionErrorString.
|
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.
Implements PssMathParser::MathParser.
|
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.
|
private |
|
private |
Gets pointer to the argument.
| a_key | Name of the argument |
|
private |
Getter of the Argument m_dvalue.
| a_key | Name of the argument |
|
private |
Gets pointer to the constant.
| a_key | Name of the constant |
|
private |
Getter of the constant double value.
| a_key | Name of the constant |
|
private |
Gets the number of specific entities.
This is used to count the Arguments for creating a new argument.
| a_entityType | This is one of the entities |
|
private |
Gets pointer to the generator.
| a_key | Name of the generator |
|
virtual |
Get the math precision for printing.
Implements PssMathParser::MathParser.
|
private |
Gets pointer to the operator.
| a_key | Name of the operator |
|
static |
Returns the value of the function that operates on two args.
| a_arg1 | First argument |
| a_arg2 | Secont Argument |
| a_operatorKey | Name of the operator or function |
|
private |
Tests if the m_argumentMap has an argument with the given name.
| a_argumentName | Name of the tested argument |
|
private |
Tests if the constantMap has a constant with the given name.
| a_constantName | Name of the tested constant |
|
private |
Tests if the m_generatorVec has a generator with the given name.
| a_generatorName | Name of the tested generator |
|
private |
Tests if the operatorMap has an operator with the given name.
| a_operatorName | Name of the tested operator |
|
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.
| a_entityName | The name of the entity to be checked |
|
private |
Tests if the given name is an Generator.
This is same as hasGeneratorMap function, but is here for completness with other functions.
| a_entityName | The name of the entity to be checked |
|
static |
Check if string is number.
| a_str | String to test if it is number |
|
private |
Tests if the given name is an Operator.
This is same as hasOperatorMap function, but is here for completness with other functions.
| a_entityName | The name of the entity to be checked |
|
private |
Checks if the operator operates on one arg.
| a_entityName | The operator name |
|
private |
Checks if the operator operates on two args.
| a_entityName | The operator name |
|
static |
Checks if the character exists in the vector of special chars.
| a_char |
|
static |
isSpecialCharacter but exclude parenthesis
| a_char |
|
virtual |
Gets the vector m_math into string.
Implements PssMathParser::MathParser.
|
virtual |
Gets the vector m_math into string, with explanation of elements.
Implements PssMathParser::MathParser.
|
static |
Overloaded multiplication of two doubles.
| a_arg1 | |
| a_arg2 |
|
private |
Getter of the operatorMap number of elements (size)
|
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; ```
| a_str | Variable, constant, number or operator as a string |
| a_char | Explains the a_str ('n'=number, 's'=special, 'a'=alpha, 'N'=last number, 'S'=last special, 'A'=last alpha) |
|
virtual |
Getter of the expression in reverse Polish notation.
Implements PssMathParser::MathParser.
| uint32_t MathExpression::reversePolishErrorNum | ( | ) |
Getter of m_reversePolishErrorNum.
| const string MathExpression::reversePolishErrorString | ( | ) |
Getter of m_reversePolishErrorString.
|
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.
| a_reversePolish | The reverse polish with space as a separator |
Implements PssMathParser::MathParser.
|
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.
Implements PssMathParser::MathParser.
|
private |
Setter of the m_dvalue of Argument.
| a_key | Name of the argument |
| a_value | Value to set |
|
virtual |
Load the infix expression to the object.
Clear the m_expression. Set m_expression and remove spaces and tabs.
| a_expression | String with the infix expression |
Implements PssMathParser::MathParser.
|
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(); ```
| a_expression |
Implements PssMathParser::MathParser.
|
virtual |
Sets the precission (digits after comma) when printing numbers.
| mathPrintPrecision | Integer of precission default is 7 |
Implements PssMathParser::MathParser.
|
virtual |
Sets the value of the variable from the argument map.
| a_name | Name of the variable |
| a_value | Value of the variable (double) |
Implements PssMathParser::MathParser.
|
static |
Overloaded subtraction of two doubles.
| a_arg1 | |
| a_arg2 |
|
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.
|
private |
Map of Arguments
|
private |
The infix notation
|
private |
Error num in the infix notation
|
private |
Error string in the infix notation
|
private |
Vector of generators
|
private |
The expression in entities
|
private |
Precision when printing numbers
|
private |
The RP notation
|
private |
Error num in the RP notation
|
private |
Error string in the RP notation
|
private |
Stack for the Shunting-yard algorithm
|
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.
|
static |
Special characters found in the infix notation.
Special characters
The vector holds the special characters in the infix notation. Operators and parenthesis.