diff options
author | graehl <graehl@ec762483-ff6d-05da-a07a-a48fb63a330f> | 2010-08-21 02:59:43 +0000 |
---|---|---|
committer | graehl <graehl@ec762483-ff6d-05da-a07a-a48fb63a330f> | 2010-08-21 02:59:43 +0000 |
commit | 0ff36ba3633660dc0c10c7899b6596755a5d7168 (patch) | |
tree | 10ecedaabd08f746d9777994c664f2c9784f4d40 | |
parent | 730a6c0bb666a394027ba7863e94699d96005afe (diff) |
semiring
git-svn-id: https://ws10smt.googlecode.com/svn/trunk@610 ec762483-ff6d-05da-a07a-a48fb63a330f
-rw-r--r-- | utils/logval.h | 1 | ||||
-rwxr-xr-x | utils/max_plus.h | 47 |
2 files changed, 20 insertions, 28 deletions
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 <cstdlib> #include <cmath> #include <limits> +#include <cassert> #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<b sorting will do worst (closest to 0) first. so get used to passing predicates like std::greater<MaxPlus<T> > around // x+y := max{x,y} // x*y := x+y @@ -13,11 +16,9 @@ #include <iostream> #include <cstdlib> #include <cmath> +#include <cassert> #include <limits> #include "semiring.h" -#define TROPICAL_DEBUG(x) -#undef LOGVAL_LOG0 -#define LOGVAL_LOG0 -std::numeric_limits<T>::infinity() template <class T> 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<b means a better than b (i.e. gt) inline bool lt(Self const& o) const { - return v_<o.v_; + return v_ < o.v_; } inline bool gt(Self const& o) const { - return o.v_<v_; + return o.v_ > 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 <class T> struct semiring_traits<MaxPlus<T> > : default_semiring_traits<MaxPlus<T> > { static const bool has_logplus=true; static const bool has_besteq=true; +#if MAX_PLUS_ORDER + static const bool have_order=true; +#endif }; - -template <class T> - -#if 0 +#if MAX_PLUS_ORDER template <class T> -bool operator=(const MaxPlus<T>& lhs, const MaxPlus<T>& rhs) { - return (lhs.v_ <= rhs.v_); +bool operator<(const MaxPlus<T>& lhs, const MaxPlus<T>& rhs) { + return (lhs.v_ < rhs.v_); } template <class T> |