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
487
488
489
490
491
492
493
494
495
496
497
//! Collection of utilities for unit tests
//!
//! This crate offers tools for unit tests, especially for projects involving
//! numerical methods
//!
extern crate num;
use num::{Float, Zero};

fn float_max<T>(a: T, b: T) -> T
where
    T: Float,
{
    if a >= b {
        a
    } else {
        b
    }
}

fn float_min<T>(a: T, b: T) -> T
where
    T: Float,
{
    if a >= b {
        b
    } else {
        a
    }
}

/// Whether two floats are nearly equal (up to specified tolerance)
///
/// ## Arguments
/// - `a` first float
/// - `b` second float
/// - `rel_tol` relative tolerance (must be positive)
/// - `abs_tol` absolute tolerance (must be positive)
///
///
/// ## Results
///
/// Returns true if and only if `a` is nearly equal to `b`.
///
/// In particular, this function will return true if and only if BOTH of the following
/// conditions are satisfied
///
/// - `a==b`, e.g., if the two floats are identical or both equal to infinity
/// - `|a-b| <= max(abs_tol, rel_tol*max(|a|, |b|))`
///
/// The function will return false if either of `a` or `b` is `NaN`.
///
/// It works with `f64` and `f32`
///
/// ## Panics
///
/// The function will panic if the specified relative or absolute tolerance is
/// not positive.
///
pub fn nearly_equal<T>(a: T, b: T, rel_tol: T, abs_tol: T) -> bool
where
    T: Float + Zero,
{
    assert!(rel_tol > T::zero(), "relative tolerance nonpositive");
    assert!(abs_tol > T::zero(), "absolute tolerance nonpositive");

    let abs_a = a.abs();
    let abs_b = b.abs();
    let abs_diff = (a - b).abs();

    if a.is_nan() || b.is_nan() {
        false
    } else if a == b || abs_diff <= T::min_positive_value() {
        true
    } else {
        let max_abs_a_b = float_max(abs_a, abs_b);
        abs_diff <= float_min(abs_tol, rel_tol * max_abs_a_b)
    }
}

