00001 #pragma once
00002 #ifndef OPENGM_VIEW_CONVERT_FUNCTION_HXX
00003 #define OPENGM_VIEW_CONVERT_FUNCTION_HXX
00004
00005 #include "opengm/functions/function_properties_base.hxx"
00006
00007 namespace opengm {
00008
00010 namespace detail_convert_function {
00011 template<class OPERATOR, class ACCUMULATOR, class PROBABILITY>
00012 struct ValueToProbability;
00013
00014 template<class PROBABILITY>
00015 struct ValueToProbability<Multiplier, Maximizer, PROBABILITY>
00016 {
00017 typedef PROBABILITY ProbabilityType;
00018 template<class T>
00019 static ProbabilityType convert(const T x)
00020 { return static_cast<ProbabilityType>(x); }
00021 };
00022
00023 template<class PROBABILITY>
00024 struct ValueToProbability<Multiplier, Minimizer, PROBABILITY>
00025 {
00026 typedef PROBABILITY ProbabilityType;
00027 template<class T>
00028 static ProbabilityType convert(const T x)
00029 { return static_cast<ProbabilityType>(1) / static_cast<ProbabilityType>(x); }
00030 };
00031
00032 template<class PROBABILITY>
00033 struct ValueToProbability<Adder, Maximizer, PROBABILITY>
00034 {
00035 typedef PROBABILITY ProbabilityType;
00036 template<class T>
00037 static ProbabilityType convert(const T x)
00038 { return static_cast<ProbabilityType>(std::exp(x)); }
00039 };
00040
00041 template<class PROBABILITY>
00042 struct ValueToProbability<Adder, Minimizer, PROBABILITY>
00043 {
00044 typedef PROBABILITY ProbabilityType;
00045 template<class T>
00046 static ProbabilityType convert(const T x)
00047 { return static_cast<ProbabilityType>(std::exp(-x)); }
00048 };
00049 }
00051
00055 template<class GM,class ACC,class VALUE_TYPE>
00056 class ViewConvertFunction
00057 : public FunctionBase<ViewConvertFunction<GM,ACC,VALUE_TYPE>,
00058 typename GM::ValueType, typename GM::IndexType, typename GM::LabelType>
00059 {
00060 public:
00061 typedef VALUE_TYPE ValueType;
00062 typedef VALUE_TYPE value_type;
00063 typedef typename GM::FactorType FactorType;
00064 typedef typename GM::OperatorType OperatorType;
00065 typedef typename GM::IndexType IndexType;
00066 typedef typename GM::LabelType LabelType;
00067
00068 ViewConvertFunction();
00069 ViewConvertFunction(const FactorType &);
00070 template<class Iterator> ValueType operator()(Iterator begin) const;
00071 IndexType shape(const IndexType) const;
00072 IndexType dimension() const;
00073 IndexType size() const;
00074
00075 private:
00076 FactorType const* factor_;
00077 };
00078
00079 template<class GM,class ACC,class VALUE_TYPE>
00080 inline
00081 ViewConvertFunction<GM,ACC,VALUE_TYPE>::ViewConvertFunction()
00082 : factor_(NULL)
00083 {}
00084
00085 template<class GM,class ACC,class VALUE_TYPE>
00086 inline
00087 ViewConvertFunction<GM,ACC,VALUE_TYPE>::ViewConvertFunction
00088 (
00089 const typename ViewConvertFunction<GM,ACC,VALUE_TYPE>::FactorType & factor
00090 )
00091 : factor_(&factor)
00092 {}
00093
00094 template<class GM,class ACC,class VALUE_TYPE>
00095 template<class Iterator>
00096 inline typename ViewConvertFunction<GM,ACC,VALUE_TYPE>::ValueType
00097 ViewConvertFunction<GM,ACC,VALUE_TYPE>::operator()
00098 (
00099 Iterator begin
00100 ) const {
00101 return detail_convert_function::ValueToProbability<OperatorType,ACC,ValueType>::convert(factor_->operator()(begin));
00102 }
00103
00104 template<class GM,class ACC,class VALUE_TYPE>
00105 inline typename ViewConvertFunction<GM,ACC,VALUE_TYPE>::IndexType
00106 ViewConvertFunction<GM,ACC,VALUE_TYPE>::shape
00107 (
00108 const typename ViewConvertFunction<GM,ACC,VALUE_TYPE>::IndexType index
00109 ) const{
00110 return factor_->numberOfLabels(index);
00111 }
00112
00113 template<class GM,class ACC,class VALUE_TYPE>
00114 inline typename ViewConvertFunction<GM,ACC,VALUE_TYPE>::IndexType
00115 ViewConvertFunction<GM,ACC,VALUE_TYPE>::dimension() const {
00116 return factor_->numberOfVariables();
00117 }
00118
00119 template<class GM,class ACC,class VALUE_TYPE>
00120 inline typename ViewConvertFunction<GM,ACC,VALUE_TYPE>::IndexType
00121 ViewConvertFunction<GM,ACC,VALUE_TYPE>::size() const {
00122 return factor_->size( );
00123 }
00124
00125 }
00126
00127 #endif // #ifndef OPENGM_VIEW_CONVERT_FUNCTION_HXX