OpenGM  2.3.x
Discrete Graphical Model Library
scaled_view.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_SCALEDVIEWFUNCTION_HXX
3 #define OPENGM_SCALEDVIEWFUNCTION_HXX
4 
6 
7 namespace opengm {
8 
12 template<class GM> class ScaledViewFunction
13 : public FunctionBase<ScaledViewFunction<GM>,
14  typename GM::ValueType,
15  typename GM::IndexType,
16  typename GM::LabelType>
17 {
18 public:
19  typedef typename GM::ValueType ValueType;
20  typedef typename GM::IndexType IndexType;
21  typedef typename GM::LabelType LabelType;
22  typedef typename GM::OperatorType OperatorType;
23 
24  ScaledViewFunction(const std::vector<IndexType>&);
25  ScaledViewFunction(const GM&, const IndexType, const ValueType);
26  template<class Iterator> ValueType operator()(Iterator begin) const;
27  size_t dimension() const;
28  size_t shape(const size_t) const;
29  size_t size() const;
30 
31 private:
32  GM const* gm_;
33  IndexType factorIndex_;
34  ValueType scale_;
35  std::vector<IndexType> shape_;
36  size_t size_;
37 };
38 
43 template<class GM>
44 inline
46 (
47  const GM& gm,
48  const typename ScaledViewFunction<GM>::IndexType factorIndex,
49  const ValueType scale
50 )
51 : gm_(&gm),
52  factorIndex_(factorIndex),
53  scale_(scale)
54 {
55  size_=1;
56  shape_.resize(gm[factorIndex].numberOfVariables());
57  for(size_t i=0; i<gm[factorIndex].numberOfVariables();++i) {
58  shape_[i] = gm[factorIndex].numberOfLabels(i);
59  size_*=gm[factorIndex].numberOfLabels(i);
60  }
61 }
64 template<class GM>
65 inline
67 (
68  const std::vector<IndexType>& shape
69 )
70 : gm_(NULL),
71  factorIndex_(0),
72  scale_(0),
73  shape_(shape)
74 {
75  size_=1;
76  for(size_t i=0; i<shape_.size();++i) {
77  size_*=shape[i];
78  }
79 }
80 
81 template<class GM>
82 inline size_t
84 {
85  return size_;
86 }
87 
88 template<class GM>
89 template<class Iterator>
92 (
93  Iterator begin
94 ) const
95 {
96  if(gm_==NULL) {
97  return OperatorType::template neutral<ValueType>();
98  }
99  else {
100  return scale_*gm_->operator[](factorIndex_)(begin);
101  }
102 }
103 
104 template<class GM>
105 inline size_t
107  const size_t i
108 ) const {
109  return shape_[i];
110 }
111 
112 template<class GM>
113 inline size_t
115  return shape_.size();
116 }
117 
118 } // namespace opengm
119 
120 #endif // #ifndef OPENGM_SCALEDVIEWFUNCTION_HXX
The OpenGM namespace.
Definition: config.hxx:43
Fallback implementation of member functions of OpenGM functions.
Function that scales a factor of another graphical model.
Definition: scaled_view.hxx:12
GM::OperatorType OperatorType
Definition: scaled_view.hxx:22
ValueType operator()(Iterator begin) const
Definition: scaled_view.hxx:92
ScaledViewFunction(const std::vector< IndexType > &)
Constructor.
Definition: scaled_view.hxx:67
size_t shape(const size_t) const