Skip to main content

conspire/math/assert/
mod.rs

1mod eq;
2mod error;
3mod fd;
4mod fd_eq;
5
6#[cfg(test)]
7mod test;
8
9pub use self::{
10    eq::AssertEq, error::AssertionError, fd::FiniteDifference, fd::perturbation, fd_eq::AssertFd,
11};
12
13use self::eq::{non_negative_impl, zero_impl, zero_within_tols_impl};
14use crate::{
15    ABS_TOL, EPSILON, REL_TOL,
16    math::{Scalar, Tensor},
17};
18use std::fmt::Display;
19
20/// Specifies tolerances used by [`AssertEq`] functionalities.
21pub struct Assert {
22    pub abs_tol: Scalar,
23    pub rel_tol: Scalar,
24    pub fd_tol: Scalar,
25}
26
27impl Default for Assert {
28    fn default() -> Self {
29        Self {
30            abs_tol: ABS_TOL,
31            rel_tol: REL_TOL,
32            fd_tol: 3.0 * EPSILON,
33        }
34    }
35}
36
37impl Assert {
38    /// Asserts exact equality.
39    pub fn eq<T, Rhs>(a: T, b: Rhs) -> Result<(), AssertionError>
40    where
41        T: AssertEq<Rhs>,
42    {
43        T::eq(a, b)
44    }
45    /// Asserts equality within `self.abs_tol` and `self.rel_tol`.
46    pub fn eq_within_tols<T, Rhs>(&self, a: T, b: Rhs) -> Result<(), AssertionError>
47    where
48        T: AssertEq<Rhs>,
49    {
50        T::eq_within_tols(self, a, b)
51    }
52    /// Asserts finite-difference equality within `self.fd_tol`.
53    pub fn eq_within_fd_tol<T, Rhs>(&self, a: T, b: Rhs) -> Result<(), AssertionError>
54    where
55        T: AssertFd<Rhs>,
56    {
57        T::eq_within_fd_tol(self, a, b)
58    }
59    /// Asserts non-negativity.
60    pub fn non_negative<T>(a: &T) -> Result<(), AssertionError>
61    where
62        T: Default + Display + PartialOrd,
63    {
64        non_negative_impl(a)
65    }
66    /// Asserts exact equality with zero.
67    pub fn zero<T>(a: &T) -> Result<(), AssertionError>
68    where
69        T: Display + Tensor,
70    {
71        zero_impl(a)
72    }
73    /// Asserts equality with zero within `self.abs_tol` and `self.rel_tol`.
74    pub fn zero_within_tols<T>(&self, a: &T) -> Result<(), AssertionError>
75    where
76        T: Display + Tensor,
77    {
78        zero_within_tols_impl(self, a)
79    }
80}