12 #ifndef MLPACK_CORE_MATH_LOG_ADD_IMPL_HPP 13 #define MLPACK_CORE_MATH_LOG_ADD_IMPL_HPP 50 if (std::isinf(d) || std::isinf(r))
53 return r + log(1 + exp(d));
65 typename T::elem_type maxVal = max(x);
66 if (maxVal == -std::numeric_limits<typename T::elem_type>::infinity())
69 return maxVal + log(sum(exp(x - maxVal)));;
77 template<
typename T,
bool InPlace>
78 void LogSumExp(
const T& x, arma::Col<typename T::elem_type>& y)
80 arma::Col<typename T::elem_type> maxs;
85 maxs = max(max(x, 1), y);
87 y = maxs + log(sum(exp(x - repmat(maxs, 1, x.n_cols)), 1) +
95 y = maxs + log(sum(exp(x - repmat(maxs, 1, x.n_cols)), 1));
100 y.replace(-std::numeric_limits<typename T::elem_type>::quiet_NaN(),
101 -std::numeric_limits<typename T::elem_type>::infinity());
110 template<
typename T,
bool InPlace>
111 void LogSumExpT(
const T& x, arma::Col<typename T::elem_type>& y)
113 arma::Row<typename T::elem_type> maxs;
118 maxs = max(max(x, 0), y.t());
120 y = maxs.t() + log(sum(exp(x - repmat(maxs, x.n_rows, 1)), 0) +
121 exp(y.t() - maxs)).t();
126 arma::Row<typename T::elem_type> maxs = max(x, 0);
128 y = (maxs + log(sum(exp(x - repmat(maxs, x.n_rows, 1)), 0))).t();
133 y.replace(-std::numeric_limits<typename T::elem_type>::quiet_NaN(),
134 -std::numeric_limits<typename T::elem_type>::infinity());
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