From 514b1320cc3d102887f51d87df698308faebcce3 Mon Sep 17 00:00:00 2001 From: Stefan Kroboth Date: Sun, 21 Jan 2024 09:50:15 +0100 Subject: [PATCH 1/4] Run clippy for FileCheckpoint and Paramwriter --- .github/workflows/ci.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1fbffb461..a98f50c7b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -133,8 +133,12 @@ jobs: run: cargo clippy -p argmin --all-targets --all-features -- -D warnings - name: Clippy (argmin-observer-slog) run: cargo clippy -p argmin-observer-slog --all-targets --features "serde1" -- -D warnings + - name: Clippy (argmin-observer-paramwriter) + run: cargo clippy -p argmin-observer-paramwriter --all-targets -- -D warnings - name: Clippy (argmin-observer-spectator) run: cargo clippy -p argmin-observer-spectator --all-targets -- -D warnings + - name: Clippy (argmin-checkpointing-file) + run: cargo clippy -p argmin-checkpointing-file --all-targets -- -D warnings - name: Clippy (spectator) run: cargo clippy -p spectator --all-targets -- -D warnings From f2bfd80b7fc85a720d5b3d6d9b13adeb24046617 Mon Sep 17 00:00:00 2001 From: Stefan Kroboth Date: Sun, 21 Jan 2024 09:51:00 +0100 Subject: [PATCH 2/4] Use approx crate in ndarray backend tests whenever applicable --- .../ndarray_latest/src/lib.rs | 85 +++++++++++++++---- argmin-math/ndarray-tests-src/add.rs | 18 ++-- argmin-math/ndarray-tests-src/conj.rs | 16 ++-- argmin-math/ndarray-tests-src/div.rs | 17 ++-- argmin-math/ndarray-tests-src/dot.rs | 55 ++++++------ argmin-math/ndarray-tests-src/eye.rs | 11 ++- argmin-math/ndarray-tests-src/inv.rs | 11 +-- argmin-math/ndarray-tests-src/l1norm.rs | 9 +- argmin-math/ndarray-tests-src/l2norm.rs | 10 +-- argmin-math/ndarray-tests-src/minmax.rs | 12 +-- argmin-math/ndarray-tests-src/mul.rs | 56 ++++++------ argmin-math/ndarray-tests-src/random.rs | 4 +- argmin-math/ndarray-tests-src/scaledadd.rs | 11 ++- argmin-math/ndarray-tests-src/scaledsub.rs | 9 +- argmin-math/ndarray-tests-src/signum.rs | 3 +- argmin-math/ndarray-tests-src/sub.rs | 15 ++-- argmin-math/ndarray-tests-src/transpose.rs | 12 +-- argmin-math/ndarray-tests-src/zero.rs | 11 ++- 18 files changed, 199 insertions(+), 166 deletions(-) diff --git a/argmin-math/ndarray-linalg-tests/ndarray_latest/src/lib.rs b/argmin-math/ndarray-linalg-tests/ndarray_latest/src/lib.rs index afd01831f..1913e036c 100644 --- a/argmin-math/ndarray-linalg-tests/ndarray_latest/src/lib.rs +++ b/argmin-math/ndarray-linalg-tests/ndarray_latest/src/lib.rs @@ -1,51 +1,102 @@ mod add { - include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/add.rs")); + include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../../ndarray-tests-src/add.rs" + )); } mod conj { - include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/conj.rs")); + include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../../ndarray-tests-src/conj.rs" + )); } mod div { - include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/div.rs")); + include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../../ndarray-tests-src/div.rs" + )); } mod dot { - include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/dot.rs")); + include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../../ndarray-tests-src/dot.rs" + )); } mod eye { - include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/eye.rs")); + include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../../ndarray-tests-src/eye.rs" + )); } mod inv { - include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/inv.rs")); + include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../../ndarray-tests-src/inv.rs" + )); } mod l1norm { - include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/l1norm.rs")); + include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../../ndarray-tests-src/l1norm.rs" + )); } mod l2norm { - include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/l2norm.rs")); + include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../../ndarray-tests-src/l2norm.rs" + )); } mod minmax { - include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/minmax.rs")); + include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../../ndarray-tests-src/minmax.rs" + )); } mod mul { - include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/mul.rs")); + include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../../ndarray-tests-src/mul.rs" + )); } mod random { - include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/random.rs")); + include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../../ndarray-tests-src/random.rs" + )); } mod scaledadd { - include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/scaledadd.rs")); + include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../../ndarray-tests-src/scaledadd.rs" + )); } mod scaledsub { - include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/scaledsub.rs")); + include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../../ndarray-tests-src/scaledsub.rs" + )); } mod signum { - include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/signum.rs")); + include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../../ndarray-tests-src/signum.rs" + )); } mod sub { - include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/sub.rs")); + include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../../ndarray-tests-src/sub.rs" + )); } mod transpose { - include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/transpose.rs")); + include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../../ndarray-tests-src/transpose.rs" + )); } mod zero { - include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/zero.rs")); + include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../../ndarray-tests-src/zero.rs" + )); } diff --git a/argmin-math/ndarray-tests-src/add.rs b/argmin-math/ndarray-tests-src/add.rs index 7eaf215f1..67d410a47 100644 --- a/argmin-math/ndarray-tests-src/add.rs +++ b/argmin-math/ndarray-tests-src/add.rs @@ -5,15 +5,14 @@ // http://opensource.org/licenses/MIT>, at your option. This file may not be // copied, modified, or distributed except according to those terms. - #[cfg(test)] mod tests { #[allow(unused_imports)] use super::*; + use approx::assert_relative_eq; use argmin_math::ArgminAdd; - use ndarray::{Array1, Array2}; - use ndarray::array; + use ndarray::{Array1, Array2}; use paste::item; macro_rules! make_test { @@ -26,7 +25,7 @@ mod tests { let target = array![35 as $t, 38 as $t, 42 as $t]; let res = as ArgminAdd<$t, Array1<$t>>>::add(&a, &b); for i in 0..3 { - assert!(((target[i] - res[i]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -39,7 +38,7 @@ mod tests { let target = array![35 as $t, 38 as $t, 42 as $t]; let res = <$t as ArgminAdd, Array1<$t>>>::add(&b, &a); for i in 0..3 { - assert!(((target[i] - res[i]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -52,7 +51,7 @@ mod tests { let target = array![42 as $t, 42 as $t, 42 as $t]; let res = as ArgminAdd, Array1<$t>>>::add(&a, &b); for i in 0..3 { - assert!(((target[i] - res[i]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -105,7 +104,7 @@ mod tests { let res = as ArgminAdd, Array2<$t>>>::add(&a, &b); for i in 0..3 { for j in 0..2 { - assert!(((target[(j, i)] - res[(j, i)]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[(j, i)] as f64, res[(j, i)] as f64, epsilon = std::f64::EPSILON); } } } @@ -126,7 +125,7 @@ mod tests { let res = as ArgminAdd<$t, Array2<$t>>>::add(&a, &b); for i in 0..3 { for j in 0..2 { - assert!(((target[(j, i)] - res[(j, i)]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[(j, i)] as f64, res[(j, i)] as f64, epsilon = std::f64::EPSILON); } } } @@ -172,5 +171,4 @@ mod tests { make_test!(u64); make_test!(f32); make_test!(f64); - -} \ No newline at end of file +} diff --git a/argmin-math/ndarray-tests-src/conj.rs b/argmin-math/ndarray-tests-src/conj.rs index 377cecb1f..fd69fcd69 100644 --- a/argmin-math/ndarray-tests-src/conj.rs +++ b/argmin-math/ndarray-tests-src/conj.rs @@ -5,16 +5,15 @@ // http://opensource.org/licenses/MIT>, at your option. This file may not be // copied, modified, or distributed except according to those terms. - - #[cfg(test)] mod tests { #[allow(unused_imports)] use super::*; + use approx::assert_relative_eq; use argmin_math::ArgminConj; + use ndarray::array; use ndarray::{Array1, Array2}; use num_complex::Complex; - use ndarray::array; use paste::item; macro_rules! make_test { @@ -57,8 +56,8 @@ mod tests { let res = > as ArgminConj>::conj(&a); for i in 0..2 { for j in 0..3 { - assert!((b[(j, i)].re as f64 - res[(j, i)].re as f64).abs() < std::f64::EPSILON); - assert!((b[(j, i)].im as f64 - res[(j, i)].im as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(b[(j, i)].re as f64, res[(j, i)].re as f64, epsilon = std::f64::EPSILON); + assert_relative_eq!(b[(j, i)].im as f64, res[(j, i)].im as f64, epsilon = std::f64::EPSILON); } } } @@ -70,7 +69,7 @@ mod tests { let a = Array1::from(vec![1 as $t, 4 as $t, 8 as $t]); let res = as ArgminConj>::conj(&a); for i in 0..3 { - assert!((a[i] as f64 - res[i] as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(a[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -85,7 +84,7 @@ mod tests { let res = as ArgminConj>::conj(&a); for i in 0..3 { for j in 0..2 { - assert!((a[(j, i)] as f64 - res[(j, i)] as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(a[(j, i)] as f64, res[(j, i)] as f64, epsilon = std::f64::EPSILON); } } } @@ -100,5 +99,4 @@ mod tests { make_test!(i64); make_test!(f32); make_test!(f64); - -} \ No newline at end of file +} diff --git a/argmin-math/ndarray-tests-src/div.rs b/argmin-math/ndarray-tests-src/div.rs index 04a285b0c..973c1564e 100644 --- a/argmin-math/ndarray-tests-src/div.rs +++ b/argmin-math/ndarray-tests-src/div.rs @@ -5,16 +5,14 @@ // http://opensource.org/licenses/MIT>, at your option. This file may not be // copied, modified, or distributed except according to those terms. - - #[cfg(test)] mod tests { #[allow(unused_imports)] use super::*; + use approx::assert_relative_eq; use argmin_math::ArgminDiv; - use ndarray::{Array1, Array2}; - use ndarray::array; + use ndarray::{Array1, Array2}; use paste::item; macro_rules! make_test { @@ -27,7 +25,7 @@ mod tests { let target = array![2 as $t, 8 as $t, 4 as $t]; let res = as ArgminDiv<$t, Array1<$t>>>::div(&a, &b); for i in 0..3 { - assert!(((target[i] - res[i]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -40,7 +38,7 @@ mod tests { let target = array![16 as $t, 8 as $t, 4 as $t]; let res = <$t as ArgminDiv, Array1<$t>>>::div(&b, &a); for i in 0..3 { - assert!(((target[i] - res[i]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -53,7 +51,7 @@ mod tests { let target = array![2 as $t, 3 as $t, 2 as $t]; let res = as ArgminDiv, Array1<$t>>>::div(&a, &b); for i in 0..3 { - assert!(((target[i] - res[i]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -106,7 +104,7 @@ mod tests { let res = as ArgminDiv, Array2<$t>>>::div(&a, &b); for i in 0..3 { for j in 0..2 { - assert!(((target[(j, i)] - res[(j, i)]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[(j, i)] as f64, res[(j, i)] as f64, epsilon = std::f64::EPSILON); } } } @@ -152,5 +150,4 @@ mod tests { make_test!(u64); make_test!(f32); make_test!(f64); - -} \ No newline at end of file +} diff --git a/argmin-math/ndarray-tests-src/dot.rs b/argmin-math/ndarray-tests-src/dot.rs index 248fd2ac4..e823f67fe 100644 --- a/argmin-math/ndarray-tests-src/dot.rs +++ b/argmin-math/ndarray-tests-src/dot.rs @@ -5,15 +5,15 @@ // http://opensource.org/licenses/MIT>, at your option. This file may not be // copied, modified, or distributed except according to those terms. - #[cfg(test)] mod tests { #[allow(unused_imports)] use super::*; + use approx::assert_relative_eq; use argmin_math::ArgminDot; + use ndarray::array; use ndarray::{Array1, Array2}; use num_complex::Complex; - use ndarray::array; use paste::item; macro_rules! make_test { @@ -24,7 +24,7 @@ mod tests { let a = array![1 as $t, 2 as $t, 3 as $t]; let b = array![4 as $t, 5 as $t, 6 as $t]; let res: $t = as ArgminDot, $t>>::dot(&a, &b); - assert!((((res - 32 as $t) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res as f64, 32 as f64, epsilon = std::f64::EPSILON); } } @@ -37,7 +37,7 @@ mod tests { as ArgminDot<$t, Array1<$t>>>::dot(&a, &b); let res = array![2 as $t, 4 as $t, 6 as $t]; for i in 0..3 { - assert!((((res[i] - product[i]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[i] as f64, product[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -51,7 +51,7 @@ mod tests { <$t as ArgminDot, Array1<$t>>>::dot(&b, &a); let res = array![2 as $t, 4 as $t, 6 as $t]; for i in 0..3 { - assert!((((res[i] - product[i]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[i] as f64, product[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -70,7 +70,7 @@ mod tests { as ArgminDot, Array2<$t>>>::dot(&a, &b); for i in 0..3 { for j in 0..3 { - assert!((((res[(i, j)] - product[(i, j)]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[(i, j)] as f64, product[(i, j)] as f64, epsilon = std::f64::EPSILON); } } } @@ -89,7 +89,7 @@ mod tests { let product: Array1<$t> = as ArgminDot, Array1<$t>>>::dot(&a, &b); for i in 0..3 { - assert!((((res[i] - product[i]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[i] as f64, product[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -116,7 +116,7 @@ mod tests { as ArgminDot, Array2<$t>>>::dot(&a, &b); for i in 0..3 { for j in 0..3 { - assert!((((res[(i, j)] - product[(i, j)]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[(i, j)] as f64, product[(i, j)] as f64, epsilon = std::f64::EPSILON); } } } @@ -139,7 +139,7 @@ mod tests { as ArgminDot<$t, Array2<$t>>>::dot(&a, &(2 as $t)); for i in 0..3 { for j in 0..3 { - assert!((((res[(i, j)] - product[(i, j)]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[(i, j)] as f64, product[(i, j)] as f64, epsilon = std::f64::EPSILON); } } } @@ -162,7 +162,7 @@ mod tests { <$t as ArgminDot, Array2<$t>>>::dot(&(2 as $t), &a); for i in 0..3 { for j in 0..3 { - assert!((((res[(i, j)] - product[(i, j)]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[(i, j)] as f64, product[(i, j)] as f64, epsilon = std::f64::EPSILON); } } } @@ -183,8 +183,8 @@ mod tests { ]; let res: Complex<$t> = > as ArgminDot>, Complex<$t>>>::dot(&a, &b); let target = a[0]*b[0] + a[1]*b[1] + a[2]*b[2]; - assert!((((res - target).re as f64).abs()) < std::f64::EPSILON); - assert!((((res - target).im as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res.re as f64, target.re as f64, epsilon = std::f64::EPSILON); + assert_relative_eq!(res.im as f64, target.im as f64, epsilon = std::f64::EPSILON); } } @@ -201,8 +201,8 @@ mod tests { > as ArgminDot, Array1>>>::dot(&a, &b); let res = array![a[0]*b, a[1]*b, a[2]*b]; for i in 0..3 { - assert!(((res[i].re as f64 - product[i].re as f64).abs()) < std::f64::EPSILON); - assert!(((res[i].im as f64 - product[i].im as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[i].re as f64, product[i].re as f64, epsilon = std::f64::EPSILON); + assert_relative_eq!(res[i].im as f64, product[i].im as f64, epsilon = std::f64::EPSILON); } } } @@ -220,8 +220,8 @@ mod tests { as ArgminDot>, Array1>>>::dot(&b, &a); let res = array![a[0]*b, a[1]*b, a[2]*b]; for i in 0..3 { - assert!(((res[i].re as f64 - product[i].re as f64).abs()) < std::f64::EPSILON); - assert!(((res[i].im as f64 - product[i].im as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[i].re as f64, product[i].re as f64, epsilon = std::f64::EPSILON); + assert_relative_eq!(res[i].im as f64, product[i].im as f64, epsilon = std::f64::EPSILON); } } } @@ -245,8 +245,8 @@ mod tests { > as ArgminDot>, Array2>>>::dot(&a, &b); for i in 0..2 { for j in 0..2 { - assert!(((res[(i, j)].re as f64 - product[(i, j)].re as f64).abs()) < std::f64::EPSILON); - assert!(((res[(i, j)].im as f64 - product[(i, j)].im as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[(i, j)].re as f64, product[(i, j)].re as f64, epsilon = std::f64::EPSILON); + assert_relative_eq!(res[(i, j)].im as f64, product[(i, j)].im as f64, epsilon = std::f64::EPSILON); } } } @@ -270,8 +270,8 @@ mod tests { let product: Array1> = > as ArgminDot>, Array1>>>::dot(&a, &b); for i in 0..2 { - assert!(((res[i].re as f64 - product[i].re as f64).abs()) < std::f64::EPSILON); - assert!(((res[i].im as f64 - product[i].im as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[i].re as f64, product[i].re as f64, epsilon = std::f64::EPSILON); + assert_relative_eq!(res[i].im as f64, product[i].im as f64, epsilon = std::f64::EPSILON); } } } @@ -301,8 +301,8 @@ mod tests { > as ArgminDot>, Array2>>>::dot(&a, &b); for i in 0..2 { for j in 0..2 { - assert!(((res[(i, j)].re as f64 - product[(i, j)].re as f64).abs()) < std::f64::EPSILON); - assert!(((res[(i, j)].im as f64 - product[(i, j)].im as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[(i, j)].re as f64, product[(i, j)].re as f64, epsilon = std::f64::EPSILON); + assert_relative_eq!(res[(i, j)].im as f64, product[(i, j)].im as f64, epsilon = std::f64::EPSILON); } } } @@ -324,8 +324,8 @@ mod tests { > as ArgminDot, Array2>>>::dot(&a, &b); for i in 0..2 { for j in 0..2 { - assert!(((res[(i, j)].re as f64 - product[(i, j)].re as f64).abs()) < std::f64::EPSILON); - assert!(((res[(i, j)].im as f64 - product[(i, j)].im as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[(i, j)].re as f64, product[(i, j)].re as f64, epsilon = std::f64::EPSILON); + assert_relative_eq!(res[(i, j)].im as f64, product[(i, j)].im as f64, epsilon = std::f64::EPSILON); } } } @@ -347,8 +347,8 @@ mod tests { as ArgminDot>, Array2>>>::dot(&b, &a); for i in 0..2 { for j in 0..2 { - assert!(((res[(i, j)].re as f64 - product[(i, j)].re as f64).abs()) < std::f64::EPSILON); - assert!(((res[(i, j)].im as f64 - product[(i, j)].im as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[(i, j)].re as f64, product[(i, j)].re as f64, epsilon = std::f64::EPSILON); + assert_relative_eq!(res[(i, j)].im as f64, product[(i, j)].im as f64, epsilon = std::f64::EPSILON); } } } @@ -368,5 +368,4 @@ mod tests { make_test!(usize); make_test!(f32); make_test!(f64); - -} \ No newline at end of file +} diff --git a/argmin-math/ndarray-tests-src/eye.rs b/argmin-math/ndarray-tests-src/eye.rs index 59a3843df..3b05c7bdb 100644 --- a/argmin-math/ndarray-tests-src/eye.rs +++ b/argmin-math/ndarray-tests-src/eye.rs @@ -5,14 +5,14 @@ // http://opensource.org/licenses/MIT>, at your option. This file may not be // copied, modified, or distributed except according to those terms. - #[cfg(test)] mod tests { #[allow(unused_imports)] use super::*; + use approx::assert_relative_eq; use argmin_math::ArgminEye; + use ndarray::array; use ndarray::Array2; - use ndarray::array; use paste::item; macro_rules! make_test { @@ -28,7 +28,7 @@ mod tests { ]; for i in 0..3 { for j in 0..3 { - assert!((((res[(i, j)] - e[(i, j)]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[(i, j)] as f64, e[(i, j)] as f64, epsilon = std::f64::EPSILON); } } } @@ -50,7 +50,7 @@ mod tests { ]; for i in 0..3 { for j in 0..3 { - assert!((((res[(i, j)] - e[(i, j)]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[(i, j)] as f64, e[(i, j)] as f64, epsilon = std::f64::EPSILON); } } } @@ -83,5 +83,4 @@ mod tests { make_test!(u64); make_test!(f32); make_test!(f64); - -} \ No newline at end of file +} diff --git a/argmin-math/ndarray-tests-src/inv.rs b/argmin-math/ndarray-tests-src/inv.rs index e98749e42..7b492c36b 100644 --- a/argmin-math/ndarray-tests-src/inv.rs +++ b/argmin-math/ndarray-tests-src/inv.rs @@ -9,12 +9,10 @@ mod tests { #[allow(unused_imports)] use super::*; + use approx::assert_relative_eq; use argmin_math::ArgminInv; - use ndarray::array; use ndarray::Array2; - - use paste::item; macro_rules! make_test { @@ -33,10 +31,7 @@ mod tests { let res = as ArgminInv>>::inv(&a).unwrap(); for i in 0..2 { for j in 0..2 { - // TODO: before ndarray 0.14 / ndarray-linalg 0.13, comparison with - // EPSILON worked, now errors are larger (and dependent on the BLAS - // backend) - assert!((((res[(i, j)] - target[(i, j)]) as f64).abs()) < 0.000001); + assert_relative_eq!(res[(i, j)], target[(i, j)], epsilon = std::$t::EPSILON.sqrt()); } } } @@ -48,7 +43,7 @@ mod tests { let a = 2.0; let target = 0.5; let res = <$t as ArgminInv<$t>>::inv(&a).unwrap(); - assert!(((res - target) as f64).abs() < 0.000001); + assert_relative_eq!(res as f64, target as f64, epsilon = std::f64::EPSILON); } } }; diff --git a/argmin-math/ndarray-tests-src/l1norm.rs b/argmin-math/ndarray-tests-src/l1norm.rs index 9abd762be..512045e40 100644 --- a/argmin-math/ndarray-tests-src/l1norm.rs +++ b/argmin-math/ndarray-tests-src/l1norm.rs @@ -9,6 +9,7 @@ mod tests { #[allow(unused_imports)] use super::*; + use approx::assert_relative_eq; use argmin_math::ArgminL1Norm; use ndarray::{array, Array1}; use num_complex::Complex; @@ -22,7 +23,7 @@ mod tests { let a = array![4 as $t, 3 as $t]; let res = as ArgminL1Norm<$t>>::l1_norm(&a); let target = 7 as $t; - assert!(((target - res) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target as f64, res as f64, epsilon = std::f64::EPSILON); } } @@ -32,7 +33,7 @@ mod tests { let a = array![Complex::new(4 as $t, 2 as $t), Complex::new(3 as $t, 4 as $t)]; let res = > as ArgminL1Norm<$t>>::l1_norm(&a); let target = a[0].l1_norm() + a[1].l1_norm(); - assert!(((target - res) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target as f64, res as f64, epsilon = std::f64::EPSILON); } } }; @@ -46,7 +47,7 @@ mod tests { let a = array![-4 as $t, -3 as $t]; let res = as ArgminL1Norm<$t>>::l1_norm(&a); let target = 7 as $t; - assert!(((target - res) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target as f64, res as f64, epsilon = std::f64::EPSILON); } } @@ -56,7 +57,7 @@ mod tests { let a = array![Complex::new(-4 as $t, -2 as $t), Complex::new(-3 as $t, -4 as $t)]; let res = > as ArgminL1Norm<$t>>::l1_norm(&a); let target = a[0].l1_norm() + a[1].l1_norm(); - assert!(((target - res) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target as f64, res as f64, epsilon = std::f64::EPSILON); } } }; diff --git a/argmin-math/ndarray-tests-src/l2norm.rs b/argmin-math/ndarray-tests-src/l2norm.rs index 7fe8914dc..c933fdf85 100644 --- a/argmin-math/ndarray-tests-src/l2norm.rs +++ b/argmin-math/ndarray-tests-src/l2norm.rs @@ -9,8 +9,8 @@ mod tests { #[allow(unused_imports)] use super::*; + use approx::assert_relative_eq; use argmin_math::ArgminL2Norm; - use ndarray::{array, Array1}; use num_complex::Complex; use num_integer::Roots; @@ -24,7 +24,7 @@ mod tests { let a = array![4 as $t, 3 as $t]; let res = as ArgminL2Norm<$t>>::l2_norm(&a); let target = 5 as $t; - assert!(((target - res) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target as f64, res as f64, epsilon = std::f64::EPSILON); } } @@ -34,7 +34,7 @@ mod tests { let a = array![Complex::new(4 as $t, 2 as $t), Complex::new(3 as $t, 4 as $t)]; let res = > as ArgminL2Norm<$t>>::l2_norm(&a); let target = (a[0].norm_sqr() + a[1].norm_sqr()).sqrt(); - assert!(((target - res) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target as f64, res as f64, epsilon = std::f64::EPSILON); } } }; @@ -48,7 +48,7 @@ mod tests { let a = array![-4 as $t, -3 as $t]; let res = as ArgminL2Norm<$t>>::l2_norm(&a); let target = 5 as $t; - assert!(((target - res) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target as f64, res as f64, epsilon = std::f64::EPSILON); } } @@ -58,7 +58,7 @@ mod tests { let a = array![Complex::new(-4 as $t, -2 as $t), Complex::new(-3 as $t, -4 as $t)]; let res = > as ArgminL2Norm<$t>>::l2_norm(&a); let target = (a[0].norm_sqr() + a[1].norm_sqr()).sqrt(); - assert!(((target - res) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target as f64, res as f64, epsilon = std::f64::EPSILON); } } }; diff --git a/argmin-math/ndarray-tests-src/minmax.rs b/argmin-math/ndarray-tests-src/minmax.rs index f615efa03..f2a8bd13f 100644 --- a/argmin-math/ndarray-tests-src/minmax.rs +++ b/argmin-math/ndarray-tests-src/minmax.rs @@ -5,14 +5,14 @@ // http://opensource.org/licenses/MIT>, at your option. This file may not be // copied, modified, or distributed except according to those terms. - #[cfg(test)] mod tests { #[allow(unused_imports)] use super::*; + use approx::assert_relative_eq; use argmin_math::ArgminMinMax; + use ndarray::array; use ndarray::{Array1, Array2}; - use ndarray::array; use paste::item; macro_rules! make_test { @@ -27,8 +27,8 @@ mod tests { let res_max = as ArgminMinMax>::max(&a, &b); let res_min = as ArgminMinMax>::min(&a, &b); for i in 0..3 { - assert!(((target_max[i] - res_max[i]) as f64).abs() < std::f64::EPSILON); - assert!(((target_min[i] - res_min[i]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target_max[i] as f64, res_max[i] as f64, epsilon = std::f64::EPSILON); + assert_relative_eq!(target_min[i] as f64, res_min[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -56,8 +56,8 @@ mod tests { let res_min = as ArgminMinMax>::min(&a, &b); for i in 0..3 { for j in 0..2 { - assert!(((target_max[(j, i)] - res_max[(j, i)]) as f64).abs() < std::f64::EPSILON); - assert!(((target_min[(j, i)] - res_min[(j, i)]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target_max[(j, i)] as f64, res_max[(j, i)] as f64, epsilon = std::f64::EPSILON); + assert_relative_eq!(target_min[(j, i)] as f64, res_min[(j, i)] as f64, epsilon = std::f64::EPSILON); } } } diff --git a/argmin-math/ndarray-tests-src/mul.rs b/argmin-math/ndarray-tests-src/mul.rs index 6149f4f72..20aefd042 100644 --- a/argmin-math/ndarray-tests-src/mul.rs +++ b/argmin-math/ndarray-tests-src/mul.rs @@ -5,15 +5,15 @@ // http://opensource.org/licenses/MIT>, at your option. This file may not be // copied, modified, or distributed except according to those terms. - #[cfg(test)] mod tests { #[allow(unused_imports)] use super::*; + use approx::assert_relative_eq; use argmin_math::ArgminMul; + use ndarray::array; use ndarray::{Array1, Array2}; use num_complex::Complex; - use ndarray::array; use paste::item; macro_rules! make_test { @@ -26,7 +26,7 @@ mod tests { let target = array![2 as $t, 8 as $t, 16 as $t]; let res = as ArgminMul<$t, Array1<$t>>>::mul(&a, &b); for i in 0..3 { - assert!(((target[i] - res[i]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -42,8 +42,8 @@ mod tests { let target = array![a[0] * b, a[1] * b]; let res = > as ArgminMul, Array1>>>::mul(&a, &b); for i in 0..2 { - assert!((target[i].re as f64 - res[i].re as f64).abs() < std::f64::EPSILON); - assert!((target[i].im as f64 - res[i].im as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[i].re as f64, res[i].re as f64, epsilon = std::f64::EPSILON); + assert_relative_eq!(target[i].im as f64, res[i].im as f64, epsilon = std::f64::EPSILON); } } } @@ -59,8 +59,8 @@ mod tests { let target = array![a[0] * b, a[1] * b]; let res = > as ArgminMul<$t, Array1>>>::mul(&a, &b); for i in 0..2 { - assert!((target[i].re as f64 - res[i].re as f64).abs() < std::f64::EPSILON); - assert!((target[i].im as f64 - res[i].im as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[i].re as f64, res[i].re as f64, epsilon = std::f64::EPSILON); + assert_relative_eq!(target[i].im as f64, res[i].im as f64, epsilon = std::f64::EPSILON); } } } @@ -73,7 +73,7 @@ mod tests { let target = array![2 as $t, 8 as $t, 16 as $t]; let res = <$t as ArgminMul, Array1<$t>>>::mul(&b, &a); for i in 0..3 { - assert!(((target[i] - res[i]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -89,8 +89,8 @@ mod tests { let target = array![a[0] * b, a[1] * b]; let res = as ArgminMul>, Array1>>>::mul(&b, &a); for i in 0..2 { - assert!((target[i].re as f64 - res[i].re as f64).abs() < std::f64::EPSILON); - assert!((target[i].im as f64 - res[i].im as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[i].re as f64, res[i].re as f64, epsilon = std::f64::EPSILON); + assert_relative_eq!(target[i].im as f64, res[i].im as f64, epsilon = std::f64::EPSILON); } } } @@ -106,8 +106,8 @@ mod tests { let target = array![a[0] * b, a[1] * b]; let res = <$t as ArgminMul>, Array1>>>::mul(&b, &a); for i in 0..2 { - assert!((target[i].re as f64 - res[i].re as f64).abs() < std::f64::EPSILON); - assert!((target[i].im as f64 - res[i].im as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[i].re as f64, res[i].re as f64, epsilon = std::f64::EPSILON); + assert_relative_eq!(target[i].im as f64, res[i].im as f64, epsilon = std::f64::EPSILON); } } } @@ -120,7 +120,7 @@ mod tests { let target = array![2 as $t, 12 as $t, 32 as $t]; let res = as ArgminMul, Array1<$t>>>::mul(&a, &b); for i in 0..3 { - assert!(((target[i] - res[i]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -139,8 +139,8 @@ mod tests { let target = array![a[0]*b[0], a[1]*b[1]]; let res = > as ArgminMul>, Array1>>>::mul(&a, &b); for i in 0..2 { - assert!((target[i].re as f64 - res[i].re as f64).abs() < std::f64::EPSILON); - assert!((target[i].im as f64 - res[i].im as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[i].re as f64, res[i].re as f64, epsilon = std::f64::EPSILON); + assert_relative_eq!(target[i].im as f64, res[i].im as f64, epsilon = std::f64::EPSILON); } } } @@ -193,7 +193,7 @@ mod tests { let res = as ArgminMul, Array2<$t>>>::mul(&a, &b); for i in 0..3 { for j in 0..2 { - assert!(((target[(j, i)] - res[(j, i)]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[(j, i)] as f64, res[(j, i)] as f64, epsilon = std::f64::EPSILON); } } } @@ -220,8 +220,8 @@ mod tests { let res = > as ArgminMul>, Array2>>>::mul(&a, &b); for i in 0..2 { for j in 0..3 { - assert!((target[(j, i)].re as f64 - res[(j, i)].re as f64).abs() < std::f64::EPSILON); - assert!((target[(j, i)].im as f64 - res[(j, i)].im as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[(j, i)].re as f64, res[(j, i)].re as f64, epsilon = std::f64::EPSILON); + assert_relative_eq!(target[(j, i)].im as f64, res[(j, i)].im as f64, epsilon = std::f64::EPSILON); } } } @@ -270,7 +270,7 @@ mod tests { let res = as ArgminMul<$t, Array2<$t>>>::mul(&a, &b); for i in 0..3 { for j in 0..2 { - assert!(((target[(j, i)] - res[(j, i)]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[(j, i)] as f64, res[(j, i)] as f64, epsilon = std::f64::EPSILON); } } } @@ -293,8 +293,8 @@ mod tests { let res = > as ArgminMul, Array2>>>::mul(&a, &b); for i in 0..2 { for j in 0..3 { - assert!((target[(j, i)].re as f64 - res[(j, i)].re as f64).abs() < std::f64::EPSILON); - assert!((target[(j, i)].im as f64 - res[(j, i)].im as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[(j, i)].re as f64, res[(j, i)].re as f64, epsilon = std::f64::EPSILON); + assert_relative_eq!(target[(j, i)].im as f64, res[(j, i)].im as f64, epsilon = std::f64::EPSILON); } } } @@ -317,8 +317,8 @@ mod tests { let res = > as ArgminMul<$t, Array2>>>::mul(&a, &b); for i in 0..2 { for j in 0..3 { - assert!((target[(j, i)].re as f64 - res[(j, i)].re as f64).abs() < std::f64::EPSILON); - assert!((target[(j, i)].im as f64 - res[(j, i)].im as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[(j, i)].re as f64, res[(j, i)].re as f64, epsilon = std::f64::EPSILON); + assert_relative_eq!(target[(j, i)].im as f64, res[(j, i)].im as f64, epsilon = std::f64::EPSILON); } } } @@ -339,7 +339,7 @@ mod tests { let res = <$t as ArgminMul, Array2<$t>>>::mul(&a, &b); for i in 0..3 { for j in 0..2 { - assert!(((target[(j, i)] - res[(j, i)]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[(j, i)] as f64, res[(j, i)] as f64, epsilon = std::f64::EPSILON); } } } @@ -362,8 +362,8 @@ mod tests { let res = as ArgminMul>, Array2>>>::mul(&b, &a); for i in 0..2 { for j in 0..3 { - assert!((target[(j, i)].re as f64 - res[(j, i)].re as f64).abs() < std::f64::EPSILON); - assert!((target[(j, i)].im as f64 - res[(j, i)].im as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[(j, i)].re as f64, res[(j, i)].re as f64, epsilon = std::f64::EPSILON); + assert_relative_eq!(target[(j, i)].im as f64, res[(j, i)].im as f64, epsilon = std::f64::EPSILON); } } } @@ -386,8 +386,8 @@ mod tests { let res = <$t as ArgminMul>, Array2>>>::mul(&b, &a); for i in 0..2 { for j in 0..3 { - assert!((target[(j, i)].re as f64 - res[(j, i)].re as f64).abs() < std::f64::EPSILON); - assert!((target[(j, i)].im as f64 - res[(j, i)].im as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[(j, i)].re as f64, res[(j, i)].re as f64, epsilon = std::f64::EPSILON); + assert_relative_eq!(target[(j, i)].im as f64, res[(j, i)].im as f64, epsilon = std::f64::EPSILON); } } } diff --git a/argmin-math/ndarray-tests-src/random.rs b/argmin-math/ndarray-tests-src/random.rs index 24a2ba969..a45623dc1 100644 --- a/argmin-math/ndarray-tests-src/random.rs +++ b/argmin-math/ndarray-tests-src/random.rs @@ -5,16 +5,14 @@ // http://opensource.org/licenses/MIT>, at your option. This file may not be // copied, modified, or distributed except according to those terms. - #[cfg(test)] mod tests { #[allow(unused_imports)] use super::*; - use argmin_math::ArgminRandom; use ndarray::{array, Array1, Array2}; - use rand::SeedableRng; use paste::item; + use rand::SeedableRng; macro_rules! make_test { ($t:ty) => { diff --git a/argmin-math/ndarray-tests-src/scaledadd.rs b/argmin-math/ndarray-tests-src/scaledadd.rs index 920ad5e87..003dc139e 100644 --- a/argmin-math/ndarray-tests-src/scaledadd.rs +++ b/argmin-math/ndarray-tests-src/scaledadd.rs @@ -9,6 +9,7 @@ mod tests { #[allow(unused_imports)] use super::*; + use approx::assert_relative_eq; use argmin_math::ArgminScaledAdd; use ndarray::{array, Array1, Array2}; use paste::item; @@ -24,7 +25,7 @@ mod tests { let res = as ArgminScaledAdd, $t, Array1<$t>>>::scaled_add(&a, &b, &c); let target = array![9 as $t, 12 as $t, 15 as $t]; for i in 0..3 { - assert!((((res[i] - target[i]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[i] as f64, target[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -60,7 +61,7 @@ mod tests { let res = as ArgminScaledAdd, Array1<$t>, Array1<$t>>>::scaled_add(&a, &b, &c); let target = array![13 as $t, 12 as $t, 9 as $t]; for i in 0..3 { - assert!((((res[i] - target[i]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[i] as f64, target[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -120,7 +121,7 @@ mod tests { ]; for i in 0..2 { for j in 0..2 { - assert!((((res[(i, j)] - target[(i, j)]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[(i, j)] as f64, target[(i, j)] as f64, epsilon = std::f64::EPSILON); } } } @@ -145,7 +146,7 @@ mod tests { ]; for i in 0..2 { for j in 0..2 { - assert!((((res[(i, j)] - target[(i, j)]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[(i, j)] as f64, target[(i, j)] as f64, epsilon = std::f64::EPSILON); } } } @@ -153,8 +154,6 @@ mod tests { }; } - // make_test!(isize); - // make_test!(usize); make_test!(i8); make_test!(u8); make_test!(i16); diff --git a/argmin-math/ndarray-tests-src/scaledsub.rs b/argmin-math/ndarray-tests-src/scaledsub.rs index 5c2b42298..9b0eb01f6 100644 --- a/argmin-math/ndarray-tests-src/scaledsub.rs +++ b/argmin-math/ndarray-tests-src/scaledsub.rs @@ -9,6 +9,7 @@ mod tests { #[allow(unused_imports)] use super::*; + use approx::assert_relative_eq; use argmin_math::ArgminScaledSub; use ndarray::{array, Array1, Array2}; use paste::item; @@ -24,7 +25,7 @@ mod tests { let res = as ArgminScaledSub, $t, Array1<$t>>>::scaled_sub(&a, &b, &c); let target = array![2 as $t, 10 as $t, 18 as $t]; for i in 0..3 { - assert!((((res[i] - target[i]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[i] as f64, target[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -60,7 +61,7 @@ mod tests { let res = as ArgminScaledSub, Array1<$t>, Array1<$t>>>::scaled_sub(&a, &b, &c); let target = array![8 as $t, 10 as $t, 24 as $t]; for i in 0..3 { - assert!((((res[i] - target[i]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[i] as f64, target[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -120,7 +121,7 @@ mod tests { ]; for i in 0..2 { for j in 0..2 { - assert!((((res[(i, j)] - target[(i, j)]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[(i, j)] as f64, target[(i, j)] as f64, epsilon = std::f64::EPSILON); } } } @@ -145,7 +146,7 @@ mod tests { ]; for i in 0..2 { for j in 0..2 { - assert!((((res[(i, j)] - target[(i, j)]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[(i, j)] as f64, target[(i, j)] as f64, epsilon = std::f64::EPSILON); } } } diff --git a/argmin-math/ndarray-tests-src/signum.rs b/argmin-math/ndarray-tests-src/signum.rs index f19198aa1..43c884409 100644 --- a/argmin-math/ndarray-tests-src/signum.rs +++ b/argmin-math/ndarray-tests-src/signum.rs @@ -5,15 +5,14 @@ // http://opensource.org/licenses/MIT>, at your option. This file may not be // copied, modified, or distributed except according to those terms. - #[cfg(test)] mod tests { #[allow(unused_imports)] use super::*; use argmin_math::ArgminSignum; + use ndarray::array; use ndarray::{Array1, Array2}; use num_complex::Complex; - use ndarray::array; use paste::item; macro_rules! make_test { diff --git a/argmin-math/ndarray-tests-src/sub.rs b/argmin-math/ndarray-tests-src/sub.rs index 903e94d02..46d7e2bdc 100644 --- a/argmin-math/ndarray-tests-src/sub.rs +++ b/argmin-math/ndarray-tests-src/sub.rs @@ -5,15 +5,14 @@ // http://opensource.org/licenses/MIT>, at your option. This file may not be // copied, modified, or distributed except according to those terms. - #[cfg(test)] mod tests { #[allow(unused_imports)] use super::*; + use approx::assert_relative_eq; use argmin_math::ArgminSub; + use ndarray::array; use ndarray::{Array1, Array2}; - - use ndarray::array; use paste::item; macro_rules! make_test { @@ -26,7 +25,7 @@ mod tests { let target = array![35 as $t, 38 as $t, 42 as $t]; let res = as ArgminSub<$t, Array1<$t>>>::sub(&a, &b); for i in 0..3 { - assert!(((target[i] - res[i]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -39,7 +38,7 @@ mod tests { let target = array![33 as $t, 30 as $t, 26 as $t]; let res = <$t as ArgminSub, Array1<$t>>>::sub(&b, &a); for i in 0..3 { - assert!(((target[i] - res[i]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -52,7 +51,7 @@ mod tests { let target = array![40 as $t, 34 as $t, 26 as $t]; let res = as ArgminSub, Array1<$t>>>::sub(&a, &b); for i in 0..3 { - assert!(((target[i] - res[i]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -105,7 +104,7 @@ mod tests { let res = as ArgminSub, Array2<$t>>>::sub(&a, &b); for i in 0..3 { for j in 0..2 { - assert!(((target[(j, i)] - res[(j, i)]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[(j, i)] as f64, res[(j, i)] as f64, epsilon = std::f64::EPSILON); } } } @@ -126,7 +125,7 @@ mod tests { let res = as ArgminSub<$t, Array2<$t>>>::sub(&a, &b); for i in 0..3 { for j in 0..2 { - assert!(((target[(j, i)] - res[(j, i)]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[(j, i)] as f64, res[(j, i)] as f64, epsilon = std::f64::EPSILON); } } } diff --git a/argmin-math/ndarray-tests-src/transpose.rs b/argmin-math/ndarray-tests-src/transpose.rs index 564cee5c6..140e0215f 100644 --- a/argmin-math/ndarray-tests-src/transpose.rs +++ b/argmin-math/ndarray-tests-src/transpose.rs @@ -8,15 +8,15 @@ // Note: This is not really the preferred way I think. Maybe this should also be implemented for // ArrayViews, which would probably make it more efficient. - #[cfg(test)] mod tests { #[allow(unused_imports)] use super::*; + use approx::assert_relative_eq; use argmin_math::ArgminTranspose; use ndarray::{Array1, Array2}; - - use ndarray::array; + + use ndarray::array; use paste::item; macro_rules! make_test { @@ -29,7 +29,7 @@ mod tests { let target = array![1 as $t, 4 as $t]; let res = as ArgminTranspose>>::t(a); for i in 0..2 { - assert!(((target[i] - res[i]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -48,7 +48,7 @@ mod tests { let res = as ArgminTranspose>>::t(a); for i in 0..2 { for j in 0..2 { - assert!(((target[(i, j)] - res[(i, j)]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[(i, j)] as f64, res[(i, j)] as f64, epsilon = std::f64::EPSILON); } } } @@ -69,7 +69,7 @@ mod tests { let res = as ArgminTranspose>>::t(a); for i in 0..2 { for j in 0..3 { - assert!(((target[(i, j)] - res[(i, j)]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[(i, j)] as f64, res[(i, j)] as f64, epsilon = std::f64::EPSILON); } } } diff --git a/argmin-math/ndarray-tests-src/zero.rs b/argmin-math/ndarray-tests-src/zero.rs index 94a10db4e..4ab13aa27 100644 --- a/argmin-math/ndarray-tests-src/zero.rs +++ b/argmin-math/ndarray-tests-src/zero.rs @@ -5,14 +5,13 @@ // http://opensource.org/licenses/MIT>, at your option. This file may not be // copied, modified, or distributed except according to those terms. - #[cfg(test)] mod tests { #[allow(unused_imports)] use super::*; - use argmin_math::{ArgminZeroLike}; - - use ndarray::{array, Array1, Array2}; + use approx::assert_relative_eq; + use argmin_math::ArgminZeroLike; + use ndarray::{array, Array1, Array2}; use paste::item; macro_rules! make_test { @@ -40,7 +39,7 @@ mod tests { fn []() { let a = (array![42 as $t, 42 as $t, 42 as $t, 42 as $t]).zero_like(); for i in 0..4 { - assert!(((0 as $t - a[i]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(0.0 as f64, a[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -69,7 +68,7 @@ mod tests { let a = (array![[42 as $t, 42 as $t], [42 as $t, 42 as $t]]).zero_like(); for i in 0..2 { for j in 0..2 { - assert!(((0 as $t - a[(i, j)]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(0.0 as f64, a[(i, j)] as f64, epsilon = std::f64::EPSILON); } } } From cf6a459b3a19f36542e34bf0f07dfc7517616d61 Mon Sep 17 00:00:00 2001 From: Stefan Kroboth Date: Sun, 21 Jan 2024 10:43:35 +0100 Subject: [PATCH 3/4] Use approx crate in nalgebra backend tests whenever applicable --- argmin-math/ndarray-tests-src/minmax.rs | 2 -- argmin-math/src/nalgebra_m/add.rs | 11 ++++++----- argmin-math/src/nalgebra_m/conj.rs | 9 ++++----- argmin-math/src/nalgebra_m/div.rs | 9 +++++---- argmin-math/src/nalgebra_m/dot.rs | 17 +++++++++-------- argmin-math/src/nalgebra_m/eye.rs | 5 +++-- argmin-math/src/nalgebra_m/inv.rs | 3 ++- argmin-math/src/nalgebra_m/l1norm.rs | 5 +++-- argmin-math/src/nalgebra_m/l2norm.rs | 5 +++-- argmin-math/src/nalgebra_m/minmax.rs | 12 +++++------- argmin-math/src/nalgebra_m/mul.rs | 16 +++++++++------- argmin-math/src/nalgebra_m/scaledadd.rs | 9 +++++---- argmin-math/src/nalgebra_m/scaledsub.rs | 9 +++++---- argmin-math/src/nalgebra_m/signum.rs | 6 +++--- argmin-math/src/nalgebra_m/sub.rs | 11 ++++++----- argmin-math/src/nalgebra_m/transpose.rs | 7 ++++--- argmin-math/src/nalgebra_m/zero.rs | 7 +++---- 17 files changed, 75 insertions(+), 68 deletions(-) diff --git a/argmin-math/ndarray-tests-src/minmax.rs b/argmin-math/ndarray-tests-src/minmax.rs index f2a8bd13f..ae212b2f3 100644 --- a/argmin-math/ndarray-tests-src/minmax.rs +++ b/argmin-math/ndarray-tests-src/minmax.rs @@ -73,8 +73,6 @@ mod tests { make_test!(u32); make_test!(i64); make_test!(u64); - make_test!(isize); - make_test!(usize); make_test!(f32); make_test!(f64); } diff --git a/argmin-math/src/nalgebra_m/add.rs b/argmin-math/src/nalgebra_m/add.rs index 83a6fb49e..dfdc78693 100644 --- a/argmin-math/src/nalgebra_m/add.rs +++ b/argmin-math/src/nalgebra_m/add.rs @@ -68,6 +68,7 @@ where #[cfg(test)] mod tests { use super::*; + use approx::assert_relative_eq; use nalgebra::{DMatrix, DVector, Matrix2x3, Vector3}; use paste::item; @@ -81,7 +82,7 @@ mod tests { let target = Vector3::new(35 as $t, 38 as $t, 42 as $t); let res = as ArgminAdd<$t, Vector3<$t>>>::add(&a, &b); for i in 0..3 { - assert!(((target[i] - res[i]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -94,7 +95,7 @@ mod tests { let target = Vector3::new(35 as $t, 38 as $t, 42 as $t); let res = <$t as ArgminAdd, Vector3<$t>>>::add(&b, &a); for i in 0..3 { - assert!(((target[i] - res[i]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -107,7 +108,7 @@ mod tests { let target = Vector3::new(42 as $t, 42 as $t, 42 as $t); let res = as ArgminAdd, Vector3<$t>>>::add(&a, &b); for i in 0..3 { - assert!(((target[i] - res[i]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -160,7 +161,7 @@ mod tests { let res = as ArgminAdd, Matrix2x3<$t>>>::add(&a, &b); for i in 0..3 { for j in 0..2 { - assert!(((target[(j, i)] - res[(j, i)]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[(j, i)] as f64, res[(j, i)] as f64, epsilon = std::f64::EPSILON); } } } @@ -181,7 +182,7 @@ mod tests { let res = as ArgminAdd<$t, Matrix2x3<$t>>>::add(&a, &b); for i in 0..3 { for j in 0..2 { - assert!(((target[(j, i)] - res[(j, i)]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[(j, i)] as f64, res[(j, i)] as f64, epsilon = std::f64::EPSILON); } } } diff --git a/argmin-math/src/nalgebra_m/conj.rs b/argmin-math/src/nalgebra_m/conj.rs index 580f00355..6bf5d501b 100644 --- a/argmin-math/src/nalgebra_m/conj.rs +++ b/argmin-math/src/nalgebra_m/conj.rs @@ -28,6 +28,7 @@ where #[cfg(test)] mod tests { use super::*; + use approx::assert_relative_eq; use nalgebra::Vector3; use num_complex::Complex; use paste::item; @@ -49,9 +50,8 @@ mod tests { ); let res = > as ArgminConj>::conj(&a); for i in 0..3 { - let tmp = b[i] - res[i]; - let norm = ((tmp.re * tmp.re + tmp.im * tmp.im) as f64).sqrt(); - assert!(norm < std::f64::EPSILON); + assert_relative_eq!(b[i].re, res[i].re, epsilon = std::$t::EPSILON); + assert_relative_eq!(b[i].im, res[i].im, epsilon = std::$t::EPSILON); } } } @@ -63,8 +63,7 @@ mod tests { let b = Vector3::new(1 as $t, 4 as $t, 8 as $t); let res = as ArgminConj>::conj(&a); for i in 0..3 { - let diff = (b[i] as f64 - res[i] as f64).abs(); - assert!(diff < std::f64::EPSILON); + assert_relative_eq!(b[i], res[i], epsilon = std::$t::EPSILON); } } } diff --git a/argmin-math/src/nalgebra_m/div.rs b/argmin-math/src/nalgebra_m/div.rs index 5f592af12..a44eb07ea 100644 --- a/argmin-math/src/nalgebra_m/div.rs +++ b/argmin-math/src/nalgebra_m/div.rs @@ -68,6 +68,7 @@ where #[cfg(test)] mod tests { use super::*; + use approx::assert_relative_eq; use nalgebra::{DMatrix, DVector, Matrix2x3, Vector3}; use paste::item; @@ -81,7 +82,7 @@ mod tests { let target = Vector3::new(2 as $t, 8 as $t, 4 as $t); let res = as ArgminDiv<$t, Vector3<$t>>>::div(&a, &b); for i in 0..3 { - assert!(((target[i] - res[i]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -94,7 +95,7 @@ mod tests { let target = Vector3::new(16 as $t, 8 as $t, 4 as $t); let res = <$t as ArgminDiv, Vector3<$t>>>::div(&b, &a); for i in 0..3 { - assert!(((target[i] - res[i]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -107,7 +108,7 @@ mod tests { let target = Vector3::new(2 as $t, 3 as $t, 2 as $t); let res = as ArgminDiv, Vector3<$t>>>::div(&a, &b); for i in 0..3 { - assert!(((target[i] - res[i]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -160,7 +161,7 @@ mod tests { let res = as ArgminDiv, Matrix2x3<$t>>>::div(&a, &b); for i in 0..3 { for j in 0..2 { - assert!(((target[(j, i)] - res[(j, i)]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[(j, i)] as f64, res[(j, i)] as f64, epsilon = std::f64::EPSILON); } } } diff --git a/argmin-math/src/nalgebra_m/dot.rs b/argmin-math/src/nalgebra_m/dot.rs index c61e8bbc1..b15c52fd5 100644 --- a/argmin-math/src/nalgebra_m/dot.rs +++ b/argmin-math/src/nalgebra_m/dot.rs @@ -88,6 +88,7 @@ where #[cfg(test)] mod tests { use super::*; + use approx::assert_relative_eq; use nalgebra::{Matrix3, RowVector3, Vector3}; use paste::item; @@ -99,7 +100,7 @@ mod tests { let a = Vector3::new(1 as $t, 2 as $t, 3 as $t); let b = Vector3::new(4 as $t, 5 as $t, 6 as $t); let res: $t = as ArgminDot, $t>>::dot(&a, &b); - assert!((((res - 32 as $t) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res as f64, 32 as f64, epsilon = std::f64::EPSILON); } } @@ -112,7 +113,7 @@ mod tests { as ArgminDot<$t, Vector3<$t>>>::dot(&a, &b); let res = Vector3::new(2 as $t, 4 as $t, 6 as $t); for i in 0..3 { - assert!((((res[i] - product[i]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[i] as f64, product[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -126,7 +127,7 @@ mod tests { <$t as ArgminDot, Vector3<$t>>>::dot(&b, &a); let res = Vector3::new(2 as $t, 4 as $t, 6 as $t); for i in 0..3 { - assert!((((res[i] - product[i]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[i] as f64, product[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -145,7 +146,7 @@ mod tests { as ArgminDot, Matrix3<$t>>>::dot(&a, &b); for i in 0..3 { for j in 0..3 { - assert!((((res[(i, j)] - product[(i, j)]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[(i, j)] as f64, product[(i, j)] as f64, epsilon = std::f64::EPSILON); } } } @@ -164,7 +165,7 @@ mod tests { let product: Vector3<$t> = as ArgminDot, Vector3<$t>>>::dot(&a, &b); for i in 0..3 { - assert!((((res[i] - product[i]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[i] as f64, product[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -191,7 +192,7 @@ mod tests { as ArgminDot, Matrix3<$t>>>::dot(&a, &b); for i in 0..3 { for j in 0..3 { - assert!((((res[(i, j)] - product[(i, j)]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[(i, j)] as f64, product[(i, j)] as f64, epsilon = std::f64::EPSILON); } } } @@ -214,7 +215,7 @@ mod tests { as ArgminDot<$t, Matrix3<$t>>>::dot(&a, &(2 as $t)); for i in 0..3 { for j in 0..3 { - assert!((((res[(i, j)] - product[(i, j)]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[(i, j)] as f64, product[(i, j)] as f64, epsilon = std::f64::EPSILON); } } } @@ -237,7 +238,7 @@ mod tests { <$t as ArgminDot, Matrix3<$t>>>::dot(&(2 as $t), &a); for i in 0..3 { for j in 0..3 { - assert!((((res[(i, j)] - product[(i, j)]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[(i, j)] as f64, product[(i, j)] as f64, epsilon = std::f64::EPSILON); } } } diff --git a/argmin-math/src/nalgebra_m/eye.rs b/argmin-math/src/nalgebra_m/eye.rs index 1e87bd623..525cfe372 100644 --- a/argmin-math/src/nalgebra_m/eye.rs +++ b/argmin-math/src/nalgebra_m/eye.rs @@ -36,6 +36,7 @@ where #[cfg(test)] mod tests { use super::*; + use approx::assert_relative_eq; use nalgebra::{Matrix2x3, Matrix3}; use paste::item; @@ -52,7 +53,7 @@ mod tests { ); for i in 0..3 { for j in 0..3 { - assert!((((res[(i, j)] - e[(i, j)]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[(i, j)] as f64, e[(i, j)] as f64, epsilon = std::f64::EPSILON); } } } @@ -74,7 +75,7 @@ mod tests { ); for i in 0..3 { for j in 0..3 { - assert!((((res[(i, j)] - e[(i, j)]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[(i, j)] as f64, e[(i, j)] as f64, epsilon = std::f64::EPSILON); } } } diff --git a/argmin-math/src/nalgebra_m/inv.rs b/argmin-math/src/nalgebra_m/inv.rs index 175c377a3..a75034b0c 100644 --- a/argmin-math/src/nalgebra_m/inv.rs +++ b/argmin-math/src/nalgebra_m/inv.rs @@ -40,6 +40,7 @@ where #[cfg(test)] mod tests { use super::*; + use approx::assert_relative_eq; use nalgebra::Matrix2; use paste::item; @@ -59,7 +60,7 @@ mod tests { let res = as ArgminInv>>::inv(&a).unwrap(); for i in 0..2 { for j in 0..2 { - assert!((((res[(i, j)] - target[(i, j)]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[(i, j)], target[(i, j)], epsilon = std::$t::EPSILON); } } } diff --git a/argmin-math/src/nalgebra_m/l1norm.rs b/argmin-math/src/nalgebra_m/l1norm.rs index c19d69ef7..c52e8faf2 100644 --- a/argmin-math/src/nalgebra_m/l1norm.rs +++ b/argmin-math/src/nalgebra_m/l1norm.rs @@ -28,6 +28,7 @@ where #[cfg(test)] mod tests { use super::*; + use approx::assert_relative_eq; use nalgebra::Vector2; use paste::item; @@ -39,7 +40,7 @@ mod tests { let a = Vector2::new(4 as $t, 3 as $t); let res = as ArgminL1Norm<$t>>::l1_norm(&a); let target = 7 as $t; - assert!(((target - res) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target as $t, res as $t, epsilon = std::$t::EPSILON); } } }; @@ -53,7 +54,7 @@ mod tests { let a = Vector2::new(-4 as $t, -3 as $t); let res = as ArgminL1Norm<$t>>::l1_norm(&a); let target = 7 as $t; - assert!(((target - res) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target as $t, res as $t, epsilon = std::$t::EPSILON); } } }; diff --git a/argmin-math/src/nalgebra_m/l2norm.rs b/argmin-math/src/nalgebra_m/l2norm.rs index 9cef1915f..07382a63b 100644 --- a/argmin-math/src/nalgebra_m/l2norm.rs +++ b/argmin-math/src/nalgebra_m/l2norm.rs @@ -28,6 +28,7 @@ where #[cfg(test)] mod tests { use super::*; + use approx::assert_relative_eq; use nalgebra::Vector2; use paste::item; @@ -39,7 +40,7 @@ mod tests { let a = Vector2::new(4 as $t, 3 as $t); let res = as ArgminL2Norm<$t>>::l2_norm(&a); let target = 5 as $t; - assert!(((target - res) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target as $t, res as $t, epsilon = std::$t::EPSILON); } } }; @@ -53,7 +54,7 @@ mod tests { let a = Vector2::new(-4 as $t, -3 as $t); let res = as ArgminL2Norm<$t>>::l2_norm(&a); let target = 5 as $t; - assert!(((target - res) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target as $t, res as $t, epsilon = std::$t::EPSILON); } } }; diff --git a/argmin-math/src/nalgebra_m/minmax.rs b/argmin-math/src/nalgebra_m/minmax.rs index c73bf16c7..58f0484ee 100644 --- a/argmin-math/src/nalgebra_m/minmax.rs +++ b/argmin-math/src/nalgebra_m/minmax.rs @@ -33,6 +33,7 @@ where #[cfg(test)] mod tests { use super::*; + use approx::assert_relative_eq; use nalgebra::{Matrix2x3, Vector3}; use paste::item; @@ -48,8 +49,8 @@ mod tests { let res_max = as ArgminMinMax>::max(&a, &b); let res_min = as ArgminMinMax>::min(&a, &b); for i in 0..3 { - assert!(((target_max[i] - res_max[i]) as f64).abs() < std::f64::EPSILON); - assert!(((target_min[i] - res_min[i]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target_max[i] as f64, res_max[i] as f64, epsilon = std::f64::EPSILON); + assert_relative_eq!(target_min[i] as f64, res_min[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -77,13 +78,12 @@ mod tests { let res_min = as ArgminMinMax>::min(&a, &b); for i in 0..3 { for j in 0..2 { - assert!(((target_max[(j, i)] - res_max[(j, i)]) as f64).abs() < std::f64::EPSILON); - assert!(((target_min[(j, i)] - res_min[(j, i)]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target_max[(j, i)] as f64, res_max[(j, i)] as f64, epsilon = std::f64::EPSILON); + assert_relative_eq!(target_min[(j, i)] as f64, res_min[(j, i)] as f64, epsilon = std::f64::EPSILON); } } } } - }; } @@ -95,8 +95,6 @@ mod tests { make_test!(u32); make_test!(i64); make_test!(u64); - make_test!(isize); - make_test!(usize); make_test!(f32); make_test!(f64); } diff --git a/argmin-math/src/nalgebra_m/mul.rs b/argmin-math/src/nalgebra_m/mul.rs index 78d71d963..024da8fda 100644 --- a/argmin-math/src/nalgebra_m/mul.rs +++ b/argmin-math/src/nalgebra_m/mul.rs @@ -68,6 +68,7 @@ where #[cfg(test)] mod tests { use super::*; + use approx::assert_relative_eq; use nalgebra::{Matrix2x3, Vector3}; use paste::item; @@ -81,7 +82,7 @@ mod tests { let target = Vector3::new(2 as $t, 8 as $t, 16 as $t); let res = as ArgminMul<$t, Vector3<$t>>>::mul(&a, &b); for i in 0..3 { - assert!(((target[i] - res[i]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -94,7 +95,7 @@ mod tests { let target = Vector3::new(2 as $t, 8 as $t, 16 as $t); let res = <$t as ArgminMul, Vector3<$t>>>::mul(&b, &a); for i in 0..3 { - assert!(((target[i] - res[i]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -107,8 +108,9 @@ mod tests { let target = Vector3::new(2 as $t, 12 as $t, 32 as $t); let res = as ArgminMul, Vector3<$t>>>::mul(&a, &b); for i in 0..3 { - assert!(((target[i] - res[i]) as f64).abs() < std::f64::EPSILON); - } } + assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON); + } + } } item! { @@ -129,7 +131,7 @@ mod tests { let res = as ArgminMul, Matrix2x3<$t>>>::mul(&a, &b); for i in 0..3 { for j in 0..2 { - assert!(((target[(j, i)] - res[(j, i)]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[(j, i)] as f64, res[(j, i)] as f64, epsilon = std::f64::EPSILON); } } } @@ -150,7 +152,7 @@ mod tests { let res = as ArgminMul<$t, Matrix2x3<$t>>>::mul(&a, &b); for i in 0..3 { for j in 0..2 { - assert!(((target[(j, i)] - res[(j, i)]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[(j, i)] as f64, res[(j, i)] as f64, epsilon = std::f64::EPSILON); } } } @@ -171,7 +173,7 @@ mod tests { let res = <$t as ArgminMul, Matrix2x3<$t>>>::mul(&a, &b); for i in 0..3 { for j in 0..2 { - assert!(((target[(j, i)] - res[(j, i)]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[(j, i)] as f64, res[(j, i)] as f64, epsilon = std::f64::EPSILON); } } } diff --git a/argmin-math/src/nalgebra_m/scaledadd.rs b/argmin-math/src/nalgebra_m/scaledadd.rs index 4e8fbde81..2b533d37c 100644 --- a/argmin-math/src/nalgebra_m/scaledadd.rs +++ b/argmin-math/src/nalgebra_m/scaledadd.rs @@ -8,6 +8,7 @@ #[cfg(test)] mod tests { use crate::ArgminScaledAdd; + use approx::assert_relative_eq; use nalgebra::{DVector, Matrix2, Vector3}; use paste::item; @@ -22,7 +23,7 @@ mod tests { let res = as ArgminScaledAdd, $t, Vector3<$t>>>::scaled_add(&a, &b, &c); let target = Vector3::new(9 as $t, 12 as $t, 15 as $t); for i in 0..3 { - assert!((((res[i] - target[i]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[i] as f64, target[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -58,7 +59,7 @@ mod tests { let res = as ArgminScaledAdd, Vector3<$t>, Vector3<$t>>>::scaled_add(&a, &b, &c); let target = Vector3::new(13 as $t, 12 as $t, 9 as $t); for i in 0..3 { - assert!((((res[i] - target[i]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[i] as f64, target[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -118,7 +119,7 @@ mod tests { ); for i in 0..2 { for j in 0..2 { - assert!((((res[(i, j)] - target[(i, j)]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[(i, j)] as f64, target[(i, j)] as f64, epsilon = std::f64::EPSILON); } } } @@ -143,7 +144,7 @@ mod tests { ); for i in 0..2 { for j in 0..2 { - assert!((((res[(i, j)] - target[(i, j)]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[(i, j)] as f64, target[(i, j)] as f64, epsilon = std::f64::EPSILON); } } } diff --git a/argmin-math/src/nalgebra_m/scaledsub.rs b/argmin-math/src/nalgebra_m/scaledsub.rs index ece68c90d..17c3c7ec9 100644 --- a/argmin-math/src/nalgebra_m/scaledsub.rs +++ b/argmin-math/src/nalgebra_m/scaledsub.rs @@ -8,6 +8,7 @@ #[cfg(test)] mod tests { use crate::ArgminScaledSub; + use approx::assert_relative_eq; use nalgebra::{DVector, Matrix2, Vector3}; use paste::item; @@ -22,7 +23,7 @@ mod tests { let res = as ArgminScaledSub, $t, Vector3<$t>>>::scaled_sub(&a, &b, &c); let target = Vector3::new(2 as $t, 10 as $t, 18 as $t); for i in 0..3 { - assert!((((res[i] - target[i]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[i] as f64, target[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -58,7 +59,7 @@ mod tests { let res = as ArgminScaledSub, Vector3<$t>, Vector3<$t>>>::scaled_sub(&a, &b, &c); let target = Vector3::new(8 as $t, 10 as $t, 24 as $t); for i in 0..3 { - assert!((((res[i] - target[i]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[i] as f64, target[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -118,7 +119,7 @@ mod tests { ); for i in 0..2 { for j in 0..2 { - assert!((((res[(i, j)] - target[(i, j)]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[(i, j)] as f64, target[(i, j)] as f64, epsilon = std::f64::EPSILON); } } } @@ -143,7 +144,7 @@ mod tests { ); for i in 0..2 { for j in 0..2 { - assert!((((res[(i, j)] - target[(i, j)]) as f64).abs()) < std::f64::EPSILON); + assert_relative_eq!(res[(i, j)] as f64, target[(i, j)] as f64, epsilon = std::f64::EPSILON); } } } diff --git a/argmin-math/src/nalgebra_m/signum.rs b/argmin-math/src/nalgebra_m/signum.rs index 24ed1417e..cb0093637 100644 --- a/argmin-math/src/nalgebra_m/signum.rs +++ b/argmin-math/src/nalgebra_m/signum.rs @@ -28,6 +28,7 @@ where #[cfg(test)] mod tests { use super::*; + use approx::assert_relative_eq; use nalgebra::{Matrix2x3, Vector3}; use paste::item; @@ -40,8 +41,7 @@ mod tests { let b = Vector3::new(1 as $t, -1 as $t, -1 as $t); let res = as ArgminSignum>::signum(a); for i in 0..3 { - let diff = (b[i] as f64 - res[i] as f64).abs(); - assert!(diff < std::f64::EPSILON); + assert_relative_eq!(b[i], res[i], epsilon = std::$t::EPSILON); } } } @@ -60,7 +60,7 @@ mod tests { let res = b.signum(); for i in 0..3 { for j in 0..2 { - assert!(((target[(j, i)] - res[(j, i)]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[(j, i)], res[(j, i)], epsilon = std::$t::EPSILON); } } } diff --git a/argmin-math/src/nalgebra_m/sub.rs b/argmin-math/src/nalgebra_m/sub.rs index 5a8ab776c..25d72e101 100644 --- a/argmin-math/src/nalgebra_m/sub.rs +++ b/argmin-math/src/nalgebra_m/sub.rs @@ -59,6 +59,7 @@ where #[cfg(test)] mod tests { use super::*; + use approx::assert_relative_eq; use nalgebra::{Matrix2x3, Vector3}; use paste::item; @@ -72,7 +73,7 @@ mod tests { let target = Vector3::new(35 as $t, 38 as $t, 42 as $t); let res = as ArgminSub<$t, Vector3<$t>>>::sub(&a, &b); for i in 0..3 { - assert!(((target[i] - res[i]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -85,7 +86,7 @@ mod tests { let target = Vector3::new(33 as $t, 30 as $t, 26 as $t); let res = <$t as ArgminSub, Vector3<$t>>>::sub(&b, &a); for i in 0..3 { - assert!(((target[i] - res[i]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -98,7 +99,7 @@ mod tests { let target =Vector3::new(40 as $t, 34 as $t, 26 as $t); let res = as ArgminSub, Vector3<$t>>>::sub(&a, &b); for i in 0..3 { - assert!(((target[i] - res[i]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -121,7 +122,7 @@ mod tests { let res = as ArgminSub, Matrix2x3<$t>>>::sub(&a, &b); for i in 0..3 { for j in 0..2 { - assert!(((target[(j, i)] - res[(j, i)]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[(j, i)] as f64, res[(j, i)] as f64, epsilon = std::f64::EPSILON); } } } @@ -142,7 +143,7 @@ mod tests { let res = as ArgminSub<$t, Matrix2x3<$t>>>::sub(&a, &b); for i in 0..3 { for j in 0..2 { - assert!(((target[(j, i)] - res[(j, i)]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[(j, i)] as f64, res[(j, i)] as f64, epsilon = std::f64::EPSILON); } } } diff --git a/argmin-math/src/nalgebra_m/transpose.rs b/argmin-math/src/nalgebra_m/transpose.rs index deb891d88..54795c43c 100644 --- a/argmin-math/src/nalgebra_m/transpose.rs +++ b/argmin-math/src/nalgebra_m/transpose.rs @@ -32,6 +32,7 @@ where #[cfg(test)] mod tests { use super::*; + use approx::assert_relative_eq; use nalgebra::{Matrix2, Matrix2x3, Matrix3x2, RowVector2, Vector2}; use paste::item; @@ -44,7 +45,7 @@ mod tests { let target = RowVector2::new(1 as $t, 4 as $t); let res = as ArgminTranspose>>::t(a); for i in 0..2 { - assert!(((target[i] - res[i]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -63,7 +64,7 @@ mod tests { let res = as ArgminTranspose>>::t(a); for i in 0..2 { for j in 0..2 { - assert!(((target[(i, j)] - res[(i, j)]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[(i, j)] as f64, res[(i, j)] as f64, epsilon = std::f64::EPSILON); } } } @@ -84,7 +85,7 @@ mod tests { let res = as ArgminTranspose>>::t(a); for i in 0..2 { for j in 0..3 { - assert!(((target[(i, j)] - res[(i, j)]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(target[(i, j)] as f64, res[(i, j)] as f64, epsilon = std::f64::EPSILON); } } } diff --git a/argmin-math/src/nalgebra_m/zero.rs b/argmin-math/src/nalgebra_m/zero.rs index 2194a0772..bffa29351 100644 --- a/argmin-math/src/nalgebra_m/zero.rs +++ b/argmin-math/src/nalgebra_m/zero.rs @@ -30,6 +30,7 @@ where #[cfg(test)] mod tests { use super::*; + use approx::assert_relative_eq; use nalgebra::{DVector, Matrix2, Vector2, Vector4}; use paste::item; @@ -49,7 +50,7 @@ mod tests { fn []() { let a = Vector4::new(42 as $t, 42 as $t, 42 as $t, 42 as $t).zero_like(); for i in 0..4 { - assert!(((0 as $t - a[i]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(0 as f64, a[i] as f64, epsilon = std::f64::EPSILON); } } } @@ -73,7 +74,7 @@ mod tests { for i in 0..2 { for j in 0..2 { - assert!(((0 as $t - a[(i, j)]) as f64).abs() < std::f64::EPSILON); + assert_relative_eq!(0 as f64, a[(i, j)] as f64, epsilon = std::f64::EPSILON); } } } @@ -81,8 +82,6 @@ mod tests { }; } - make_test!(isize); - make_test!(usize); make_test!(i8); make_test!(u8); make_test!(i16); From 4dcce411e39de6afb7f160d5ecd1c76dc349eb6b Mon Sep 17 00:00:00 2001 From: Stefan Kroboth Date: Sun, 21 Jan 2024 10:51:36 +0100 Subject: [PATCH 4/4] Remove argmin-math tests for `usize` and `isize` --- argmin-math/ndarray-tests-src/conj.rs | 1 - argmin-math/ndarray-tests-src/dot.rs | 2 -- argmin-math/ndarray-tests-src/eye.rs | 2 -- argmin-math/ndarray-tests-src/l1norm.rs | 3 --- argmin-math/ndarray-tests-src/l2norm.rs | 3 --- argmin-math/ndarray-tests-src/mul.rs | 2 -- argmin-math/ndarray-tests-src/random.rs | 4 +--- argmin-math/ndarray-tests-src/signum.rs | 1 - argmin-math/ndarray-tests-src/zero.rs | 2 -- 9 files changed, 1 insertion(+), 19 deletions(-) diff --git a/argmin-math/ndarray-tests-src/conj.rs b/argmin-math/ndarray-tests-src/conj.rs index fd69fcd69..6006c5b8f 100644 --- a/argmin-math/ndarray-tests-src/conj.rs +++ b/argmin-math/ndarray-tests-src/conj.rs @@ -92,7 +92,6 @@ mod tests { }; } - make_test!(isize); make_test!(i8); make_test!(i16); make_test!(i32); diff --git a/argmin-math/ndarray-tests-src/dot.rs b/argmin-math/ndarray-tests-src/dot.rs index e823f67fe..996e29dac 100644 --- a/argmin-math/ndarray-tests-src/dot.rs +++ b/argmin-math/ndarray-tests-src/dot.rs @@ -364,8 +364,6 @@ mod tests { make_test!(u32); make_test!(i64); make_test!(u64); - make_test!(isize); - make_test!(usize); make_test!(f32); make_test!(f64); } diff --git a/argmin-math/ndarray-tests-src/eye.rs b/argmin-math/ndarray-tests-src/eye.rs index 3b05c7bdb..b550e7b63 100644 --- a/argmin-math/ndarray-tests-src/eye.rs +++ b/argmin-math/ndarray-tests-src/eye.rs @@ -71,8 +71,6 @@ mod tests { }; } - make_test!(isize); - make_test!(usize); make_test!(i8); make_test!(u8); make_test!(i16); diff --git a/argmin-math/ndarray-tests-src/l1norm.rs b/argmin-math/ndarray-tests-src/l1norm.rs index 512045e40..bb205cfda 100644 --- a/argmin-math/ndarray-tests-src/l1norm.rs +++ b/argmin-math/ndarray-tests-src/l1norm.rs @@ -63,8 +63,6 @@ mod tests { }; } - make_test!(isize); - make_test!(usize); make_test!(i8); make_test!(u8); make_test!(i16); @@ -76,7 +74,6 @@ mod tests { make_test!(f32); make_test!(f64); - make_test_signed!(isize); make_test_signed!(i8); make_test_signed!(i16); make_test_signed!(i32); diff --git a/argmin-math/ndarray-tests-src/l2norm.rs b/argmin-math/ndarray-tests-src/l2norm.rs index c933fdf85..657286e14 100644 --- a/argmin-math/ndarray-tests-src/l2norm.rs +++ b/argmin-math/ndarray-tests-src/l2norm.rs @@ -64,8 +64,6 @@ mod tests { }; } - make_test!(isize); - make_test!(usize); make_test!(i8); make_test!(u8); make_test!(i16); @@ -77,7 +75,6 @@ mod tests { make_test!(f32); make_test!(f64); - make_test_signed!(isize); make_test_signed!(i8); make_test_signed!(i16); make_test_signed!(i32); diff --git a/argmin-math/ndarray-tests-src/mul.rs b/argmin-math/ndarray-tests-src/mul.rs index 20aefd042..8ee47d8a8 100644 --- a/argmin-math/ndarray-tests-src/mul.rs +++ b/argmin-math/ndarray-tests-src/mul.rs @@ -403,8 +403,6 @@ mod tests { make_test!(u32); make_test!(i64); make_test!(u64); - make_test!(isize); - make_test!(usize); make_test!(f32); make_test!(f64); } diff --git a/argmin-math/ndarray-tests-src/random.rs b/argmin-math/ndarray-tests-src/random.rs index a45623dc1..4746c2174 100644 --- a/argmin-math/ndarray-tests-src/random.rs +++ b/argmin-math/ndarray-tests-src/random.rs @@ -23,7 +23,7 @@ mod tests { let b = array![2 as $t, 3 as $t, 5 as $t]; let mut rng = rand::rngs::StdRng::seed_from_u64(42); let random = Array1::<$t>::rand_from_range(&a, &b, &mut rng); - for i in 0..3usize { + for i in 0..3 { assert!(random[i] >= a[i]); assert!(random[i] <= b[i]); } @@ -62,8 +62,6 @@ mod tests { make_test!(u32); make_test!(i64); make_test!(u64); - make_test!(isize); - make_test!(usize); make_test!(f32); make_test!(f64); } diff --git a/argmin-math/ndarray-tests-src/signum.rs b/argmin-math/ndarray-tests-src/signum.rs index 43c884409..5c09558ce 100644 --- a/argmin-math/ndarray-tests-src/signum.rs +++ b/argmin-math/ndarray-tests-src/signum.rs @@ -119,7 +119,6 @@ mod tests { }; } - make_test!(isize); make_test!(i8); make_test!(i16); make_test!(i32); diff --git a/argmin-math/ndarray-tests-src/zero.rs b/argmin-math/ndarray-tests-src/zero.rs index 4ab13aa27..5f39e3299 100644 --- a/argmin-math/ndarray-tests-src/zero.rs +++ b/argmin-math/ndarray-tests-src/zero.rs @@ -76,8 +76,6 @@ mod tests { }; } - make_test!(isize); - make_test!(usize); make_test!(i8); make_test!(u8); make_test!(i16);