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

Summation (marginalization) over one or more variables. More...

#include <doAST.h>

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

Public Member Functions

 ASTsum (std::string_view var, std::unique_ptr< ASTtree< GUM_SCALAR > > term)
 Single-variable summation ( \sum_{\text{var}} \text{term} ).
 ASTsum (const std::vector< std::string > &vars, std::unique_ptr< ASTtree< GUM_SCALAR > > term)
 Multi-variable overload: recursively builds nested sums in the order of vars[0], vars[1], ....
const std::string & var () const
const ASTtree< GUM_SCALAR > & term () const
std::string toString (std::string_view prefix="") const override
 Human-readable multi-line rendering (for debugging / logs).
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 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::string _type

Static Protected Attributes

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

Private Attributes

std::string _var
 variable to eliminate
std::unique_ptr< ASTtree< GUM_SCALAR > > _term
 sub-expression

Detailed Description

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

Summation (marginalization) over one or more variables.

Represents ( \sum_{v} \text{term} ) or a nested version for several variables. Operationally, eval marginalizes the variable(s) from the evaluated subterm tensor.

Definition at line 404 of file doAST.h.

Constructor & Destructor Documentation

◆ ASTsum() [1/2]

template<GUM_Numeric GUM_SCALAR>
gum::ASTsum< GUM_SCALAR >::ASTsum ( std::string_view var,
std::unique_ptr< ASTtree< GUM_SCALAR > > term )

Single-variable summation ( \sum_{\text{var}} \text{term} ).

Definition at line 530 of file doAST_tpl.h.

531 :
Summation (marginalization) over one or more variables.
Definition doAST.h:404
std::string _var
variable to eliminate
Definition doAST.h:434
std::unique_ptr< ASTtree< GUM_SCALAR > > _term
sub-expression
Definition doAST.h:435
const ASTtree< GUM_SCALAR > & term() const
Definition doAST_tpl.h:556
const std::string & var() const
Definition doAST_tpl.h:551

References gum::ASTtree< GUM_SCALAR >::ASTtree(), _term, _var, term(), and var().

Referenced by fastToLatex().

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

◆ ASTsum() [2/2]

template<GUM_Numeric GUM_SCALAR>
gum::ASTsum< GUM_SCALAR >::ASTsum ( const std::vector< std::string > & vars,
std::unique_ptr< ASTtree< GUM_SCALAR > > term )

Multi-variable overload: recursively builds nested sums in the order of vars[0], vars[1], ....

Definition at line 535 of file doAST_tpl.h.

536 :
537 ASTtree< GUM_SCALAR >("_sum_") {
538 if (vars.empty()) { throw gum::InvalidArgument("ASTsum: variable list cannot be empty"); }
539
540 _var = vars.front();
541
542 if (vars.size() > 1) {
543 std::vector< std::string > tail(vars.begin() + 1, vars.end());
545 } else {
547 }
548 }

References gum::ASTtree< GUM_SCALAR >::ASTtree(), _var, and term().

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::ASTsum< GUM_SCALAR >::copy ( ) const
overridevirtual

Deep clone of the sub-tree.

Implements gum::ASTtree< GUM_SCALAR >.

Definition at line 613 of file doAST_tpl.h.

613 {
615 }

References _term, and _var.

◆ eval()

template<GUM_Numeric GUM_SCALAR>
Tensor< GUM_SCALAR > gum::ASTsum< 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 619 of file doAST_tpl.h.

619 {
620 auto base = _term->eval(contextual_bn);
621
622 const auto& dv = contextual_bn.variable(contextual_bn.idFromName(_var));
624 dv_set.insert(&dv);
625 return base.sumOut(dv_set);
626 }

References _term, _var, gum::DiscreteGraphicalModel::idFromName(), gum::Set< Key >::insert(), and gum::DiscreteGraphicalModel::variable().

Here is the call graph for this function:

◆ fastToLatex()

