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 >(
38 new MultiDimNoisyORCompound< GUM_SCALAR >(
40 static_cast<
const MultiDimNoisyORCompound< GUM_SCALAR >& >(
42 }
else if (
dynamic_cast<
const MultiDimNoisyORNet< GUM_SCALAR >* >(
44 p =
new Potential< GUM_SCALAR >(
new MultiDimNoisyORNet< GUM_SCALAR >(
46 static_cast<
const MultiDimNoisyORNet< GUM_SCALAR >& >(*impl)));
47 }
else if (
dynamic_cast<
48 const aggregator::MultiDimAggregator< GUM_SCALAR >* >(
50 p =
new Potential< GUM_SCALAR >(
51 static_cast< MultiDimImplementation< GUM_SCALAR >* >(
54 for (
auto var: impl->variablesSequence())
55 p->add(*(bij.second(var)));
56 }
else if (
dynamic_cast<
const MultiDimBucket< GUM_SCALAR >* >(impl)) {
58 const_cast< MultiDimBucket< GUM_SCALAR >* >(
59 static_cast<
const MultiDimBucket< GUM_SCALAR >* >(impl))
63 p =
new Potential< GUM_SCALAR >(
new MultiDimBijArray< GUM_SCALAR >(
65 static_cast<
const MultiDimBucket< GUM_SCALAR >* >(impl)
67 }
catch (OperationNotAllowed&) {
70 return new Potential< GUM_SCALAR >();
74 "encountered an unexpected MultiDim implementation");
77 if (
dynamic_cast<
const MultiDimArray< GUM_SCALAR >* >(impl)) {
78 p =
new Potential< GUM_SCALAR >(
new MultiDimBijArray< GUM_SCALAR >(
80 static_cast<
const MultiDimArray< GUM_SCALAR >& >(*impl)));
81 }
else if (
dynamic_cast<
const MultiDimBijArray< GUM_SCALAR >* >(impl)) {
82 p =
new Potential< GUM_SCALAR >(
new MultiDimBijArray< GUM_SCALAR >(
84 static_cast<
const MultiDimBijArray< GUM_SCALAR >& >(*impl)));
85 }
else if (
dynamic_cast<
const MultiDimSparse< GUM_SCALAR >* >(impl)) {
87 "There is no MultiDimSparse in PRMs, normally...");
92 "encountered an unexpected MultiDim implementation");
97 }
catch (Exception&) {
105 template <
typename GUM_SCALAR >
108 return new Potential< GUM_SCALAR >(t1 * t2);
111 template <
typename GUM_SCALAR >
115 Potential< GUM_SCALAR >* pot =
nullptr;
116 Potential< GUM_SCALAR >* tmp =
nullptr;
118 Set<
const DiscreteVariable* > var_set;
120 Set<
const Potential< GUM_SCALAR >* > pots;
122 for (
const auto p: pool)
123 if (p->contains(*var)) pots.insert(p);
125 if (pots.size() == 0) {
127 }
else if (pots.size() == 1) {
128 tmp =
const_cast< Potential< GUM_SCALAR >* >(*pots.begin());
129 pot =
new Potential< GUM_SCALAR >(tmp->margSumOut(var_set));
131 MultiDimCombinationDefault< GUM_SCALAR, Potential > Comb(multPotential);
132 tmp = Comb.combine(pots);
133 pot =
new Potential< GUM_SCALAR >(tmp->margSumOut(var_set));
137 for (
const auto p: pots) {
138 pool.erase(
const_cast< Potential< GUM_SCALAR >* >(p));
140 if (trash.exists(
const_cast< Potential< GUM_SCALAR >* >(p))) {
141 trash.erase(
const_cast< Potential< GUM_SCALAR >* >(p));
142 delete const_cast< Potential< GUM_SCALAR >* >(p);
150 template <
typename GUM_SCALAR >
154 for (
auto var: elim_order) {
155 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)