![]() |
aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
|
Evaluates a string as a algebraic formula. More...
#include <formula.h>
Public Member Functions | |
| Formula (const std::string &f) | |
| Class constructor. | |
| Formula (const Formula &source) | |
| Copy constructor. | |
| Formula (Formula &&source) | |
| Move constructor. | |
| ~Formula () | |
| Class destructor. | |
Constructors and destructor | |
| Formula (short s) | |
| Constructor. | |
| Formula (unsigned short us) | |
| Constructor. | |
| Formula (int i) | |
| Constructor. | |
| Formula (unsigned int ui) | |
| Constructor. | |
| Formula (long l) | |
| Constructor. | |
| Formula (unsigned long ul) | |
| Constructor. | |
| Formula (long long l) | |
| Constructor. | |
| Formula (unsigned long long ul) | |
| Constructor. | |
| Formula (float f) | |
| Constructor. | |
| Formula (double d) | |
| Constructor. | |
Operators | |
| Formula & | operator= (const Formula &source) |
| Copy operator. | |
| Formula & | operator= (Formula &&source) |
| Move operator. | |
| operator double () const | |
| Allows implicit conversion to doubles. | |
Accessors & modifiers | |
| HashTable< std::string, double > & | variables () |
| Returns the variables used by this gum::Formula. | |
| const HashTable< std::string, double > & | variables () const |
| Returns the variables used by this gum::Formula. | |
| double | result () const |
| Returns the result of this gum::Formula. | |
| const std::string & | formula () const |
| Returns the formula. | |
| std::string & | formula () |
| Returns the formula. | |
Private Member Functions | |
| void | _initialise_ () |
| Initialise the formula scanner and parser. | |
| bool | _popOperator_ (FormulaPart o) |
| Pop the operator in the inner formula's stack. | |
| void | _reduceOperatorOrFunction_ (FormulaPart item, std::stack< FormulaPart > &stack) const |
| Evaluate an operator or function and push its result. | |
| void | _push_unaryOperator_ (char o) |
| Push an unary operator. | |
| void | _push_operator_ (FormulaPart t) |
| Push an operator. | |
| bool | _isUnaryOperator_ (char o) |
| Returns true if o is an unary operator. | |
| void | _push_output_ (FormulaPart t) |
| Push the gum::FormulaPart in the output vector. | |
| void | _push_stack_ (FormulaPart t) |
| Push the gum::FormulaPart in the stack. | |
Private accessors & modifiers used by the Formula Parser | |
| void | _push_number_ (const double &v) |
| Push a number in the formula. | |
| void | _push_operator_ (char o) |
| Push an operator in the formula. | |
| void | _push_leftParenthesis_ () |
| Push a left parenthesis in the formula. | |
| void | _push_rightParenthesis_ () |
| Push a right parenthesis in the formula. | |
| void | _push_function_ (const std::string &func) |
| Push a function in the formula. | |
| void | _push_variable_ (const std::string &var) |
| Push a variable in the formula. | |
| void | _push_identifier_ (const std::string &ident) |
| Use this if you don't know if ident is a function or a variable. | |
| void | _push_comma_ () |
| Push a comma in the formula. | |
| void | _finalize_ () |
| Finalize the formula and prepare it for evaluation. | |
Private Attributes | |
| std::string | _formula_ |
| The formula to evaluate. | |
| std::unique_ptr< gum::formula::Scanner > | _scanner_ |
| The scanner used by the formula. | |
| std::unique_ptr< gum::formula::Parser > | _parser_ |
| The parser used by the formula. | |
| FormulaPart | _last_token_ |
| The last token added to the formula. | |
| std::vector< FormulaPart > | _output_ |
| The output stack, will contain one value after evaluation. | |
| std::stack< FormulaPart > | _stack_ |
| A stack used during evaluation. | |
| HashTable< std::string, double > | _variables_ |
| The variables available in this formula. | |
Friends | |
| class | gum::formula::Parser |
Evaluates a string as a algebraic formula.
Implementation of the Shunting-yard algorithm to convert infix notation to RPN. The gum::Formula::result() method implements the postfix algorithm to compute the formula result.
| gum::Formula::Formula | ( | short | s | ) |
Constructor.
Definition at line 207 of file formula.cpp.
References Formula(), _formula_, _initialise_(), _last_token_, and gum::to_string().
Referenced by Formula(), Formula(), Formula(), Formula(), Formula(), Formula(), Formula(), Formula(), Formula(), Formula(), Formula(), Formula(), Formula(), ~Formula(), gum::formula::Parser, operator=(), and operator=().
| gum::Formula::Formula | ( | unsigned short | us | ) |
Constructor.
Definition at line 212 of file formula.cpp.
References Formula(), _formula_, _initialise_(), _last_token_, and gum::to_string().
| gum::Formula::Formula | ( | int | i | ) |
Constructor.
Definition at line 217 of file formula.cpp.
References Formula(), _formula_, _initialise_(), _last_token_, and gum::to_string().
| gum::Formula::Formula | ( | unsigned int | ui | ) |
Constructor.
Definition at line 222 of file formula.cpp.
References Formula(), _formula_, _initialise_(), _last_token_, and gum::to_string().
| gum::Formula::Formula | ( | long | l | ) |
Constructor.
Definition at line 227 of file formula.cpp.
References Formula(), _formula_, _initialise_(), _last_token_, and gum::to_string().
| gum::Formula::Formula | ( | unsigned long | ul | ) |
Constructor.
Definition at line 232 of file formula.cpp.
References Formula(), _formula_, _initialise_(), _last_token_, and gum::to_string().
| gum::Formula::Formula | ( | long long | l | ) |
Constructor.
Definition at line 237 of file formula.cpp.
References Formula(), _formula_, _initialise_(), _last_token_, and gum::to_string().
| gum::Formula::Formula | ( | unsigned long long | ul | ) |
Constructor.
Definition at line 242 of file formula.cpp.
References Formula(), _formula_, _initialise_(), _last_token_, and gum::to_string().
| gum::Formula::Formula | ( | float | f | ) |
Constructor.
Definition at line 248 of file formula.cpp.
References Formula(), _formula_, _initialise_(), _last_token_, and gum::to_string().
| gum::Formula::Formula | ( | double | d | ) |
Constructor.
Definition at line 253 of file formula.cpp.
References Formula(), _formula_, _initialise_(), _last_token_, and gum::to_string().
| gum::Formula::Formula | ( | const std::string & | f | ) |
Class constructor.
| f | An algebraic formula. |
Definition at line 258 of file formula.cpp.
References Formula(), _formula_, _initialise_(), and _last_token_.
| gum::Formula::Formula | ( | const Formula & | source | ) |
Copy constructor.
| source | The gum::Formula to copy. |
Definition at line 264 of file formula.cpp.
References Formula(), _formula_, _initialise_(), _last_token_, _output_, and _stack_.
| gum::Formula::Formula | ( | Formula && | source | ) |
Move constructor.
| source | The gum::Formula to move. |
Definition at line 272 of file formula.cpp.
References Formula(), _formula_, _last_token_, _output_, _parser_, _scanner_, and _stack_.
| gum::Formula::~Formula | ( | ) |
Class destructor.
Definition at line 281 of file formula.cpp.
References Formula().
|
private |
Finalize the formula and prepare it for evaluation.
Definition at line 361 of file formula_inl.h.
References _push_output_(), _stack_, and GUM_ERROR.
|
private |
Initialise the formula scanner and parser.
Definition at line 197 of file formula.cpp.
References _formula_, _parser_, _scanner_, and gum::formula::Parser.
Referenced by Formula(), Formula(), Formula(), Formula(), Formula(), Formula(), Formula(), Formula(), Formula(), Formula(), Formula(), Formula(), and operator=().
|
private |
Returns true if o is an unary operator.
Definition at line 296 of file formula_inl.h.
References _last_token_, gum::FormulaPart::ARG_SEP, gum::FormulaPart::NIL, gum::FormulaPart::OPERATOR, and gum::FormulaPart::PARENTHESIS.
Referenced by _push_operator_().
|
private |
Pop the operator in the inner formula's stack.
| o | The operator to pop. |
Definition at line 272 of file formula_inl.h.
References _stack_, gum::FormulaPart::isLeftAssociative(), gum::FormulaPart::isRightAssociative(), gum::FormulaPart::OPERATOR, and gum::FormulaPart::precedence().
Referenced by _push_operator_().
|
private |
Push a comma in the formula.
Definition at line 425 of file formula_inl.h.
References _last_token_, _push_output_(), _stack_, gum::FormulaPart::ARG_SEP, and GUM_ERROR.
|
private |
Push a function in the formula.
| func | The function to push. |
Definition at line 398 of file formula_inl.h.
References _push_stack_(), gum::FormulaPart::exp, gum::FormulaPart::FUNCTION, GUM_ERROR, gum::FormulaPart::ln, gum::FormulaPart::log, gum::FormulaPart::pow, and gum::FormulaPart::sqrt.
Referenced by _push_identifier_().
|
private |
Use this if you don't know if ident is a function or a variable.
Definition at line 455 of file formula_inl.h.
References _push_function_(), _push_variable_(), and GUM_ERROR.
|
private |
Push a left parenthesis in the formula.
Definition at line 332 of file formula_inl.h.
References _push_stack_(), and gum::FormulaPart::PARENTHESIS.
|
private |
Push a number in the formula.
| v | The number to push. |
Definition at line 266 of file formula_inl.h.
References _push_output_(), and gum::FormulaPart::NUMBER.
Referenced by _push_variable_().
|
private |
Push an operator in the formula.
| o | The operator to push. |
Definition at line 285 of file formula_inl.h.
References _isUnaryOperator_(), _push_operator_(), _push_unaryOperator_(), and gum::FormulaPart::OPERATOR.
Referenced by _push_operator_(), and _push_unaryOperator_().
|
private |
Push an operator.
| t | The operator to push. |
Definition at line 322 of file formula_inl.h.
References _popOperator_(), _push_output_(), _push_stack_(), and _stack_.
|
private |
Push the gum::FormulaPart in the output vector.
| t | The gum::FormulaPart to push. |
Definition at line 386 of file formula_inl.h.
References _last_token_, and _output_.
Referenced by _finalize_(), _push_comma_(), _push_number_(), _push_operator_(), and _push_rightParenthesis_().
|
private |
Push a right parenthesis in the formula.
Definition at line 338 of file formula_inl.h.
References _last_token_, _push_output_(), _stack_, gum::FormulaPart::FUNCTION, GUM_ERROR, and gum::FormulaPart::PARENTHESIS.
|
private |
Push the gum::FormulaPart in the stack.
| t | The gum::FormulaPart to push. |
Definition at line 392 of file formula_inl.h.
References _last_token_, and _stack_.
Referenced by _push_function_(), _push_leftParenthesis_(), and _push_operator_().
|
private |
Push an unary operator.
| o | The unary operator to push. |
Definition at line 315 of file formula_inl.h.
References _push_operator_(), and gum::FormulaPart::OPERATOR.
Referenced by _push_operator_().
|
private |
Push a variable in the formula.
Definition at line 445 of file formula_inl.h.
References _push_number_(), _variables_, and GUM_ERROR.
Referenced by _push_identifier_().
|
private |
Evaluate an operator or function and push its result.
| item | The operator or function to reduce. |
| stack | The stack to evaluate. |
Definition at line 371 of file formula_inl.h.
References gum::FormulaPart::argc(), gum::FormulaPart::eval(), and GUM_ERROR.
Referenced by result().
| INLINE std::string & gum::Formula::formula | ( | ) |
| INLINE const std::string & gum::Formula::formula | ( | ) | const |
|
inlineexplicit |
Allows implicit conversion to doubles.
Definition at line 364 of file formula.h.
References result().
Copy operator.
| source | The gum::Formula to copy. |
Definition at line 286 of file formula.cpp.
References Formula(), _formula_, _initialise_(), _last_token_, _output_, and _stack_.
Move operator.
| source | The gum::Formula to move. |
Definition at line 299 of file formula.cpp.
References Formula(), _formula_, _last_token_, _output_, _parser_, _scanner_, and _stack_.
| double gum::Formula::result | ( | ) | const |
Returns the result of this gum::Formula.
Each call to Formula::result() will reevaluate the formulas result.
Definition at line 313 of file formula.cpp.
References _output_, _parser_, _reduceOperatorOrFunction_(), gum::FormulaPart::FUNCTION, GUM_ERROR, gum::FormulaPart::NUMBER, and gum::FormulaPart::OPERATOR.
Referenced by gum::prm::PRMFormAttribute< GUM_SCALAR >::_fillCpf_(), operator double(), gum::operator*(), gum::operator+(), gum::operator-(), gum::operator-(), gum::operator/(), gum::operator<<(), and gum::to_string().
Returns the variables used by this gum::Formula.
Definition at line 439 of file formula_inl.h.
References _variables_.
Referenced by gum::prm::PRMFormAttribute< GUM_SCALAR >::_fillCpf_().
Returns the variables used by this gum::Formula.
Definition at line 442 of file formula_inl.h.
References _variables_.
|
friend |
Definition at line 294 of file formula.h.
References Formula(), and gum::formula::Parser.
Referenced by _initialise_(), and gum::formula::Parser.
|
private |
|
private |
The last token added to the formula.
Definition at line 470 of file formula.h.
Referenced by Formula(), Formula(), Formula(), Formula(), Formula(), Formula(), Formula(), Formula(), Formula(), Formula(), Formula(), Formula(), Formula(), _isUnaryOperator_(), _push_comma_(), _push_output_(), _push_rightParenthesis_(), _push_stack_(), operator=(), and operator=().
|
private |
The output stack, will contain one value after evaluation.
Definition at line 473 of file formula.h.
Referenced by Formula(), Formula(), _push_output_(), operator=(), operator=(), and result().
|
private |
The parser used by the formula.
Definition at line 467 of file formula.h.
Referenced by Formula(), _initialise_(), operator=(), and result().
|
private |
The scanner used by the formula.
Definition at line 464 of file formula.h.
Referenced by Formula(), _initialise_(), and operator=().
|
private |
A stack used during evaluation.
Definition at line 476 of file formula.h.
Referenced by Formula(), Formula(), _finalize_(), _popOperator_(), _push_comma_(), _push_operator_(), _push_rightParenthesis_(), _push_stack_(), operator=(), and operator=().
The variables available in this formula.
Definition at line 479 of file formula.h.
Referenced by _push_variable_(), variables(), and variables().