aGrUM  0.16.0
formula.h
Go to the documentation of this file.
1 
29 #ifndef GUM_MATH_FORMULA_H
30 #define GUM_MATH_FORMULA_H
31 
32 #include <iostream>
33 #include <list>
34 #include <memory>
35 #include <random>
36 #include <sstream>
37 #include <stack>
38 #include <string>
39 #include <vector>
40 
41 #include <agrum/agrum.h>
42 #include <agrum/core/hashTable.h>
43 
44 namespace gum {
45 
46  namespace formula {
47  class Scanner;
48  class Parser;
49  } // namespace formula
50 
60  class FormulaPart {
61  public:
63  enum token_type { NUMBER, OPERATOR, PARENTHESIS, NIL, FUNCTION, ARG_SEP };
64 
66  enum token_function { exp, log, ln, pow, sqrt, nil };
67 
70 
77  double number;
79  char character;
80  token_function function;
82 
83  // ========================================================================
85  // ========================================================================
87 
91  FormulaPart();
92 
98  FormulaPart(token_type t, double n);
99 
105  FormulaPart(token_type t, char c);
106 
113 
118  FormulaPart(const FormulaPart& source);
119 
124  FormulaPart(FormulaPart&& source);
125 
129  ~FormulaPart();
130 
132  // ========================================================================
134  // ========================================================================
136 
142  FormulaPart& operator=(const FormulaPart& source);
143 
149  FormulaPart& operator=(FormulaPart&& source);
150 
152  // ========================================================================
154  // ========================================================================
156 
161  std::string str() const;
162 
169  bool isLeftAssociative() const;
170 
177  bool isRightAssociative() const;
178 
187  int precedence() const;
188 
196  size_t argc() const;
197 
211  FormulaPart eval(const std::vector< FormulaPart >& args) const;
212 
214 
215  private:
229  double __operator_eval(const std::vector< FormulaPart >& args) const;
230 
244  double __function_eval(const std::vector< FormulaPart >& args) const;
245 
252  size_t __operator_argc() const;
253 
260  size_t __function_argc() const;
261  };
262 
263  // extern class gum::formula::Parser;
274  class Formula {
275  friend class gum::formula::Parser;
276 
277  public:
278  // ========================================================================
280  // ========================================================================
282 
286  Formula(short s);
288  Formula(unsigned short us);
289  Formula(int i);
290  Formula(unsigned int ui);
291  Formula(long l);
292  Formula(unsigned long ul);
293  Formula(long long l);
294  Formula(unsigned long long ul);
295  Formula(float f);
296  Formula(double d);
298 
303  Formula(const std::string& f);
304 
309  Formula(const Formula& source);
310 
315  Formula(Formula&& source);
316 
320  ~Formula();
321 
323  // ========================================================================
325  // ========================================================================
327 
333  Formula& operator=(const Formula& source);
334 
340  Formula& operator=(Formula&& source);
341 
345  explicit operator double() const { return result(); }
346 
348  // ========================================================================
350  // ========================================================================
352 
358 
363  const HashTable< std::string, double >& variables() const;
364 
372  double result() const;
373 
377  const std::string& formula() const;
378 
382  std::string& formula();
383 
384  private:
386  // ========================================================================
388  // ========================================================================
390 
395  void __push_number(const double& v);
396 
401  void __push_operator(char o);
402 
406  void __push_leftParenthesis();
407 
411  void __push_rightParenthesis();
412 
417  void __push_function(const std::string& func);
418 
422  void __push_variable(const std::string& var);
423 
427  void __push_identifier(const std::string& ident);
428 
432  void __push_comma();
433 
437  void __finalize();
438 
440 
442  std::string __formula;
443 
445  std::unique_ptr< gum::formula::Scanner > __scanner;
446 
448  std::unique_ptr< gum::formula::Parser > __parser;
449 
452 
454  std::vector< FormulaPart > __output;
455 
457  std::stack< FormulaPart > __stack;
458 
461 
465  void __initialise();
466 
472  bool __popOperator(FormulaPart o);
473 
479  void __reduceOperatorOrFunction(FormulaPart item,
480  std::stack< FormulaPart >& stack) const;
481 
486  void __push_unaryOperator(char o);
487 
492  void __push_operator(FormulaPart t);
493 
498  bool __isUnaryOperator(char o);
499 
504  void __push_output(FormulaPart t);
505 
510  void __push_stack(FormulaPart t);
511  };
512 
513  // // ========================================================================
514  // /// @name Arithmetic Operators
515  // // ========================================================================
516  // /// @{
517 
518  Formula operator-(const Formula& a);
519 
520  Formula operator+(const Formula& a, const Formula& b);
521 
522  Formula operator-(const Formula& a, const Formula& b);
523 
524  Formula operator*(const Formula& a, const Formula& b);
525 
526  Formula operator/(const Formula& a, const Formula& b);
527 
528  std::string to_string(const Formula& f);
529 
530  std::ostream& operator<<(std::ostream& os, const Formula& f);
531 
532  // /// @}
533 
534 } /* namespace gum */
535 
536 #ifndef GUM_NO_INLINE
538 #endif // GUM_NO_INLINE
539 
540 #endif /* GUM_MATH_FORMULA_H */
std::string __formula
The formula to evaluate.
Definition: formula.h:442
token_type
The tokens constituting a formula.
Definition: formula.h:63
Formula operator+(const Formula &a, const Formula &b)
Definition: formula_inl.h:479
Evaluates a string as a algebraic formula.
Definition: formula.h:274
std::unique_ptr< gum::formula::Parser > __parser
The parser used by the formula.
Definition: formula.h:448
token_function
The functions allowed in a formula.
Definition: formula.h:66
FormulaPart __last_token
The last token added to the formula.
Definition: formula.h:451
Represents part of a formula.
Definition: formula.h:60
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
std::ostream & operator<<(std::ostream &output, const BayesNet< GUM_SCALAR > &bn)
Prints map&#39;s DAG in output using the Graphviz-dot format.
Definition: BayesNet_tpl.h:605
Formula operator/(const Formula &a, const Formula &b)
Definition: formula_inl.h:494
std::string to_string(const Formula &f)
Definition: formula_inl.h:499
Formula operator*(const Formula &a, const Formula &b)
Definition: formula_inl.h:489
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:69
std::stack< FormulaPart > __stack
A stack used during evaluation.
Definition: formula.h:457
ListConstIterator< Val >::difference_type operator-(const ListConstIterator< Val > &iter1, const ListConstIterator< Val > &iter2)
For STL compliance, a distance operator.
Definition: list_tpl.h:349
std::vector< FormulaPart > __output
The output stack, will contain one value after evaluation.
Definition: formula.h:454
std::unique_ptr< gum::formula::Scanner > __scanner
The scanner used by the formula.
Definition: formula.h:445
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:79
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
HashTable< std::string, double > __variables
The variables available in this formula.
Definition: formula.h:460