template<GUM_Numeric GUM_SCALAR>
std::string gum::ASTsum< 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 575 of file doAST_tpl.h.

575 {
576 // Flatten chained sums: collect all summed variables along the _term chain.
578 const ASTtree< GUM_SCALAR >* a = this;
579 while (auto s = dynamic_cast< const ASTsum< GUM_SCALAR >* >(a)) {
580 if (!nameOccur.exists(s->_var)) nameOccur.insert(s->_var, 0);
581 nameOccur[s->_var] += 1;
582 vars.push_back(s->_var);
583 a = &s->term(); // move down the chain
584 }
585
586 // Build corrected (prime-adjusted) names, then sort.
588 for (const auto& v: vars) {
589 varNames.insert(v);
590 }
592
593 // Join corrected names with commas
595 for (size_t i = 0; i < corr.size(); ++i) {
596 if (i) names += ',';
597 names += corr[i];
598 }
599
600 // Inner formula is the first non-sum node's latex with current nameOccur
601 std::string out = std::format("\\sum_{{{}}}{{{}}}", names, a->protectToLatex(nameOccur));
602
603 // Restore nameOccur (decrement each var)
604 for (const auto& v: vars) {
605 // nameOccur[v] is guaranteed to exist here
606 nameOccur[v] -= 1;
607 }
608
609 return out;
610 }
std::string protectToLatex(HashTable< std::string, int > &nameOccur) const override
LaTeX rendering with full name protection.
Definition doAST_tpl.h:570
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

References ASTsum(), gum::ASTtree< GUM_SCALAR >::_latexCorrect(), gum::HashTable< Key, Val >::exists(), gum::HashTable< Key, Val >::insert(), and gum::Set< Key >::insert().

Referenced by protectToLatex().

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

◆ protectToLatex()

template<GUM_Numeric GUM_SCALAR>
std::string gum::ASTsum< 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 570 of file doAST_tpl.h.

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

References fastToLatex().

Here is the call graph for this function:

◆ term()

template<GUM_Numeric GUM_SCALAR>
const ASTtree< GUM_SCALAR > & gum::ASTsum< GUM_SCALAR >::term ( ) const
Returns
the subterm being summed over

Definition at line 556 of file doAST_tpl.h.

556 {
557 return *_term;
558 }

References _term.

Referenced by ASTsum(), and ASTsum().

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
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::ASTsum< GUM_SCALAR >::toString ( std::string_view prefix = "") const
overridevirtual

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

Parameters
prefixindentation prefix propagated to children

Implements gum::ASTtree< GUM_SCALAR >.

Definition at line 561 of file doAST_tpl.h.

561 {
562 return std::format(
563 "{}sum on {} for\n{}",
564 prefix,
565 _var,
567 }

References _term, _var, and gum::ASTtree< GUM_SCALAR >::CONTINUE_PREFIX.

◆ 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 }
std::string _type
Definition doAST.h:154

References _type.

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

Here is the caller graph for this function:

◆ var()

template<GUM_Numeric GUM_SCALAR>
const std::string & gum::ASTsum< GUM_SCALAR >::var ( ) const
nodiscard
Returns
the variable being eliminated by this summation

Definition at line 551 of file doAST_tpl.h.

551 {
552 return _var;
553 }

References _var.

Referenced by ASTsum().

Here is the caller graph for this function:

Member Data Documentation

◆ _term

template<GUM_Numeric GUM_SCALAR>
std::unique_ptr< ASTtree< GUM_SCALAR > > gum::ASTsum< GUM_SCALAR >::_term
private

sub-expression

Definition at line 435 of file doAST.h.

Referenced by ASTsum(), copy(), eval(), term(), and toString().

◆ _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().

◆ _var

template<GUM_Numeric GUM_SCALAR>
std::string gum::ASTsum< GUM_SCALAR >::_var
private

variable to eliminate

Definition at line 434 of file doAST.h.

Referenced by ASTsum(), ASTsum(), copy(), eval(), toString(), and var().

◆ 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: