-
Notifications
You must be signed in to change notification settings - Fork 3
/
logval.h
240 lines (200 loc) · 5.52 KB
/
logval.h
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
236
237
238
239
240
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. See the enclosed file LICENSE for a copy or if
* that was not distributed with this file, You can obtain one at
* http://mozilla.org/MPL/2.0/.
*
* Copyright 2017 Max H. Gerlach
*
* */
/*
* logval.h
*
* Created on: Jan 12, 2011
* Author: gerlach
*/
#ifndef LOGVAL_H_
#define LOGVAL_H_
#include <cmath>
#include <iostream>
#include <vector>
//calculations with LogVal internally use the natural logarithm of values
//to avoid overflows
class LogVal {
public:
static constexpr double LogZero = 1e-3; //ln(LogZero) << ln(1)
double lnx;
LogVal();
explicit LogVal(double x);
LogVal(const LogVal& lv);
LogVal& addSmaller(LogVal smaller); //add a smaller number
LogVal& addLarger(LogVal larger); //add a larger number
LogVal& subtractSmaller(LogVal smaller);
LogVal& subtractLarger(LogVal larger);
LogVal& operator *=(LogVal rhs);
LogVal& operator /=(LogVal rhs);
LogVal& operator +=(LogVal rhs); //these use an additional check as compared to addSmaller and addLarger
LogVal& operator -=(LogVal rhs); //of course only for rhs < lhs!
LogVal& operator *=(double rhs);
LogVal& operator /=(double rhs);
bool operator==(LogVal rhs) const;
bool operator!=(LogVal rhs) const;
bool operator<=(LogVal rhs) const;
bool operator>=(LogVal rhs) const;
bool operator<(LogVal rhs) const;
bool operator>(LogVal rhs) const;
};
/*
double toDouble(LogVal val); //returns exp(lnx)
double toDouble(double val); //does nothing to val
LogVal toLogValExp(double exponent); //returns a LogVal representing exp(exponent), i.e. set lnx to exponent
LogVal operator+(LogVal a, LogVal b);
LogVal operator-(LogVal a, LogVal b);
LogVal operator*(LogVal a, LogVal b);
LogVal operator/(LogVal a, LogVal b);
LogVal operator*(LogVal a, double b);
LogVal operator/(LogVal a, double b);
LogVal pow(LogVal base, double exponent);
std::ostream& operator<<(std::ostream& stream, LogVal val);
*/
inline bool LogVal::operator==(LogVal rhs) const {
return lnx == rhs.lnx;
}
inline bool LogVal::operator!=(LogVal rhs) const {
return lnx != rhs.lnx;
}
inline bool LogVal::operator<=(LogVal rhs) const {
return lnx <= rhs.lnx;
}
inline bool LogVal::operator>=(LogVal rhs) const {
return lnx >= rhs.lnx;
}
inline bool LogVal::operator<(LogVal rhs) const {
return lnx < rhs.lnx;
}
inline bool LogVal::operator>(LogVal rhs) const {
return lnx > rhs.lnx;
}
inline LogVal::LogVal() {
// lnx = LogZero;
}
inline LogVal::LogVal(double x) {
lnx = std::log(x);
}
inline LogVal::LogVal(const LogVal& lv) {
lnx = lv.lnx;
}
inline LogVal& LogVal::addSmaller(LogVal smaller) {
lnx += log1p(std::exp(smaller.lnx - lnx));
return *this;
}
inline LogVal& LogVal::subtractSmaller(LogVal smaller) {
lnx += log1p(-std::exp(smaller.lnx - lnx));
return *this;
}
inline LogVal& LogVal::addLarger(LogVal larger) {
lnx = larger.lnx + log1p(std::exp(lnx - larger.lnx));
return *this;
}
inline LogVal& LogVal::subtractLarger(LogVal larger) {
lnx = larger.lnx + log1p(-std::exp(lnx - larger.lnx));
return *this;
}
inline LogVal& LogVal::operator *=(LogVal rhs) {
lnx += rhs.lnx;
return *this;
}
inline LogVal& LogVal::operator /=(LogVal rhs) {
lnx -= rhs.lnx;
return *this;
}
inline LogVal& LogVal::operator +=(LogVal rhs) {
// if (lnx == std::log(0)) {
// lnx = rhs.lnx;
// } else
if (rhs.lnx <= lnx) {
addSmaller(rhs);
} else {
addLarger(rhs);
}
return *this;
}
inline LogVal& LogVal::operator -=(LogVal rhs) {
if (rhs.lnx <= lnx) {
subtractSmaller(rhs);
} else {
subtractLarger(rhs);
}
return *this;
}
inline LogVal& LogVal::operator *=(double rhs) {
lnx += std::log(rhs);
return *this;
}
inline LogVal& LogVal::operator /=(double rhs) {
lnx -= std::log(rhs);
return *this;
}
inline LogVal operator+(LogVal a, LogVal b) {
LogVal t = a;
return t += b;
}
inline LogVal operator-(LogVal a, LogVal b) {
LogVal t = a;
return t -= b;
}
inline LogVal operator*(LogVal a, LogVal b) {
LogVal t = a;
return t *= b;
}
inline LogVal operator/(LogVal a, LogVal b) {
LogVal t = a;
return t /= b;
}
inline LogVal operator*(LogVal a, double b) {
LogVal t = a;
return t *= b;
}
inline LogVal operator/(LogVal a, double b) {
LogVal t = a;
return t /= b;
}
inline LogVal pow(LogVal base, double exponent) {
LogVal r;
r.lnx = base.lnx * exponent;
return r;
}
inline std::ostream& operator<<(std::ostream& stream, LogVal val) {
// return stream << std::exp(val.lnx);
// return stream << "exp(" << val.lnx << ")";
return stream << val.lnx;
}
inline std::istream& operator>>(std::istream& stream, LogVal& val) {
// return stream << std::exp(val.lnx);
// return stream << "exp(" << val.lnx << ")";
return stream >> val.lnx;
}
inline double toDouble(LogVal val) {
return std::exp(val.lnx);
}
inline double toDouble(double val) {
return val;
}
inline LogVal toLogValExp(double exponent) {
LogVal temp;
temp.lnx = exponent;
return temp;
}
//numerically stable addition of a list of logarithmic values:
inline LogVal logSum(const std::vector<LogVal>& logvals) {
LogVal result = logvals[0];
for (unsigned n = 1; n < logvals.size(); ++n) {
LogVal l = logvals[n];
if (l < result) {
result.addSmaller(l);
} else {
result.addLarger(l);
}
}
return result;
}
#endif /* LOGVAL_H_ */