OpenGM  2.3.x
Discrete Graphical Model Library
logsumexp.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_OPERATIONS_LOGSUMEXP_HXX
3 #define OPENGM_OPERATIONS_LOGSUMEXP_HXX
4 
5 #include "adder.hxx"
6 #include <cmath>
7 
8 namespace opengm {
9 
13 struct Logsumexp
14 {
16  template<class T>
17  static T neutral()
18  { return -std::numeric_limits<T>::infinity(); }
19 
21  template<class T>
22  static void neutral(T& out)
23  { out = -std::numeric_limits<T>::infinity(); }
24 
26  template<class T>
27  static T ineutral()
28  { return std::numeric_limits<T>::infinity(); }
29 
31  template<class T>
32  static void ineutral(T& out)
33  { out = std::numeric_limits<T>::infinity(); }
34 
36  template<class T1, class T2>
37  static void op(const T1& in1, T2& out)
38 // { out = log(exp(out) + exp(in1)); }
39  {
40  T2 theMax = std::max(in1, out);
41  T2 theMin = std::min(in1, out);
42  out = theMax + log(1 + exp(theMin - theMax)); // numerically better
43  }
44 
46  template<class T1,class T2,class T3>
47  static void op(const T1 in1, const T2 in2, T3& out)
48 // { out = log(exp(in1) + exp(in2)); }
49  {
50  T2 theMax = std::max(in1, out);
51  T2 theMin = std::min(in1, out);
52  out = theMax + log(1 + exp(theMin - theMax)); // numerically better
53  }
54 
56  template<class T1, class T2>
57  static void iop(const T1& in1, T2& out)
58 // { out - in1; } // reference implementation has no effect?
59  { throw "not implemented"; }
60 
62  template<class T1,class T2,class T3>
63  static void iop(const T1 in1, const T2 in2, T3& out)
64  { out = log(exp(in1) - exp(in2)); }
65 
67  static bool hasbop()
68  { return false; }
71  template<class T>
72  static bool bop(const T& in1, const T& in2)
73  { return false; }
76  template<class T>
77  static bool ibop(const T& in1, const T& in2)
78  { return false; }
79 };
80 
81 } // namespace opengm
82 
83 #endif // #ifndef OPENGM_OPERATIONS_INTEGRATOR_HXX
The OpenGM namespace.
Definition: config.hxx:43
static void neutral(T &out)
neutral element (call by reference)
Definition: logsumexp.hxx:22
static void iop(const T1 in1, const T2 in2, T3 &out)
inverse operation (call by reference)
Definition: logsumexp.hxx:63
static void op(const T1 &in1, T2 &out)
operation (in-place)
Definition: logsumexp.hxx:37
static T neutral()
neutral element (with return)
Definition: logsumexp.hxx:17
static T ineutral()
inverse neutral element (with return)
Definition: logsumexp.hxx:27
static bool hasbop()
bool operation flag
Definition: logsumexp.hxx:67
static bool bop(const T &in1, const T &in2)
inverse boolean operation boolean operation (obsolete)
Definition: logsumexp.hxx:72
static void op(const T1 in1, const T2 in2, T3 &out)
operation (not in-place)
Definition: logsumexp.hxx:47
Logsumexp (addition in log space) as a unary accumulation.
Definition: logsumexp.hxx:13
static bool ibop(const T &in1, const T &in2)
inverse boolean operation inverse boolean operation (obsolete)
Definition: logsumexp.hxx:77
static void iop(const T1 &in1, T2 &out)
inverse operation (in-place)
Definition: logsumexp.hxx:57
static void ineutral(T &out)
inverse neutral element (call by reference)
Definition: logsumexp.hxx:32