OpenGM  2.3.x
Discrete Graphical Model Library
constant.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_CONSTANT_FUNCTION_HXX
3 #define OPENGM_CONSTANT_FUNCTION_HXX
4 
5 #include <cmath>
6 #include <algorithm>
7 #include <vector>
8 
9 #include "opengm/opengm.hxx"
12 
13 namespace opengm {
14 
18 template<class T, class I = size_t, class L = size_t>
20 : public FunctionBase<ConstantFunction<T, I, L>, T, I, L>
21 {
22 public:
23  typedef T ValueType;
24  typedef I IndexType;
25  typedef L LabelType;
26 
28  template<class ITERATOR>
29  ConstantFunction(ITERATOR, ITERATOR, const T);
30 
31  size_t shape(const IndexType) const;
32  size_t size() const;
33  size_t dimension() const;
34  template<class ITERATOR> ValueType operator()(ITERATOR) const;
35 
36  // specializations
37  bool isPotts() const { return true; }
38  bool isGeneralizedPotts() const { return true; }
39  ValueType min() const { return value_; }
40  ValueType max() const { return value_; }
41  ValueType sum() const { return value_ * static_cast<T>(size_); }
42  ValueType product() const {
43  // TODO: improve this. get rid of std::pow and write a custom pow functor class for OpenGM
44  const double x = static_cast<double>(value_); // possible loss of precision, e.g. if value_ is a long double
45  const int n = static_cast<int>(size_);
46  return static_cast<T>(std::pow(x, n)); // call of std::pow can otherwise be ambiguous, e.g. if x is int
47  }
48  MinMaxFunctor<ValueType> minMax() const { return MinMaxFunctor<T>(value_, value_); }
49 
50 private:
51  ValueType value_;
52  std::vector<IndexType> shape_;
53  size_t size_;
54 
55 template<class > friend class FunctionSerialization;
56 };
57 
60 template <class T, class I, class L>
61 struct FunctionRegistration< ConstantFunction<T, I, L> >{
62  enum ID {
64  };
65 };
66 
68 template <class T, class I, class L>
69 class FunctionSerialization<ConstantFunction<T, I, L> >{
70 public:
71  typedef typename ConstantFunction<T, I, L>::ValueType ValueType;
72 
73  static size_t indexSequenceSize(const ConstantFunction<T, I, L>&);
74  static size_t valueSequenceSize(const ConstantFunction<T, I, L>&);
75  template<class INDEX_OUTPUT_ITERATOR, class VALUE_OUTPUT_ITERATOR >
76  static void serialize(const ConstantFunction<T, I, L> &, INDEX_OUTPUT_ITERATOR, VALUE_OUTPUT_ITERATOR );
77  template<class INDEX_INPUT_ITERATOR , class VALUE_INPUT_ITERATOR>
78  static void deserialize( INDEX_INPUT_ITERATOR, VALUE_INPUT_ITERATOR, ConstantFunction<T, I, L> &);
79 };
81 
82 template <class T, class I, class L>
83 template <class ITERATOR>
84 inline
86 (
87  ITERATOR shapeBegin,
88  ITERATOR shapeEnd,
89  const T value
90 )
91 : value_(value),
92  shape_(shapeBegin, shapeEnd),
93  size_(std::accumulate(shapeBegin, shapeEnd, 1, std::multiplies<typename std::iterator_traits<ITERATOR>::value_type >()))
94 {}
95 
96 template <class T, class I, class L>
97 inline
99 : value_(0), shape_(), size_(0)
100 {}
101 
102 template <class T, class I, class L>
103 template <class ITERATOR>
106 (
107  ITERATOR begin
108 ) const {
109  return value_;
110 }
111 
115 template <class T, class I, class L>
116 inline size_t
118  const IndexType i
119 ) const {
120  OPENGM_ASSERT(i < shape_.size());
121  return shape_[i];
122 }
123 
124 // order (number of variables) of the function
125 template <class T, class I, class L>
126 inline size_t
128  return shape_.size();
129 }
130 
132 template <class T, class I, class L>
133 inline size_t
135  return size_;
136 }
137 
138 template <class T, class I, class L>
139 inline size_t
141 (
142  const ConstantFunction<T, I, L>& src
143 ) {
144  return src.dimension() + 1;
145 }
146 
147 template <class T, class I, class L>
148 inline size_t
149 FunctionSerialization<ConstantFunction<T, I, L> >::valueSequenceSize
150 (
151  const ConstantFunction<T, I, L>& src
152 ) {
153  return 1;
154 }
155 
156 template <class T, class I, class L>
157 template<class INDEX_OUTPUT_ITERATOR, class VALUE_OUTPUT_ITERATOR >
158 inline void
159 FunctionSerialization<ConstantFunction<T, I, L> >::serialize
160 (
161  const ConstantFunction<T, I, L>& src,
162  INDEX_OUTPUT_ITERATOR indexOutIterator,
163  VALUE_OUTPUT_ITERATOR valueOutIterator
164 ) {
165  *valueOutIterator = src.value_;
166  *indexOutIterator = src.dimension();
167  for(size_t i=0; i<src.dimension(); ++i) {
168  ++indexOutIterator;
169  *indexOutIterator = src.shape(i);
170  }
171 }
172 
173 template <class T, class I, class L>
174 template<class INDEX_INPUT_ITERATOR, class VALUE_INPUT_ITERATOR >
175 inline void
176 FunctionSerialization< ConstantFunction<T, I, L> >::deserialize
177 (
178  INDEX_INPUT_ITERATOR indexInIterator,
179  VALUE_INPUT_ITERATOR valueInIterator,
180  ConstantFunction<T, I, L>& dst
181 ) {
182  dst.value_ = *valueInIterator;
183  size_t dimension = *indexInIterator;
184  dst.shape_.resize(dimension);
185  for(size_t i=0; i<dimension; ++i) {
186  ++indexInIterator;
187  dst.shape_[i]=*indexInIterator;
188  }
189  dst.size_=std::accumulate(dst.shape_.begin(), dst.shape_.end(), 1, std::multiplies<size_t>());
190 }
191 
192 } // namespace opengm
193 
194 #endif // OPENGM_CONSTANT_FUNCTION_HXX
Constant function.
Definition: constant.hxx:19
The OpenGM namespace.
Definition: config.hxx:43
Fallback implementation of member functions of OpenGM functions.
ValueType product() const
Definition: constant.hxx:42
size_t size() const
number of entries a value table encoding this function would have (used for I/O)
Definition: constant.hxx:134
size_t shape(const IndexType) const
extension a value table encoding this function would have
Definition: constant.hxx:117
ValueType operator()(ITERATOR) const
Definition: constant.hxx:106
size_t dimension() const
Definition: constant.hxx:127
ValueType max() const
Definition: constant.hxx:40
MinMaxFunctor< ValueType > minMax() const
Definition: constant.hxx:48
#define OPENGM_ASSERT(expression)
Definition: opengm.hxx:77
bool isGeneralizedPotts() const
Definition: constant.hxx:38
bool isPotts() const
Definition: constant.hxx:37
const size_t FUNCTION_TYPE_ID_OFFSET
User-defined function have ids smaller than FUNCTION_TYPE_ID_OFFSET.
ValueType min() const
Definition: constant.hxx:39
ValueType sum() const
Definition: constant.hxx:41