mlpack
log_add_impl.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_CORE_MATH_LOG_ADD_IMPL_HPP
13 #define MLPACK_CORE_MATH_LOG_ADD_IMPL_HPP
14 
15 #include "log_add.hpp"
16 
17 namespace mlpack {
18 namespace math {
19 
35 template<typename T>
36 T LogAdd(T x, T y)
37 {
38  T d, r;
39  if (x > y)
40  {
41  d = y - x;
42  r = x;
43  }
44  else
45  {
46  d = x - y;
47  r = y;
48  }
49 
50  if (std::isinf(d) || std::isinf(r))
51  return r;
52 
53  return r + log(1 + exp(d));
54 }
55 
62 template<typename T>
63 typename T::elem_type AccuLog(const T& x)
64 {
65  typename T::elem_type maxVal = max(x);
66  if (maxVal == -std::numeric_limits<typename T::elem_type>::infinity())
67  return maxVal;
68 
69  return maxVal + log(sum(exp(x - maxVal)));;
70 }
71 
77 template<typename T, bool InPlace>
78 void LogSumExp(const T& x, arma::Col<typename T::elem_type>& y)
79 {
80  arma::Col<typename T::elem_type> maxs;
81 
82  if (InPlace)
83  {
84  // Compute the maximum in each column (treating y as a column too).
85  maxs = max(max(x, 1), y);
86 
87  y = maxs + log(sum(exp(x - repmat(maxs, 1, x.n_cols)), 1) +
88  exp(y - maxs));
89  }
90  else
91  {
92  // Compute the maximum element in each column.
93  maxs = max(x, 1);
94 
95  y = maxs + log(sum(exp(x - repmat(maxs, 1, x.n_cols)), 1));
96  }
97 
98  if (maxs.has_inf())
99  {
100  y.replace(-std::numeric_limits<typename T::elem_type>::quiet_NaN(),
101  -std::numeric_limits<typename T::elem_type>::infinity());
102  }
103 }
104 
110 template<typename T, bool InPlace>
111 void LogSumExpT(const T& x, arma::Col<typename T::elem_type>& y)
112 {
113  arma::Row<typename T::elem_type> maxs;
114 
115  if (InPlace)
116  {
117  // Compute the maximum element in each column.
118  maxs = max(max(x, 0), y.t());
119 
120  y = maxs.t() + log(sum(exp(x - repmat(maxs, x.n_rows, 1)), 0) +
121  exp(y.t() - maxs)).t();
122  }
123  else
124  {
125  // Compute the maximum element in each column.
126  arma::Row<typename T::elem_type> maxs = max(x, 0);
127 
128  y = (maxs + log(sum(exp(x - repmat(maxs, x.n_rows, 1)), 0))).t();
129  }
130 
131  if (maxs.has_inf())
132  {
133  y.replace(-std::numeric_limits<typename T::elem_type>::quiet_NaN(),
134  -std::numeric_limits<typename T::elem_type>::infinity());
135  }
136 }
137 
138 } // namespace math
139 } // namespace mlpack
140 
141 #endif
void LogSumExp(const T &x, arma::Col< typename T::elem_type > &y)
Compute the sum of exponentials of each element in each column, then compute the log of that...
Definition: log_add_impl.hpp:78
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
void LogSumExpT(const T &x, arma::Col< typename T::elem_type > &y)
Compute the sum of exponentials of each element in each row, then compute the log of that...
Definition: log_add_impl.hpp:111
T LogAdd(T x, T y)
Internal log-addition.
Definition: log_add_impl.hpp:36
T::elem_type AccuLog(const T &x)
Log-sum a vector of log values.
Definition: log_add_impl.hpp:63