From efe0d24fa7dbca47825638a52f51977456153bd0 Mon Sep 17 00:00:00 2001 From: "philblunsom@gmail.com" Date: Tue, 22 Jun 2010 20:34:00 +0000 Subject: Initial ci of gi dir git-svn-id: https://ws10smt.googlecode.com/svn/trunk@5 ec762483-ff6d-05da-a07a-a48fb63a330f --- gi/pyp-topics/src/log_add.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 gi/pyp-topics/src/log_add.h (limited to 'gi/pyp-topics/src/log_add.h') diff --git a/gi/pyp-topics/src/log_add.h b/gi/pyp-topics/src/log_add.h new file mode 100644 index 00000000..e0620c5a --- /dev/null +++ b/gi/pyp-topics/src/log_add.h @@ -0,0 +1,30 @@ +#ifndef log_add_hh +#define log_add_hh + +#include +#include +#include +#include + +template +struct Log +{ + static T zero() { return -std::numeric_limits::infinity(); } + + static T add(T l1, T l2) + { + if (l1 == zero()) return l2; + if (l1 > l2) + return l1 + std::log(1 + exp(l2 - l1)); + else + return l2 + std::log(1 + exp(l1 - l2)); + } + + static T subtract(T l1, T l2) + { + //std::assert(l1 >= l2); + return l1 + log(1 - exp(l2 - l1)); + } +}; + +#endif -- cgit v1.2.3