aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
formula.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
48#ifndef GUM_MATH_FORMULA_H
49#define GUM_MATH_FORMULA_H
50
51#include <iostream>
52#include <list>
53#include <memory>
54#include <sstream>
55#include <stack>
56#include <string>
57#include <vector>
58
59#include <agrum/agrum.h>
60
62
63namespace gum {
64
65 namespace formula {
66 class Scanner;
67 class Parser;
68 } // namespace formula
69
80 public:
83
86
89
97 double number;
101
102 // ========================================================================
104 // ========================================================================
106
110 FormulaPart();
111
117 FormulaPart(token_type t, double n);
118
124 FormulaPart(token_type t, char c);
125
132
137 FormulaPart(const FormulaPart& source);
138
143 FormulaPart(FormulaPart&& source);
144
148 ~FormulaPart();
149
151 // ========================================================================
153 // ========================================================================
155
161 FormulaPart& operator=(const FormulaPart& source);
162
169
171 // ========================================================================
173 // ========================================================================
175
180 std::string str() const;
181
188 bool isLeftAssociative() const;
189
196 bool isRightAssociative() const;
197
206 int precedence() const;
207
215 size_t argc() const;
216
230 FormulaPart eval(const std::vector< FormulaPart >& args) const;
231
233
234 private:
248 double _operator_eval_(const std::vector< FormulaPart >& args) const;
249
263 double _function_eval_(const std::vector< FormulaPart >& args) const;
264
271 size_t _operator_argc_() const;
272
279 size_t _function_argc_() const;
280 };
281
282 // extern class gum::formula::Parser;
293 class Formula {
295
296 public:
297 // ========================================================================
299 // ========================================================================
301
306 Formula(short s);
307 Formula(unsigned short us);
308 Formula(int i);
309 Formula(unsigned int ui);
310 Formula(long l);
311 Formula(unsigned long ul);
312 Formula(long long l);
313 Formula(unsigned long long ul);
314 Formula(float f);
315 Formula(double d);
317
322 Formula(const std::string& f);
323
328 Formula(const Formula& source);
329
334 Formula(Formula&& source);
335
339 ~Formula();
340
342 // ========================================================================
344 // ========================================================================
346
352 Formula& operator=(const Formula& source);
353
359 Formula& operator=(Formula&& source);
360
364 explicit operator double() const { return result(); }
365
367 // ========================================================================
369 // ========================================================================
371
377
383
391 double result() const;
392
396 const std::string& formula() const;
397
401 std::string& formula();
402
403 private:
405 // ========================================================================
407 // ========================================================================
409
414 void _push_number_(const double& v);
415
420 void _push_operator_(char o);
421
426
431
436 void _push_function_(const std::string& func);
437
441 void _push_variable_(const std::string& var);
442
446 void _push_identifier_(const std::string& ident);
447
451 void _push_comma_();
452
456 void _finalize_();
457
459
461 std::string _formula_;
462
464 std::unique_ptr< gum::formula::Scanner > _scanner_;
465
467 std::unique_ptr< gum::formula::Parser > _parser_;
468
471
473 std::vector< FormulaPart > _output_;
474
476 std::stack< FormulaPart > _stack_;
477
480
484 void _initialise_();
485
492
498 void _reduceOperatorOrFunction_(FormulaPart item, std::stack< FormulaPart >& stack) const;
499
504 void _push_unaryOperator_(char o);
505
511
516 bool _isUnaryOperator_(char o);
517
523
529 };
530
531 // // ========================================================================
532 // /// @name Arithmetic Operators
533 // // ========================================================================
534 // /// @{
535
536 Formula operator-(const Formula& a);
537
538 Formula operator+(const Formula& a, const Formula& b);
539
540 Formula operator-(const Formula& a, const Formula& b);
541
542 Formula operator*(const Formula& a, const Formula& b);
543
544 Formula operator/(const Formula& a, const Formula& b);
545
546 std::string to_string(const Formula& f);
547
548 std::ostream& operator<<(std::ostream& os, const Formula& f);
549
550 // /// @}
551
552} /* namespace gum */
553
554#ifndef GUM_NO_INLINE
556#endif // GUM_NO_INLINE
557
558#endif /* GUM_MATH_FORMULA_H */
Represents part of a formula.
Definition formula.h:79
double _function_eval_(const std::vector< FormulaPart > &args) const
Returns the evaluation of the vector of gum::FormulaPart as arguments of the value stored in this gum...
int precedence() const
Returns the precedence priority of the value stored in this gum::FormulaPart.
Definition formula_inl.h:88
std::string str() const
Returns a string representation of this gum::FormulaPart value.
Definition formula.cpp:163
size_t _function_argc_() const
Returns the number of arguments expected by the function stored in this gum::FormulaPart.
token_function function
The value stored by this gum::FormulaPart.
Definition formula.h:99
FormulaPart()
Class constructor.
Definition formula.cpp:105
double _operator_eval_(const std::vector< FormulaPart > &args) const
Returns the evaluation of the vector of gum::FormulaPart as arguments of the value stored in this gum...
FormulaPart & operator=(const FormulaPart &source)
Definition formula.cpp:141
~FormulaPart()
Class destuctor.
Definition formula.cpp:136
double number
The value stored by this gum::FormulaPart.
Definition formula.h:97
token_function
The functions allowed in a formula.
Definition formula.h:85
char character
The value stored by this gum::FormulaPart.
Definition formula.h:98
bool isLeftAssociative() const
Returns true if this gum::FormulaPart is left associative.
Definition formula_inl.h:53
size_t argc() const
Returns the number of argument of the function stored in this gum::FormulaPart.
size_t _operator_argc_() const
Returns the number of arguments expected by the operator stored in this gum::FormulaPart.
token_type type
The token_type stored by this gum::FormulaPart.
Definition formula.h:88
FormulaPart eval(const std::vector< FormulaPart > &args) const
Returns the evaluation of the vector of gum::FormulaPart as arguments of the value stored in this gum...
bool isRightAssociative() const
Returns true if this gum::FormulaPart is right associative.
Definition formula_inl.h:76
token_type
The tokens constituting a formula.
Definition formula.h:82
Evaluates a string as a algebraic formula.
Definition formula.h:293
void _push_unaryOperator_(char o)
Push an unary operator.
void _push_output_(FormulaPart t)
Push the gum::FormulaPart in the output vector.
~Formula()
Class destructor.
Definition formula.cpp:281
void _push_variable_(const std::string &var)
Push a variable in the formula.
void _push_leftParenthesis_()
Push a left parenthesis in the formula.
friend class gum::formula::Parser
Definition formula.h:294
std::unique_ptr< gum::formula::Parser > _parser_
The parser used by the formula.
Definition formula.h:467
void _push_stack_(FormulaPart t)
Push the gum::FormulaPart in the stack.
bool _isUnaryOperator_(char o)
Returns true if o is an unary operator.
const std::string & formula() const
Returns the formula.
void _finalize_()
Finalize the formula and prepare it for evaluation.
std::unique_ptr< gum::formula::Scanner > _scanner_
The scanner used by the formula.
Definition formula.h:464
Formula & operator=(const Formula &source)
Copy operator.
Definition formula.cpp:286
void _push_function_(const std::string &func)
Push a function in the formula.
void _initialise_()
Initialise the formula scanner and parser.
Definition formula.cpp:197
void _push_comma_()
Push a comma in the formula.
std::string _formula_
The formula to evaluate.
Definition formula.h:461
void _reduceOperatorOrFunction_(FormulaPart item, std::stack< FormulaPart > &stack) const
Evaluate an operator or function and push its result.
Formula(short s)
Constructor.
Definition formula.cpp:207
HashTable< std::string, double > _variables_
The variables available in this formula.
Definition formula.h:479
double result() const
Returns the result of this gum::Formula.
Definition formula.cpp:313
bool _popOperator_(FormulaPart o)
Pop the operator in the inner formula's stack.
std::vector< FormulaPart > _output_
The output stack, will contain one value after evaluation.
Definition formula.h:473
void _push_operator_(char o)
Push an operator in the formula.
void _push_rightParenthesis_()
Push a right parenthesis 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.
FormulaPart _last_token_
The last token added to the formula.
Definition formula.h:470
std::stack< FormulaPart > _stack_
A stack used during evaluation.
Definition formula.h:476
void _push_number_(const double &v)
Push a number in the formula.
HashTable< std::string, double > & variables()
Returns the variables used by this gum::Formula.
Class hash tables iterators.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
value_type & operator*()
Returns the value pointed to by the iterator.
std::ostream & operator<<(std::ostream &stream, const AVLTree< Val, Cmp > &tree)
display the content of a tree
Definition AVLTree.h:913
Formula operator/(const Formula &a, const Formula &b)
std::string to_string(const Formula &f)
HashTableIteratorSafe< Key, Val > operator+(Size i) const
Returns a new iterator pointing to i elements further in the hashtable.
ListConstIterator< Val >::difference_type operator-(const ListConstIterator< Val > &iter1, const ListConstIterator< Val > &iter2)
For STL compliance, a distance operator.
Definition list_tpl.h:349