OpenGM  2.3.x
Discrete Graphical Model Library
view_convert_function.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_VIEW_CONVERT_FUNCTION_HXX
3 #define OPENGM_VIEW_CONVERT_FUNCTION_HXX
4 
6 
7 namespace opengm {
8 
10 namespace detail_convert_function {
11  template<class OPERATOR, class ACCUMULATOR, class PROBABILITY>
12  struct ValueToProbability;
13 
14  template<class PROBABILITY>
15  struct ValueToProbability<Multiplier, Maximizer, PROBABILITY>
16  {
17  typedef PROBABILITY ProbabilityType;
18  template<class T>
19  static ProbabilityType convert(const T x)
20  { return static_cast<ProbabilityType>(x); }
21  template<class T>
22  static ProbabilityType convert(const T x, const T invT)
23  { return static_cast<ProbabilityType>(x); }
24  };
25 
26  template<class PROBABILITY>
27  struct ValueToProbability<Multiplier, Minimizer, PROBABILITY>
28  {
29  typedef PROBABILITY ProbabilityType;
30  template<class T>
31  static ProbabilityType convert(const T x)
32  { return static_cast<ProbabilityType>(1) / static_cast<ProbabilityType>(x); } // is this correct ?!?
33  template<class T>
34  static ProbabilityType convert(const T x, const T invT)
35  { return static_cast<ProbabilityType>(1) / static_cast<ProbabilityType>(x); } // is this correct ?!?
36  };
37 
38  template<class PROBABILITY>
39  struct ValueToProbability<Adder, Maximizer, PROBABILITY>
40  {
41  typedef PROBABILITY ProbabilityType;
42  template<class T>
43  static ProbabilityType convert(const T x)
44  { return static_cast<ProbabilityType>(std::exp(x)); }
45  template<class T>
46  static ProbabilityType convert(const T x, const T invT)
47  { return static_cast<ProbabilityType>(std::exp(invT * x)); }
48  };
49 
50  template<class PROBABILITY>
51  struct ValueToProbability<Adder, Minimizer, PROBABILITY>
52  {
53  typedef PROBABILITY ProbabilityType;
54  template<class T>
55  static ProbabilityType convert(const T x)
56  { return static_cast<ProbabilityType>(std::exp(-x)); }
57  template<class T>
58  static ProbabilityType convert(const T x, const T invT)
59  { return static_cast<ProbabilityType>(std::exp(-invT * x)); }
60  };
61 }
63 
67 template<class GM,class ACC,class VALUE_TYPE>
69 : public FunctionBase<ViewConvertFunction<GM,ACC,VALUE_TYPE>,
70  typename GM::ValueType, typename GM::IndexType, typename GM::LabelType>
71 {
72 public:
73  typedef VALUE_TYPE ValueType;
74  typedef VALUE_TYPE value_type;
75  typedef typename GM::FactorType FactorType;
76  typedef typename GM::OperatorType OperatorType;
77  typedef typename GM::IndexType IndexType;
78  typedef typename GM::LabelType LabelType;
79 
81  ViewConvertFunction(const FactorType &);
82  ViewConvertFunction(const FactorType &, const ValueType);
83  template<class Iterator> ValueType operator()(Iterator begin) const;
84  IndexType shape(const IndexType) const;
85  IndexType dimension() const;
86  IndexType size() const;
87 
88 private:
89  FactorType const* factor_;
90  ValueType inverseTemperature_;
91 };
92 
93 template<class GM,class ACC,class VALUE_TYPE>
94 inline
96  : factor_(NULL),inverseTemperature_(1)
97 {}
98 
99 template<class GM,class ACC,class VALUE_TYPE>
100 inline
102 (
104 )
105 : factor_(&factor),inverseTemperature_(1)
106 {}
107 
108 template<class GM,class ACC,class VALUE_TYPE>
109 inline
111 (
113  const VALUE_TYPE invT
114 )
115 : factor_(&factor),inverseTemperature_(invT)
116 {}
117 
118 template<class GM,class ACC,class VALUE_TYPE>
119 template<class Iterator>
122 (
123  Iterator begin
124 ) const {
125  return detail_convert_function::ValueToProbability<OperatorType,ACC,ValueType>::convert(factor_->operator()(begin),inverseTemperature_);
126 }
127 
128 template<class GM,class ACC,class VALUE_TYPE>
131 (
133 ) const{
134  return factor_->numberOfLabels(index);
135 }
136 
137 template<class GM,class ACC,class VALUE_TYPE>
140  return factor_->numberOfVariables();
141 }
142 
143 template<class GM,class ACC,class VALUE_TYPE>
146  return factor_->size( );
147 }
148 
149 } // namespace opengm
150 
151 #endif // #ifndef OPENGM_VIEW_CONVERT_FUNCTION_HXX
The OpenGM namespace.
Definition: config.hxx:43
IndexType shape(const IndexType) const
Fallback implementation of member functions of OpenGM functions.
ViewConvertFunction convert semi-ring in a lazy fashion.