summaryrefslogtreecommitdiff
path: root/utils/logval.h
blob: 4605d9198d930ed1d00d04895ab9f1e64600e110 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#ifndef LOGVAL_H_
#define LOGVAL_H_

#define LOGVAL_CHECK_NEG false

#include <boost/functional/hash.hpp>
#include <iostream>
#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
template <class T>
class LogVal {
 public:
  typedef LogVal<T> Self;

  LogVal() : s_(), v_(LOGVAL_LOG0) {}
  explicit LogVal(double x) : s_(std::signbit(x)), v_(s_ ? std::log(-x) : std::log(x)) {}
  LogVal(init_minus_1) : s_(true),v_(0) {  }
  LogVal(init_1) : s_(),v_(0) {  }
  LogVal(init_0) : s_(),v_(LOGVAL_LOG0) {  }
  LogVal(int x) : s_(x<0), v_(s_ ? std::log(-x) : std::log(x)) {}
  LogVal(unsigned x) : s_(0), v_(std::log(x)) { }
  LogVal(double lnx,bool sign) : s_(sign),v_(lnx) {}
  LogVal(double lnx,init_lnx) : s_(),v_(lnx) {}
  static Self exp(T lnx) { return Self(lnx,false); }

  // maybe the below are faster than == 1 and == 0.  i don't know.
  bool is_1() const { return v_==0&&s_==0; }
  bool is_0() const { return v_==LOGVAL_LOG0; }

  static Self One() { return Self(1); }
  static Self Zero() { return Self(); }
  static Self e() { return Self(1,false); }
  void logeq(const T& v) { s_ = false; v_ = v; }

  std::size_t hash_impl() const {
    using namespace boost;
    return hash_value(v_)+s_;
  }

  // just like std::signbit, negative means true.  weird, i know
  bool signbit() const {
    return s_;
  }
  friend inline bool signbit(Self const& x) { return x.signbit(); }

  Self& besteq(const Self& a) {
    assert(!a.s_ && !s_);
    if (a.v_ < v_)
      v_=a.v_;
    return *this;
  }

  Self& operator+=(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_));
      }
    } 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_;
      }
    }
    return *this;
  }

  Self& operator*=(const Self& a) {
    s_ = (s_ != a.s_);
    v_ += a.v_;
    return *this;
  }

  Self& operator/=(const Self& a) {
    s_ = (s_ != a.s_);
    v_ -= a.v_;
    return *this;
  }

  Self& operator-=(const Self& a) {
    Self b = a;
    b.negate();
    return *this += b;
  }

  // Self(fabs(log(x)),x.s_)
  friend Self abslog(Self x) {
    if (x.v_<0) x.v_=-x.v_;
    return x;
  }

  Self& poweq(const T& power) {
#if LOGVAL_CHECK_NEG
    if (s_) {
      std::cerr << "poweq(T) not implemented when s_ is true\n";
      std::abort();
    } else
#endif
      v_ *= power;
    return *this;
  }

  //remember, s_ means negative.
  inline bool lt(Self const& o) const {
    return s_==o.s_ ? v_ < o.v_ : s_ > o.s_;
  }
  inline bool gt(Self const& o) const {
    return s_==o.s_ ? o.v_ < v_ : s_ < o.s_;
  }

  Self operator-() const {
    return Self(v_,-s_);
  }
  void negate() { s_ = !s_; }

  Self inverse() const { return Self(-v_,s_); }

  Self pow(const T& power) const {
    Self res = *this;
    res.poweq(power);
    return res;
  }

  Self root(const T& root) const {
    return pow(1/root);
  }

  operator T() const {
    if (s_) return -std::exp(v_); else return std::exp(v_);
  }
  T as_float() const {
    if (s_) return -std::exp(v_); else return std::exp(v_);
  }

  bool s_;
  T v_;
};

template <class T>
struct semiring_traits<LogVal<T> > : default_semiring_traits<LogVal<T> > {
  static const bool has_logplus=true;
  static const bool has_besteq=true;
  static const bool has_order=true;
  static const bool has_subtract=true;
  static const bool has_negative=true;
};

// copy elision - as opposed to explicit copy of LogVal<T> const& o1, we should be able to construct Logval r=a+(b+c) as a single result in place in r.  todo: return std::move(o1) - C++0x
template<class T>
LogVal<T> operator+(LogVal<T> o1, const LogVal<T>& o2) {
  o1 += o2;
  return o1;
}

template<class T>
LogVal<T> operator*(LogVal<T> o1, const LogVal<T>& o2) {
  o1 *= o2;
  return o1;
}

template<class T>
LogVal<T> operator/(LogVal<T> o1, const LogVal<T>& o2) {
  o1 /= o2;
  return o1;
}

template<class T>
LogVal<T> operator-(LogVal<T> o1, const LogVal<T>& o2) {
  o1 -= o2;
  return o1;
}

template<class T>
T log(const LogVal<T>& o) {
#ifdef LOGVAL_CHECK_NEG
  if (o.s_) return log(-1.0);
#endif
  return o.v_;
}

template <class T>
LogVal<T> pow(const LogVal<T>& b, const T& e) {
  return b.pow(e);
}

template <class T>
bool operator<(const LogVal<T>& lhs, const LogVal<T>& rhs) {
  if (lhs.s_ == rhs.s_) {
    return (lhs.v_ < rhs.v_);
  } else {
    return lhs.s_ > rhs.s_;
  }
}

#if 0
template <class T>
bool operator<=(const LogVal<T>& lhs, const LogVal<T>& rhs) {
  return (lhs.v_ <= rhs.v_);
}

template <class T>
bool operator>(const LogVal<T>& lhs, const LogVal<T>& rhs) {
  return (lhs.v_ > rhs.v_);
}

template <class T>
bool operator>=(const LogVal<T>& lhs, const LogVal<T>& rhs) {
  return (lhs.v_ >= rhs.v_);
}
#endif


template <class T>
std::size_t hash_value(const LogVal<T>& x) { return x.hash_impl(); }

template <class T>
bool operator==(const LogVal<T>& lhs, const LogVal<T>& rhs) {
  return (lhs.v_ == rhs.v_) && (lhs.s_ == rhs.s_);
}

template <class T>
bool operator!=(const LogVal<T>& lhs, const LogVal<T>& rhs) {
  return !(lhs == rhs);
}

#endif