2 #ifndef OPENGM_MESSAGEPASSING_OPERATIONS_HXX
3 #define OPENGM_MESSAGEPASSING_OPERATIONS_HXX
14 namespace messagepassingOperations {
18 template<
class OP,
class M>
19 inline void clean(M& out) {
20 for(
size_t n=0; n<out.size(); ++n ) {
25 template<
class OP,
class ACC,
class M>
30 typename M::ValueType v;
32 for(
size_t n=0; n<out.size(); ++n)
35 if( opengm::meta::Compare<OP,opengm::Multiplier>::value && v <= 0.00001)
37 if(opengm::meta::Compare<OP,opengm::Multiplier>::value)
39 for(
size_t n=0; n<out.size();++n ) {
45 template<
class OP,
class M,
class T>
46 inline void weightedMean
56 const T oneMinusAlpha=
static_cast<T
>(1)-alpha;
58 for(
size_t n=0; n<out.size();++n ) {
59 OP::hop(in1(n),alpha, v1);
60 OP::hop(in2(n),oneMinusAlpha,v2);
66 template<
class OP,
class BUFFER,
class M>
69 const std::vector<BUFFER>& vec,
75 for(
size_t j = 0; j < vec.size(); ++j) {
76 const typename BUFFER::ArrayType& b = vec[j].current();
78 for(
size_t n=0; n<out.size(); ++n)
84 template<
class GM,
class BUFFER,
class M>
87 const std::vector<BUFFER>& vec,
88 const std::vector<typename GM::ValueType>& rho,
91 typedef typename GM::OperatorType OP;
96 for(
size_t j = 0; j < vec.size(); ++j) {
97 const typename BUFFER::ArrayType& b = vec[j].current();
98 typename GM::ValueType e = rho[j];
99 typename GM::ValueType v;
100 for(
size_t n=0; n<out.size(); ++n) {
108 template<
class OP,
class BUFVEC,
class M,
class INDEX>
119 for(
size_t j = 0; j < i; ++j) {
120 const M& f = vec[j].current();
121 for(
size_t n=0; n<out.size(); ++n)
122 OP::op(f(n), out(n));
124 for(
size_t j = i+1; j < vec.size(); ++j) {
125 const M& f = vec[j].current();
126 for(
size_t n=0; n<out.size(); ++n)
127 OP::op(f(n), out(n));
132 template<
class GM,
class BUFVEC,
class M,
class INDEX>
137 const std::vector<typename GM::ValueType>& rho,
140 typedef typename GM::OperatorType OP;
142 typename GM::ValueType v;
143 const typename GM::ValueType e = rho[i]-1;
144 const M& b = vec[i].current();
145 for(
size_t n=0; n<out.size(); ++n) {
148 OP::hop(b(n),e,out(n));
151 for(
size_t j = 0; j < i; ++j) {
152 const M& b = vec[j].current();
153 const typename GM::ValueType e = rho[j];
155 for(
size_t n=0; n<out.size(); ++n) {
160 for(
size_t j = i+1; j < vec.size(); ++j) {
161 const M& b = vec[j].current();
162 const typename GM::ValueType e = rho[j];
164 for(
size_t n=0; n<out.size(); ++n) {
173 template<
class GM,
class ACC,
class BUFVEC,
class ARRAY ,
class INDEX>
174 struct OperateF_Functor{
185 template<
class FUNCTION>
186 void operator()(
const FUNCTION & f){
187 typedef typename GM::OperatorType OP;
188 if(f.dimension()==2) {
190 typename GM::ValueType v;
191 for(
size_t n=0; n<out_.size(); ++n)
192 ACC::neutral(out_(n));
194 for(count[0]=0;count[0]<f.shape(0);++count[0]){
195 for(count[1]=0;count[1]<f.shape(1);++count[1]) {
197 OP::op(vec_[1].current()(count[1]), v);
198 ACC::op(v,out_(count[0]));
202 for(count[0]=0;count[0]<f.shape(0);++count[0]){
203 for(count[1]=0;count[1]<f.shape(1);++count[1]) {
205 OP::op(vec_[0].current()(count[0]), v);
206 ACC::op(v,out_(count[1]));
213 typedef typename GM::IndexType IndexType;
214 typedef typename GM::LabelType
LabelType;
216 for(
size_t n=0; n<f.shape(i_); ++n)
217 ACC::neutral(out_(n));
219 typedef typename FUNCTION::FunctionShapeIteratorType FunctionShapeIteratorType;
220 opengm::ShapeWalker<FunctionShapeIteratorType> shapeWalker(f.functionShapeBegin(),f.dimension());
221 for(IndexType scalarIndex=0;scalarIndex<f.size();++scalarIndex,++shapeWalker) {
225 typename GM::ValueType value=f(shapeWalker.coordinateTuple().begin());
226 for(IndexType j=0;j<static_cast<typename GM::IndexType>(i_);++j) {
227 const LabelType label=
static_cast<LabelType
>(shapeWalker.coordinateTuple()[j]);
228 OP::op(vec_[j].current()(label),value);
231 for(IndexType j=i_+1;j< vec_.size();++j) {
232 const LabelType label=
static_cast<LabelType
>(shapeWalker.coordinateTuple()[j]);
233 OP::op(vec_[j].current()(label),value);
236 ACC::op(value,out_(shapeWalker.coordinateTuple()[i_]));
247 template<
class GM,
class ACC,
class BUFVEC,
class ARRAY,
class INDEX>
250 const typename GM::FactorType& f,
255 OperateF_Functor<GM,ACC,BUFVEC,ARRAY,INDEX> functor(vec,i,out);
256 f.callFunctor(functor);
261 template<
class GM,
class ACC,
class BUFVEC,
class M ,
class INDEX>
262 struct OperateWF_Functor{
263 typedef typename GM::IndexType IndexType;
264 typedef typename GM::LabelType
LabelType;
265 typedef typename GM::ValueType ValueType;
266 typedef typename GM::OperatorType OP;
268 OperateWF_Functor(
const ValueType rho,
const BUFVEC & vec,
const INDEX i,M & out)
269 : rho_(rho), vec_(vec), i_(i), out_(out){}
271 template<
class FUNCTION>
272 void operator()(
const FUNCTION & f){
274 for(
size_t n=0; n<f.shape(i_); ++n)
275 ACC::neutral(out_(n));
277 typedef typename FUNCTION::FunctionShapeIteratorType FunctionShapeIteratorType;
278 opengm::ShapeWalker<FunctionShapeIteratorType> shapeWalker(f.functionShapeBegin(),f.dimension());
279 for(IndexType scalarIndex=0;scalarIndex<f.size();++scalarIndex,++shapeWalker) {
284 OP::ihop(f(shapeWalker.coordinateTuple().begin()),rho_,value);
285 for(IndexType j=0;j<static_cast<typename GM::IndexType>(i_);++j) {
286 const LabelType label=
static_cast<LabelType
>(shapeWalker.coordinateTuple()[j]);
287 OP::op(vec_[j].current()(label),value);
290 for(IndexType j=i_+1;j< vec_.size();++j) {
291 const LabelType label=
static_cast<LabelType
>(shapeWalker.coordinateTuple()[j]);
292 OP::op(vec_[j].current()(label),value);
295 ACC::op(value,out_(shapeWalker.coordinateTuple()[i_]));
299 const ValueType rho_;
305 template<
class GM,
class ACC,
class BUFVEC,
class M,
class INDEX>
306 inline void operateWF
308 const typename GM::FactorType& f,
309 const typename GM::ValueType rho,
314 OperateWF_Functor<GM,ACC,BUFVEC,M,INDEX> functor(rho,vec,i,out);
315 f.callFunctor(functor);
320 template<
class GM,
class BUFVEC>
321 struct OperatorF2_Functor{
322 typedef typename GM::IndexType IndexType;
323 typedef typename GM::LabelType
LabelType;
324 typedef typename GM::ValueType ValueType;
325 typedef typename GM::OperatorType OP;
326 OperatorF2_Functor(
const BUFVEC& vec,
typename GM::IndependentFactorType& out):vec_(vec), out_(out){}
328 template<
class FUNCTION>
329 void operator()(
const FUNCTION & f){
332 typedef typename FUNCTION::FunctionShapeIteratorType FunctionShapeIteratorType;
333 opengm::ShapeWalker<FunctionShapeIteratorType> shapeWalker(f.functionShapeBegin(),f.dimension());
334 for(IndexType scalarIndex=0;scalarIndex<f.size();++scalarIndex,++shapeWalker) {
336 ValueType value=f(shapeWalker.coordinateTuple().begin());
337 for(IndexType j=0;j<static_cast<typename GM::IndexType>(vec_.size());++j) {
338 const LabelType label=
static_cast<LabelType
>(shapeWalker.coordinateTuple()[j]);
339 OP::op(vec_[j].current()(label),value);
341 out_(scalarIndex)=value;
346 typename GM::IndependentFactorType& out_;
348 template<
class GM,
class BUFVEC>
351 const typename GM::FactorType& f,
353 typename GM::IndependentFactorType& out
356 OperatorF2_Functor<GM, BUFVEC> functor(vec,out);
357 f.callFunctor(functor);
361 template<
class GM,
class BUFVEC>
362 struct OperatorWF2_Functor{
363 typedef typename GM::IndexType IndexType;
364 typedef typename GM::LabelType
LabelType;
365 typedef typename GM::ValueType ValueType;
366 typedef typename GM::OperatorType OP;
367 OperatorWF2_Functor(ValueType rho,
const BUFVEC& vec,
typename GM::IndependentFactorType& out) : rho_(rho), vec_(vec), out_(out){}
369 template<
class FUNCTION>
370 void operator()(
const FUNCTION & f){
372 typedef typename FUNCTION::FunctionShapeIteratorType FunctionShapeIteratorType;
373 opengm::ShapeWalker<FunctionShapeIteratorType> shapeWalker(f.functionShapeBegin(),f.dimension());
374 for(IndexType scalarIndex=0;scalarIndex<f.size();++scalarIndex,++shapeWalker) {
376 OP::ihop(f(shapeWalker.coordinateTuple().begin()),rho_,value);
377 for(IndexType j=0;j<static_cast<typename GM::IndexType>(vec_.size());++j) {
378 const LabelType label=
static_cast<LabelType
>(shapeWalker.coordinateTuple()[j]);
379 OP::op(vec_[j].current()(label),value);
381 out_(scalarIndex)=value;
385 const ValueType rho_;
387 typename GM::IndependentFactorType& out_;
390 template<
class GM,
class BUFVEC>
391 inline void operateWF
393 const typename GM::FactorType& f,
394 const typename GM::ValueType rho,
396 typename GM::IndependentFactorType& out
398 OperatorWF2_Functor<GM, BUFVEC> functor(rho,vec,out);
399 f.callFunctor(functor);
#define OPENGM_ASSERT(expression)