/// Asserts that two numbers are nearly equal
///
/// ## Arguments
/// - `a` first float
/// - `b` second float
/// - `rel_tol` relative tolerance (must be positive)
/// - `abs_tol` absolute tolerance (must be positive)
/// - `msg` an error message that will be thrown if the two numbers are not nearly equal
///
/// ## Panics
///
/// The function panics if the two floating-point numbers are not almost equal to one
/// another up to the specified tolerances
pub fn assert_nearly_equal<T>(a: T, b: T, rel_tol: T, abs_tol: T, msg: &'static str)
where
    T: Float + Zero,
{
    assert!(nearly_equal(a, b, rel_tol, abs_tol), msg);
}

/// Checks whether two arrays are element-wise nearly equal
///
/// ## Arguments
///
/// - `a` first array
/// - `b` second array
/// - `rel_tol` relative tolerance
/// - `abs_tol` absolute tolerance
///
/// ## Returns
///
/// The function returns true if and only if the application of `nearly_equal`
/// on all elements of the two arrays returns true, i.e., if the two arrays
/// are element-wise almost equal
///
/// ## Panics
///
/// The function will panic in the following cases:
/// - if the specified relative or absolute tolerance is not positive and
/// - if the two arrays have different lengths
///
pub fn nearly_equal_array<T>(a: &[T], b: &[T], rel_tol: T, abs_tol: T) -> bool
where
    T: Float + Zero,
{
    assert!(a.len() == b.len());
    for (&a, &b) in a.iter().zip(b.iter()) {
        if !nearly_equal(a, b, rel_tol, abs_tol) {
            return false;
        }
    }
    true
}

/// Asserts that two given arrays are almost equal
///
/// ## Arguments
///
/// - `a` first array of floating-point numbers
/// - `b` second array of floating point numbers
/// - `rel_tol` relative tolerance (must be positive)
/// - `abs_tol` absolute tolerance (must be positive)
/// - `msg` an error message that will be thrown if the assertion fails
///
/// ## Panics
///
/// The function will panic if there is at least one coordinate at which the two
/// arrays differ at a relative tolerance more than `rel_tol` and an absolute
/// tolerance of more than `abs_tol`
pub fn assert_nearly_equal_array<T>(a: &[T], b: &[T], rel_tol: T, abs_tol: T, msg: &'static str)
where
    T: Float + Zero,
{
    assert!(a.len() == b.len());
    a.iter()
        .zip(b.iter())
        .enumerate()
        .for_each(|(idx, (&ai, &bi))| {
            if !nearly_equal(ai, bi, rel_tol, abs_tol) {
                panic!("({}) arrays not equal at entry {}", msg, idx)
            }
        });
}

/// Checks whether a given array contains any `NaN` elements
///
/// ## Arguments
///
/// - `a` an array of floating-point numbers
///
/// ## Returns
///
/// Returns `true` if and only if there is at least one element which is `NaN`
///
/// ## Panics
///
/// No panics
pub fn is_any_nan<T>(a: &[T]) -> bool
where
    T: Float,
{
    for &a in a.iter() {
        if a.is_nan() {
            return true;
        }
    }
    false
}

/// Asserts that no element of an array is `NaN`
///
/// ## Arguments
///
/// - `a` an array of floating-point numbers
/// - `msg` error name
///
/// ## Panics
///
/// This function will panic if any element of the given array is `NaN`
///
pub fn assert_none_is_nan<T>(a: &[T], msg: &str)
where
    T: Float,
{
    for (idx, &a) in a.iter().enumerate() {
        if a.is_nan() {
            panic!("({}) nan at poisition {}", msg, idx);
        }
    }
}

/// Asserts that all elements in an array are greater than or equal a given value
///
/// ## Arguments
///
/// - `a` given array of floating-point numbers
/// - `lim` the lower bound on the array; all elements must be greater than or equal
///    to `lim`, otherwise the function panics
/// - `msg` error message
///
/// ## Panics
///
/// The function panic if there is at least on element in `a` which is smaller than `lim`
///
pub fn assert_all_ge<T>(a: &[T], lim: T, msg: &str)
where
    T: Float + std::fmt::Display,
{
    for (idx, &a) in a.iter().enumerate() {
        if a < lim {
            panic!("({}) array[{}] = {} is lower than {}", msg, idx, a, lim);
        }
    }
}

/// Asserts that all elements in an array are less than or equal a given value
///
/// ## Arguments
///
/// - `a` given array of floating-point numbers
/// - `lim` the upper bound on the array; all elements must be less than or equal
///    to `lim`, otherwise the function panics
/// - `msg` error message
///
/// ## Panics
///
/// The function panic if there is at least on element in `a` which is greater than `lim`
///
pub fn assert_all_le<T>(a: &[T], lim: T, msg: &str)
where
    T: Float + std::fmt::Display,
{
    for (idx, &a) in a.iter().enumerate() {
        if a > lim {
            panic!("({}) array[{}] = {} is greater than {}", msg, idx, a, lim);
        }
    }
}

/// Returns `true` if and only if all elements of a given array are finite
///
/// Note that values like `std::f64::INFINITY`, `std::f64::NEG_INFINITY` and
/// `std::f64::NAN` are not finite.
///
/// ## Arguments
///
/// - `a` given array of floating-point numbers
///
/// ## Panics
///
/// Does not panic
///
pub fn is_finite_array<T>(a: &[T]) -> bool
where
    T: Float,
{
    for val in a.iter() {
        if !val.is_finite() {
            return false;
        }
    }

    true
}

/// Asserts that all elements in a given array are finite
///
/// ## Arguments
///
/// - `a` given array of floating-point numbers
/// - `msg` error message
///
/// ## Panics
///
/// This function panics if there is at least one element in the given array
/// which is not finite
pub fn assert_is_finite_array<T>(a: &[T], msg: &str)
where
    T: Float + std::fmt::Display,
{
    for (idx, &a) in a.iter().enumerate() {
        if !a.is_finite() {
            panic!("({}) array[{}] = {} is not finite", msg, idx, a);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn infinities() {
        let a = std::f64::INFINITY;
        let b = std::f64::INFINITY;
        assert!(nearly_equal(a, b, 0.1, 0.1));
    }

    #[test]
    fn nans() {
        let a = std::f64::NAN;
        let b = std::f64::NAN;
        let c = 1.0;
        assert!(!nearly_equal(a, b, 0.1, 0.1));
        assert!(!nearly_equal(a, c, 0.1, 0.1));
    }

    #[test]
    #[should_panic]
    fn no_nonpositive_rel_tol() {
        nearly_equal(5.0, 6.0, 0.0, 1e-7);
    }

    #[test]
    #[should_panic]
    fn no_nonpositive_abs_tol() {
        nearly_equal(5.0, 6.0, 0.01, 0.0);
    }

    #[test]
    fn not_nearly_equal() {
        let a = 1e-8;
        let b = 1e-5;
        assert!(!nearly_equal(a, b, 1e-6, 1e-6))
    }

    #[test]
    fn not_nearly_equal_rel_tol() {
        let a = 1e-14;
        let b = 1e-5;
        assert!(!nearly_equal(a, b, 1e-6, 0.1))
    }

    #[test]
    fn really_nearly_equal() {
        let a = 1.;
        let b = 1. + std::f64::MIN_POSITIVE;
        assert!(nearly_equal(
            a,
            b,
            std::f64::MIN_POSITIVE,
            std::f64::MIN_POSITIVE
        ))
    }

    #[test]
    fn absolutely_equal() {
        let a = 5.;
        let b = 5.;
        assert!(nearly_equal(
            a,
            b,
            std::f64::MIN_POSITIVE,
            std::f64::MIN_POSITIVE
        ))
    }

    #[test]
    fn with_f32() {
        let a = 1000.0_f32;
        let b = 1001.0_f32;
        assert!(nearly_equal(a, b, 0.01, 1.0))
    }

    #[test]
    #[should_panic]
    fn assert_numbers_equal() {
        assert_nearly_equal(1.0, 2.0, 0.01, 0.001, "wtf");
    }

    #[test]
    fn arrays_equal() {
        let x = [1.0, 2.0, 3.0];
        let y = [1.0, 2.0 + 1e-7, 3.0 + 9.9999999e-6];
        assert!(nearly_equal_array(&x, &y, 1e-4, 1e-5));
    }

    #[test]
    fn arrays_not_equal() {
        let x = [1.0, 2.0, 3.0];
        let y = [1.0, 2.0 + 1e-7, 3.0 + 1e-4];
        assert!(!nearly_equal_array(&x, &y, 1e-4, 1e-5));
    }

    #[test]
    fn arrays_identical() {
        let x = [1.0, 2.0, 3.0];
        assert!(nearly_equal_array(&x, &x, 1e-4, 1e-5));
    }

    #[test]
    #[should_panic]
    fn assert_arrays_not_equal() {
        let x = [1.0, 2.0, 3.0];
        let y = [1.0, 2.0 + 1e-7, 3.0 + 1e-4];
        assert_nearly_equal_array(&x, &y, 1e-4, 1e-5, "arrays not equal");
    }

    #[test]
    #[should_panic]
    fn assert_arrays_different_lens() {
        let x = [1.0, 2.0, 3.0];
        let y = [1.0, 2.0 + 1e-7];
        assert_nearly_equal_array(&x, &y, 1e-4, 1e-5, "arrays not equal");
    }

    #[test]
    fn any_is_nan() {
        let x: [f64; 2] = [0.0, 1.0];
        assert!(!is_any_nan(&x));

        let y: [f64; 3] = [0.0, std::f64::NAN, 1.0];
        assert!(is_any_nan(&y));

        let y: [f64; 3] = [0.0, std::f64::NEG_INFINITY, std::f64::INFINITY];
        assert!(!is_any_nan(&y));
    }

    #[test]
    #[should_panic]
    fn none_is_none_panic() {
        let y: [f64; 3] = [0.0, std::f64::NAN, 1.0];
        assert_none_is_nan(&y, "y");
    }

    #[test]
    fn assert_all_positive() {
        let y = [0.0, 1e-10, 1e-16];
        assert_all_ge(&y, 0., "y");
    }

    #[test]
    #[should_panic]
    fn assert_all_positive_panic() {
        let y = [0.0, 1e-10, -1e-12, 10.0];
        assert_all_ge(&y, 0., "y");
    }

    #[test]
    fn assert_all_le_one_f32() {
        let y = [0.0_f32, 1.0, 0.5, -100.0];
        assert_all_le(&y, 1.0, "y");
    }

    #[test]
    #[should_panic]
    fn assert_all_le_one_panic() {
        let y = [0.0, 1.0, 1.0 + 4e-16, -100.0];
        assert_all_le(&y, 1.0, "y");
    }

    #[test]
    fn finite_array() {
        let y = [0.0, 1.0, 2.0, -100.0];
        assert_is_finite_array(&y, "y");
    }

    #[test]
    #[should_panic]
    fn finite_array_panic() {
        let y = [0.0, 1.0, std::f64::INFINITY, -100.0];
        assert_is_finite_array(&y, "y");
    }

    #[test]
    #[should_panic]
    fn finite_array_panic_neg_inf() {
        let y = [0.0, 1.0, std::f64::NEG_INFINITY, -100.0];
        assert_is_finite_array(&y, "y");
    }

    #[test]
    #[should_panic]
    fn nan_is_not_finite() {
        let z: [f64; 3] = [0.0, std::f64::NAN, 1.0];
        assert_is_finite_array(&z, "z");
    }
}