-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.h
486 lines (449 loc) · 18.8 KB
/
log.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/**
* \file log.h
*
* Contains useful functions for performing arithmetic in log space, such as
* computing the maximum of an array, computing sum-exp, log-sum-exp, and
* normalization. This file also contains log_cache, which caches the
* logarithms of positive integers. All functions in this file avoid loss of
* precision by performing operations in log space when feasible.
*
* In the following example, the array
* `{ -100.0, -100.0, -100.0 + log(2.0), -100.0 + log(4.0) }` of log
* probabilities is normalized and printed. The expected output is
* `[0.125000, 0.125000, 0.250000, 0.500000]`.
*
* ```{.cpp}
* #include <math/log.h>
* #include <core/io.h>
* using namespace core;
*
* int main() {
* double a[] = { -100.0, -100.0, -100.0 + log(2.0), -100.0 + log(4.0) };
* normalize_exp(a, 4);
* print(a, 4, stdout);
* }
* ```
*
* <!-- Created on: Aug 1, 2015
* Author: asaparov -->
*/
#ifndef LOG_H_
#define LOG_H_
#include <core/array.h>
#include <math.h>
/**
* This value is used for `log(0)` in some operations to avoid floating-point
* infinities and improve performance.
*/
#define LOG_ZERO -1.0e16
/**
* Returns `src`. This function is defined so that the function in this file
* `max`, `sum_exp`, and `normalize` behave correctly when `T` is `float`.
*/
inline float log_probability(float src) { return src; }
/**
* Returns `src`. This function is defined so that the function in this file
* `max`, `sum_exp`, and `normalize` behave correctly when `T` is `double`.
*/
inline double log_probability(double src) { return src; }
template<typename T>
struct default_value_type { typedef double type; };
template<>
struct default_value_type<float> { typedef float type; };
/**
* Computes the maximum log probability of the elements in `src` with the given
* `length`, only inspecting the elements at every `skip` locations, until
* `length` elements have been considered. For example, if `skip = 2`, this
* function will find the maximum of the elements at every *even* index. The
* computed maximum is stored in `max`.
* \tparam T the type of every element in `src`, for which the function `V log_probability(const T&)` is defined.
* \tparam V the type of the log probabilities.
*/
template<typename T, typename V>
inline void max(V& max, const T* src, unsigned int length, unsigned int skip)
{
for (unsigned int i = 0; i < length; i++)
if (log_probability(src[i * skip]) > max)
max = log_probability(src[i * skip]);
}
/**
* Computes the maximum sum of the log probability of the elements in `src` and
* the elements in `prior` with the given `length`, only inspecting the
* elements in `src` at every `skip` locations, until `length` elements have
* been considered. For example, if `skip = 2`, this function will find the
* maximum of the elements at every *even* index. Note that `skip` does not
* affect the inspection of elements in `prior`. The computed maximum is stored
* in `max`.
*
* So more precisely, this function computes:
* \f[ max_i \{ f(\text{src}[i \cdot \text{skip}]) + g(\text{prior}[i]) \}. \f]
* where \f$ f(\cdot) \f$ is the function `V log_probability(const T&)` and
* \f$ g(\cdot) \f$ is the function `V log_probability(const P&)`.
*
* \tparam T the type of every element in `src`, for which the function `V log_probability(const T&)` is defined.
* \tparam P the type of every element in `prior`, for which the function `V log_probability(const P&)` is defined.
* \tparam V the type of the log probabilities.
*/
template<typename T, typename P, typename V>
inline void max(V& max, const T* src, const P* prior, unsigned int length, unsigned int skip)
{
for (unsigned int i = 0; i < length; i++) {
V value = log_probability(src[i * skip]) + log_probability(prior[i]);
if (value > max) max = value;
}
}
/**
* Returns the maximum log probability of the elements in `src` with the given
* `length`, only inspecting the elements at every `skip` locations, until
* `length` elements have been considered. For example, if `skip = 2`, this
* function will find the maximum of the elements at every *even* index.
* \tparam T the type of every element in `src`, for which the function `V log_probability(const T&)` is defined.
* \tparam V the type of the log probabilities. By default, if `T` is a floating-point type, `V` is the same type.
*/
template<typename T, typename V = typename default_value_type<T>::type>
inline V max(const T* src, unsigned int length, unsigned int skip)
{
V maximum = log_probability(src[0]);
if (length > 1)
max(maximum, src + skip, length - 1, skip);
return maximum;
}
/**
* Returns the maximum sum of the log probability of the elements in `src` and
* the elements in `prior` with the given `length`, only inspecting the
* elements in `src` at every `skip` locations, until `length` elements have
* been considered. For example, if `skip = 2`, this function will find the
* maximum of the elements at every *even* index. Note that `skip` does not
* affect the inspection of elements in `prior`. The computed maximum is stored
* in `max`.
*
* So more precisely, this function computes:
* \f[ max_i \{ f(\text{src}[i \cdot \text{skip}]) + g(\text{prior}[i]) \}. \f]
* where \f$ f(\cdot) \f$ is the function `V log_probability(const T&)` and
* \f$ g(\cdot) \f$ is the function `V log_probability(const P&)`.
*
* \tparam T the type of every element in `src`, for which the function `V log_probability(const T&)` is defined.
* \tparam P the type of every element in `prior`, for which the function `V log_probability(const P&)` is defined.
* \tparam V the type of the log probabilities. By default, if `T` is a floating-point type, `V` is the same type.
*/
template<typename T, typename P, typename V = typename default_value_type<T>::type>
inline V max(const T* src, const P* prior,
unsigned int length, unsigned int skip)
{
V maximum = src[0] + prior[0];
if (length > 1)
max(maximum, src + skip, prior + 1, length - 1, skip);
return maximum;
}
/**
* Returns the maximum log probability of the elements in the collection `src`.
* \tparam Collection the type of a collection of items, each of type `T`, for
* which the function `V log_probability(const T&)` is defined.
* \tparam V the type of the log probabilities.
*/
template<typename Collection, typename V = typename default_value_type<decltype(*std::declval<Collection>().begin())>::type>
inline V max(const Collection& src)
{
auto i = src.begin();
V maximum = log_probability(*i);
for (; i != src.end(); ++i) {
V value = log_probability(*i);
if (value > maximum)
maximum = value;
}
return maximum;
}
/**
* Returns the maximum log probability of the given native array `src`.
* \tparam T the type of every element in `src`, for which the function
* `V log_probability(const T&)` is defined, where `V` is the return type
* of this function.
*/
template<typename T, typename V = typename default_value_type<T>::type>
inline V max(const T* src, unsigned int length) {
return max<T, V>(src, length, 1);
}
/**
* This function returns
* \f[ \sum_{i=0}^{\text{length}-1} \exp\{ f(\text{src}[i * \text{skip}]) - \text{shift} \} \f]
* where \f$ f(\cdot) \f$ is the function `V log_probability(const T&)`.
* \tparam T the type of every element in `src`, for which the function `V log_probability(const T&)` is defined.
* \tparam V the type of the log probabilities.
*/
template<typename T, typename V>
inline V sumexp(const T* src, unsigned int length, unsigned int skip, const V& shift) {
V sum = 0.0;
for (unsigned int i = 0; i < length; i++)
sum += exp(log_probability(src[i * skip]) - shift);
return sum;
}
/**
* This function returns
* \f[ \sum_{i=0}^{\text{length}-1} \exp\{ f(\text{src}[i * \text{skip}]) + g(\text{prior}[i]) - \text{shift} \} \f]
* where \f$ f(\cdot) \f$ is the function `V log_probability(const T&)` and
* \f$ g(\cdot) \f$ is the function `V log_probability(const P&)`.
* \tparam T the type of every element in `src`, for which the function `V log_probability(const T&)` is defined.
* \tparam P the type of every element in `prior`, for which the function `V log_probability(const P&)` is defined.
* \tparam V the type of the log probabilities.
*/
template<typename T, typename P, typename V>
inline V sumexp(const T* src, const P* prior, unsigned int length, unsigned int skip, const V& shift) {
V sum = 0.0;
for (unsigned int i = 0; i < length; i++)
sum += exp(log_probability(src[i * skip]) + log_probability(prior[i]) - shift);
return sum;
}
/**
* This function returns
* \f[ \sum_{c\in\text{src}} \exp\{ f(c) - \text{shift} \} \f]
* where \f$ f(\cdot) \f$ is the function `V log_probability(const T&)`.
* \tparam Collection the type of a collection of items, each of type `T`, for
* which the function `V log_probability(const T&)` is defined.
* \tparam V the type of the log probabilities.
*/
template<typename Collection, typename V>
inline V sumexp(const Collection& src, const V& shift) {
V sum = 0.0;
for (const auto& element : src)
sum += exp(log_probability(element) - shift);
return sum;
}
/**
* Copies the log probabilities from `src` into `dst`, subtracting
* `normalization` from each element. This function assumes `dst` is
* initialized and has sufficient capacity. This function only considers
* elements at every `skip` positions, until `length` elements have been
* considered. For example, if `skip = 2`, only the elements at even indices
* are copied. Note that `src` and `dst` can be the same.
* \tparam T the type of every element in `src`, for which the function `V log_probability(const T&)` is defined.
* \tparam V the type of the log probabilities.
*/
template<typename T, typename V>
inline void normalize(const T* src, V* dst, unsigned int length, unsigned int skip, const V& normalization)
{
for (unsigned int i = 0; i < length; i++)
dst[i * skip] = log_probability(src[i * skip]) - normalization;
}
/**
* Copies the log probabilities from `src` into `dst`, subtracting
* `normalization` from each element, and taking the natural exponent of the
* result. This function assumes `dst` is initialized and has sufficient
* capacity. This function only considers elements at every `skip` positions,
* until `length` elements have been considered.
* For example, if `skip = 2`, only the elements at even indices are copied.
* Note that `src` and `dst` can be the same.
* \tparam T the type of every element in `src`, for which the function `V log_probability(const T&)` is defined.
* \tparam V the type of the log probabilities.
*/
template<typename T, typename V>
inline void normalize_exp(const T* src, V* dst,
unsigned int length, unsigned int skip, const V& normalization)
{
for (unsigned int i = 0; i < length; i++)
dst[i * skip] = exp(log_probability(src[i * skip]) - normalization);
}
/**
* This function computes
* \f[ \exp\{ f(\text{src}[i * \text{skip}]) + g(\text{prior}[i]) - \text{normalization} \} \f]
* and stores the result in `dst[i]`, where `i` ranges from `0` to
* `length - 1`, and \f$ f(\cdot) \f$ is the function
* `V log_probability(const T&)` and \f$ g(\cdot) \f$ is the function
* `V log_probability(const P&)`. Note that `src` and `dst` can be the same.
* \tparam T the type of every element in `src`, for which the function `V log_probability(const T&)` is defined.
* \tparam P the type of every element in `prior`, for which the function `V log_probability(const P&)` is defined.
* \tparam V the type of the log probabilities.
*/
template<typename T, typename P, typename V>
inline void normalize_exp(const T* src, const P* prior, V* dst,
unsigned int length, unsigned int skip, const V& normalization)
{
for (unsigned int i = 0; i < length; i++)
dst[i * skip] = exp(log_probability(src[i * skip]) + log_probability(prior[i]) - normalization);
}
/**
* Returns the natural logarithm of the sum of the natural exponent of the
* elements in `src`, skipping every `skip` elements, until `length` elements
* have been considered.
* \tparam T the type of every element in `src`, for which the function `V log_probability(const T&)` is defined.
* \tparam V the type of the log probabilities.
*/
template<typename T, typename V = typename default_value_type<T>::type>
inline V logsumexp(const T* src, unsigned int length, unsigned int skip = 1) {
V maximum = max<T, V>(src, length);
return log(sumexp(src, length, skip, maximum)) + maximum;
}
/**
* Returns the natural logarithm of the sum of the natural exponent of the
* elements in `src`, with the given `prior`, skipping every `skip` elements,
* until `length` elements have been considered. Note that `skip` does not
* affect the inspection of the elements in `prior`.
* \tparam T the type of every element in `src`, for which the function `V log_probability(const T&)` is defined.
* \tparam P the type of every element in `prior`, for which the function `V log_probability(const P&)` is defined.
* \tparam V the type of the log probabilities.
*/
template<typename T, typename P, typename V = typename default_value_type<T>::type>
inline V logsumexp(const T* src, const P* prior, unsigned int length, unsigned int skip = 1) {
V maximum = max<T, P, V>(src, prior, length);
return log(sumexp(src, prior, length, skip, maximum)) + maximum;
}
/**
* Returns the natural logarithm of the sum of the natural exponent of the
* elements in the collection `src`.
* \tparam Collection the type of a collection of items, each of type `T`, for
* which the function `V log_probability(const T&)` is defined.
* \tparam V the type of the log probabilities.
*/
template<typename Collection, typename V = typename default_value_type<decltype(*std::declval<Collection>().begin())>::type>
inline V logsumexp(const Collection& src) {
V maximum = max<Collection, V>(src);
return log(sumexp(src, maximum)) + maximum;
}
/**
* Computes the *normalized log probabilities* of the elements in `src` and
* stores them in `dst`, skipping every `skip` elements, until `length`
* elements have been considered. Note that `src` and `dst` can be the same.
* \tparam T the type of every element in `src`, for which the function `V log_probability(const T&)` is defined.
* \tparam V the type of the log probabilities.
*/
template<typename T, typename V>
void normalize(const T* src, V* dst, unsigned int length, unsigned int skip = 1) {
normalize(src, dst, length, skip, logsumexp(src, length, skip));
}
/**
* Computes the *normalized probabilities* of the elements in `src` and stores
* them in `dst`, skipping every `skip` elements, until `length` elements have
* been considered. Note that `src` and `dst` can be the same.
* \tparam T the type of every element in `src`, for which the function `V log_probability(const T&)` is defined.
* \tparam V the type of the log probabilities.
*/
template<typename T, typename V>
void normalize_exp(const T* src, V* dst, unsigned int length, unsigned int skip = 1) {
normalize_exp(src, dst, length, skip, logsumexp(src, length, skip));
}
/**
* Computes the *normalized probabilities* of the elements in `src` and stores
* them in `dst`, skipping every `skip` elements, until `length` elements have
* been considered. Note that `skip` does not affect the inspection of the
* elements in `prior`. Also note that `src` and `dst` can be the same.
* \tparam T the type of every element in `src`, for which the function `V log_probability(const T&)` is defined.
* \tparam P the type of every element in `prior`, for which the function `V log_probability(const P&)` is defined.
* \tparam V the type of the log probabilities.
*/
template<typename T, typename P, typename V>
void normalize_exp(const T* src, const P* prior, V* dst, unsigned int length, unsigned int skip = 1) {
normalize_exp(src, prior, dst, length, skip, logsumexp(src, prior, length, skip));
}
/**
* Normalizes the elements in `src`. Like all functions in this file, loss of
* precision is avoided by performing operations in log space when feasible.
*/
template<typename T>
inline void normalize_exp(T* x, unsigned int length) {
normalize_exp(x, x, length, 1);
}
/**
* Returns the natural logarithm of the sum of the natural exponents of
* `first` and `second`.
* \tparam V satisfies [is_arithmetic](http://en.cppreference.com/w/cpp/types/is_arithmetic).
*/
template<typename V>
inline V logsumexp(const V& first, const V& second) {
if (first > second)
return first + log(1 + exp(second - first));
else return second + log(1 + exp(first - second));
}
/**
* Returns the natural logarithm of `exp(first) - exp(second)`.
* \tparam V satisfies [is_arithmetic](http://en.cppreference.com/w/cpp/types/is_arithmetic).
*/
template<typename V>
inline V logdiffexp(const V& first, const V& second) {
if (first > second)
return first + log(1 - exp(second - first));
else return second + log(-1 + exp(first - second));
}
/**
* This structure caches natural logarithms of positive integers.
*
* The following example displays a typical use-case of this cache. The
* expected output is
* `log(2) = 0.693147, log(183) = 5.209486, sum from i=1 to 999 of log(i) = 5905.220423`.
*
* ```{.cpp}
* #include <math/log.h>
* using namespace core;
*
* int main() {
* log_cache<double> cache(1000);
* printf("log(2) = %lf, ", cache.get(2));
* printf("log(183) = %lf, ", cache.get(183));
*
* double sum = 0.0;
* for (unsigned int i = 1; i < 1000; i++)
* sum += cache.get(i);
* printf("sum from i=1 to 999 of log(i) = %lf\n", sum);
* }
* ```
*
* \tparam V satisfies [is_arithmetic](http://en.cppreference.com/w/cpp/types/is_arithmetic).
*/
template<typename V>
struct log_cache {
V* values;
unsigned int size;
/**
* Constructs the cache with the given `initial_size`. The natural
* logarithms up to and includig `log(initial_size - 1)` are precomputed
* and stored.
*/
log_cache(unsigned int initial_size) : values(NULL), size(0) {
if (!resize<true>(initial_size))
exit(EXIT_FAILURE);
}
~log_cache() { free(values); }
/**
* Checks that the natural logarithms up to and including
* `log(requested_size - 1)` are computed and stored.
*/
inline bool ensure_size(unsigned int requested_size) {
if (requested_size > size) {
bool result = resize(requested_size);
return result;
}
return true;
}
/**
* Returns the natural logarithm of `x`. This function assumes that
* `log(x)` has already been computed, either by the constructor or by
* ensure_size, and no bounds checking is performed.
*/
inline V get(unsigned int x) const {
return values[x];
}
/**
* Returns a static instance of a log_cache, with type `V`.
*/
static inline log_cache<V>& instance() {
static thread_local log_cache<V> cache(16);
return cache;
}
private:
template<bool FirstResize = false>
inline bool resize(unsigned int new_size) {
V* new_values = (V*) realloc(values, sizeof(V) * new_size);
if (new_values == NULL) {
fprintf(stderr, "log_cache.resize ERROR: Out of memory.\n");
return false;
}
values = new_values;
if (FirstResize) {
values[0] = (V) LOG_ZERO;
size = 1;
}
for (unsigned int i = size; i < new_size; i++)
values[i] = (V) log(i);
size = new_size;
return true;
}
};
#endif /* LOG_H_ */