From 0ff36ba3633660dc0c10c7899b6596755a5d7168 Mon Sep 17 00:00:00 2001 From: graehl Date: Sat, 21 Aug 2010 02:59:43 +0000 Subject: semiring git-svn-id: https://ws10smt.googlecode.com/svn/trunk@610 ec762483-ff6d-05da-a07a-a48fb63a330f --- utils/logval.h | 1 + utils/max_plus.h | 47 +++++++++++++++++++---------------------------- 2 files changed, 20 insertions(+), 28 deletions(-) (limited to 'utils') diff --git a/utils/logval.h b/utils/logval.h index 833da7b8..4605d919 100644 --- a/utils/logval.h +++ b/utils/logval.h @@ -8,6 +8,7 @@ #include #include #include +#include #include "semiring.h" //TODO: template for supporting negation or not - most uses are for nonnegative "probs" only; probably some 10-20% speedup available diff --git a/utils/max_plus.h b/utils/max_plus.h index 5ca26522..2c55b33c 100755 --- a/utils/max_plus.h +++ b/utils/max_plus.h @@ -1,6 +1,9 @@ #ifndef MAX_PLUS_H_ #define MAX_PLUS_H_ +#define MAX_PLUS_ORDER 0 +#define MAX_PLUS_DEBUG(x) + // max-plus algebra. ordering a > b really means that (i.e. default a > around // x+y := max{x,y} // x*y := x+y @@ -13,11 +16,9 @@ #include #include #include +#include #include #include "semiring.h" -#define TROPICAL_DEBUG(x) -#undef LOGVAL_LOG0 -#define LOGVAL_LOG0 -std::numeric_limits::infinity() template class MaxPlus { @@ -29,7 +30,7 @@ class MaxPlus { MaxPlus(init_0) : v_(LOGVAL_LOG0) { } MaxPlus(int x) : v_(std::log(x)) {} MaxPlus(unsigned x) : v_(std::log(x)) { } - MaxPlus(double lnx,bool sign) : v_(lnx) { TROPICAL_DEBUG(assert(!sign)); } + MaxPlus(double lnx,bool sign) : v_(lnx) { MAX_PLUS_DEBUG(assert(!sign)); } MaxPlus(double lnx,init_lnx) : v_(lnx) {} static Self exp(T lnx) { return MaxPlus(lnx,false); } @@ -45,19 +46,10 @@ class MaxPlus { Self& logpluseq(const Self& a) { if (a.is_0()) return *this; - if (a.s_ == s_) { - if (a.v_ < v_) { - v_ = v_ + log1p(std::exp(a.v_ - v_)); - } else { - v_ = a.v_ + log1p(std::exp(v_ - a.v_)); - } + if (a.v_ < v_) { + v_ = v_ + log1p(std::exp(a.v_ - v_)); } else { - if (a.v_ < v_) { - v_ = v_ + log1p(-std::exp(a.v_ - v_)); - } else { - v_ = a.v_ + log1p(-std::exp(v_ - a.v_)); - s_ = !s_; - } + v_ = a.v_ + log1p(std::exp(v_ - a.v_)); } return *this; } @@ -131,18 +123,17 @@ class MaxPlus { // intentionally not defining an operator < or operator > - because you may want to default (for library convenience) a v_; } - friend inline bool operator==(Self const& lhs, Self const&rhs) { + friend inline bool operator==(Self const& lhs, Self const& rhs) { return lhs.v_ == rhs.v_; } - friend inline bool operator!=(Self const& lhs, Self const&rhs) { + friend inline bool operator!=(Self const& lhs, Self const& rhs) { return lhs.v_ != rhs.v_; } - std::size_t hash() const { using namespace boost; return hash_value(v_); @@ -153,7 +144,7 @@ class MaxPlus { /* operator T() const { - return std::exp(v_); + return std::exp(v_); } */ T as_float() const { @@ -167,15 +158,15 @@ template struct semiring_traits > : default_semiring_traits > { static const bool has_logplus=true; static const bool has_besteq=true; +#if MAX_PLUS_ORDER + static const bool have_order=true; +#endif }; - -template - -#if 0 +#if MAX_PLUS_ORDER template -bool operator=(const MaxPlus& lhs, const MaxPlus& rhs) { - return (lhs.v_ <= rhs.v_); +bool operator<(const MaxPlus& lhs, const MaxPlus& rhs) { + return (lhs.v_ < rhs.v_); } template -- cgit v1.2.3