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
//! Assertions for value and array

use ndarray::prelude::*;
use float_cmp::ApproxEqRatio;
use num_complex::Complex;

/// test two values are close in relative tolerance sense
pub trait AssertClose: Sized + Copy {
    type Tol;
    fn assert_close(self, truth: Self, rtol: Self::Tol);
}

macro_rules! impl_AssertClose {
    ($scalar:ty) => {
impl AssertClose for $scalar {
    type Tol = $scalar;
    fn assert_close(self, truth: Self, rtol: Self::Tol) {
        if !self.approx_eq_ratio(&truth, rtol) {
            panic!("Not close: val={}, truth={}, rtol={}", self, truth, rtol);
        }
    }
}
impl AssertClose for Complex<$scalar> {
    type Tol = $scalar;
    fn assert_close(self, truth: Self, rtol: Self::Tol) {
        if !(self.re.approx_eq_ratio(&truth.re, rtol) && self.im.approx_eq_ratio(&truth.im, rtol)) {
            panic!("Not close: val={}, truth={}, rtol={}", self, truth, rtol);
        }
    }
}
}} // impl_AssertClose
impl_AssertClose!(f64);
impl_AssertClose!(f32);

/// test two arrays are close
pub trait AssertAllClose {
    type Tol;
    /// test two arrays are close in L2-norm with relative tolerance
    fn assert_allclose_l2(&self, truth: &Self, rtol: Self::Tol);
    /// test two arrays are close in inf-norm with absolute tolerance
    fn assert_allclose_inf(&self, truth: &Self, atol: Self::Tol);
}

macro_rules! impl_AssertAllClose {
    ($scalar:ty, $float:ty, $abs:ident) => {
impl AssertAllClose for [$scalar]{
    type Tol = $float;
    fn assert_allclose_inf(&self, truth: &Self, atol: Self::Tol) {
        for (x, y) in self.iter().zip(truth.iter()) {
            let tol = (x - y).$abs();
            if tol > atol {
                panic!("Not close in inf-norm (atol={}): \ntest = \n{:?}\nTruth = \n{:?}",
                       atol, self, truth);
            }
        }
    }
    fn assert_allclose_l2(&self, truth: &Self, rtol: Self::Tol) {
        let nrm: Self::Tol = truth.iter().map(|x| x.$abs().powi(2)).sum();
        let dev: Self::Tol = self.iter().zip(truth.iter()).map(|(x, y)| (x-y).$abs().powi(2)).sum();
        if dev / nrm > rtol.powi(2) {
            panic!("Not close in L2-norm (rtol={}): \ntest = \n{:?}\nTruth = \n{:?}",
                   rtol, self, truth);
        }
    }
}

impl AssertAllClose for Vec<$scalar> {
    type Tol = $float;
    fn assert_allclose_inf(&self, truth: &Self, atol: Self::Tol) {
        self.as_slice().assert_allclose_inf(&truth, atol);
    }
    fn assert_allclose_l2(&self, truth: &Self, rtol: Self::Tol) {
        self.as_slice().assert_allclose_l2(&truth, rtol);
    }
}

impl<D:Dimension> AssertAllClose for Array<$scalar, D> {
    type Tol = $float;
    fn assert_allclose_inf(&self, truth: &Self, atol: Self::Tol) {
        self.as_slice().unwrap().assert_allclose_inf(
            truth.as_slice().unwrap(), atol);
    }
    fn assert_allclose_l2(&self, truth: &Self, rtol: Self::Tol) {
        self.as_slice().unwrap().assert_allclose_l2(
            truth.as_slice().unwrap(), rtol);
    }
}
}} // impl_AssertAllClose

impl_AssertAllClose!(f64, f64, abs);
impl_AssertAllClose!(f32, f32, abs);
impl_AssertAllClose!(Complex<f64>, f64, norm);
impl_AssertAllClose!(Complex<f32>, f32, norm);