22 #include <agrum/PRM/utils_prm.h> 27 template <
typename GUM_SCALAR >
31 const MultiDimImplementation< GUM_SCALAR >* impl = source.content();
32 Potential< GUM_SCALAR >* p = 0;
35 if (
dynamic_cast<
const MultiDimReadOnly< GUM_SCALAR >* >(impl)) {
36 if (
dynamic_cast<
const MultiDimNoisyORCompound< GUM_SCALAR >* >(impl)) {
37 p =
new Potential< GUM_SCALAR >(
new MultiDimNoisyORCompound< GUM_SCALAR >(
39 static_cast<
const MultiDimNoisyORCompound< GUM_SCALAR >& >(*impl)));
40 }
else if (
dynamic_cast<
const MultiDimNoisyORNet< GUM_SCALAR >* >(impl)) {
41 p =
new Potential< GUM_SCALAR >(
new MultiDimNoisyORNet< GUM_SCALAR >(
43 static_cast<
const MultiDimNoisyORNet< GUM_SCALAR >& >(*impl)));
44 }
else if (
dynamic_cast<
const aggregator::MultiDimAggregator< GUM_SCALAR >* >(impl)) {
45 p =
new Potential< GUM_SCALAR >(
46 static_cast< MultiDimImplementation< GUM_SCALAR >* >(impl->newFactory()));
48 for (
auto var: impl->variablesSequence())
49 p->add(*(bij.second(var)));
50 }
else if (
dynamic_cast<
const MultiDimBucket< GUM_SCALAR >* >(impl)) {
52 const_cast< MultiDimBucket< GUM_SCALAR >* >(
53 static_cast<
const MultiDimBucket< GUM_SCALAR >* >(impl))
57 p =
new Potential< GUM_SCALAR >(
new MultiDimBijArray< GUM_SCALAR >(
59 static_cast<
const MultiDimBucket< GUM_SCALAR >* >(impl)->bucket()));
60 }
catch (OperationNotAllowed&) {
63 return new Potential< GUM_SCALAR >();
66 GUM_ERROR(FatalError,
"encountered an unexpected MultiDim implementation")
69 if (
dynamic_cast<
const MultiDimArray< GUM_SCALAR >* >(impl)) {
70 p =
new Potential< GUM_SCALAR >(
new MultiDimBijArray< GUM_SCALAR >(
72 static_cast<
const MultiDimArray< GUM_SCALAR >& >(*impl)));
73 }
else if (
dynamic_cast<
const MultiDimBijArray< GUM_SCALAR >* >(impl)) {
74 p =
new Potential< GUM_SCALAR >(
new MultiDimBijArray< GUM_SCALAR >(
76 static_cast<
const MultiDimBijArray< GUM_SCALAR >& >(*impl)));
77 }
else if (
dynamic_cast<
const MultiDimSparse< GUM_SCALAR >* >(impl)) {
78 GUM_ERROR(FatalError,
"There is no MultiDimSparse in PRMs, normally...")
82 GUM_ERROR(FatalError,
"encountered an unexpected MultiDim implementation")
87 }
catch (Exception&) {
95 template <
typename GUM_SCALAR >
98 return new Potential< GUM_SCALAR >(t1 * t2);
101 template <
typename GUM_SCALAR >
105 Potential< GUM_SCALAR >* pot =
nullptr;
106 Potential< GUM_SCALAR >* tmp =
nullptr;
108 Set<
const DiscreteVariable* > var_set;
110 Set<
const Potential< GUM_SCALAR >* > pots;
112 for (
const auto p: pool)
113 if (p->contains(*var)) pots.insert(p);
115 if (pots.size() == 0) {
117 }
else if (pots.size() == 1) {
118 tmp =
const_cast< Potential< GUM_SCALAR >* >(*pots.begin());
119 pot =
new Potential< GUM_SCALAR >(tmp->margSumOut(var_set));
121 MultiDimCombinationDefault< GUM_SCALAR, Potential > Comb(multPotential);
122 tmp = Comb.combine(pots);
123 pot =
new Potential< GUM_SCALAR >(tmp->margSumOut(var_set));
127 for (
const auto p: pots) {
128 pool.erase(
const_cast< Potential< GUM_SCALAR >* >(p));
130 if (trash.exists(
const_cast< Potential< GUM_SCALAR >* >(p))) {
131 trash.erase(
const_cast< Potential< GUM_SCALAR >* >(p));
132 delete const_cast< Potential< GUM_SCALAR >* >(p);
140 template <
typename GUM_SCALAR >
144 for (
auto var: elim_order) {
145 eliminateNode(var, pool, trash);
INLINE void emplace(Args &&... args)
ParamScopeData(const std::string &s, const PRMReferenceSlot< GUM_SCALAR > &ref, Idx d)
Potential< GUM_SCALAR > * copyPotential(const Bijection< const DiscreteVariable *, const DiscreteVariable * > &bij, const Potential< GUM_SCALAR > &source)
Returns a copy of a Potential after applying a bijection over the variables in source.
void eliminateNode(const DiscreteVariable *var, Set< Potential< GUM_SCALAR > * > &pool, Set< Potential< GUM_SCALAR > * > &trash)
Proceeds with the elimination of var in pool.
void eliminateNodes(const std::vector< const DiscreteVariable * > &elim_order, Set< Potential< GUM_SCALAR > * > &pool, Set< Potential< GUM_SCALAR > * > &trash)
Potential< GUM_SCALAR > * multPotential(const Potential< GUM_SCALAR > &t1, const Potential< GUM_SCALAR > &t2)