aGrUM 3.0.0
a C++ library for (probabilistic) graphical models
gum::ASTtree< GUM_SCALAR > Class Template Referenceabstract

Root abstract node for the AST of algebraic expressions. More...

#include <doAST.h>

Inheritance diagram for gum::ASTtree< GUM_SCALAR >:

Public Member Functions

 ASTtree (std::string_view type)
 Construct an AST node with a descriptive type string (used in dumps).
virtual ~ASTtree ()
 ASTtree (const ASTtree &)=delete
ASTtreeoperator= (const ASTtree &)=delete
 ASTtree (ASTtree &&) noexcept
ASTtreeoperator= (ASTtree &&) noexcept=default
const std::string & type () const noexcept
virtual std::string toString (std::string_view prefix="") const =0
 Human-readable multi-line rendering (for debugging / logs).
virtual std::string protectToLatex (HashTable< std::string, int > &nameOccur) const =0
 LaTeX rendering with full name protection.
virtual std::string fastToLatex (HashTable< std::string, int > &nameOccur) const =0
 Fast LaTeX rendering (lighter protection).
std::string toLatex (HashTable< std::string, int > nameOccur=HashTable< std::string, int >()) const
 Convenience wrapper using an empty occurrence table.
virtual std::unique_ptr< ASTtree< GUM_SCALAR > > copy () const =0
 Deep clone of the sub-tree.
virtual Tensor< GUM_SCALAR > eval (const IBayesNet< GUM_SCALAR > &contextual_bn) const =0
 Evaluate the expression against a contextual BN.

Static Protected Member Functions

static std::string _latexCorrect (std::string_view srcName, HashTable< std::string, int > &nameOccur)
 Sanitize a single variable name for LaTeX and ensure uniqueness.
static std::vector< std::string > _latexCorrect (const Set< std::string > &srcNames, HashTable< std::string, int > &nameOccur)
 Sanitize a set of variable names for LaTeX and ensure uniqueness.

Protected Attributes

std::string _type

Static Protected Attributes

static constexpr const char * CONTINUE_PREFIX = "| "
 Prefix used to draw tree branches in toString.

Detailed Description

template<GUM_Numeric GUM_SCALAR>
class gum::ASTtree< GUM_SCALAR >

Root abstract node for the AST of algebraic expressions.

This hierarchy represents symbolic probability expressions (e.g., ( \mathbb{P}(Y\mid X) ), products, sums) that can be:

  • pretty-printed (string / LaTeX),
  • structurally copied,
  • evaluated into a numeric Tensor<GUM_SCALAR> w.r.t. a contextual IBayesNet<GUM_SCALAR>.

Ownership & semantics

  • Nodes are move-enabled and non-copyable to avoid expensive deep copies.
  • eval interprets the node in the context of a given BN and returns a factor/tensor on the relevant variables.
  • LaTeX helpers maintain name hygiene using nameOccur to avoid clashes and to produce stable, readable notation.

Definition at line 91 of file doAST.h.

Constructor & Destructor Documentation

◆ ASTtree() [1/3]

template<GUM_Numeric GUM_SCALAR>
gum::ASTtree< GUM_SCALAR >::ASTtree ( std::string_view type)
explicit

Construct an AST node with a descriptive type string (used in dumps).

Definition at line 60 of file doAST_tpl.h.

60: _type(type) {}
const std::string & type() const noexcept
Definition doAST_tpl.h:68
std::string _type
Definition doAST.h:154

References _type, and type().

Referenced by gum::ASTBinaryOp< GUM_SCALAR >::ASTBinaryOp(), gum::ASTjointProba< GUM_SCALAR >::ASTjointProba(), gum::ASTposteriorProba< GUM_SCALAR >::ASTposteriorProba(), gum::ASTsum< GUM_SCALAR >::ASTsum(), gum::ASTsum< GUM_SCALAR >::ASTsum(), ASTtree(), ASTtree(), ~ASTtree(), copy(), gum::ASTBinaryOp< GUM_SCALAR >::operator=(), operator=(), and operator=().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ~ASTtree()

template<GUM_Numeric GUM_SCALAR>
gum::ASTtree< GUM_SCALAR >::~ASTtree ( )
virtual

Definition at line 63 of file doAST_tpl.h.

63 {
65 }
Root abstract node for the AST of algebraic expressions.
Definition doAST.h:91
ASTtree(std::string_view type)
Construct an AST node with a descriptive type string (used in dumps).
Definition doAST_tpl.h:60

References ASTtree().

Here is the call graph for this function:

◆ ASTtree() [2/3]

template<GUM_Numeric GUM_SCALAR>
gum::ASTtree< GUM_SCALAR >::ASTtree ( const ASTtree< GUM_SCALAR > & )
delete

References ASTtree().

Here is the call graph for this function:

◆ ASTtree() [3/3]

template<GUM_Numeric GUM_SCALAR>
gum::ASTtree< GUM_SCALAR >::ASTtree ( ASTtree< GUM_SCALAR > && )
inlinenoexcept

Definition at line 101 of file doAST.h.

References ASTtree().

Here is the call graph for this function:

Member Function Documentation

◆ _latexCorrect() [1/2]

template<GUM_Numeric GUM_SCALAR>
std::vector< std::string > gum::ASTtree< GUM_SCALAR >::_latexCorrect ( const Set< std::string > & srcNames,
HashTable< std::string, int > & nameOccur )
staticprotected

Sanitize a set of variable names for LaTeX and ensure uniqueness.

Definition at line 89 of file doAST_tpl.h.

90 {
91 // Transform each name using the single-name overload, then sort.
93 for (const auto& n: srcNames) {
94 out.push_back(_latexCorrect(n, nameOccur));
95 }
96 std::sort(out.begin(), out.end());
97 return out;
98 }
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

References _latexCorrect().

Here is the call graph for this function:

◆ _latexCorrect() [2/2]

template<GUM_Numeric GUM_SCALAR>
std::string gum::ASTtree< GUM_SCALAR >::_latexCorrect ( std::string_view srcName,
HashTable< std::string, int > & nameOccur )
staticprotected

Sanitize a single variable name for LaTeX and ensure uniqueness.

Definition at line 79 of file doAST_tpl.h.

80 {
81 int count = nameOccur.getWithDefault(std::string{srcName}, 0);
82 const int nbr = (count > 1) ? (count - 1) : 0;
83 // append nbr primes
84 return std::string{srcName} + std::string(static_cast< size_t >(nbr), '\'');
85 }

References gum::HashTable< Key, Val >::getWithDefault().

Referenced by _latexCorrect(), gum::ASTjointProba< GUM_SCALAR >::fastToLatex(), gum::ASTposteriorProba< GUM_SCALAR >::fastToLatex(), gum::ASTsum< GUM_SCALAR >::fastToLatex(), gum::ASTjointProba< GUM_SCALAR >::toString(), and gum::ASTposteriorProba< GUM_SCALAR >::toString().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ copy()

template<GUM_Numeric GUM_SCALAR>
virtual std::unique_ptr< ASTtree< GUM_SCALAR > > gum::ASTtree< GUM_SCALAR >::copy ( ) const
pure virtual

Deep clone of the sub-tree.

Implemented in gum::ASTdiv< GUM_SCALAR >, gum::ASTjointProba< GUM_SCALAR >, gum::ASTminus< GUM_SCALAR >, gum::ASTmult< GUM_SCALAR >, gum::ASTplus< GUM_SCALAR >, gum::ASTposteriorProba< GUM_SCALAR >, and gum::ASTsum< GUM_SCALAR >.

References ASTtree(), and copy().

Referenced by copy().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ eval()

template<GUM_Numeric GUM_SCALAR>
virtual Tensor< GUM_SCALAR > gum::ASTtree< GUM_SCALAR >::eval ( const IBayesNet< GUM_SCALAR > & contextual_bn) const
pure virtual

Evaluate the expression against a contextual BN.

Parameters
contextual_bnBayesian network providing CPTs and variable domains
Returns
resulting factor/tensor

Implemented in gum::ASTdiv< GUM_SCALAR >, gum::ASTjointProba< GUM_SCALAR >, gum::ASTminus< GUM_SCALAR >, gum::ASTmult< GUM_SCALAR >, gum::ASTplus< GUM_SCALAR >, gum::ASTposteriorProba< GUM_SCALAR >, and gum::ASTsum< GUM_SCALAR >.

References eval().

Referenced by eval().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fastToLatex()

template<GUM_Numeric GUM_SCALAR>
virtual std::string gum::ASTtree< GUM_SCALAR >::fastToLatex ( HashTable< std::string, int > & nameOccur) const
pure virtual

Fast LaTeX rendering (lighter protection).

Assumes prior hygiene of names; typically avoids heavier bookkeeping performed in protectToLatex.

Parameters
nameOccursymbol occurrence table (may be consulted/updated)

Implemented in gum::ASTdiv< GUM_SCALAR >, gum::ASTjointProba< GUM_SCALAR >, gum::ASTminus< GUM_SCALAR >, gum::ASTmult< GUM_SCALAR >, gum::ASTplus< GUM_SCALAR >, gum::ASTposteriorProba< GUM_SCALAR >, and gum::ASTsum< GUM_SCALAR >.

References fastToLatex(), and toLatex().

Referenced by fastToLatex(), and toLatex().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator=() [1/2]

template<GUM_Numeric GUM_SCALAR>
ASTtree & gum::ASTtree< GUM_SCALAR >::operator= ( ASTtree< GUM_SCALAR > && )
defaultnoexcept

References ASTtree(), and type().

Here is the call graph for this function:

◆ operator=() [2/2]

template<GUM_Numeric GUM_SCALAR>
ASTtree & gum::ASTtree< GUM_SCALAR >::operator= ( const ASTtree< GUM_SCALAR > & )
delete

References ASTtree().

Here is the call graph for this function:

◆ protectToLatex()

template<GUM_Numeric GUM_SCALAR>
virtual std::string gum::ASTtree< GUM_SCALAR >::protectToLatex ( HashTable< std::string, int > & nameOccur) const
pure virtual

LaTeX rendering with full name protection.

Produces LaTeX with variable-name disambiguation, using nameOccur as a registry of encountered names and their occurrence counts.

Parameters
nameOccursymbol occurrence table (updated in place)

Implemented in gum::ASTdiv< GUM_SCALAR >, gum::ASTjointProba< GUM_SCALAR >, gum::ASTminus< GUM_SCALAR >, gum::ASTmult< GUM_SCALAR >, gum::ASTplus< GUM_SCALAR >, gum::ASTposteriorProba< GUM_SCALAR >, and gum::ASTsum< GUM_SCALAR >.

References protectToLatex().

Referenced by protectToLatex().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ toLatex()

template<GUM_Numeric GUM_SCALAR>
std::string gum::ASTtree< GUM_SCALAR >::toLatex ( HashTable< std::string, int > nameOccur = HashTable< std::string, int >()) const

Convenience wrapper using an empty occurrence table.

Returns
LaTeX string for the whole sub-tree.

Definition at line 73 of file doAST_tpl.h.

73 {
74 // We do not rely on nameOccur mutation at this stage; pass through.
75 return fastToLatex(nameOccur);
76 }
virtual std::string fastToLatex(HashTable< std::string, int > &nameOccur) const =0
Fast LaTeX rendering (lighter protection).

References fastToLatex().

Referenced by fastToLatex().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ toString()

template<GUM_Numeric GUM_SCALAR>
virtual std::string gum::ASTtree< GUM_SCALAR >::toString ( std::string_view prefix = "") const
pure virtual

Human-readable multi-line rendering (for debugging / logs).

Parameters
prefixindentation prefix propagated to children

Implemented in gum::ASTBinaryOp< GUM_SCALAR >, gum::ASTjointProba< GUM_SCALAR >, gum::ASTposteriorProba< GUM_SCALAR >, and gum::ASTsum< GUM_SCALAR >.

References toString().

Referenced by toString().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ type()

template<GUM_Numeric GUM_SCALAR>
const std::string & gum::ASTtree< GUM_SCALAR >::type ( ) const
noexcept
Returns
the runtime type tag (human-readable)

Definition at line 68 of file doAST_tpl.h.

68 {
69 return _type;
70 }

References _type.

Referenced by gum::ASTBinaryOp< GUM_SCALAR >::ASTBinaryOp(), ASTtree(), and operator=().

Here is the caller graph for this function:

Member Data Documentation

◆ _type

template<GUM_Numeric GUM_SCALAR>
std::string gum::ASTtree< GUM_SCALAR >::_type
protected

Definition at line 154 of file doAST.h.

Referenced by ASTtree(), gum::ASTBinaryOp< GUM_SCALAR >::toString(), and type().

◆ CONTINUE_PREFIX

template<GUM_Numeric GUM_SCALAR>
const char* gum::ASTtree< GUM_SCALAR >::CONTINUE_PREFIX = "| "
staticconstexprprotected

Prefix used to draw tree branches in toString.

Definition at line 153 of file doAST.h.

Referenced by gum::ASTBinaryOp< GUM_SCALAR >::toString(), and gum::ASTsum< GUM_SCALAR >::toString().


The documentation for this class was generated from the following files: