aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
partialInstantiationPattern4MultiDimArray.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  *
25  * @brief the pattern used by all the partial instantiations of multidimensional
26  * tables
27  *
28  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
29  */
30 
31 #include <agrum/tools/core/sequence.h>
32 
33 // check if we allowed these patterns to be used
34 #ifndef GUM_PARTIAL_INSTANTIATION_PATTERN_ALLOWED
35 
36 // #warning To use partialIntantiationPattern, you must define
37 // GUM_PARTIAL_INSTANTIATION_PATTERN_ALLOWED
38 
39 #else
40 namespace gum {
41 
42  // a specialized function instantiating some variables of a table and returning
43  // the result
44 
45 # ifdef GUM_MULTI_DIM_PARTIAL_INSTANTIATION_NAME
46 # define GUM_MULTI_DIM_PARTIAL_INSTANTIATION_TYPE GUM_SCALAR
47  template < typename GUM_SCALAR >
48  MultiDimArray< GUM_SCALAR >* GUM_MULTI_DIM_PARTIAL_INSTANTIATION_NAME(
49  const MultiDimArray< GUM_SCALAR >* table,
50  const HashTable< const DiscreteVariable*, Idx >& inst_vars)
51 # endif
52 
53  // clang-format off
54 
55 #ifdef GUM_MULTI_DIM_PARTIAL_INSTANTIATION_POINTER_NAME
56 #define GUM_MULTI_DIM_PARTIAL_INSTANTIATION_TYPE GUM_SCALAR *
57 #define GUM_MULTI_DIM_PARTIAL_INSTANTIATION_POINTER
58  template <typename GUM_SCALAR>
59  MultiDimArray<GUM_SCALAR*>*
60  GUM_MULTI_DIM_PARTIAL_INSTANTIATION_POINTER_NAME(
61  const MultiDimArray<GUM_SCALAR*>* table,
62  const HashTable<const DiscreteVariable*, Idx>& inst_vars )
63 #endif
64 
65 #ifdef GUM_MULTI_DIM_PARTIAL_INSTANTIATION_IMPL2ARRAY_NAME
66 #define GUM_MULTI_DIM_PARTIAL_INSTANTIATION_TYPE GUM_SCALAR
67  template <typename GUM_SCALAR>
68  MultiDimImplementation<GUM_SCALAR>*
69  GUM_MULTI_DIM_PARTIAL_INSTANTIATION_IMPL2ARRAY_NAME(
70  const MultiDimImplementation<GUM_SCALAR>* ttable,
71  const HashTable<const DiscreteVariable*, Idx>& inst_vars )
72 #endif
73 
74 #ifdef GUM_MULTI_DIM_PARTIAL_INSTANTIATION_POINTER_IMPL2ARRAY_NAME
75 #define GUM_MULTI_DIM_PARTIAL_INSTANTIATION_TYPE GUM_SCALAR *
76 #define GUM_MULTI_DIM_PARTIAL_INSTANTIATION_POINTER
77  template <typename GUM_SCALAR>
78  MultiDimImplementation<GUM_SCALAR*>*
79  GUM_MULTI_DIM_PARTIAL_INSTANTIATION_POINTER_IMPL2ARRAY_NAME(
80  const MultiDimImplementation<GUM_SCALAR*>* ttable,
81  const HashTable<const DiscreteVariable*, Idx>& inst_vars )
82 #endif
83 
84  // clang-format on
85 
86  {
87 # ifdef GUM_MULTI_DIM_PARTIAL_INSTANTIATION_IMPL2ARRAY_NAME
88  const MultiDimArray< GUM_SCALAR >* table
89  = reinterpret_cast< const MultiDimArray< GUM_SCALAR >* >(ttable);
90 # endif
91 
92 # ifdef GUM_MULTI_DIM_PARTIAL_INSTANTIATION_POINTER_IMPL2ARRAY_NAME
93  const MultiDimArray< GUM_SCALAR* >* table
94  = reinterpret_cast< const MultiDimArray< GUM_SCALAR* >* >(ttable);
95 # endif
96  // get the variables of the uninstantiated table
97  const Sequence< const DiscreteVariable* >& table_vars = table->variablesSequence();
98 
99  // Compute the offset of the variables. In addition, get the offset in
100  // table induced by the instantiation inst_var
101  Idx table_alone_offset = 0;
102  Idx offset = 1;
103  HashTable< const DiscreteVariable*, Idx > var1offset(table_vars.size());
104 
105  for (const auto var: table_vars) {
106  if (inst_vars.exists(var)) table_alone_offset += inst_vars[var] * offset;
107 
108  var1offset.insert(var, offset);
109  offset *= var->domainSize();
110  }
111 
112  // Compute the sequence of variables in the result table. Compute as well
113  // the offsets and the domain size of the variables that belong to result.
114  // Finally, compute has_before_incr: this is a Boolean indicating whether
115  // the instantiated variables are the last variables in the variables
116  // sequence of table (true) or not (false). If this Boolean is true, then
117  // we can fill result by parsing both table and result using only
118  // 1-increments.
119  Sequence< const DiscreteVariable* > result_varSeq;
120  std::vector< Idx > table_and_result_offset;
121  std::vector< Idx > table_and_result_domain;
122  Idx result_domain_size = 1;
123  bool has_before_incr = true;
124  bool found_inst_var = false;
125 
126  for (const auto var: table_vars) {
127  if (!inst_vars.exists(var)) {
128  table_and_result_domain.push_back(var->domainSize());
129  table_and_result_offset.push_back(var1offset[var]);
130  result_domain_size *= var->domainSize();
131  result_varSeq << var;
132 
133  if (found_inst_var) has_before_incr = false;
134  } else {
135  found_inst_var = true;
136  }
137  }
138 
139  // table_and_result_value is a vector indictating, for each uninstantiated
140  // variable, how many increments we can still perform on that variable
141  // before we must perform a "major" increment: for instance, let A and B be
142  // two variables of size 10. Then, if table_and_result_value[A] = 3 and
143  // table_and_result_value[B] = 2, this means that the offset they represent
144  // is 78 (10^2 - 32). If we still increment B twice, then the offset should
145  // be 80, which means that we shall increment A once and decrease B by 10.
146  // The value by which variables shall be decreased is indicated in
147  // table_and_result_down
148  std::vector< Idx > table_and_result_value = table_and_result_domain;
149  std::vector< Idx > table_and_result_down = table_and_result_offset;
150 
151  for (unsigned int i = 0; i < table_and_result_down.size(); ++i) {
152  table_and_result_down[i] *= (table_and_result_domain[i] - 1);
153  }
154 
155  // create a table "result" containing only the variables that are not
156  // instantiated: the variables are stored in the order in which they appear
157  // in "table". Hence, ++ operations on an instantiation on table will more
158  // or less correspond to a ++ operation on an instantiation on result
159  MultiDimArray< GUM_MULTI_DIM_PARTIAL_INSTANTIATION_TYPE >* result
160  = new MultiDimArray< GUM_MULTI_DIM_PARTIAL_INSTANTIATION_TYPE >;
161  result->beginMultipleChanges();
162 
163  for (const auto var: result_varSeq)
164  *result << *var;
165 
166  result->endMultipleChanges();
167 
168 # ifdef GUM_MULTI_DIM_PARTIAL_INSTANTIATION_POINTER
169  // fill the matrix with any element
170  {
171  const GUM_SCALAR& any_element = *(table->unsafeGet(0));
172 
173  for (Idx i = 0; i < result_domain_size; ++i) {
174  result->unsafeSet(i, new GUM_SCALAR(any_element));
175  }
176  }
177 # endif /* GUM_MULTI_DIM_PARTIAL_INSTANTIATION_POINTER */
178 
179  // compute the result: it is now sufficient to loop over the variables that
180  // were not instantiated. ptable and presult are pointers on the arrays
181  // that are directly used for this loop
182  GUM_MULTI_DIM_PARTIAL_INSTANTIATION_TYPE* ptable
183  = const_cast< GUM_MULTI_DIM_PARTIAL_INSTANTIATION_TYPE* >(
184  &(table->unsafeGet(table_alone_offset)));
185  GUM_MULTI_DIM_PARTIAL_INSTANTIATION_TYPE* presult
186  = const_cast< GUM_MULTI_DIM_PARTIAL_INSTANTIATION_TYPE* >(&(result->unsafeGet(0)));
187 
188  // but before doing so, check whether the instantiated variables are the
189  // last ones or not. If so, we can optimize the parsing of ptable and
190  // presult as both tables need be parsed using only 1-increments
191  if (has_before_incr) {
192  for (Idx i = 0; i < result_domain_size; ++i) {
193 # ifdef GUM_MULTI_DIM_PARTIAL_INSTANTIATION_POINTER
194  **presult = **ptable;
195 # else
196  *presult = *ptable;
197 # endif
198 
199  // update the offset of result and table
200  ++ptable;
201  ++presult;
202  }
203  } else {
204  // here, some uninstantiated variables exist after the instantiated ones
205  // in the variables sequence of table. So, we must perform a more
206  // complicated parsing of ptable
207  Idx table_offset = 0;
208 
209  for (Idx j = 0; j < result_domain_size; ++j) {
210 # ifdef GUM_MULTI_DIM_PARTIAL_INSTANTIATION_POINTER
211  **presult = *(ptable[table_offset]);
212 # else
213  *presult = ptable[table_offset];
214 # endif
215 
216  // update the offset of table
217  for (unsigned int k = 0; k < table_and_result_value.size(); ++k) {
218  --table_and_result_value[k];
219 
220  if (table_and_result_value[k]) {
221  table_offset += table_and_result_offset[k];
222  break;
223  }
224 
225  table_and_result_value[k] = table_and_result_domain[k];
226  table_offset -= table_and_result_down[k];
227  }
228 
229  // update the offset of result
230  ++presult;
231  }
232  }
233 
234  return result;
235  }
236 
237 # undef GUM_MULTI_DIM_PARTIAL_INSTANTIATION_TYPE
238 
239 # ifdef GUM_MULTI_DIM_PARTIAL_INSTANTIATION_POINTER
240 # undef GUM_MULTI_DIM_PARTIAL_INSTANTIATION_POINTER
241 # endif
242 
243 } /* End of namespace gum */
244 
245 #endif /* GUM_PARTIAL_INSTANTIATION_PATTERN_ALLOWED */