aGrUM  0.13.2
formula.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
26 #ifndef GUM_MATH_FORMULA_H
27 #define GUM_MATH_FORMULA_H
28 
29 #include <iostream>
30 #include <list>
31 #include <memory>
32 #include <random>
33 #include <sstream>
34 #include <stack>
35 #include <string>
36 #include <vector>
37 
38 #include <agrum/agrum.h>
39 #include <agrum/core/hashTable.h>
40 
41 namespace gum {
42 
43  namespace formula {
44  class Scanner;
45  class Parser;
46  } // namespace formula
47 
57  class FormulaPart {
58  public:
60  enum token_type { NUMBER, OPERATOR, PARENTHESIS, NIL, FUNCTION, ARG_SEP };
61 
63  enum token_function { exp, log, ln, pow, sqrt, nil };
64 
67 
74  double number;
76  char character;
77  token_function function;
79 
80  // ========================================================================
82  // ========================================================================
84 
88  FormulaPart();
89 
95  FormulaPart(token_type t, double n);
96 
102  FormulaPart(token_type t, char c);
103 
110 
115  FormulaPart(const FormulaPart& source);
116 
121  FormulaPart(FormulaPart&& source);
122 
126  ~FormulaPart();
127 
129  // ========================================================================
131  // ========================================================================
133 
139  FormulaPart& operator=(const FormulaPart& source);
140 
146  FormulaPart& operator=(FormulaPart&& source);
147 
149  // ========================================================================
151  // ========================================================================
153 
158  std::string str() const;
159 
166  bool isLeftAssociative() const;
167 
174  bool isRightAssociative() const;
175 
184  int precedence() const;
185 
193  size_t argc() const;
194 
208  FormulaPart eval(const std::vector< FormulaPart >& args) const;
209 
211 
212  private:
226  double __operator_eval(const std::vector< FormulaPart >& args) const;
227 
241  double __function_eval(const std::vector< FormulaPart >& args) const;
242 
249  size_t __operator_argc() const;
250 
257  size_t __function_argc() const;
258  };
259 
260  // extern class gum::formula::Parser;
271  class Formula {
272  friend class gum::formula::Parser;
273 
274  public:
275  // ========================================================================
277  // ========================================================================
279 
283  Formula(short s);
285  Formula(unsigned short us);
286  Formula(int i);
287  Formula(unsigned int ui);
288  Formula(long l);
289  Formula(unsigned long ul);
290  Formula(long long l);
291  Formula(unsigned long long ul);
292  Formula(float f);
293  Formula(double d);
295 
300  Formula(const std::string& f);
301 
306  Formula(const Formula& source);
307 
312  Formula(Formula&& source);
313 
317  ~Formula();
318 
320  // ========================================================================
322  // ========================================================================
324 
330  Formula& operator=(const Formula& source);
331 
337  Formula& operator=(Formula&& source);
338 
342  explicit operator double() const { return result(); }
343 
345  // ========================================================================
347  // ========================================================================
349 
355 
360  const HashTable< std::string, double >& variables() const;
361 
369  double result() const;
370 
374  const std::string& formula() const;
375 
379  std::string& formula();
380 
381  private:
383  // ========================================================================
385  // ========================================================================
387 
392  void __push_number(const double& v);
393 
398  void __push_operator(char o);
399 
403  void __push_leftParenthesis();
404 
408  void __push_rightParenthesis();
409 
414  void __push_function(const std::string& func);
415 
419  void __push_variable(const std::string& var);
420 
424  void __push_identifier(const std::string& ident);
425 
429  void __push_comma();
430 
434  void __finalize();
435 
437 
439  std::string __formula;
440 
442  std::unique_ptr< gum::formula::Scanner > __scanner;
443 
445  std::unique_ptr< gum::formula::Parser > __parser;
446 
449 
451  std::vector< FormulaPart > __output;
452 
454  std::stack< FormulaPart > __stack;
455 
458 
462  void __initialise();
463 
469  bool __popOperator(FormulaPart o);
470 
476  void __reduceOperatorOrFunction(FormulaPart item,
477  std::stack< FormulaPart >& stack) const;
478 
483  void __push_unaryOperator(char o);
484 
489  void __push_operator(FormulaPart t);
490 
495  bool __isUnaryOperator(char o);
496 
501  void __push_output(FormulaPart t);
502 
507  void __push_stack(FormulaPart t);
508  };
509 
510  // // ========================================================================
511  // /// @name Arithmetic Operators
512  // // ========================================================================
513  // /// @{
514 
515  Formula operator-(const Formula& a);
516 
517  Formula operator+(const Formula& a, const Formula& b);
518 
519  Formula operator-(const Formula& a, const Formula& b);
520 
521  Formula operator*(const Formula& a, const Formula& b);
522 
523  Formula operator/(const Formula& a, const Formula& b);
524 
525  std::string to_string(const Formula& f);
526 
527  std::ostream& operator<<(std::ostream& os, const Formula& f);
528 
529  // /// @}
530 
531 } /* namespace gum */
532 
533 #ifndef GUM_NO_INLINE
535 #endif // GUM_NO_INLINE
536 
537 #endif /* GUM_MATH_FORMULA_H */
std::string __formula
The formula to evaluate.
Definition: formula.h:439
token_type
The tokens constituting a formula.
Definition: formula.h:60
Formula operator+(const Formula &a, const Formula &b)
Definition: formula_inl.h:459
Evaluates a string as a algebraic formula.
Definition: formula.h:271
std::unique_ptr< gum::formula::Parser > __parser
The parser used by the formula.
Definition: formula.h:445
token_function
The functions allowed in a formula.
Definition: formula.h:63
FormulaPart __last_token
The last token added to the formula.
Definition: formula.h:448
Represents part of a formula.
Definition: formula.h:57
gum is the global namespace for all aGrUM entities
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:573
Formula operator/(const Formula &a, const Formula &b)
Definition: formula_inl.h:474
std::string to_string(const Formula &f)
Definition: formula_inl.h:479
Formula operator*(const Formula &a, const Formula &b)
Definition: formula_inl.h:469
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:66
std::stack< FormulaPart > __stack
A stack used during evaluation.
Definition: formula.h:454
ListConstIterator< Val >::difference_type operator-(const ListConstIterator< Val > &iter1, const ListConstIterator< Val > &iter2)
For STL compliance, a distance operator.
Definition: list_tpl.h:346
std::vector< FormulaPart > __output
The output stack, will contain one value after evaluation.
Definition: formula.h:451
std::unique_ptr< gum::formula::Scanner > __scanner
The scanner used by the formula.
Definition: formula.h:442
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:76
Class hash tables iterators.
HashTable< std::string, double > __variables
The variables available in this formula.
Definition: formula.h:457