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