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

Difference of two AST sub-expressions (left minus right). More...

#include <doAST.h>

Inheritance diagram for gum::ASTminus< GUM_SCALAR >:
Collaboration diagram for gum::ASTminus< GUM_SCALAR >:

Public Member Functions

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

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::unique_ptr< ASTtree< GUM_SCALAR > > _op1
std::unique_ptr< ASTtree< GUM_SCALAR > > _op2
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::ASTminus< GUM_SCALAR >

Difference of two AST sub-expressions (left minus right).

LaTeX: renders as ( (\cdot) - (\cdot) ). eval computes element-wise subtraction on aligned tensors.

Definition at line 237 of file doAST.h.

Constructor & Destructor Documentation

◆ ASTminus()

template<GUM_Numeric GUM_SCALAR>
gum::ASTminus< GUM_SCALAR >::ASTminus ( std::unique_ptr< ASTtree< GUM_SCALAR > > op1,
std::unique_ptr< ASTtree< GUM_SCALAR > > op2 )

Definition at line 164 of file doAST_tpl.h.

165 :
ASTBinaryOp(std::string_view typ, std::unique_ptr< ASTtree< GUM_SCALAR > > op1, std::unique_ptr< ASTtree< GUM_SCALAR > > op2)
Definition doAST_tpl.h:104
const ASTtree< GUM_SCALAR > & op2() const
Definition doAST_tpl.h:115
const ASTtree< GUM_SCALAR > & op1() const
Definition doAST_tpl.h:110
Difference of two AST sub-expressions (left minus right).
Definition doAST.h:237

References gum::ASTBinaryOp< GUM_SCALAR >::ASTBinaryOp(), gum::ASTBinaryOp< GUM_SCALAR >::op1(), and gum::ASTBinaryOp< GUM_SCALAR >::op2().

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 )
staticprotectedinherited

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 }
Root abstract node for the AST of algebraic expressions.
Definition doAST.h:91
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 )
staticprotectedinherited

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>
std::unique_ptr< ASTtree< GUM_SCALAR > > gum::ASTminus< GUM_SCALAR >::copy ( ) const
overridevirtual

Deep clone of the sub-tree.

Implements gum::ASTtree< GUM_SCALAR >.

Definition at line 180 of file doAST_tpl.h.

180 {
181 auto l = this->_op1->copy();
182 auto r = this->_op2->copy();
184 }
std::unique_ptr< ASTtree< GUM_SCALAR > > _op2
Definition doAST.h:201
std::unique_ptr< ASTtree< GUM_SCALAR > > _op1
Definition doAST.h:201

References gum::ASTBinaryOp< GUM_SCALAR >::_op1, and gum::ASTBinaryOp< GUM_SCALAR >::_op2.

◆ eval()

template<GUM_Numeric GUM_SCALAR>
Tensor< GUM_SCALAR > gum::ASTminus< GUM_SCALAR >::eval ( const IBayesNet< GUM_SCALAR > & contextual_bn) const
overridevirtual

Evaluate the expression against a contextual BN.

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

Implements gum::ASTtree< GUM_SCALAR >.

Definition at line 187 of file doAST_tpl.h.

187 {
188 return this->_op1->eval(bn) - this->_op2->eval(bn);
189 }

References gum::ASTBinaryOp< GUM_SCALAR >::_op1, and gum::ASTBinaryOp< GUM_SCALAR >::_op2.

◆ fastToLatex()

template<GUM_Numeric GUM_SCALAR>
std::string gum::ASTminus< GUM_SCALAR >::fastToLatex ( HashTable< std::string, int > & nameOccur) const
overridevirtual

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)

Implements gum::ASTtree< GUM_SCALAR >.

Definition at line 175 of file doAST_tpl.h.

175 {
176 return this->_op1->fastToLatex(nameOccur) + "-" + this->_op2->fastToLatex(nameOccur);
177 }

References gum::ASTBinaryOp< GUM_SCALAR >::_op1, and gum::ASTBinaryOp< GUM_SCALAR >::_op2.

Referenced by protectToLatex().

Here is the caller graph for this function:

◆ op1()

template<GUM_Numeric GUM_SCALAR>
const ASTtree< GUM_SCALAR > & gum::ASTBinaryOp< GUM_SCALAR >::op1 ( ) const
inherited
Returns
left operand

Definition at line 110 of file doAST_tpl.h.

110 {
111 return *_op1;
112 }

References _op1.

Referenced by ASTBinaryOp(), gum::ASTdiv< GUM_SCALAR >::ASTdiv(), gum::ASTminus< GUM_SCALAR >::ASTminus(), gum::ASTmult< GUM_SCALAR >::ASTmult(), gum::ASTplus< GUM_SCALAR >::ASTplus(), and operator=().

Here is the caller graph for this function:

◆ op2()

template<GUM_Numeric GUM_SCALAR>
const ASTtree< GUM_SCALAR > & gum::ASTBinaryOp< GUM_SCALAR >::op2 ( ) const
inherited
Returns
right operand

Definition at line 115 of file doAST_tpl.h.

115 {
116 return *_op2;
117 }

References _op2.

Referenced by ASTBinaryOp(), gum::ASTdiv< GUM_SCALAR >::ASTdiv(), gum::ASTminus< GUM_SCALAR >::ASTminus(), gum::ASTmult< GUM_SCALAR >::ASTmult(), gum::ASTplus< GUM_SCALAR >::ASTplus(), and operator=().

Here is the caller graph for this function:

◆ protectToLatex()

template<GUM_Numeric GUM_SCALAR>
std::string gum::ASTminus< GUM_SCALAR >::protectToLatex ( HashTable< std::string, int > & nameOccur) const
overridevirtual

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)

Implements gum::ASTtree< GUM_SCALAR >.

Definition at line 170 of file doAST_tpl.h.

170 {
171 return "\\left(" + fastToLatex(nameOccur) + "\\right)";
172 }
std::string fastToLatex(HashTable< std::string, int > &nameOccur) const override
Fast LaTeX rendering (lighter protection).
Definition doAST_tpl.h:175

References fastToLatex().

Here is the call 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
inherited

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>
std::string gum::ASTBinaryOp< GUM_SCALAR >::toString ( std::string_view prefix = "") const
overridevirtualinherited

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

Parameters
prefixindentation prefix propagated to children

Implements gum::ASTtree< GUM_SCALAR >.

Definition at line 120 of file doAST_tpl.h.

120 {
122 return std::format("{}{}\n{}\n{}",
123 prefix,
124 this->_type,
125 _op1->toString(cont),
126 _op2->toString(cont));
127 }
Base class for binary algebraic operators (e.g., +, -, ×, ÷).
Definition doAST.h:175
std::string _type
Definition doAST.h:154

References _op1, _op2, gum::ASTtree< GUM_SCALAR >::_type, and gum::ASTtree< GUM_SCALAR >::CONTINUE_PREFIX.

Referenced by operator=().

Here is the caller graph for this function:

◆ type()

template<GUM_Numeric GUM_SCALAR>
const std::string & gum::ASTtree< GUM_SCALAR >::type ( ) const
noexceptinherited
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

◆ _op1

◆ _op2

◆ _type

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

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 = "| "
staticconstexprprotectedinherited

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: