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
//! This crate defines a basic [`LogProb`] wrapper for floats. The struct is designed so
//! that only values that are coherent for a log-probability are acceptable. This means that
//! [`LogProb`] can store:
//!
//! - Any finite negative float value (e.g. -0.23, -32535.05, -66.0).
//! - Negative infinity (corresponding to 0.0 probability)
//! - 0.0 *and* -0.0.
//!
//! If any other value is passed, [`LogProb::new`] returns a [`FloatIsNanOrPositive`] error.
//! You can also construct new [`LogProb`] from values in \[0,1\] by using
//! [`LogProb::from_raw_prob`]
//!
//! The crate also includes the ability to add log probabilities (equivalent take the product of
//! their corresponding raw probabilities):
//!
//! ```
//! use logprob::LogProb;
//! let x = LogProb::from_raw_prob(0.5).unwrap();
//! let y = LogProb::from_raw_prob(0.5).unwrap();
//! let z = x + y;
//! assert_eq!(z, LogProb::from_raw_prob(0.25).unwrap());
//! ```
//!
//! It is also possible to take product of a [`LogProb`] and an unsigned integer, which
//! corresponds to taking the exponent of the log-probability to the power of the integer.
//! ```
//! # use logprob::LogProb;
//! let x = LogProb::from_raw_prob(0.5_f64).unwrap();
//! let y: u8 = 2;
//! let z = x * y;
//! assert_eq!(z, LogProb::from_raw_prob(0.25).unwrap());
//! ```
//!
//!Finally, the crate also includes reasonably efficient implementations of
// so that one can take the sum of
//!raw-probabilities directly with [`LogProb`].
//!
//! ```
//! # use logprob::LogProb;
//! let x = LogProb::from_raw_prob(0.5_f64).unwrap();
//! let y = LogProb::from_raw_prob(0.25).unwrap();
//! let z = x.add_log_prob(y).unwrap();
//! assert_eq!(z, LogProb::from_raw_prob(0.75).unwrap());
//! ```
//!
//! This can also work for slices or iterators (by importing [`log_sum_exp`] or the trait,
//! [`LogSumExp`] respectively. Note that for empty vectors or iterators, the
//! functions return a [`LogProb`] with negative infinity, corresponding to 0 probability.
//! ```
//! # use logprob::LogProb;
//! use logprob::{LogSumExp, log_sum_exp};
//! let x = LogProb::from_raw_prob(0.5_f64).unwrap();
//! let y = LogProb::from_raw_prob(0.25).unwrap();
//! # #[cfg(feature = "alloc")]
//! let z = [x,y].iter().log_sum_exp().unwrap();
//! # #[cfg(not(feature = "alloc"))]
//! # let z = [x,y].iter().log_sum_exp_no_alloc().unwrap();
//! assert_eq!(z, LogProb::from_raw_prob(0.75).unwrap());
//! let v = log_sum_exp(&[x,y]).unwrap();
//! assert_eq!(z, LogProb::from_raw_prob(0.75).unwrap());
//! ```
//!
//! By default, the both [`log_sum_exp`] and [`LogProb::add_log_prob`] return a
//! [`ProbabilitiesSumToGreaterThanOne`] error if the sum is overflows what is a possible
//! [`LogProb`] value. However, one can use either the `clamped` or `float` versions of these
//! functions to return either a value clamped at 0.0 or the underlying float value which may be
//! greater than 0.0.
//! ```
//! # use logprob::LogProb;
//! # use logprob::{LogSumExp, log_sum_exp};
//! let x = LogProb::from_raw_prob(0.5_f64).unwrap();
//! let y = LogProb::from_raw_prob(0.75).unwrap();
//! # #[cfg(feature = "alloc")]
//! let z = [x,y].iter().log_sum_exp_clamped();
//! # #[cfg(not(feature = "alloc"))]
//! # let z = [x,y].iter().log_sum_exp_clamped_no_alloc();
//! assert_eq!(z, LogProb::new(0.0).unwrap());
//!
//! # #[cfg(feature = "alloc")]
//! let z = [x,y].into_iter().log_sum_exp_float();
//! # #[cfg(not(feature = "alloc"))]
//! # let z = [x,y].into_iter().log_sum_exp_float_no_alloc();
//!
//! approx::assert_relative_eq!(z, (1.25_f64).ln());
//!
//! ```
extern crate std;
extern crate alloc;
use Borrow;
use Hash;
use Float;
pub use ;
pub use ;
///Struct that can only hold float values that correspond to negative log
///probabilities.
///
///## Subtraction and Addition
///[`LogProb`] implements both [`Add`](core::ops::Add) and [`Sub`](core::ops::Sub) to represent multiplication and divisions of
///probabilities respectively.
///
///[`Sub`](core::ops::Sub) can panic if the denominator is negative infinity (as this is division by zero) or if
///the numerator is greater than the denominator (as this would lead to a number greater than 1.0
///in probability space).
///
///Both of these will panic in debug mode, while in release they will silenty saturate (see [`LogProb::saturating_sub`] for details).
///```should_panic
///# use logprob::LogProb;
///let _ = LogProb::new(f32::NEG_INFINITY).unwrap() - LogProb::new(f32::NEG_INFINITY).unwrap();
///```
///
///```should_panic
///# use logprob::LogProb;
///let _ = LogProb::new(-3.0).unwrap() - LogProb::new(-4.0).unwrap();
///```
///
///## `Ord` and `Hash`
///`LogProb` implements both `Hash` and `Ord` since we no longer have `NaN` values.
///However, one should always remember that floating point numbers won't necessarily correspond to
///the exact real number that one might expect when doing different mathematical operations.
///
/// ```
/// # #[cfg(feature = "std")]
/// # use std::collections::HashSet;
/// # #[cfg(feature = "std")]
/// # use std::collections::BTreeSet;
/// # use logprob::LogProb;
/// # fn main() -> anyhow::Result<()> {
/// let a = LogProb::new(-0.1_f64 - 0.2_f64).unwrap();
/// let b = LogProb::new(-0.3_f64).unwrap();
///
/// # #[cfg(feature = "std")]
/// let set = HashSet::from([a, b]);
/// # #[cfg(feature = "std")]
/// let o_set =BTreeSet::from([a,b]);
///
/// //Since the floats aren't exactly equal like one might expect,
/// //we have 2 elements in both collections
/// # #[cfg(feature = "std")]
/// assert_eq!(set.len(), 2);
/// # #[cfg(feature = "std")]
/// assert_eq!(o_set.len(), 2);
/// # Ok(())
/// # }
/// ```
;
pub use ;