aGrUM 3.0.0
a C++ library for (probabilistic) graphical models
doAST.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 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-2026 *
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
46
47#ifndef GUM_DO_AST_H
48#define GUM_DO_AST_H
49
50#include <map>
51#include <memory>
52#include <set>
53#include <string>
54#include <vector>
55
56#include <agrum/agrum.h>
57
58#include <agrum/BN/IBayesNet.h>
59
60#include <string_view>
61
62namespace gum {
63
64 template < GUM_Numeric GUM_SCALAR >
65 class IBayesNet;
66 template < GUM_Numeric GUM_SCALAR >
67 class Tensor;
68
69 // ================================================================
70 // ASTtree
71 // ================================================================
90 template < GUM_Numeric GUM_SCALAR >
91 class ASTtree {
92 public:
94 explicit ASTtree(std::string_view type);
95
96 virtual ~ASTtree();
97
98 ASTtree(const ASTtree&) = delete;
99 ASTtree& operator=(const ASTtree&) = delete;
100
101 ASTtree(ASTtree&&) noexcept { GUM_CONS_MOV(ASTtree) }
102
103 ASTtree& operator=(ASTtree&&) noexcept = default;
104
106 const std::string& type() const noexcept;
107
112 virtual std::string toString(std::string_view prefix = "") const = 0;
113
122 virtual std::string protectToLatex(HashTable< std::string, int >& nameOccur) const = 0;
123
132 virtual std::string fastToLatex(HashTable< std::string, int >& nameOccur) const = 0;
133
138 std::string toLatex(HashTable< std::string, int > nameOccur
139 = HashTable< std::string, int >()) const;
140
142 virtual std::unique_ptr< ASTtree< GUM_SCALAR > > copy() const = 0;
143
149 virtual Tensor< GUM_SCALAR > eval(const IBayesNet< GUM_SCALAR >& contextual_bn) const = 0;
150
151 protected:
153 static constexpr const char* CONTINUE_PREFIX = "| ";
154 std::string _type;
155
157 static std::string _latexCorrect(std::string_view srcName,
158 HashTable< std::string, int >& nameOccur);
160 static std::vector< std::string > _latexCorrect(const Set< std::string >& srcNames,
161 HashTable< std::string, int >& nameOccur);
162 };
163
164 // ================================================================
165 // ASTBinaryOp
166 // ================================================================
174 template < GUM_Numeric GUM_SCALAR >
175 class ASTBinaryOp: public ASTtree< GUM_SCALAR > {
176 public:
182 ASTBinaryOp(std::string_view typ,
183 std::unique_ptr< ASTtree< GUM_SCALAR > > op1,
184 std::unique_ptr< ASTtree< GUM_SCALAR > > op2);
185
186 ASTBinaryOp(const ASTBinaryOp&) = delete;
188 ASTBinaryOp(ASTBinaryOp&&) noexcept = default;
189 ASTBinaryOp& operator=(ASTBinaryOp&&) noexcept = default;
190
192 const ASTtree< GUM_SCALAR >& op1() const;
193
195 const ASTtree< GUM_SCALAR >& op2() const;
196
198 std::string toString(std::string_view prefix = "") const override;
199
200 protected:
201 std::unique_ptr< ASTtree< GUM_SCALAR > > _op1, _op2;
202 };
203
204 // ================================================================
205 // ASTplus (ASTBinaryOp)
206 // ================================================================
214 template < GUM_Numeric GUM_SCALAR >
215 class ASTplus: public ASTBinaryOp< GUM_SCALAR > {
216 public:
217 ASTplus(std::unique_ptr< ASTtree< GUM_SCALAR > > op1,
218 std::unique_ptr< ASTtree< GUM_SCALAR > > op2);
219
220 std::string protectToLatex(HashTable< std::string, int >& nameOccur) const override;
221 std::string fastToLatex(HashTable< std::string, int >& nameOccur) const override;
222 std::unique_ptr< ASTtree< GUM_SCALAR > > copy() const override;
223 Tensor< GUM_SCALAR > eval(const IBayesNet< GUM_SCALAR >& contextual_bn) const override;
224 };
225
226 // ================================================================
227 // ASTminus (ASTBinaryOp)
228 // ================================================================
236 template < GUM_Numeric GUM_SCALAR >
237 class ASTminus: public ASTBinaryOp< GUM_SCALAR > {
238 public:
239 ASTminus(std::unique_ptr< ASTtree< GUM_SCALAR > > op1,
240 std::unique_ptr< ASTtree< GUM_SCALAR > > op2);
241
242 std::string protectToLatex(HashTable< std::string, int >& nameOccur) const override;
243 std::string fastToLatex(HashTable< std::string, int >& nameOccur) const override;
244 std::unique_ptr< ASTtree< GUM_SCALAR > > copy() const override;
245 Tensor< GUM_SCALAR > eval(const IBayesNet< GUM_SCALAR >& contextual_bn) const override;
246 };
247
248 // ================================================================
249 // ASTmult (ASTBinaryOp)
250 // ================================================================
256 template < GUM_Numeric GUM_SCALAR >
257 class ASTmult: public ASTBinaryOp< GUM_SCALAR > {
258 public:
259 ASTmult(std::unique_ptr< ASTtree< GUM_SCALAR > > op1,
260 std::unique_ptr< ASTtree< GUM_SCALAR > > op2);
261
262 std::string protectToLatex(HashTable< std::string, int >& nameOccur) const override;
263 std::string fastToLatex(HashTable< std::string, int >& nameOccur) const override;
264 std::unique_ptr< ASTtree< GUM_SCALAR > > copy() const override;
265 Tensor< GUM_SCALAR > eval(const IBayesNet< GUM_SCALAR >& contextual_bn) const override;
266 };
267
268 // ================================================================
269 // ASTdiv (ASTBinaryOp)
270 // ================================================================
276 template < GUM_Numeric GUM_SCALAR >
277 class ASTdiv: public ASTBinaryOp< GUM_SCALAR > {
278 public:
279 ASTdiv(std::unique_ptr< ASTtree< GUM_SCALAR > > op1,
280 std::unique_ptr< ASTtree< GUM_SCALAR > > op2);
281
282 std::string protectToLatex(HashTable< std::string, int >& nameOccur) const override;
283 std::string fastToLatex(HashTable< std::string, int >& nameOccur) const override;
284 std::unique_ptr< ASTtree< GUM_SCALAR > > copy() const override;
285 Tensor< GUM_SCALAR > eval(const IBayesNet< GUM_SCALAR >& contextual_bn) const override;
286 };
287
288 // ================================================================
289 // ASTposteriorProba : P_bn(vars | knw_min)
290 // ================================================================
303 template < GUM_Numeric GUM_SCALAR >
304 class ASTposteriorProba: public ASTtree< GUM_SCALAR > {
305 public:
308 explicit ASTposteriorProba(const DAGmodel& bn,
310 const Set< std::string >& knw);
311
314 explicit ASTposteriorProba(const DAG& bn,
317 const Set< std::string >& knw);
318
322
324 const Set< std::string >& vars() const noexcept;
325
327 const Set< std::string >& knw() const noexcept;
328
330 std::string toString(std::string_view prefix = "") const override;
332 std::string protectToLatex(HashTable< std::string, int >& nameOccur) const override;
334 std::string fastToLatex(HashTable< std::string, int >& nameOccur) const override;
335
337 std::unique_ptr< ASTtree< GUM_SCALAR > > copy() const override;
339 Tensor< GUM_SCALAR > eval(const IBayesNet< GUM_SCALAR >& contextual_bn) const override;
340
341 private:
344
345
346 static void _ensure_nonempty(const Set< std::string >& vars);
347
350 const Set< std::string >& knw);
351
355 const Set< std::string >& knw);
356 };
357
358 // ================================================================
359 // ASTjointProba : P(vars) in observational BN
360 // ================================================================
367 template < GUM_Numeric GUM_SCALAR >
368 class ASTjointProba: public ASTtree< GUM_SCALAR > {
369 public:
372
374 const Set< std::string >& varNames() const noexcept;
375
377 std::string toString(std::string_view prefix = "") const override;
379 std::string protectToLatex(HashTable< std::string, int >& nameOccur) const override;
381 std::string fastToLatex(HashTable< std::string, int >& nameOccur) const override;
382
384 std::unique_ptr< ASTtree< GUM_SCALAR > > copy() const override;
386 Tensor< GUM_SCALAR > eval(const IBayesNet< GUM_SCALAR >& contextual_bn) const override;
387
388 private:
390 };
391
392 // ================================================================
393 // ASTsum : sum out over variable of sub-term
394 // ================================================================
403 template < GUM_Numeric GUM_SCALAR >
404 class ASTsum: public ASTtree< GUM_SCALAR > {
405 public:
407 ASTsum(std::string_view var, std::unique_ptr< ASTtree< GUM_SCALAR > > term);
408
413 ASTsum(const std::vector< std::string >& vars, std::unique_ptr< ASTtree< GUM_SCALAR > > term);
414
416 [[nodiscard]] const std::string& var() const;
417
419 const ASTtree< GUM_SCALAR >& term() const;
420
422 std::string toString(std::string_view prefix = "") const override;
424 std::string protectToLatex(HashTable< std::string, int >& nameOccur) const override;
426 std::string fastToLatex(HashTable< std::string, int >& nameOccur) const override;
427
429 std::unique_ptr< ASTtree< GUM_SCALAR > > copy() const override;
431 Tensor< GUM_SCALAR > eval(const IBayesNet< GUM_SCALAR >& contextual_bn) const override;
432
433 private:
434 std::string _var;
435 std::unique_ptr< ASTtree< GUM_SCALAR > > _term;
436 };
437
438 // ================================================================
439 // productOfTrees (utility function)
440 // ================================================================
450 template < GUM_Numeric GUM_SCALAR >
451 std::unique_ptr< ASTtree< GUM_SCALAR > >
452 productOfTrees(std::vector< std::unique_ptr< ASTtree< GUM_SCALAR > > >&& lterms);
453
454
455// ================================================================
456// Extern template declarations
457// ================================================================
458#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
459 extern template class ASTtree< double >;
460 extern template class ASTplus< double >;
461 extern template class ASTminus< double >;
462 extern template class ASTmult< double >;
463 extern template class ASTdiv< double >;
464 extern template class ASTposteriorProba< double >;
465 extern template class ASTjointProba< double >;
466 extern template class ASTsum< double >;
467 extern template std::unique_ptr< ASTtree< double > >
468 productOfTrees< double >(std::vector< std::unique_ptr< ASTtree< double > > >&&);
469#endif
470
471} // namespace gum
472
474
475#endif // GUM_DO_AST_H
Class representing the minimal interface for Bayesian network with no numerical data.
ASTBinaryOp(std::string_view typ, std::unique_ptr< ASTtree< GUM_SCALAR > > op1, std::unique_ptr< ASTtree< GUM_SCALAR > > op2)
Definition doAST_tpl.h:104
std::string toString(std::string_view prefix="") const override
Human-readable multi-line rendering (for debugging / logs).
Definition doAST_tpl.h:120
const ASTtree< GUM_SCALAR > & op2() const
Definition doAST_tpl.h:115
const ASTtree< GUM_SCALAR > & op1() const
Definition doAST_tpl.h:110
std::unique_ptr< ASTtree< GUM_SCALAR > > _op2
Definition doAST.h:201
std::unique_ptr< ASTtree< GUM_SCALAR > > _op1
Definition doAST.h:201
ASTBinaryOp & operator=(const ASTBinaryOp &)=delete
ASTBinaryOp(ASTBinaryOp &&) noexcept=default
ASTBinaryOp(const ASTBinaryOp &)=delete
Elementwise division of two AST sub-expressions (left / right).
Definition doAST.h:277
std::string fastToLatex(HashTable< std::string, int > &nameOccur) const override
Fast LaTeX rendering (lighter protection).
Definition doAST_tpl.h:237
std::string protectToLatex(HashTable< std::string, int > &nameOccur) const override
LaTeX rendering with full name protection.
Definition doAST_tpl.h:232
ASTdiv(std::unique_ptr< ASTtree< GUM_SCALAR > > op1, std::unique_ptr< ASTtree< GUM_SCALAR > > op2)
Definition doAST_tpl.h:227
Tensor< GUM_SCALAR > eval(const IBayesNet< GUM_SCALAR > &contextual_bn) const override
Evaluate the expression against a contextual BN.
Definition doAST_tpl.h:250
std::unique_ptr< ASTtree< GUM_SCALAR > > copy() const override
Deep clone of the sub-tree.
Definition doAST_tpl.h:243
Joint probability term ( \mathbb{P}(\mathrm{vars}) ) in an observational BN.
Definition doAST.h:368
std::string fastToLatex(HashTable< std::string, int > &nameOccur) const override
Fast LaTeX rendering (lighter protection).
Definition doAST_tpl.h:486
ASTjointProba(const Set< std::string > &varNames)
Build a joint (\mathbb{P}(\mathrm{varNames})).
Definition doAST_tpl.h:448
const Set< std::string > & varNames() const noexcept
Definition doAST_tpl.h:455
std::unique_ptr< ASTtree< GUM_SCALAR > > copy() const override
Deep clone of the sub-tree.
Definition doAST_tpl.h:500
std::string toString(std::string_view prefix="") const override
Human-readable multi-line rendering (for debugging / logs).
Definition doAST_tpl.h:460
std::string protectToLatex(HashTable< std::string, int > &nameOccur) const override
LaTeX rendering with full name protection.
Definition doAST_tpl.h:480
Set< std::string > _varNames
Definition doAST.h:389
Tensor< GUM_SCALAR > eval(const IBayesNet< GUM_SCALAR > &contextual_bn) const override
Evaluate the expression against a contextual BN.
Definition doAST_tpl.h:506
Difference of two AST sub-expressions (left minus right).
Definition doAST.h:237
std::string fastToLatex(HashTable< std::string, int > &nameOccur) const override
Fast LaTeX rendering (lighter protection).
Definition doAST_tpl.h:175
std::unique_ptr< ASTtree< GUM_SCALAR > > copy() const override
Deep clone of the sub-tree.
Definition doAST_tpl.h:180
std::string protectToLatex(HashTable< std::string, int > &nameOccur) const override
LaTeX rendering with full name protection.
Definition doAST_tpl.h:170
ASTminus(std::unique_ptr< ASTtree< GUM_SCALAR > > op1, std::unique_ptr< ASTtree< GUM_SCALAR > > op2)
Definition doAST_tpl.h:164
Tensor< GUM_SCALAR > eval(const IBayesNet< GUM_SCALAR > &contextual_bn) const override
Evaluate the expression against a contextual BN.
Definition doAST_tpl.h:187
Elementwise product of two AST sub-expressions.
Definition doAST.h:257
std::string fastToLatex(HashTable< std::string, int > &nameOccur) const override
Fast LaTeX rendering (lighter protection).
Definition doAST_tpl.h:207
Tensor< GUM_SCALAR > eval(const IBayesNet< GUM_SCALAR > &contextual_bn) const override
Evaluate the expression against a contextual BN.
Definition doAST_tpl.h:219
ASTmult(std::unique_ptr< ASTtree< GUM_SCALAR > > op1, std::unique_ptr< ASTtree< GUM_SCALAR > > op2)
Definition doAST_tpl.h:195
std::unique_ptr< ASTtree< GUM_SCALAR > > copy() const override
Deep clone of the sub-tree.
Definition doAST_tpl.h:212
std::string protectToLatex(HashTable< std::string, int > &nameOccur) const override
LaTeX rendering with full name protection.
Definition doAST_tpl.h:201
Sum of two AST sub-expressions.
Definition doAST.h:215
ASTplus(std::unique_ptr< ASTtree< GUM_SCALAR > > op1, std::unique_ptr< ASTtree< GUM_SCALAR > > op2)
Definition doAST_tpl.h:133
Posterior probability term ( \mathbb{P}_{bn}(\mathrm{vars}\mid\mathrm{knw}) ).
Definition doAST.h:304
std::string toString(std::string_view prefix="") const override
Human-readable multi-line rendering (for debugging / logs).
Definition doAST_tpl.h:289
static void _ensure_nonempty(const Set< std::string > &vars)
Definition doAST_tpl.h:405
Tensor< GUM_SCALAR > eval(const IBayesNet< GUM_SCALAR > &contextual_bn) const override
Evaluate the expression against a contextual BN.
Definition doAST_tpl.h:367
Set< std::string > _vars
names of conditioned variables
Definition doAST.h:342
static Set< std::string > _compute_knw_from_dag(const DAG &dag, const Bijection< NodeId, std::string > &id2name, const Set< std::string > &vars, const Set< std::string > &knw)
Definition doAST_tpl.h:427
const Set< std::string > & knw() const noexcept
Definition doAST_tpl.h:284
std::string fastToLatex(HashTable< std::string, int > &nameOccur) const override
Fast LaTeX rendering (lighter protection).
Definition doAST_tpl.h:329
Set< std::string > _knw
names of conditioning variables (already minimalized)
Definition doAST.h:343
ASTposteriorProba(const DAGmodel &bn, const Set< std::string > &vars, const Set< std::string > &knw)
Constructor for ( \mathbb{P}_{bn}(\mathrm{vars}\mid\mathrm{knw}) ); knw will be minimalized using bn.
Definition doAST_tpl.h:258
static Set< std::string > _compute_knw_from_bn(const DAGmodel &bn, const Set< std::string > &vars, const Set< std::string > &knw)
Definition doAST_tpl.h:411
const Set< std::string > & vars() const noexcept
Definition doAST_tpl.h:279
std::string protectToLatex(HashTable< std::string, int > &nameOccur) const override
LaTeX rendering with full name protection.
Definition doAST_tpl.h:322
std::unique_ptr< ASTtree< GUM_SCALAR > > copy() const override
Deep clone of the sub-tree.
Definition doAST_tpl.h:360
Summation (marginalization) over one or more variables.
Definition doAST.h:404
std::string _var
variable to eliminate
Definition doAST.h:434
std::string protectToLatex(HashTable< std::string, int > &nameOccur) const override
LaTeX rendering with full name protection.
Definition doAST_tpl.h:570
std::string toString(std::string_view prefix="") const override
Human-readable multi-line rendering (for debugging / logs).
Definition doAST_tpl.h:561
Tensor< GUM_SCALAR > eval(const IBayesNet< GUM_SCALAR > &contextual_bn) const override
Evaluate the expression against a contextual BN.
Definition doAST_tpl.h:619
std::string fastToLatex(HashTable< std::string, int > &nameOccur) const override
Fast LaTeX rendering (lighter protection).
Definition doAST_tpl.h:575
std::unique_ptr< ASTtree< GUM_SCALAR > > _term
sub-expression
Definition doAST.h:435
ASTsum(std::string_view var, std::unique_ptr< ASTtree< GUM_SCALAR > > term)
Single-variable summation ( \sum_{\text{var}} \text{term} ).
Definition doAST_tpl.h:530
const ASTtree< GUM_SCALAR > & term() const
Definition doAST_tpl.h:556
std::unique_ptr< ASTtree< GUM_SCALAR > > copy() const override
Deep clone of the sub-tree.
Definition doAST_tpl.h:613
const std::string & var() const
Definition doAST_tpl.h:551
Root abstract node for the AST of algebraic expressions.
Definition doAST.h:91
virtual std::string protectToLatex(HashTable< std::string, int > &nameOccur) const =0
LaTeX rendering with full name protection.
virtual std::unique_ptr< ASTtree< GUM_SCALAR > > copy() const =0
Deep clone of the sub-tree.
virtual std::string toString(std::string_view prefix="") const =0
Human-readable multi-line rendering (for debugging / logs).
ASTtree(ASTtree &&) noexcept
Definition doAST.h:101
virtual ~ASTtree()
Definition doAST_tpl.h:63
ASTtree & operator=(ASTtree &&) noexcept=default
virtual std::string fastToLatex(HashTable< std::string, int > &nameOccur) const =0
Fast LaTeX rendering (lighter protection).
ASTtree(std::string_view type)
Construct an AST node with a descriptive type string (used in dumps).
Definition doAST_tpl.h:60
std::string toLatex(HashTable< std::string, int > nameOccur=HashTable< std::string, int >()) const
Convenience wrapper using an empty occurrence table.
Definition doAST_tpl.h:73
static constexpr const char * CONTINUE_PREFIX
Prefix used to draw tree branches in toString.
Definition doAST.h:153
static std::string _latexCorrect(std::string_view srcName, HashTable< std::string, int > &nameOccur)
Sanitize a single variable name for LaTeX and ensure uniqueness.
Definition doAST_tpl.h:79
const std::string & type() const noexcept
Definition doAST_tpl.h:68
ASTtree(const ASTtree &)=delete
virtual Tensor< GUM_SCALAR > eval(const IBayesNet< GUM_SCALAR > &contextual_bn) const =0
Evaluate the expression against a contextual BN.
std::string _type
Definition doAST.h:154
ASTtree & operator=(const ASTtree &)=delete
Base class for dag.
Definition DAG.h:121
Virtual base class for PGMs using a DAG.
Definition DAGmodel.h:64
The class for generic Hash Tables.
Definition hashTable.h:640
Class representing the minimal interface for Bayesian network with no numerical data.
Definition IBayesNet.h:75
Representation of a set.
Definition set.h:129
aGrUM's Tensor is a multi-dimensional array with tensor operators.
Definition tensor.h:85
Complete concept for GUM_SCALAR template parameter.
Definition concepts.h:148
Template implementation of CausalModel/doAST.h.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
std::unique_ptr< ASTtree< GUM_SCALAR > > productOfTrees(std::vector< std::unique_ptr< ASTtree< GUM_SCALAR > > > &&lterms)
Build a product AST from a list of terms.
Definition doAST_tpl.h:633
template std::unique_ptr< ASTtree< double > > productOfTrees< double >(std::vector< std::unique_ptr< ASTtree< double > > > &&)
STL namespace.