9 #ifndef OPENGM_MESSAGEPASSING_OPERATIONS_HXX
10 #define OPENGM_MESSAGEPASSING_OPERATIONS_HXX
21 namespace messagepassingOperations {
25 template<
class OP,
class M>
26 inline void clean(M& out) {
27 for(
size_t n=0; n<out.size(); ++n ) {
43 template<
class OP,
class ACC,
class M>
48 typename M::ValueType v;
50 for(
size_t n=0; n<out.size(); ++n)
53 if( opengm::meta::Compare<OP,opengm::Multiplier>::value && v <= 0.00001)
55 if(opengm::meta::Compare<OP,opengm::Multiplier>::value)
57 for(
size_t n=0; n<out.size();++n ) {
74 template<
class OP,
class M,
class T>
75 inline void weightedMean
85 const T oneMinusAlpha=
static_cast<T
>(1)-alpha;
87 for(
size_t n=0; n<out.size();++n ) {
88 OP::hop(in1(n),alpha, v1);
89 OP::hop(in2(n),oneMinusAlpha,v2);
113 template<
class OP,
class BUFFER,
class M>
116 const std::vector<BUFFER>& vec,
122 for(
size_t j = 0; j < vec.size(); ++j) {
123 const typename BUFFER::ArrayType& b = vec[j].current();
125 for(
size_t n=0; n<out.size(); ++n)
126 OP::op(b(n), out(n));
131 template<
class GM,
class BUFFER,
class M>
134 const std::vector<BUFFER>& vec,
135 const std::vector<typename GM::ValueType>& rho,
138 typedef typename GM::OperatorType OP;
143 for(
size_t j = 0; j < vec.size(); ++j) {
144 const typename BUFFER::ArrayType& b = vec[j].current();
145 typename GM::ValueType e = rho[j];
146 typename GM::ValueType v;
147 for(
size_t n=0; n<out.size(); ++n) {
155 template<
class OP,
class BUFVEC,
class M,
class INDEX>
166 for(
size_t j = 0; j < i; ++j) {
167 const M& f = vec[j].current();
168 for(
size_t n=0; n<out.size(); ++n)
169 OP::op(f(n), out(n));
171 for(
size_t j = i+1; j < vec.size(); ++j) {
172 const M& f = vec[j].current();
173 for(
size_t n=0; n<out.size(); ++n)
174 OP::op(f(n), out(n));
179 template<
class GM,
class BUFVEC,
class M,
class INDEX>
184 const std::vector<typename GM::ValueType>& rho,
187 typedef typename GM::OperatorType OP;
189 typename GM::ValueType v;
190 const typename GM::ValueType e = rho[i]-1;
191 const M& b = vec[i].current();
192 for(
size_t n=0; n<out.size(); ++n) {
195 OP::hop(b(n),e,out(n));
198 for(
size_t j = 0; j < i; ++j) {
199 const M& b = vec[j].current();
200 const typename GM::ValueType e = rho[j];
202 for(
size_t n=0; n<out.size(); ++n) {
207 for(
size_t j = i+1; j < vec.size(); ++j) {
208 const M& b = vec[j].current();
209 const typename GM::ValueType e = rho[j];
211 for(
size_t n=0; n<out.size(); ++n) {
220 template<
class GM,
class ACC,
class BUFVEC,
class ARRAY,
class INDEX>
223 const typename GM::FactorType& f,
228 typedef typename GM::OperatorType OP;
229 if(f.numberOfVariables()==2) {
231 typename GM::ValueType v;
232 for(
size_t n=0; n<out.size(); ++n)
233 ACC::neutral(out(n));
234 for(count[0]=0;count[0]<f.numberOfLabels(0);++count[0]) {
235 for(count[1]=0;count[1]<f.numberOfLabels(1);++count[1]) {
238 OP::op(vec[1].current()(count[1]), v);
240 OP::op(vec[0].current()(count[0]), v);
241 ACC::op(v,out(count[i]));
247 typedef typename GM::IndexType IndexType;
248 typedef typename GM::LabelType
LabelType;
250 for(
size_t n=0; n<f.numberOfLabels(i); ++n)
251 ACC::neutral(out(n));
253 typedef typename GM::FactorType::ShapeIteratorType FactorShapeIteratorType;
254 opengm::ShapeWalker<FactorShapeIteratorType> shapeWalker(f.shapeBegin(),f.numberOfVariables());
255 for(IndexType scalarIndex=0;scalarIndex<f.size();++scalarIndex,++shapeWalker) {
259 typename GM::ValueType value=f(shapeWalker.coordinateTuple().begin());
260 for(IndexType j=0;j<static_cast<typename GM::IndexType>(i);++j) {
261 const LabelType label=
static_cast<LabelType
>(shapeWalker.coordinateTuple()[j]);
262 OP::op(vec[j].current()(label),value);
265 for(IndexType j=i+1;j< vec.size();++j) {
266 const LabelType label=
static_cast<LabelType
>(shapeWalker.coordinateTuple()[j]);
267 OP::op(vec[j].current()(label),value);
270 ACC::op(value,out(shapeWalker.coordinateTuple()[i]));
276 template<
class GM,
class ACC,
class BUFVEC,
class M,
class INDEX>
277 inline void operateWF
279 const typename GM::FactorType& f,
280 const typename GM::ValueType rho,
285 typedef typename GM::IndexType IndexType;
286 typedef typename GM::LabelType
LabelType;
287 typedef typename GM::OperatorType OP;
289 for(
size_t n=0; n<f.numberOfLabels(i); ++n)
290 ACC::neutral(out(n));
292 typedef typename GM::FactorType::ShapeIteratorType FactorShapeIteratorType;
293 opengm::ShapeWalker<FactorShapeIteratorType> shapeWalker(f.shapeBegin(),f.numberOfVariables());
294 for(IndexType scalarIndex=0;scalarIndex<f.size();++scalarIndex,++shapeWalker) {
298 typename GM::ValueType value;
299 OP::ihop(f(shapeWalker.coordinateTuple().begin()),rho,value);
300 for(IndexType j=0;j<static_cast<typename GM::IndexType>(i);++j) {
301 const LabelType label=
static_cast<LabelType
>(shapeWalker.coordinateTuple()[j]);
302 OP::op(vec[j].current()(label),value);
305 for(IndexType j=i+1;j< vec.size();++j) {
306 const LabelType label=
static_cast<LabelType
>(shapeWalker.coordinateTuple()[j]);
307 OP::op(vec[j].current()(label),value);
310 ACC::op(value,out(shapeWalker.coordinateTuple()[i]));
341 template<
class GM,
class BUFVEC>
344 const typename GM::FactorType& f,
346 typename GM::IndependentFactorType& out
351 typedef typename GM::IndexType IndexType;
352 typedef typename GM::LabelType
LabelType;
353 typedef typename GM::OperatorType OP;
355 typedef typename GM::FactorType::ShapeIteratorType FactorShapeIteratorType;
356 opengm::ShapeWalker<FactorShapeIteratorType> shapeWalker(f.shapeBegin(),f.numberOfVariables());
357 for(IndexType scalarIndex=0;scalarIndex<f.size();++scalarIndex,++shapeWalker) {
359 typename GM::ValueType value=f(shapeWalker.coordinateTuple().begin());
360 for(IndexType j=0;j<static_cast<typename GM::IndexType>(vec.size());++j) {
361 const LabelType label=
static_cast<LabelType
>(shapeWalker.coordinateTuple()[j]);
362 OP::op(vec[j].current()(label),value);
364 out(scalarIndex)=value;
381 template<
class GM,
class BUFVEC>
382 inline void operateWF
384 const typename GM::FactorType& f,
385 const typename GM::ValueType rho,
387 typename GM::IndependentFactorType& out
389 typedef typename GM::OperatorType OP;
390 typedef typename GM::IndexType IndexType;
391 typedef typename GM::LabelType
LabelType;
392 typedef typename GM::FactorType::ShapeIteratorType FactorShapeIteratorType;
393 opengm::ShapeWalker<FactorShapeIteratorType> shapeWalker(f.shapeBegin(),f.numberOfVariables());
394 for(IndexType scalarIndex=0;scalarIndex<f.size();++scalarIndex,++shapeWalker) {
396 typename GM::ValueType value;
397 OP::ihop(f(shapeWalker.coordinateTuple().begin()),rho,value);
398 for(IndexType j=0;j<static_cast<typename GM::IndexType>(vec.size());++j) {
399 const LabelType label=
static_cast<LabelType
>(shapeWalker.coordinateTuple()[j]);
400 OP::op(vec[j].current()(label),value);
402 out(scalarIndex)=value;
#define OPENGM_ASSERT(expression)