/usr/include/opengm/inference/messagepassing/messagepassing_operations_withFunctors.hxx is in libopengm-dev 2.3.6-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | #pragma once
#ifndef OPENGM_MESSAGEPASSING_OPERATIONS_HXX
#define OPENGM_MESSAGEPASSING_OPERATIONS_HXX
#include <opengm/opengm.hxx>
#include <opengm/operations/adder.hxx>
#include <opengm/operations/multiplier.hxx>
#include <opengm/operations/minimizer.hxx>
#include <opengm/operations/maximizer.hxx>
/// \cond HIDDEN_SYMBOLS
namespace opengm {
namespace messagepassingOperations {
// out = M(M.shape, OP:neutral)
/// \todo unroll loop
template<class OP, class M>
inline void clean(M& out) {
for(size_t n=0; n<out.size(); ++n ) {
OP::neutral(out(n));
}
}
template<class OP, class ACC, class M>
inline void normalize
(
M& out
) {
typename M::ValueType v;
ACC::neutral(v);
for(size_t n=0; n<out.size(); ++n)
ACC::op(out(n),v);
if( opengm::meta::Compare<OP,opengm::Multiplier>::value && v <= 0.00001)
return;
if(opengm::meta::Compare<OP,opengm::Multiplier>::value)
OPENGM_ASSERT(v > 0.00001); // ??? this should be checked in released code
for(size_t n=0; n<out.size();++n ) {
OP::iop(v,out(n));
}
}
/// out = op( hop(in1,alpha), hop(in2,1-alpha) )
template<class OP, class M, class T>
inline void weightedMean
(
const M& in1,
const M& in2,
const T alpha,
M& out
) {
/// TODO
/// Speedup
T v1,v2;
const T oneMinusAlpha=static_cast<T>(1)-alpha;
for(size_t n=0; n<out.size();++n ) {
OP::hop(in1(n),alpha, v1);
OP::hop(in2(n),oneMinusAlpha,v2);
OP::op(v1,v2,out(n));
}
}
/// out = op(vec[0].current, ..., vec[n].current )
template<class OP, class BUFFER, class M>
inline void operate
(
const std::vector<BUFFER>& vec,
M& out
) {
/// ???
/// switch order of loops ?
clean<OP>(out);
for(size_t j = 0; j < vec.size(); ++j) {
const typename BUFFER::ArrayType& b = vec[j].current();
OPENGM_ASSERT(b.size()==out.size());
for(size_t n=0; n<out.size(); ++n)
OP::op(b(n), out(n));
}
}
/// out = op( out , op_i( hop(vec[i],rho[i]) ) )
template<class GM, class BUFFER, class M>
inline void operateW
(
const std::vector<BUFFER>& vec,
const std::vector<typename GM::ValueType>& rho,
M& out
) {
typedef typename GM::OperatorType OP;
clean<OP>(out);
/// ???
/// switch order of loops ?
/// => loop over out?
for(size_t j = 0; j < vec.size(); ++j) {
const typename BUFFER::ArrayType& b = vec[j].current();
typename GM::ValueType e = rho[j];
typename GM::ValueType v;
for(size_t n=0; n<out.size(); ++n) {
OP::hop(b(n),e,v);
OP::op(v,out(n));
}
}
}
/// out = op( vec[0].current, ..., vec[i-1].current,vec[i+1].current, ... , vec[n].current )
template<class OP, class BUFVEC, class M, class INDEX>
inline void operate
(
const BUFVEC& vec,
const INDEX i,
M& out
) {
clean<OP>(out);
/// TODO
/// switch order of loops ? (loop over out?)
/// => clean could be inside the loop over the result
for(size_t j = 0; j < i; ++j) {
const M& f = vec[j].current();
for(size_t n=0; n<out.size(); ++n)
OP::op(f(n), out(n));
}
for(size_t j = i+1; j < vec.size(); ++j) {
const M& f = vec[j].current();
for(size_t n=0; n<out.size(); ++n)
OP::op(f(n), out(n));
}
}
/// out = op( hop(vec[i],rho[i]-1), op_j/i( hop(vec[j],rho[j]) ) )
template<class GM, class BUFVEC, class M, class INDEX>
inline void operateW
(
const BUFVEC& vec,
const INDEX i,
const std::vector<typename GM::ValueType>& rho,
M& out
) {
typedef typename GM::OperatorType OP;
OPENGM_ASSERT(vec[i].current().size()==out.size());
typename GM::ValueType v;
const typename GM::ValueType e = rho[i]-1;
const M& b = vec[i].current();
for(size_t n=0; n<out.size(); ++n) {
//OP::hop(b(n),e,v);
//OP::op(v,out(n));
OP::hop(b(n),e,out(n));
}
for(size_t j = 0; j < i; ++j) {
const M& b = vec[j].current();
const typename GM::ValueType e = rho[j];
OPENGM_ASSERT(b.size()==out.size());
for(size_t n=0; n<out.size(); ++n) {
OP::hop(b(n),e,v);
OP::op(v,out(n));
}
}
for(size_t j = i+1; j < vec.size(); ++j) {
const M& b = vec[j].current();
const typename GM::ValueType e = rho[j];
OPENGM_ASSERT(b.size()==out.size());
for(size_t n=0; n<out.size(); ++n) {
OP::hop(b(n),e,v);
OP::op(v,out(n));
}
}
}
/// out = acc( op(f, vec[0].current, ..., vec[n].current ), -i)
template<class GM, class ACC, class BUFVEC, class ARRAY ,class INDEX>
struct OperateF_Functor{
OperateF_Functor(
const BUFVEC & vec,
const INDEX i,
ARRAY & out
)
: vec_(vec),
i_(i),
out_(out){
}
template<class FUNCTION>
void operator()(const FUNCTION & f){
typedef typename GM::OperatorType OP;
if(f.dimension()==2) {
size_t count[2];
typename GM::ValueType v;
for(size_t n=0; n<out_.size(); ++n)
ACC::neutral(out_(n));
if(i_==0){
for(count[0]=0;count[0]<f.shape(0);++count[0]){
for(count[1]=0;count[1]<f.shape(1);++count[1]) {
v = f(count);
OP::op(vec_[1].current()(count[1]), v);
ACC::op(v,out_(count[0]));
}
}
}else{
for(count[0]=0;count[0]<f.shape(0);++count[0]){
for(count[1]=0;count[1]<f.shape(1);++count[1]) {
v = f(count);
OP::op(vec_[0].current()(count[0]), v);
ACC::op(v,out_(count[1]));
}
}
}
}
else{
// accumulation over all variables except x
typedef typename GM::IndexType IndexType;
typedef typename GM::LabelType LabelType;
// neutral initialization of output
for(size_t n=0; n<f.shape(i_); ++n)
ACC::neutral(out_(n));
// factor shape iterator
typedef typename FUNCTION::FunctionShapeIteratorType FunctionShapeIteratorType;
opengm::ShapeWalker<FunctionShapeIteratorType> shapeWalker(f.functionShapeBegin(),f.dimension());
for(IndexType scalarIndex=0;scalarIndex<f.size();++scalarIndex,++shapeWalker) {
// loop over the variables
// initialize output value with value of the factor at this coordinate
// operate j=[0,..i-1]
typename GM::ValueType value=f(shapeWalker.coordinateTuple().begin());
for(IndexType j=0;j<static_cast<typename GM::IndexType>(i_);++j) {
const LabelType label=static_cast<LabelType>(shapeWalker.coordinateTuple()[j]);
OP::op(vec_[j].current()(label),value);
}
// operate j=[i+1,..,vec.size()]
for(IndexType j=i_+1;j< vec_.size();++j) {
const LabelType label=static_cast<LabelType>(shapeWalker.coordinateTuple()[j]);
OP::op(vec_[j].current()(label),value);
}
// accumulate
ACC::op(value,out_(shapeWalker.coordinateTuple()[i_]));
}
}
}
const BUFVEC & vec_;
const INDEX i_;
ARRAY & out_;
};
template<class GM, class ACC, class BUFVEC, class ARRAY, class INDEX>
inline void operateF
(
const typename GM::FactorType& f,
const BUFVEC& vec,
const INDEX i,
ARRAY& out
) {
OperateF_Functor<GM,ACC,BUFVEC,ARRAY,INDEX> functor(vec,i,out);
f.callFunctor(functor);
}
/// out = acc_-i( op( ihop(f,rho), op_j/i( vec[j] ) ) )
template<class GM, class ACC, class BUFVEC, class M ,class INDEX>
struct OperateWF_Functor{
typedef typename GM::IndexType IndexType;
typedef typename GM::LabelType LabelType;
typedef typename GM::ValueType ValueType;
typedef typename GM::OperatorType OP;
OperateWF_Functor(const ValueType rho, const BUFVEC & vec, const INDEX i,M & out)
: rho_(rho), vec_(vec), i_(i), out_(out){}
template<class FUNCTION>
void operator()(const FUNCTION & f){
// neutral initialization of output
for(size_t n=0; n<f.shape(i_); ++n)
ACC::neutral(out_(n));
// factor shape iterator
typedef typename FUNCTION::FunctionShapeIteratorType FunctionShapeIteratorType;
opengm::ShapeWalker<FunctionShapeIteratorType> shapeWalker(f.functionShapeBegin(),f.dimension());
for(IndexType scalarIndex=0;scalarIndex<f.size();++scalarIndex,++shapeWalker) {
// loop over the variables
// initialize output value with value of the factor at this coordinate
// operate j=[0,..i-1]
ValueType value;
OP::ihop(f(shapeWalker.coordinateTuple().begin()),rho_,value);
for(IndexType j=0;j<static_cast<typename GM::IndexType>(i_);++j) {
const LabelType label=static_cast<LabelType>(shapeWalker.coordinateTuple()[j]);
OP::op(vec_[j].current()(label),value);
}
// operate j=[i+1,..,vec.size()]
for(IndexType j=i_+1;j< vec_.size();++j) {
const LabelType label=static_cast<LabelType>(shapeWalker.coordinateTuple()[j]);
OP::op(vec_[j].current()(label),value);
}
// accumulate
ACC::op(value,out_(shapeWalker.coordinateTuple()[i_]));
}
}
const ValueType rho_;
const BUFVEC & vec_;
const INDEX i_;
M & out_;
};
template<class GM, class ACC, class BUFVEC, class M, class INDEX>
inline void operateWF
(
const typename GM::FactorType& f,
const typename GM::ValueType rho,
const BUFVEC& vec,
const INDEX i,
M& out
) {
OperateWF_Functor<GM,ACC,BUFVEC,M,INDEX> functor(rho,vec,i,out);
f.callFunctor(functor);
}
/// out = op(f, vec[0].current, ..., vec[n].current )
template<class GM, class BUFVEC>
struct OperatorF2_Functor{
typedef typename GM::IndexType IndexType;
typedef typename GM::LabelType LabelType;
typedef typename GM::ValueType ValueType;
typedef typename GM::OperatorType OP;
OperatorF2_Functor(const BUFVEC& vec, typename GM::IndependentFactorType& out):vec_(vec), out_(out){}
template<class FUNCTION>
void operator()(const FUNCTION & f){
OPENGM_ASSERT(out_.numberOfVariables()!=0);
// shape iterator
typedef typename FUNCTION::FunctionShapeIteratorType FunctionShapeIteratorType;
opengm::ShapeWalker<FunctionShapeIteratorType> shapeWalker(f.functionShapeBegin(),f.dimension());
for(IndexType scalarIndex=0;scalarIndex<f.size();++scalarIndex,++shapeWalker) {
// loop over the variables
ValueType value=f(shapeWalker.coordinateTuple().begin());
for(IndexType j=0;j<static_cast<typename GM::IndexType>(vec_.size());++j) {
const LabelType label=static_cast<LabelType>(shapeWalker.coordinateTuple()[j]);
OP::op(vec_[j].current()(label),value);
}
out_(scalarIndex)=value;
}
}
const BUFVEC& vec_;
typename GM::IndependentFactorType& out_;
};
template<class GM, class BUFVEC>
inline void operateF
(
const typename GM::FactorType& f,
const BUFVEC& vec,
typename GM::IndependentFactorType& out
)
{
OperatorF2_Functor<GM, BUFVEC> functor(vec,out);
f.callFunctor(functor);
}
/// out = op( ihop(f,rho), op_j(vec[j]) )
template<class GM, class BUFVEC>
struct OperatorWF2_Functor{
typedef typename GM::IndexType IndexType;
typedef typename GM::LabelType LabelType;
typedef typename GM::ValueType ValueType;
typedef typename GM::OperatorType OP;
OperatorWF2_Functor(ValueType rho, const BUFVEC& vec, typename GM::IndependentFactorType& out) : rho_(rho), vec_(vec), out_(out){}
template<class FUNCTION>
void operator()(const FUNCTION & f){
// shape iterator
typedef typename FUNCTION::FunctionShapeIteratorType FunctionShapeIteratorType;
opengm::ShapeWalker<FunctionShapeIteratorType> shapeWalker(f.functionShapeBegin(),f.dimension());
for(IndexType scalarIndex=0;scalarIndex<f.size();++scalarIndex,++shapeWalker) { // loop over the variables
ValueType value;
OP::ihop(f(shapeWalker.coordinateTuple().begin()),rho_,value);
for(IndexType j=0;j<static_cast<typename GM::IndexType>(vec_.size());++j) {
const LabelType label=static_cast<LabelType>(shapeWalker.coordinateTuple()[j]);
OP::op(vec_[j].current()(label),value);
}
out_(scalarIndex)=value;
}
}
const ValueType rho_;
const BUFVEC& vec_;
typename GM::IndependentFactorType& out_;
};
template<class GM, class BUFVEC>
inline void operateWF
(
const typename GM::FactorType& f,
const typename GM::ValueType rho,
const BUFVEC& vec,
typename GM::IndependentFactorType& out
) {
OperatorWF2_Functor<GM, BUFVEC> functor(rho,vec,out);
f.callFunctor(functor);
}
} // namespace messagepassingOperations
} // namespace opengm
/// \endcond
#endif
|