aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
formula.cpp
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
43
44// Keep this here because of cyclic dependencies
45#include <agrum/base/core/math/cocoR/Parser.h>
46#include <agrum/base/core/math/cocoR/Scanner.h>
47
48namespace gum {
49
50 // ==========================================================================
51 // === Class FormulaPart ===
52 // ==========================================================================
53
54 // Helper function for debuging
55 void print_stack(std::stack< FormulaPart > s) {
56 std::cout << std::endl;
57 std::list< FormulaPart > l;
58 while (!s.empty()) {
59 l.push_front(s.top());
60 s.pop();
61 }
62
63 std::cout << "Stack: ";
64 for (const auto& elt: l) {
65 std::cout << elt.str() << " ";
66 }
67 std::cout << std::endl;
68 }
69
70 // Helper function for debuging
71 void print_output(std::vector< FormulaPart > v) {
72 std::cout << "Output: ";
73 for (const auto& elt: v) {
74 std::cout << elt.str() << " ";
75 }
76 std::cout << std::endl;
77 }
78
80 switch (func) {
82 return "exp";
83 }
85 return "log";
86 }
88 return "ln";
89 }
91 return "pow";
92 }
94 return "sqrt";
95 }
97 return "nil";
98 }
99 default : {
100 GUM_ERROR(OperationNotAllowed, "unknown function")
101 }
102 }
103 }
104
106 GUM_CONSTRUCTOR(FormulaPart);
107 }
108
110 type(t), number(n), character('\0'), function(nil) {
111 GUM_CONSTRUCTOR(FormulaPart);
112 }
113
115 type(t), number(NAN), character(c), function(nil) {
116 GUM_CONSTRUCTOR(FormulaPart);
117 }
118
120 type(t), number(NAN), character('\0'), function(func) {
121 GUM_CONSTRUCTOR(FormulaPart);
122 }
123
125 type(source.type), number(source.number), character(source.character),
126 function(source.function) {
127 GUM_CONS_CPY(FormulaPart);
128 }
129
131 type(std::move(source.type)), number(std::move(source.number)),
132 character(std::move(source.character)), function(std::move(source.function)) {
133 GUM_CONS_MOV(FormulaPart);
134 }
135
137 GUM_DESTRUCTOR(FormulaPart);
138 ;
139 }
140
142 if (this == &source) { return *this; }
143
144 type = source.type;
145 number = source.number;
146 character = source.character;
147 function = source.function;
148
149 return *this;
150 }
151
153 if (this == &source) { return *this; }
154
155 type = std::move(source.type);
156 number = std::move(source.number);
157 character = std::move(source.character);
158 function = std::move(source.function);
159
160 return *this;
161 }
162
163 std::string FormulaPart::str() const {
164 std::ostringstream s;
165 switch (type) {
166 case token_type::NUMBER : {
167 s << number;
168 break;
169 }
170
172 case token_type::OPERATOR : {
173 if (character == '\0') {
174 s << "\\0";
175 } else {
176 s << character;
177 }
178 break;
179 }
180
181 case token_type::FUNCTION : {
182 s << func2str(function);
183 break;
184 }
185
186 default : {
187 GUM_ERROR(OperationNotAllowed, "unknown type")
188 }
189 }
190 return s.str();
191 }
192
193 // ==========================================================================
194 // === Class Formula ===
195 // ==========================================================================
196
198 auto c_str = (unsigned char*)_formula_.c_str();
199 auto scanner = new gum::formula::Scanner(c_str, (int)_formula_.size());
200 _scanner_ = std::unique_ptr< gum::formula::Scanner >(scanner);
201
202 auto parser = new gum::formula::Parser(scanner);
203 _parser_ = std::unique_ptr< gum::formula::Parser >(parser);
204 _parser_->formula(this);
205 }
206
208 GUM_CONSTRUCTOR(Formula);
209 _initialise_();
210 }
211
213 GUM_CONSTRUCTOR(Formula);
214 _initialise_();
215 }
216
218 GUM_CONSTRUCTOR(Formula);
219 _initialise_();
220 }
221
223 GUM_CONSTRUCTOR(Formula);
224 _initialise_();
225 }
226
228 GUM_CONSTRUCTOR(Formula);
229 _initialise_();
230 }
231
233 GUM_CONSTRUCTOR(Formula);
234 _initialise_();
235 }
236
238 GUM_CONSTRUCTOR(Formula);
239 _initialise_();
240 }
241
242 Formula::Formula(unsigned long long ul) :
244 GUM_CONSTRUCTOR(Formula);
245 _initialise_();
246 }
247
249 GUM_CONSTRUCTOR(Formula);
250 _initialise_();
251 }
252
254 GUM_CONSTRUCTOR(Formula);
255 _initialise_();
256 }
257
258 Formula::Formula(const std::string& f) : _formula_(f), _last_token_(FormulaPart()) {
259 GUM_CONSTRUCTOR(Formula);
260
261 _initialise_();
262 }
263
264 Formula::Formula(const Formula& source) :
266 _stack_(source._stack_) {
267 GUM_CONS_CPY(Formula);
268
269 _initialise_();
270 }
271
273 _formula_(std::move(source._formula_)), _scanner_(std::move(source._scanner_)),
274 _parser_(std::move(source._parser_)), _last_token_(std::move(source._last_token_)),
275 _output_(std::move(source._output_)), _stack_(std::move(source._stack_)) {
276 GUM_CONS_CPY(Formula);
277
278 _parser_->formula(this);
279 }
280
282 GUM_DESTRUCTOR(Formula);
283 ;
284 }
285
287 if (this == &source) { return *this; }
288
289 _formula_ = source._formula_;
290 _last_token_ = source._last_token_;
291 _output_ = source._output_;
292 _stack_ = source._stack_;
293
294 _initialise_();
295
296 return *this;
297 }
298
300 if (this == &source) { return *this; }
301
302 _formula_ = std::move(source._formula_);
303 _scanner_ = std::move(source._scanner_);
304 _parser_ = std::move(source._parser_);
305 _parser_->formula(this);
306 _last_token_ = std::move(source._last_token_);
307 _output_ = std::move(source._output_);
308 _stack_ = std::move(source._stack_);
309
310 return *this;
311 }
312
313 double Formula::result() const {
314 _parser_->Parse();
315
316 std::stack< FormulaPart > stack;
317 if (_output_.empty()) { GUM_ERROR(OperationNotAllowed, "no output found") }
318
319 for (auto item: _output_) {
320 switch (item.type) {
322 stack.push(item);
323 break;
324 }
325
328 _reduceOperatorOrFunction_(item, stack);
329 break;
330 }
331
332 default : {
333 GUM_ERROR(OperationNotAllowed, "expecting numbers, operators or functions")
334 }
335 }
336 }
337
338 if (stack.size() != 1) {
339 GUM_ERROR(OperationNotAllowed, "too many inputs")
340
341 } else if (stack.top().type != FormulaPart::token_type::NUMBER) {
342 GUM_ERROR(OperationNotAllowed, "too many inputs")
343 }
344 return stack.top().number;
345 }
346
347} // namespace gum
348
349#ifdef GUM_NO_INLINE
351#endif // GUM_NO_INLINE
Represents part of a formula.
Definition formula.h:79
std::string str() const
Returns a string representation of this gum::FormulaPart value.
Definition formula.cpp:163
token_function function
The value stored by this gum::FormulaPart.
Definition formula.h:99
FormulaPart()
Class constructor.
Definition formula.cpp:105
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
token_type type
The token_type stored by this gum::FormulaPart.
Definition formula.h:88
token_type
The tokens constituting a formula.
Definition formula.h:82
~Formula()
Class destructor.
Definition formula.cpp:281
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
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 _initialise_()
Initialise the formula scanner and parser.
Definition formula.cpp:197
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
double result() const
Returns the result of this gum::Formula.
Definition formula.cpp:313
std::vector< FormulaPart > _output_
The output stack, will contain one value after evaluation.
Definition formula.h:473
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
Exception : operation not allowed.
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
Headers files for the gum::FormulaPart and gum::Formula classes.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
void print_stack(std::stack< FormulaPart > s)
Definition formula.cpp:55
std::string func2str(FormulaPart::token_function func)
Definition formula.cpp:79
std::string to_string(const Formula &f)
void print_output(std::vector< FormulaPart > v)
Definition formula.cpp:71
STL namespace.