diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 38039013e..a98d9d414 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -87,32 +87,32 @@ jobs: - name: argmin-math (ndarray_v0_13-nolinalg) run: cargo test -p argmin-math --no-default-features --features "ndarray_v0_13-nolinalg" # ndarray with linalg, without serde - - name: argmin-math (ndarray_latest,_dev_linalg_latest) - run: cargo test -p argmin-math --no-default-features --features "ndarray_latest,_dev_linalg_latest" - - name: argmin-math (ndarray_v0_15,_dev_linalg_0_16) - run: cargo test -p argmin-math --no-default-features --features "ndarray_v0_15,_dev_linalg_0_16" - - name: argmin-math (ndarray_v0_14,_dev_linalg_0_13) - run: cargo test -p argmin-math --no-default-features --features "ndarray_v0_14,_dev_linalg_0_13" - - name: argmin-math (ndarray_v0_13,_dev_linalg_0_12) - run: cargo test -p argmin-math --no-default-features --features "ndarray_v0_13,_dev_linalg_0_12" + - name: argmin-math (ndarray_latest) + run: cargo test --manifest-path ./argmin-math-ndarray-linalg-tests/ndarray_latest/Cargo.toml + - name: argmin-math (ndarray_v0_15) + run: cargo test --manifest-path ./argmin-math-ndarray-linalg-tests/ndarray_0_15/Cargo.toml + - name: argmin-math (ndarray_v0_14) + run: cargo test --manifest-path ./argmin-math-ndarray-linalg-tests/ndarray_0_14/Cargo.toml + - name: argmin-math (ndarray_v0_13) + run: cargo test --manifest-path ./argmin-math-ndarray-linalg-tests/ndarray_0_13/Cargo.toml # ndarray without linalg, with serde - - name: argmin-math (ndarray_latest-serde,_dev_linalg_latest) - run: cargo test -p argmin-math --no-default-features --features "ndarray_latest-serde,_dev_linalg_latest" - - name: argmin-math (ndarray_v0_15-serde,_dev_linalg_0_16) - run: cargo test -p argmin-math --no-default-features --features "ndarray_v0_15-serde,_dev_linalg_0_16" - - name: argmin-math (ndarray_v0_14-serde,_dev_linalg_0_13) - run: cargo test -p argmin-math --no-default-features --features "ndarray_v0_14-serde,_dev_linalg_0_13" - - name: argmin-math (ndarray_v0_13-serde,_dev_linalg_0_12) - run: cargo test -p argmin-math --no-default-features --features "ndarray_v0_13-serde,_dev_linalg_0_12" + - name: argmin-math (ndarray_latest-serde) + run: cargo test -p argmin-math --no-default-features --features "ndarray_latest-nolinalg-serde" + - name: argmin-math (ndarray_v0_15-serde) + run: cargo test -p argmin-math --no-default-features --features "ndarray_v0_15-nolinalg-serde" + - name: argmin-math (ndarray_v0_14-serde) + run: cargo test -p argmin-math --no-default-features --features "ndarray_v0_14-nolinalg-serde" + - name: argmin-math (ndarray_v0_13-serde) + run: cargo test -p argmin-math --no-default-features --features "ndarray_v0_13-nolinalg-serde" # ndarray with linalg, with serde - - name: argmin-math (ndarray_latest-serde,_dev_linalg_latest) - run: cargo test -p argmin-math --no-default-features --features "ndarray_latest-serde,_dev_linalg_latest" - - name: argmin-math (ndarray_v0_15-serde,_dev_linalg_0_16) - run: cargo test -p argmin-math --no-default-features --features "ndarray_v0_15-serde,_dev_linalg_0_16" - - name: argmin-math (ndarray_v0_14-serde,_dev_linalg_0_13) - run: cargo test -p argmin-math --no-default-features --features "ndarray_v0_14-serde,_dev_linalg_0_13" - - name: argmin-math (ndarray_v0_13-serde,_dev_linalg_0_12) - run: cargo test -p argmin-math --no-default-features --features "ndarray_v0_13-serde,_dev_linalg_0_12" + - name: argmin-math (ndarray_latest) + run: cargo test --manifest-path ./argmin-math-ndarray-linalg-tests/ndarray_latest/Cargo.toml --features serde + - name: argmin-math (ndarray_v0_15) + run: cargo test --manifest-path ./argmin-math-ndarray-linalg-tests/ndarray_0_15/Cargo.toml --features serde + - name: argmin-math (ndarray_v0_14) + run: cargo test --manifest-path ./argmin-math-ndarray-linalg-tests/ndarray_0_14/Cargo.toml --features serde + - name: argmin-math (ndarray_v0_13) + run: cargo test --manifest-path ./argmin-math-ndarray-linalg-tests/ndarray_0_13/Cargo.toml --features serde # nalgebra without serde - name: argmin-math (nalgebra_latest) run: cargo test -p argmin-math --no-default-features --features "nalgebra_latest" diff --git a/Cargo.toml b/Cargo.toml index 342e8fcbb..3a062715b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,4 +8,5 @@ members = [ exclude = [ "media/book/tests", + "argmin-math-ndarray-linalg-tests", ] diff --git a/argmin-math-ndarray-linalg-tests/README.md b/argmin-math-ndarray-linalg-tests/README.md new file mode 100644 index 000000000..640fda2af --- /dev/null +++ b/argmin-math-ndarray-linalg-tests/README.md @@ -0,0 +1,3 @@ +This folder contains tests for the argmin-math crate. +Separate projects are needed to test against multiple versions of dependencies because of the required additivity of features even when they are not active and because only one crate can specify a `links` flag in a given dependency graph. This is mainly necessary for the backend dependency of ndarray-linalg. +Read more here: https://github.com/argmin-rs/argmin/issues/368#issue-1929115127 \ No newline at end of file diff --git a/argmin-math-ndarray-linalg-tests/ndarray_0_13/Cargo.toml b/argmin-math-ndarray-linalg-tests/ndarray_0_13/Cargo.toml new file mode 100644 index 000000000..f60d7ca45 --- /dev/null +++ b/argmin-math-ndarray-linalg-tests/ndarray_0_13/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "ndarray_0_13" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dev-dependencies] +argmin-math = { path = "../../argmin-math", version = "0.3", features = [ + "ndarray_v0_13", +] } +ndarray = { version = "0.13", default-features = false } +ndarray-linalg = { version = "0.12", default-features = false, features = ["intel-mkl"] } +num-complex = { version = "0.2", default-features = false } +num-traits = { version = "0.2", default-features = false } +num-integer = { version = "0.1", default-features = false } +paste = "1" +approx = "0.5.0" + +[features] +serde = ["argmin-math/ndarray_v0_13-serde"] \ No newline at end of file diff --git a/argmin-math-ndarray-linalg-tests/ndarray_0_13/src/lib.rs b/argmin-math-ndarray-linalg-tests/ndarray_0_13/src/lib.rs new file mode 100644 index 000000000..d998a5e2d --- /dev/null +++ b/argmin-math-ndarray-linalg-tests/ndarray_0_13/src/lib.rs @@ -0,0 +1,51 @@ +mod add { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/add.rs")); +} +mod conj { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/conj.rs")); +} +mod div { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/div.rs")); +} +mod dot { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/dot.rs")); +} +mod eye { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/eye.rs")); +} +mod inv { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/inv.rs")); +} +mod l1norm { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/l1norm.rs")); +} +mod l2norm { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/l2norm.rs")); +} +mod minmax { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/minmax.rs")); +} +mod mul { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/mul.rs")); +} +mod random { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/random.rs")); +} +mod scaledadd { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/scaledadd.rs")); +} +mod scaledsub { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/scaledsub.rs")); +} +mod signum { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/signum.rs")); +} +mod sub { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/sub.rs")); +} +mod transpose { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/transpose.rs")); +} +mod zero { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/zero.rs")); +} diff --git a/argmin-math-ndarray-linalg-tests/ndarray_0_14/Cargo.toml b/argmin-math-ndarray-linalg-tests/ndarray_0_14/Cargo.toml new file mode 100644 index 000000000..f372f36a0 --- /dev/null +++ b/argmin-math-ndarray-linalg-tests/ndarray_0_14/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "ndarray_0_14" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dev-dependencies] +argmin-math = { path = "../../argmin-math", version = "0.3", default-features=false, features = [ + "ndarray_v0_14", +] } +ndarray = { version = "0.14", default-features = false } +ndarray-linalg = { version = "0.13", features = ["intel-mkl"] } +num-complex = { version = "0.3", default-features = false, features = ["std"] } +num-traits = { version = "0.2" } +num-integer = { version = "0.1" } +# Higher versions break intel-mkl-tool: https://github.com/rust-math/intel-mkl-src/issues/68#issue-1065394662 +anyhow = { version = "<=1.0.48" } +paste = "1" +approx = "0.5.0" + +[features] +serde = ["argmin-math/ndarray_v0_14-serde"] \ No newline at end of file diff --git a/argmin-math-ndarray-linalg-tests/ndarray_0_14/src/lib.rs b/argmin-math-ndarray-linalg-tests/ndarray_0_14/src/lib.rs new file mode 100644 index 000000000..d998a5e2d --- /dev/null +++ b/argmin-math-ndarray-linalg-tests/ndarray_0_14/src/lib.rs @@ -0,0 +1,51 @@ +mod add { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/add.rs")); +} +mod conj { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/conj.rs")); +} +mod div { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/div.rs")); +} +mod dot { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/dot.rs")); +} +mod eye { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/eye.rs")); +} +mod inv { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/inv.rs")); +} +mod l1norm { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/l1norm.rs")); +} +mod l2norm { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/l2norm.rs")); +} +mod minmax { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/minmax.rs")); +} +mod mul { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/mul.rs")); +} +mod random { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/random.rs")); +} +mod scaledadd { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/scaledadd.rs")); +} +mod scaledsub { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/scaledsub.rs")); +} +mod signum { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/signum.rs")); +} +mod sub { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/sub.rs")); +} +mod transpose { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/transpose.rs")); +} +mod zero { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/zero.rs")); +} diff --git a/argmin-math-ndarray-linalg-tests/ndarray_0_15/Cargo.toml b/argmin-math-ndarray-linalg-tests/ndarray_0_15/Cargo.toml new file mode 100644 index 000000000..2c0509597 --- /dev/null +++ b/argmin-math-ndarray-linalg-tests/ndarray_0_15/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "ndarray_0_15" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dev-dependencies] +argmin-math = { path = "../../argmin-math", version = "0.3", features = [ + "ndarray_v0_15", +] } +ndarray = { version = "0.15", default-features = false } +ndarray-linalg = { version = "0.16", default-features = false, features = ["intel-mkl"] } +num-complex = { version = "0.4", default-features = false } +num-traits = { version = "0.2", default-features = false } +num-integer = { version = "0.1", default-features = false } +paste = "1" +approx = "0.5.0" + +[features] +serde = ["argmin-math/ndarray_v0_15-serde"] \ No newline at end of file diff --git a/argmin-math-ndarray-linalg-tests/ndarray_0_15/src/lib.rs b/argmin-math-ndarray-linalg-tests/ndarray_0_15/src/lib.rs new file mode 100644 index 000000000..d998a5e2d --- /dev/null +++ b/argmin-math-ndarray-linalg-tests/ndarray_0_15/src/lib.rs @@ -0,0 +1,51 @@ +mod add { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/add.rs")); +} +mod conj { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/conj.rs")); +} +mod div { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/div.rs")); +} +mod dot { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/dot.rs")); +} +mod eye { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/eye.rs")); +} +mod inv { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/inv.rs")); +} +mod l1norm { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/l1norm.rs")); +} +mod l2norm { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/l2norm.rs")); +} +mod minmax { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/minmax.rs")); +} +mod mul { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/mul.rs")); +} +mod random { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/random.rs")); +} +mod scaledadd { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/scaledadd.rs")); +} +mod scaledsub { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/scaledsub.rs")); +} +mod signum { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/signum.rs")); +} +mod sub { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/sub.rs")); +} +mod transpose { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/transpose.rs")); +} +mod zero { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/zero.rs")); +} diff --git a/argmin-math-ndarray-linalg-tests/ndarray_latest/Cargo.toml b/argmin-math-ndarray-linalg-tests/ndarray_latest/Cargo.toml new file mode 100644 index 000000000..02bc7a8a7 --- /dev/null +++ b/argmin-math-ndarray-linalg-tests/ndarray_latest/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "ndarray_latest" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dev-dependencies] +argmin-math = { path = "../../argmin-math", version = "0.3", features = [ + "ndarray_latest", +] } +ndarray = { version = "0.15", default-features = false } +ndarray-linalg = { version = "0.16", default-features = false, features = ["intel-mkl"] } +num-complex = { version = "0.4", default-features = false } +num-traits = { version = "0.2", default-features = false } +num-integer = { version = "0.1", default-features = false } +paste = "1" +approx = "0.5.0" + +[features] +serde = ["argmin-math/ndarray_latest-serde"] \ No newline at end of file diff --git a/argmin-math-ndarray-linalg-tests/ndarray_latest/src/lib.rs b/argmin-math-ndarray-linalg-tests/ndarray_latest/src/lib.rs new file mode 100644 index 000000000..d998a5e2d --- /dev/null +++ b/argmin-math-ndarray-linalg-tests/ndarray_latest/src/lib.rs @@ -0,0 +1,51 @@ +mod add { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/add.rs")); +} +mod conj { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/conj.rs")); +} +mod div { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/div.rs")); +} +mod dot { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/dot.rs")); +} +mod eye { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/eye.rs")); +} +mod inv { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/inv.rs")); +} +mod l1norm { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/l1norm.rs")); +} +mod l2norm { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/l2norm.rs")); +} +mod minmax { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/minmax.rs")); +} +mod mul { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/mul.rs")); +} +mod random { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/random.rs")); +} +mod scaledadd { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/scaledadd.rs")); +} +mod scaledsub { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/scaledsub.rs")); +} +mod signum { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/signum.rs")); +} +mod sub { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/sub.rs")); +} +mod transpose { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/transpose.rs")); +} +mod zero { + include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/zero.rs")); +} diff --git a/argmin-math-ndarray-linalg-tests/src/add.rs b/argmin-math-ndarray-linalg-tests/src/add.rs new file mode 100644 index 000000000..1e9503897 --- /dev/null +++ b/argmin-math-ndarray-linalg-tests/src/add.rs @@ -0,0 +1,176 @@ +// Copyright 2018-2022 argmin developers +// +// Licensed under the Apache License, Version 2.0 or the MIT license , 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::ArgminAdd; + use ndarray::{Array1, Array2}; + + use ndarray::array; + use paste::item; + + macro_rules! make_test { + ($t:ty) => { + item! { + #[test] + fn []() { + let a = array![1 as $t, 4 as $t, 8 as $t]; + let b = 34 as $t; + 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); + } + } + } + + item! { + #[test] + fn []() { + let a = array![1 as $t, 4 as $t, 8 as $t]; + let b = 34 as $t; + 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); + } + } + } + + item! { + #[test] + fn []() { + let a = array![1 as $t, 4 as $t, 8 as $t]; + let b = array![41 as $t, 38 as $t, 34 as $t]; + 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); + } + } + } + + item! { + #[test] + #[should_panic] + fn []() { + let a = array![1 as $t, 4 as $t]; + let b = array![41 as $t, 38 as $t, 34 as $t]; + as ArgminAdd, Array1<$t>>>::add(&a, &b); + } + } + + item! { + #[test] + #[should_panic] + fn []() { + let a = array![]; + let b = array![41 as $t, 38 as $t, 34 as $t]; + as ArgminAdd, Array1<$t>>>::add(&a, &b); + } + } + + item! { + #[test] + #[should_panic] + fn []() { + let a = array![41 as $t, 38 as $t, 34 as $t]; + let b = array![]; + as ArgminAdd, Array1<$t>>>::add(&a, &b); + } + } + + item! { + #[test] + fn []() { + let a = array![ + [1 as $t, 4 as $t, 8 as $t], + [2 as $t, 5 as $t, 9 as $t] + ]; + let b = array![ + [41 as $t, 38 as $t, 34 as $t], + [40 as $t, 37 as $t, 33 as $t] + ]; + let target = array![ + [42 as $t, 42 as $t, 42 as $t], + [42 as $t, 42 as $t, 42 as $t] + ]; + 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); + } + } + } + } + + item! { + #[test] + fn []() { + let a = array![ + [1 as $t, 4 as $t, 8 as $t], + [2 as $t, 5 as $t, 9 as $t] + ]; + let b = 2 as $t; + let target = array![ + [3 as $t, 6 as $t, 10 as $t], + [4 as $t, 7 as $t, 11 as $t] + ]; + 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); + } + } + } + } + + item! { + #[test] + #[should_panic] + fn []() { + let a = array![ + [1 as $t, 4 as $t, 8 as $t], + [2 as $t, 5 as $t, 9 as $t] + ]; + let b = array![ + [41 as $t, 38 as $t], + ]; + as ArgminAdd, Array2<$t>>>::add(&a, &b); + } + } + + item! { + #[test] + #[should_panic] + fn []() { + let a = array![ + [1 as $t, 4 as $t, 8 as $t], + [2 as $t, 5 as $t, 9 as $t] + ]; + let b = array![[]]; + as ArgminAdd, Array2<$t>>>::add(&a, &b); + } + } + }; + } + + make_test!(i8); + make_test!(u8); + make_test!(i16); + make_test!(u16); + make_test!(i32); + make_test!(u32); + make_test!(i64); + make_test!(u64); + make_test!(f32); + make_test!(f64); + +} \ No newline at end of file diff --git a/argmin-math-ndarray-linalg-tests/src/conj.rs b/argmin-math-ndarray-linalg-tests/src/conj.rs new file mode 100644 index 000000000..6b31659cd --- /dev/null +++ b/argmin-math-ndarray-linalg-tests/src/conj.rs @@ -0,0 +1,104 @@ +// Copyright 2018-2022 argmin developers +// +// Licensed under the Apache License, Version 2.0 or the MIT license , 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::ArgminConj; + use ndarray::{Array1, Array2}; + use num_complex::Complex; + use ndarray::array; + use paste::item; + + macro_rules! make_test { + ($t:ty) => { + item! { + #[test] + fn []() { + let a = Array1::from(vec![ + Complex::new(1 as $t, 2 as $t), + Complex::new(4 as $t, -3 as $t), + Complex::new(8 as $t, 0 as $t) + ]); + let b = vec![ + Complex::new(1 as $t, -2 as $t), + Complex::new(4 as $t, 3 as $t), + Complex::new(8 as $t, 0 as $t) + ]; + 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); + } + } + } + + item! { + #[test] + fn []() { + let a = array![ + [Complex::new(1 as $t, 2 as $t), Complex::new(4 as $t, -5 as $t)], + [Complex::new(3 as $t, -5 as $t), Complex::new(8 as $t, 3 as $t)], + [Complex::new(4 as $t, -2 as $t), Complex::new(9 as $t, -9 as $t)], + ]; + let b = array![ + [Complex::new(1 as $t, -2 as $t), Complex::new(4 as $t, 5 as $t)], + [Complex::new(3 as $t, 5 as $t), Complex::new(8 as $t, -3 as $t)], + [Complex::new(4 as $t, 2 as $t), Complex::new(9 as $t, 9 as $t)], + ]; + 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); + } + } + } + } + + item! { + #[test] + fn []() { + 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); + } + } + } + + item! { + #[test] + fn []() { + let a = array![ + [1 as $t, 4 as $t, 8 as $t], + [2 as $t, 5 as $t, 9 as $t], + ]; + 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); + } + } + } + } + }; + } + + make_test!(isize); + make_test!(i8); + make_test!(i16); + make_test!(i32); + make_test!(i64); + make_test!(f32); + make_test!(f64); + +} \ No newline at end of file diff --git a/argmin-math-ndarray-linalg-tests/src/div.rs b/argmin-math-ndarray-linalg-tests/src/div.rs new file mode 100644 index 000000000..68655d6a7 --- /dev/null +++ b/argmin-math-ndarray-linalg-tests/src/div.rs @@ -0,0 +1,156 @@ +// Copyright 2018-2022 argmin developers +// +// Licensed under the Apache License, Version 2.0 or the MIT license , 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::ArgminDiv; + use ndarray::{Array1, Array2}; + + use ndarray::array; + use paste::item; + + macro_rules! make_test { + ($t:ty) => { + item! { + #[test] + fn []() { + let a = array![4 as $t, 16 as $t, 8 as $t]; + let b = 2 as $t; + 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); + } + } + } + + item! { + #[test] + fn []() { + let a = array![2 as $t, 4 as $t, 8 as $t]; + let b = 32 as $t; + 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); + } + } + } + + item! { + #[test] + fn []() { + let a = array![4 as $t, 9 as $t, 8 as $t]; + let b = array![2 as $t, 3 as $t, 4 as $t]; + 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); + } + } + } + + item! { + #[test] + #[should_panic] + fn []() { + let a = array![1 as $t, 4 as $t]; + let b = array![41 as $t, 38 as $t, 34 as $t]; + as ArgminDiv, Array1<$t>>>::div(&a, &b); + } + } + + item! { + #[test] + #[should_panic] + fn []() { + let a = array![]; + let b = array![41 as $t, 38 as $t, 34 as $t]; + as ArgminDiv, Array1<$t>>>::div(&a, &b); + } + } + + item! { + #[test] + #[should_panic] + fn []() { + let a = array![41 as $t, 38 as $t, 34 as $t]; + let b = array![]; + as ArgminDiv, Array1<$t>>>::div(&a, &b); + } + } + + item! { + #[test] + fn []() { + let a = array![ + [4 as $t, 12 as $t, 8 as $t], + [9 as $t, 20 as $t, 45 as $t] + ]; + let b = array![ + [2 as $t, 3 as $t, 4 as $t], + [3 as $t, 4 as $t, 5 as $t] + ]; + let target = array![ + [2 as $t, 4 as $t, 2 as $t], + [3 as $t, 5 as $t, 9 as $t] + ]; + 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); + } + } + } + } + + item! { + #[test] + #[should_panic] + fn []() { + let a = array![ + [1 as $t, 4 as $t, 8 as $t], + [2 as $t, 5 as $t, 9 as $t] + ]; + let b = array![ + [41 as $t, 38 as $t], + ]; + as ArgminDiv, Array2<$t>>>::div(&a, &b); + } + } + + item! { + #[test] + #[should_panic] + fn []() { + let a = array![ + [1 as $t, 4 as $t, 8 as $t], + [2 as $t, 5 as $t, 9 as $t] + ]; + let b = array![[]]; + as ArgminDiv, Array2<$t>>>::div(&a, &b); + } + } + }; + } + + make_test!(i8); + make_test!(u8); + make_test!(i16); + make_test!(u16); + make_test!(i32); + make_test!(u32); + make_test!(i64); + make_test!(u64); + make_test!(f32); + make_test!(f64); + +} \ No newline at end of file diff --git a/argmin-math-ndarray-linalg-tests/src/dot.rs b/argmin-math-ndarray-linalg-tests/src/dot.rs new file mode 100644 index 000000000..0bd3cfd18 --- /dev/null +++ b/argmin-math-ndarray-linalg-tests/src/dot.rs @@ -0,0 +1,372 @@ +// Copyright 2018-2022 argmin developers +// +// Licensed under the Apache License, Version 2.0 or the MIT license , 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::ArgminDot; + use ndarray::{Array1, Array2}; + use num_complex::Complex; + use ndarray::array; + use paste::item; + + macro_rules! make_test { + ($t:ty) => { + item! { + #[test] + fn []() { + 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); + } + } + + item! { + #[test] + fn []() { + let a = array![1 as $t, 2 as $t, 3 as $t]; + let b = 2 as $t; + let product: Array1<$t> = + 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); + } + } + } + + item! { + #[test] + fn []() { + let a = array![1 as $t, 2 as $t, 3 as $t]; + let b = 2 as $t; + let product: Array1<$t> = + <$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); + } + } + } + + item! { + #[test] + fn []() { + 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 = array![ + [4 as $t, 5 as $t, 6 as $t], + [8 as $t, 10 as $t, 12 as $t], + [12 as $t, 15 as $t, 18 as $t] + ]; + let product: Array2<$t> = + 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); + } + } + } + } + + item! { + #[test] + fn []() { + let a = array![ + [1 as $t, 2 as $t, 3 as $t], + [4 as $t, 5 as $t, 6 as $t], + [7 as $t, 8 as $t, 9 as $t] + ]; + let b = array![1 as $t, 2 as $t, 3 as $t]; + let res = array![14 as $t, 32 as $t, 50 as $t]; + 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); + } + } + } + + item! { + #[test] + fn []() { + let a = array![ + [1 as $t, 2 as $t, 3 as $t], + [4 as $t, 5 as $t, 6 as $t], + [3 as $t, 2 as $t, 1 as $t] + ]; + let b = array![ + [3 as $t, 2 as $t, 1 as $t], + [6 as $t, 5 as $t, 4 as $t], + [2 as $t, 4 as $t, 3 as $t] + ]; + let res = array![ + [21 as $t, 24 as $t, 18 as $t], + [54 as $t, 57 as $t, 42 as $t], + [23 as $t, 20 as $t, 14 as $t] + ]; + let product: Array2<$t> = + 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); + } + } + } + } + + item! { + #[test] + fn []() { + let a = array![ + [1 as $t, 2 as $t, 3 as $t], + [4 as $t, 5 as $t, 6 as $t], + [3 as $t, 2 as $t, 1 as $t] + ]; + let res = array![ + [2 as $t, 4 as $t, 6 as $t], + [8 as $t, 10 as $t, 12 as $t], + [6 as $t, 4 as $t, 2 as $t] + ]; + let product: Array2<$t> = + 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); + } + } + } + } + + item! { + #[test] + fn []() { + let a = array![ + [1 as $t, 2 as $t, 3 as $t], + [4 as $t, 5 as $t, 6 as $t], + [3 as $t, 2 as $t, 1 as $t] + ]; + let res = array![ + [2 as $t, 4 as $t, 6 as $t], + [8 as $t, 10 as $t, 12 as $t], + [6 as $t, 4 as $t, 2 as $t] + ]; + let product: Array2<$t> = + <$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); + } + } + } + } + + item! { + #[test] + fn []() { + let a = array![ + Complex::new(2 as $t, 2 as $t), + Complex::new(5 as $t, 2 as $t), + Complex::new(3 as $t, 2 as $t), + ]; + let b = array![ + Complex::new(5 as $t, 3 as $t), + Complex::new(2 as $t, 4 as $t), + Complex::new(8 as $t, 4 as $t), + ]; + 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); + } + } + + item! { + #[test] + fn []() { + let a = array![ + Complex::new(2 as $t, 2 as $t), + Complex::new(5 as $t, 2 as $t), + Complex::new(3 as $t, 2 as $t), + ]; + let b = Complex::new(4 as $t, 2 as $t); + let product: Array1> = + > 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); + } + } + } + + item! { + #[test] + fn []() { + let a = array![ + Complex::new(2 as $t, 2 as $t), + Complex::new(5 as $t, 2 as $t), + Complex::new(3 as $t, 2 as $t), + ]; + let b = Complex::new(4 as $t, 2 as $t); + let product: Array1> = + 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); + } + } + } + + item! { + #[test] + fn []() { + let a = array![ + Complex::new(2 as $t, 2 as $t), + Complex::new(5 as $t, 2 as $t), + ]; + let b = array![ + Complex::new(5 as $t, 1 as $t), + Complex::new(2 as $t, 1 as $t), + ]; + let res = array![ + [a[0]*b[0], a[0]*b[1]], + [a[1]*b[0], a[1]*b[1]], + ]; + let product: Array2> = + > 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); + } + } + } + } + + item! { + #[test] + fn []() { + let a = array![ + [Complex::new(2 as $t, 2 as $t), Complex::new(5 as $t, 2 as $t)], + [Complex::new(2 as $t, 2 as $t), Complex::new(5 as $t, 2 as $t)], + ]; + let b = array![ + Complex::new(5 as $t, 1 as $t), + Complex::new(2 as $t, 1 as $t), + ]; + let res = array![ + a[(0, 0)] * b[0] + a[(0, 1)] * b[1], + a[(1, 0)] * b[0] + a[(1, 1)] * b[1], + ]; + 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); + } + } + } + + item! { + #[test] + fn []() { + let a = array![ + [Complex::new(2 as $t, 1 as $t), Complex::new(5 as $t, 2 as $t)], + [Complex::new(4 as $t, 2 as $t), Complex::new(7 as $t, 1 as $t)], + ]; + let b = array![ + [Complex::new(2 as $t, 2 as $t), Complex::new(5 as $t, 1 as $t)], + [Complex::new(3 as $t, 1 as $t), Complex::new(4 as $t, 2 as $t)], + ]; + let res = array![ + [ + a[(0, 0)] * b[(0, 0)] + a[(0, 1)] * b[(1, 0)], + a[(0, 0)] * b[(0, 1)] + a[(0, 1)] * b[(1, 1)] + ], + [ + a[(1, 0)] * b[(0, 0)] + a[(1, 1)] * b[(1, 0)], + a[(1, 0)] * b[(0, 1)] + a[(1, 1)] * b[(1, 1)] + ], + ]; + let product: Array2> = + > 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); + } + } + } + } + + item! { + #[test] + fn []() { + let a = array![ + [Complex::new(2 as $t, 1 as $t), Complex::new(5 as $t, 2 as $t)], + [Complex::new(4 as $t, 2 as $t), Complex::new(7 as $t, 1 as $t)], + ]; + let b = Complex::new(4 as $t, 1 as $t); + let res = array![ + [a[(0, 0)] * b, a[(0, 1)] * b], + [a[(1, 0)] * b, a[(1, 1)] * b] + ]; + let product: Array2> = + > 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); + } + } + } + } + + item! { + #[test] + fn []() { + let a = array![ + [Complex::new(2 as $t, 1 as $t), Complex::new(5 as $t, 2 as $t)], + [Complex::new(4 as $t, 2 as $t), Complex::new(7 as $t, 1 as $t)], + ]; + let b = Complex::new(4 as $t, 1 as $t); + let res = array![ + [a[(0, 0)] * b, a[(0, 1)] * b], + [a[(1, 0)] * b, a[(1, 1)] * b], + ]; + let product: Array2> = + 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); + } + } + } + } + }; + } + + make_test!(i8); + make_test!(u8); + make_test!(i16); + make_test!(u16); + make_test!(i32); + make_test!(u32); + make_test!(i64); + make_test!(u64); + make_test!(isize); + make_test!(usize); + make_test!(f32); + make_test!(f64); + +} \ No newline at end of file diff --git a/argmin-math-ndarray-linalg-tests/src/eye.rs b/argmin-math-ndarray-linalg-tests/src/eye.rs new file mode 100644 index 000000000..4fcca57c7 --- /dev/null +++ b/argmin-math-ndarray-linalg-tests/src/eye.rs @@ -0,0 +1,87 @@ +// Copyright 2018-2022 argmin developers +// +// Licensed under the Apache License, Version 2.0 or the MIT license , 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::ArgminEye; + use ndarray::Array2; + use ndarray::array; + use paste::item; + + macro_rules! make_test { + ($t:ty) => { + item! { + #[test] + fn []() { + let e: Array2<$t> = as ArgminEye>::eye(3); + let res = array![ + [1 as $t, 0 as $t, 0 as $t], + [0 as $t, 1 as $t, 0 as $t], + [0 as $t, 0 as $t, 1 as $t] + ]; + for i in 0..3 { + for j in 0..3 { + assert!((((res[(i, j)] - e[(i, j)]) as f64).abs()) < std::f64::EPSILON); + } + } + } + } + + item! { + #[test] + fn []() { + let a = array![ + [0 as $t, 2 as $t, 6 as $t], + [3 as $t, 2 as $t, 7 as $t], + [9 as $t, 8 as $t, 1 as $t] + ]; + let e: Array2<$t> = a.eye_like(); + let res = array![ + [1 as $t, 0 as $t, 0 as $t], + [0 as $t, 1 as $t, 0 as $t], + [0 as $t, 0 as $t, 1 as $t] + ]; + for i in 0..3 { + for j in 0..3 { + assert!((((res[(i, j)] - e[(i, j)]) as f64).abs()) < std::f64::EPSILON); + } + } + } + } + + item! { + #[test] + #[should_panic] + #[allow(unused)] + fn []() { + let a = array![ + [0 as $t, 2 as $t, 6 as $t], + [3 as $t, 2 as $t, 7 as $t], + ]; + let e: Array2<$t> = a.eye_like(); + } + } + }; + } + + make_test!(isize); + make_test!(usize); + make_test!(i8); + make_test!(u8); + make_test!(i16); + make_test!(u16); + make_test!(i32); + make_test!(u32); + make_test!(i64); + make_test!(u64); + make_test!(f32); + make_test!(f64); + +} \ No newline at end of file diff --git a/argmin-math-ndarray-linalg-tests/src/inv.rs b/argmin-math-ndarray-linalg-tests/src/inv.rs new file mode 100644 index 000000000..806c701ea --- /dev/null +++ b/argmin-math-ndarray-linalg-tests/src/inv.rs @@ -0,0 +1,59 @@ +// Copyright 2018-2022 argmin developers +// +// Licensed under the Apache License, Version 2.0 or the MIT license , 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::ArgminInv; + + use ndarray::array; + use ndarray::Array2; + + + use paste::item; + + macro_rules! make_test { + ($t:ty) => { + item! { + #[test] + fn []() { + let a = array![ + [2 as $t, 5 as $t], + [1 as $t, 3 as $t], + ]; + let target = array![ + [3 as $t, -5 as $t], + [-1 as $t, 2 as $t], + ]; + 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); + } + } + } + } + + item! { + #[test] + fn []() { + 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); + } + } + }; + } + + make_test!(f32); + make_test!(f64); +} diff --git a/argmin-math-ndarray-linalg-tests/src/l1norm.rs b/argmin-math-ndarray-linalg-tests/src/l1norm.rs new file mode 100644 index 000000000..eef89c14e --- /dev/null +++ b/argmin-math-ndarray-linalg-tests/src/l1norm.rs @@ -0,0 +1,85 @@ +// Copyright 2018-2022 argmin developers +// +// Licensed under the Apache License, Version 2.0 or the MIT license , 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::ArgminL1Norm; + use ndarray::{array, Array1}; + use num_complex::Complex; + use paste::item; + + macro_rules! make_test { + ($t:ty) => { + item! { + #[test] + fn []() { + 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); + } + } + + item! { + #[test] + fn []() { + 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); + } + } + }; + } + + macro_rules! make_test_signed { + ($t:ty) => { + item! { + #[test] + fn []() { + 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); + } + } + + item! { + #[test] + fn []() { + 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); + } + } + }; + } + + make_test!(isize); + make_test!(usize); + make_test!(i8); + make_test!(u8); + make_test!(i16); + make_test!(u16); + make_test!(i32); + make_test!(u32); + make_test!(i64); + make_test!(u64); + make_test!(f32); + make_test!(f64); + + make_test_signed!(isize); + make_test_signed!(i8); + make_test_signed!(i16); + make_test_signed!(i32); + make_test_signed!(i64); + make_test_signed!(f32); + make_test_signed!(f64); +} diff --git a/argmin-math-ndarray-linalg-tests/src/l2norm.rs b/argmin-math-ndarray-linalg-tests/src/l2norm.rs new file mode 100644 index 000000000..65bbd9ed6 --- /dev/null +++ b/argmin-math-ndarray-linalg-tests/src/l2norm.rs @@ -0,0 +1,87 @@ +// Copyright 2018-2022 argmin developers +// +// Licensed under the Apache License, Version 2.0 or the MIT license , 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::ArgminL2Norm; + + use ndarray::{array, Array1}; + use num_complex::Complex; + use num_integer::Roots; + use paste::item; + + macro_rules! make_test { + ($t:ty) => { + item! { + #[test] + fn []() { + 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); + } + } + + item! { + #[test] + fn []() { + 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); + } + } + }; + } + + macro_rules! make_test_signed { + ($t:ty) => { + item! { + #[test] + fn []() { + 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); + } + } + + item! { + #[test] + fn []() { + 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); + } + } + }; + } + + make_test!(isize); + make_test!(usize); + make_test!(i8); + make_test!(u8); + make_test!(i16); + make_test!(u16); + make_test!(i32); + make_test!(u32); + make_test!(i64); + make_test!(u64); + make_test!(f32); + make_test!(f64); + + make_test_signed!(isize); + make_test_signed!(i8); + make_test_signed!(i16); + make_test_signed!(i32); + make_test_signed!(i64); + make_test_signed!(f32); + make_test_signed!(f64); +} diff --git a/argmin-math-ndarray-linalg-tests/src/minmax.rs b/argmin-math-ndarray-linalg-tests/src/minmax.rs new file mode 100644 index 000000000..9f8a41796 --- /dev/null +++ b/argmin-math-ndarray-linalg-tests/src/minmax.rs @@ -0,0 +1,80 @@ +// Copyright 2018-2022 argmin developers +// +// Licensed under the Apache License, Version 2.0 or the MIT license , 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::ArgminMinMax; + use ndarray::{Array1, Array2}; + use ndarray::array; + use paste::item; + + macro_rules! make_test { + ($t:ty) => { + item! { + #[test] + fn []() { + let a = array![1 as $t, 4 as $t, 8 as $t]; + let b = array![2 as $t, 3 as $t, 4 as $t]; + let target_max = array![2 as $t, 4 as $t, 8 as $t]; + let target_min = array![1 as $t, 3 as $t, 4 as $t]; + 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); + } + } + } + + item! { + #[test] + fn []() { + let a = array![ + [1 as $t, 4 as $t, 8 as $t], + [2 as $t, 5 as $t, 9 as $t] + ]; + let b = array![ + [2 as $t, 3 as $t, 4 as $t], + [3 as $t, 4 as $t, 5 as $t] + ]; + let target_max = array![ + [2 as $t, 4 as $t, 8 as $t], + [3 as $t, 5 as $t, 9 as $t] + ]; + let target_min = array![ + [1 as $t, 3 as $t, 4 as $t], + [2 as $t, 4 as $t, 5 as $t] + ]; + let res_max = as ArgminMinMax>::max(&a, &b); + 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); + } + } + } + } + }; + } + + make_test!(i8); + make_test!(u8); + make_test!(i16); + make_test!(u16); + make_test!(i32); + 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-linalg-tests/src/mul.rs b/argmin-math-ndarray-linalg-tests/src/mul.rs new file mode 100644 index 000000000..fc5f7a6f0 --- /dev/null +++ b/argmin-math-ndarray-linalg-tests/src/mul.rs @@ -0,0 +1,410 @@ +// Copyright 2018-2022 argmin developers +// +// Licensed under the Apache License, Version 2.0 or the MIT license , 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::ArgminMul; + use ndarray::{Array1, Array2}; + use num_complex::Complex; + use ndarray::array; + use paste::item; + + macro_rules! make_test { + ($t:ty) => { + item! { + #[test] + fn []() { + let a = array![1 as $t, 4 as $t, 8 as $t]; + let b = 2 as $t; + 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); + } + } + } + + item! { + #[test] + fn []() { + let a = array![ + Complex::new(5 as $t, 3 as $t), + Complex::new(8 as $t, 2 as $t) + ]; + let b = Complex::new(2 as $t, 3 as $t); + 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); + } + } + } + + item! { + #[test] + fn []() { + let a = array![ + Complex::new(5 as $t, 3 as $t), + Complex::new(8 as $t, 2 as $t) + ]; + let b = 2 as $t; + 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); + } + } + } + + item! { + #[test] + fn []() { + let a = array![1 as $t, 4 as $t, 8 as $t]; + let b = 2 as $t; + 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); + } + } + } + + item! { + #[test] + fn []() { + let a = array![ + Complex::new(5 as $t, 3 as $t), + Complex::new(8 as $t, 2 as $t) + ]; + let b = Complex::new(2 as $t, 3 as $t); + 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); + } + } + } + + item! { + #[test] + fn []() { + let a = array![ + Complex::new(5 as $t, 3 as $t), + Complex::new(8 as $t, 2 as $t) + ]; + let b = 2 as $t; + 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); + } + } + } + + item! { + #[test] + fn []() { + let a = array![1 as $t, 4 as $t, 8 as $t]; + let b = array![2 as $t, 3 as $t, 4 as $t]; + 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); + } + } + } + + item! { + #[test] + fn []() { + let a = array![ + Complex::new(5 as $t, 3 as $t), + Complex::new(8 as $t, 2 as $t) + ]; + let b = array![ + Complex::new(2 as $t, 3 as $t), + Complex::new(1 as $t, 2 as $t) + ]; + 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); + } + } + } + + item! { + #[test] + #[should_panic] + fn []() { + let a = array![1 as $t, 4 as $t]; + let b = array![41 as $t, 38 as $t, 34 as $t]; + as ArgminMul, Array1<$t>>>::mul(&a, &b); + } + } + + item! { + #[test] + #[should_panic] + fn []() { + let a = array![]; + let b = array![41 as $t, 38 as $t, 34 as $t]; + as ArgminMul, Array1<$t>>>::mul(&a, &b); + } + } + + item! { + #[test] + #[should_panic] + fn []() { + let a = array![41 as $t, 38 as $t, 34 as $t]; + let b = array![]; + as ArgminMul, Array1<$t>>>::mul(&a, &b); + } + } + + item! { + #[test] + fn []() { + let a = array![ + [1 as $t, 4 as $t, 8 as $t], + [2 as $t, 5 as $t, 9 as $t] + ]; + let b = array![ + [2 as $t, 3 as $t, 4 as $t], + [3 as $t, 4 as $t, 5 as $t] + ]; + let target = array![ + [2 as $t, 12 as $t, 32 as $t], + [6 as $t, 20 as $t, 45 as $t] + ]; + 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); + } + } + } + } + + item! { + #[test] + fn []() { + let a = array![ + [Complex::new(5 as $t, 3 as $t), Complex::new(8 as $t, 2 as $t)], + [Complex::new(4 as $t, 2 as $t), Complex::new(7 as $t, 1 as $t)], + [Complex::new(3 as $t, 1 as $t), Complex::new(6 as $t, 2 as $t)], + ]; + let b = array![ + [Complex::new(5 as $t, 3 as $t), Complex::new(8 as $t, 2 as $t)], + [Complex::new(4 as $t, 2 as $t), Complex::new(7 as $t, 1 as $t)], + [Complex::new(3 as $t, 1 as $t), Complex::new(6 as $t, 2 as $t)], + ]; + let target = array![ + [a[(0, 0)] * b[(0, 0)], a[(0, 1)] * b[(0, 1)]], + [a[(1, 0)] * b[(1, 0)], a[(1, 1)] * b[(1, 1)]], + [a[(2, 0)] * b[(2, 0)], a[(2, 1)] * b[(2, 1)]], + ]; + 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); + } + } + } + } + + item! { + #[test] + #[should_panic] + fn []() { + let a = array![ + [1 as $t, 4 as $t, 8 as $t], + [2 as $t, 5 as $t, 9 as $t] + ]; + let b = array![ + [41 as $t, 38 as $t], + ]; + as ArgminMul, Array2<$t>>>::mul(&a, &b); + } + } + + item! { + #[test] + #[should_panic] + fn []() { + let a = array![ + [1 as $t, 4 as $t, 8 as $t], + [2 as $t, 5 as $t, 9 as $t] + ]; + let b = array![[]]; + as ArgminMul, Array2<$t>>>::mul(&a, &b); + } + } + + item! { + #[test] + fn []() { + let a = array![ + [1 as $t, 4 as $t, 8 as $t], + [2 as $t, 5 as $t, 9 as $t] + ]; + let b = 2 as $t; + let target = array![ + [2 as $t, 8 as $t, 16 as $t], + [4 as $t, 10 as $t, 18 as $t] + ]; + 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); + } + } + } + } + + item! { + #[test] + fn []() { + let a = array![ + [Complex::new(5 as $t, 3 as $t), Complex::new(8 as $t, 2 as $t)], + [Complex::new(4 as $t, 2 as $t), Complex::new(7 as $t, 1 as $t)], + [Complex::new(3 as $t, 1 as $t), Complex::new(6 as $t, 2 as $t)], + ]; + let b = Complex::new(3 as $t, 2 as $t); + let target = array![ + [a[(0, 0)] * b, a[(0, 1)] * b], + [a[(1, 0)] * b, a[(1, 1)] * b], + [a[(2, 0)] * b, a[(2, 1)] * b], + ]; + 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); + } + } + } + } + + item! { + #[test] + fn []() { + let a = array![ + [Complex::new(5 as $t, 3 as $t), Complex::new(8 as $t, 2 as $t)], + [Complex::new(4 as $t, 2 as $t), Complex::new(7 as $t, 1 as $t)], + [Complex::new(3 as $t, 1 as $t), Complex::new(6 as $t, 2 as $t)], + ]; + let b = 3 as $t; + let target = array![ + [a[(0, 0)] * b, a[(0, 1)] * b], + [a[(1, 0)] * b, a[(1, 1)] * b], + [a[(2, 0)] * b, a[(2, 1)] * b], + ]; + 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); + } + } + } + } + + item! { + #[test] + fn []() { + let b = array![ + [1 as $t, 4 as $t, 8 as $t], + [2 as $t, 5 as $t, 9 as $t] + ]; + let a = 2 as $t; + let target = array![ + [2 as $t, 8 as $t, 16 as $t], + [4 as $t, 10 as $t, 18 as $t] + ]; + 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); + } + } + } + } + + item! { + #[test] + fn []() { + let a = array![ + [Complex::new(5 as $t, 3 as $t), Complex::new(8 as $t, 2 as $t)], + [Complex::new(4 as $t, 2 as $t), Complex::new(7 as $t, 1 as $t)], + [Complex::new(3 as $t, 1 as $t), Complex::new(6 as $t, 2 as $t)], + ]; + let b = Complex::new(3 as $t, 2 as $t); + let target = array![ + [a[(0, 0)] * b, a[(0, 1)] * b], + [a[(1, 0)] * b, a[(1, 1)] * b], + [a[(2, 0)] * b, a[(2, 1)] * b], + ]; + 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); + } + } + } + } + + item! { + #[test] + fn []() { + let a = array![ + [Complex::new(5 as $t, 3 as $t), Complex::new(8 as $t, 2 as $t)], + [Complex::new(4 as $t, 2 as $t), Complex::new(7 as $t, 1 as $t)], + [Complex::new(3 as $t, 1 as $t), Complex::new(6 as $t, 2 as $t)], + ]; + let b = 3 as $t; + let target = array![ + [a[(0, 0)] * b, a[(0, 1)] * b], + [a[(1, 0)] * b, a[(1, 1)] * b], + [a[(2, 0)] * b, a[(2, 1)] * b], + ]; + 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); + } + } + } + } + }; + } + + make_test!(i8); + make_test!(u8); + make_test!(i16); + make_test!(u16); + make_test!(i32); + 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-linalg-tests/src/random.rs b/argmin-math-ndarray-linalg-tests/src/random.rs new file mode 100644 index 000000000..d7f6cb2b4 --- /dev/null +++ b/argmin-math-ndarray-linalg-tests/src/random.rs @@ -0,0 +1,68 @@ +// Copyright 2018-2022 argmin developers +// +// Licensed under the Apache License, Version 2.0 or the MIT license , 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 paste::item; + + macro_rules! make_test { + ($t:ty) => { + item! { + #[test] + fn []() { + let a = array![1 as $t, 2 as $t, 4 as $t]; + let b = array![2 as $t, 3 as $t, 5 as $t]; + let random = Array1::<$t>::rand_from_range(&a, &b); + for i in 0..3usize { + assert!(random[i] >= a[i]); + assert!(random[i] <= b[i]); + } + } + } + + item! { + #[test] + fn []() { + let a = array![ + [1 as $t, 2 as $t, 4 as $t], + [2 as $t, 3 as $t, 5 as $t] + ]; + let b = array![ + [2 as $t, 3 as $t, 5 as $t], + [3 as $t, 4 as $t, 6 as $t] + ]; + let random = Array2::<$t>::rand_from_range(&a, &b); + for i in 0..3 { + for j in 0..2 { + assert!(random[(j, i)] >= a[(j, i)]); + assert!(random[(j, i)] <= b[(j, i)]); + } + } + } + } + }; + } + + make_test!(i8); + make_test!(u8); + make_test!(i16); + make_test!(u16); + make_test!(i32); + 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-linalg-tests/src/scaledadd.rs b/argmin-math-ndarray-linalg-tests/src/scaledadd.rs new file mode 100644 index 000000000..78be27783 --- /dev/null +++ b/argmin-math-ndarray-linalg-tests/src/scaledadd.rs @@ -0,0 +1,168 @@ +// Copyright 2018-2022 argmin developers +// +// Licensed under the Apache License, Version 2.0 or the MIT license , 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::ArgminScaledAdd; + use ndarray::{array, Array1, Array2}; + use paste::item; + + macro_rules! make_test { + ($t:ty) => { + item! { + #[test] + fn []() { + let a = array![1 as $t, 2 as $t, 3 as $t]; + let b = 2 as $t; + let c = array![4 as $t, 5 as $t, 6 as $t]; + 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); + } + } + } + + item! { + #[test] + #[should_panic] + fn []() { + let a = array![1 as $t, 2 as $t, 3 as $t]; + let b = 2 as $t; + let c = array![4 as $t, 5 as $t]; + as ArgminScaledAdd, $t, Array1<$t>>>::scaled_add(&a, &b, &c); + } + } + + item! { + #[test] + #[should_panic] + fn []() { + let a = array![1 as $t, 2 as $t]; + let b = 2 as $t; + let c = array![4 as $t, 5 as $t, 6 as $t]; + as ArgminScaledAdd, $t, Array1<$t>>>::scaled_add(&a, &b, &c); + } + } + + item! { + #[test] + fn []() { + let a = array![1 as $t, 2 as $t, 3 as $t]; + let b = array![3 as $t, 2 as $t, 1 as $t]; + let c = array![4 as $t, 5 as $t, 6 as $t]; + 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); + } + } + } + + item! { + #[test] + #[should_panic] + fn []() { + let a = array![1 as $t, 2 as $t]; + let b = array![3 as $t, 2 as $t, 1 as $t]; + let c = array![4 as $t, 5 as $t, 6 as $t]; + as ArgminScaledAdd, Array1<$t>, Array1<$t>>>::scaled_add(&a, &b, &c); + } + } + + item! { + #[test] + #[should_panic] + fn []() { + let a = array![1 as $t, 2 as $t, 3 as $t]; + let b = array![3 as $t, 2 as $t]; + let c = array![4 as $t, 5 as $t, 6 as $t]; + as ArgminScaledAdd, Array1<$t>, Array1<$t>>>::scaled_add(&a, &b, &c); + } + } + + item! { + #[test] + #[should_panic] + fn []() { + let a = array![1 as $t, 2 as $t, 3 as $t]; + let b = array![3 as $t, 2 as $t, 1 as $t]; + let c = array![4 as $t, 5 as $t]; + as ArgminScaledAdd, Array1<$t>, Array1<$t>>>::scaled_add(&a, &b, &c); + } + } + + item! { + #[test] + fn []() { + let a = array![ + [1 as $t, 2 as $t], + [3 as $t, 4 as $t], + ]; + let b = array![ + [4 as $t, 3 as $t], + [2 as $t, 1 as $t], + ]; + let c = array![ + [1 as $t, 2 as $t], + [2 as $t, 1 as $t], + ]; + let res = as ArgminScaledAdd, Array2<$t>, Array2<$t>>>::scaled_add(&a, &b, &c); + let target = array![ + [5 as $t, 8 as $t], + [7 as $t, 5 as $t], + ]; + for i in 0..2 { + for j in 0..2 { + assert!((((res[(i, j)] - target[(i, j)]) as f64).abs()) < std::f64::EPSILON); + } + } + } + } + + item! { + #[test] + fn []() { + let a = array![ + [1 as $t, 2 as $t], + [3 as $t, 4 as $t], + ]; + let b = 2 as $t; + let c = array![ + [1 as $t, 2 as $t], + [2 as $t, 1 as $t], + ]; + let res = as ArgminScaledAdd, $t, Array2<$t>>>::scaled_add(&a, &b, &c); + let target = array![ + [3 as $t, 6 as $t], + [7 as $t, 6 as $t], + ]; + for i in 0..2 { + for j in 0..2 { + assert!((((res[(i, j)] - target[(i, j)]) as f64).abs()) < std::f64::EPSILON); + } + } + } + } + }; + } + + // make_test!(isize); + // make_test!(usize); + make_test!(i8); + make_test!(u8); + make_test!(i16); + make_test!(u16); + make_test!(i32); + make_test!(u32); + make_test!(i64); + make_test!(u64); + make_test!(f32); + make_test!(f64); +} diff --git a/argmin-math-ndarray-linalg-tests/src/scaledsub.rs b/argmin-math-ndarray-linalg-tests/src/scaledsub.rs new file mode 100644 index 000000000..d324babb2 --- /dev/null +++ b/argmin-math-ndarray-linalg-tests/src/scaledsub.rs @@ -0,0 +1,166 @@ +// Copyright 2018-2022 argmin developers +// +// Licensed under the Apache License, Version 2.0 or the MIT license , 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::ArgminScaledSub; + use ndarray::{array, Array1, Array2}; + use paste::item; + + macro_rules! make_test { + ($t:ty) => { + item! { + #[test] + fn []() { + let a = array![10 as $t, 20 as $t, 30 as $t]; + let b = 2 as $t; + let c = array![4 as $t, 5 as $t, 6 as $t]; + 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); + } + } + } + + item! { + #[test] + #[should_panic] + fn []() { + let a = array![1 as $t, 2 as $t, 3 as $t]; + let b = 2 as $t; + let c = array![4 as $t, 5 as $t]; + as ArgminScaledSub, $t, Array1<$t>>>::scaled_sub(&a, &b, &c); + } + } + + item! { + #[test] + #[should_panic] + fn []() { + let a = array![1 as $t, 2 as $t]; + let b = 2 as $t; + let c = array![4 as $t, 5 as $t, 6 as $t]; + as ArgminScaledSub, $t, Array1<$t>>>::scaled_sub(&a, &b, &c); + } + } + + item! { + #[test] + fn []() { + let a = array![20 as $t, 20 as $t, 30 as $t]; + let b = array![3 as $t, 2 as $t, 1 as $t]; + let c = array![4 as $t, 5 as $t, 6 as $t]; + 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); + } + } + } + + item! { + #[test] + #[should_panic] + fn []() { + let a = array![1 as $t, 2 as $t]; + let b = array![3 as $t, 2 as $t, 1 as $t]; + let c = array![4 as $t, 5 as $t, 6 as $t]; + as ArgminScaledSub, Array1<$t>, Array1<$t>>>::scaled_sub(&a, &b, &c); + } + } + + item! { + #[test] + #[should_panic] + fn []() { + let a = array![1 as $t, 2 as $t, 3 as $t]; + let b = array![3 as $t, 2 as $t]; + let c = array![4 as $t, 5 as $t, 6 as $t]; + as ArgminScaledSub, Array1<$t>, Array1<$t>>>::scaled_sub(&a, &b, &c); + } + } + + item! { + #[test] + #[should_panic] + fn []() { + let a = array![1 as $t, 2 as $t, 3 as $t]; + let b = array![3 as $t, 2 as $t, 1 as $t]; + let c = array![4 as $t, 5 as $t]; + as ArgminScaledSub, Array1<$t>, Array1<$t>>>::scaled_sub(&a, &b, &c); + } + } + + item! { + #[test] + fn []() { + let a = array![ + [10 as $t, 20 as $t], + [30 as $t, 40 as $t], + ]; + let b = array![ + [4 as $t, 3 as $t], + [2 as $t, 1 as $t], + ]; + let c = array![ + [1 as $t, 2 as $t], + [2 as $t, 1 as $t], + ]; + let res = as ArgminScaledSub, Array2<$t>, Array2<$t>>>::scaled_sub(&a, &b, &c); + let target = array![ + [6 as $t, 14 as $t], + [26 as $t, 39 as $t], + ]; + for i in 0..2 { + for j in 0..2 { + assert!((((res[(i, j)] - target[(i, j)]) as f64).abs()) < std::f64::EPSILON); + } + } + } + } + + item! { + #[test] + fn []() { + let a = array![ + [10 as $t, 20 as $t], + [30 as $t, 40 as $t], + ]; + let b = 2 as $t; + let c = array![ + [1 as $t, 2 as $t], + [2 as $t, 1 as $t], + ]; + let res = as ArgminScaledSub, $t, Array2<$t>>>::scaled_sub(&a, &b, &c); + let target = array![ + [8 as $t, 16 as $t], + [26 as $t, 38 as $t], + ]; + for i in 0..2 { + for j in 0..2 { + assert!((((res[(i, j)] - target[(i, j)]) as f64).abs()) < std::f64::EPSILON); + } + } + } + } + }; + } + + make_test!(i8); + make_test!(u8); + make_test!(i16); + make_test!(u16); + make_test!(i32); + make_test!(u32); + make_test!(i64); + make_test!(u64); + make_test!(f32); + make_test!(f64); +} diff --git a/argmin-math-ndarray-linalg-tests/src/signum.rs b/argmin-math-ndarray-linalg-tests/src/signum.rs new file mode 100644 index 000000000..e081a0da2 --- /dev/null +++ b/argmin-math-ndarray-linalg-tests/src/signum.rs @@ -0,0 +1,130 @@ +// Copyright 2018-2022 argmin developers +// +// Licensed under the Apache License, Version 2.0 or the MIT license , 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::{Array1, Array2}; + use num_complex::Complex; + use ndarray::array; + use paste::item; + + macro_rules! make_test { + ($t:ty) => { + item! { + #[test] + fn []() { + let x = Array1::from(vec![ + Complex::new(1 as $t, 2 as $t), + Complex::new(4 as $t, -3 as $t), + Complex::new(-8 as $t, 4 as $t), + Complex::new(-8 as $t, -1 as $t), + ]); + let y = Array1::from(vec![ + Complex::new(1 as $t, 1 as $t), + Complex::new(1 as $t, -1 as $t), + Complex::new(-1 as $t, 1 as $t), + Complex::new(-1 as $t, -1 as $t), + ]); + let res = > as ArgminSignum>::signum(x); + for i in 0..4 { + let tmp = y[i] - res[i]; + let norm = ((tmp.re * tmp.re + tmp.im * tmp.im) as f64).sqrt(); + assert!(norm < std::f64::EPSILON); + } + } + } + + item! { + #[test] + fn []() { + let x = Array1::from(vec![1 as $t, -4 as $t, 8 as $t]); + let y = Array1::from(vec![1 as $t, -1 as $t, 1 as $t]); + let res = as ArgminSignum>::signum(x); + for i in 0..3 { + let diff = (y[i] - res[i]).abs() as f64; + assert!(diff < std::f64::EPSILON); + } + } + } + + item! { + #[test] + fn []() { + let x = array![ + [ + Complex::new(1 as $t, 2 as $t), + Complex::new(4 as $t, -3 as $t), + Complex::new(-8 as $t, 4 as $t), + Complex::new(-8 as $t, -1 as $t), + ], + [ + Complex::new(-1 as $t, -2 as $t), + Complex::new(4 as $t, 3 as $t), + Complex::new(-8 as $t, -4 as $t), + Complex::new(8 as $t, -1 as $t), + ] + ]; + let y = array![ + [ + Complex::new(1 as $t, 1 as $t), + Complex::new(1 as $t, -1 as $t), + Complex::new(-1 as $t, 1 as $t), + Complex::new(-1 as $t, -1 as $t), + ], + [ + Complex::new(-1 as $t, -1 as $t), + Complex::new(1 as $t, 1 as $t), + Complex::new(-1 as $t, -1 as $t), + Complex::new(1 as $t, -1 as $t), + ] + ]; + let res = > as ArgminSignum>::signum(x); + for j in 0..2 { + for i in 0..4 { + let tmp = y[(j, i)] - res[(j, i)]; + let norm = ((tmp.re * tmp.re + tmp.im * tmp.im) as f64).sqrt(); + assert!(norm < std::f64::EPSILON); + } + } + } + } + + item! { + #[test] + fn []() { + let x = array![ + [1 as $t, -4 as $t, 8 as $t], + [-2 as $t, 2 as $t, -8 as $t] + ]; + let y = array![ + [1 as $t, -1 as $t, 1 as $t], + [-1 as $t, 1 as $t, -1 as $t] + ]; + let res = as ArgminSignum>::signum(x); + for j in 0..2 { + for i in 0..3 { + let diff = (y[(j, i)] - res[(j, i)]).abs() as f64; + assert!(diff < std::f64::EPSILON); + } + } + } + } + }; + } + + make_test!(isize); + make_test!(i8); + make_test!(i16); + make_test!(i32); + make_test!(i64); + make_test!(f32); + make_test!(f64); +} diff --git a/argmin-math-ndarray-linalg-tests/src/sub.rs b/argmin-math-ndarray-linalg-tests/src/sub.rs new file mode 100644 index 000000000..f397b77a9 --- /dev/null +++ b/argmin-math-ndarray-linalg-tests/src/sub.rs @@ -0,0 +1,175 @@ +// Copyright 2018-2022 argmin developers +// +// Licensed under the Apache License, Version 2.0 or the MIT license , 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::ArgminSub; + use ndarray::{Array1, Array2}; + + use ndarray::array; + use paste::item; + + macro_rules! make_test { + ($t:ty) => { + item! { + #[test] + fn []() { + let a = array![36 as $t, 39 as $t, 43 as $t]; + let b = 1 as $t; + 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); + } + } + } + + item! { + #[test] + fn []() { + let a = array![1 as $t, 4 as $t, 8 as $t]; + let b = 34 as $t; + 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); + } + } + } + + item! { + #[test] + fn []() { + let a = array![41 as $t, 38 as $t, 34 as $t]; + let b = array![1 as $t, 4 as $t, 8 as $t]; + 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); + } + } + } + + item! { + #[test] + #[should_panic] + fn []() { + let a = array![41 as $t, 38 as $t, 34 as $t]; + let b = array![1 as $t, 4 as $t]; + as ArgminSub, Array1<$t>>>::sub(&a, &b); + } + } + + item! { + #[test] + #[should_panic] + fn []() { + let a = array![]; + let b = array![41 as $t, 38 as $t, 34 as $t]; + as ArgminSub, Array1<$t>>>::sub(&a, &b); + } + } + + item! { + #[test] + #[should_panic] + fn []() { + let a = array![41 as $t, 38 as $t, 34 as $t]; + let b = array![]; + as ArgminSub, Array1<$t>>>::sub(&a, &b); + } + } + + item! { + #[test] + fn []() { + let a = array![ + [43 as $t, 46 as $t, 50 as $t], + [44 as $t, 47 as $t, 51 as $t] + ]; + let b = array![ + [1 as $t, 4 as $t, 8 as $t], + [2 as $t, 5 as $t, 9 as $t] + ]; + let target = array![ + [42 as $t, 42 as $t, 42 as $t], + [42 as $t, 42 as $t, 42 as $t] + ]; + 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); + } + } + } + } + + item! { + #[test] + fn []() { + let a = array![ + [43 as $t, 46 as $t, 50 as $t], + [44 as $t, 47 as $t, 51 as $t] + ]; + let b = 2 as $t; + let target = array![ + [41 as $t, 44 as $t, 48 as $t], + [42 as $t, 45 as $t, 49 as $t] + ]; + 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); + } + } + } + } + + item! { + #[test] + #[should_panic] + fn []() { + let a = array![ + [41 as $t, 38 as $t], + ]; + let b = array![ + [1 as $t, 4 as $t, 8 as $t], + [2 as $t, 5 as $t, 9 as $t] + ]; + as ArgminSub, Array2<$t>>>::sub(&a, &b); + } + } + + item! { + #[test] + #[should_panic] + fn []() { + let a = array![ + [1 as $t, 4 as $t, 8 as $t], + [2 as $t, 5 as $t, 9 as $t] + ]; + let b = array![[]]; + as ArgminSub, Array2<$t>>>::sub(&a, &b); + } + } + }; + } + + make_test!(i8); + make_test!(u8); + make_test!(i16); + make_test!(u16); + make_test!(i32); + make_test!(u32); + make_test!(i64); + make_test!(u64); + make_test!(f32); + make_test!(f64); +} diff --git a/argmin-math-ndarray-linalg-tests/src/transpose.rs b/argmin-math-ndarray-linalg-tests/src/transpose.rs new file mode 100644 index 000000000..ead66b3a7 --- /dev/null +++ b/argmin-math-ndarray-linalg-tests/src/transpose.rs @@ -0,0 +1,90 @@ +// Copyright 2018-2022 argmin developers +// +// Licensed under the Apache License, Version 2.0 or the MIT license , at your option. This file may not be +// copied, modified, or distributed except according to those terms. + +// 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 argmin_math::ArgminTranspose; + use ndarray::{Array1, Array2}; + + use ndarray::array; + use paste::item; + + macro_rules! make_test { + ($t:ty) => { + item! { + // not sure if this is such a smart test :D + #[test] + fn []() { + let a = array![1 as $t, 4 as $t]; + 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); + } + } + } + + item! { + #[test] + fn []() { + let a = array![ + [1 as $t, 4 as $t], + [8 as $t, 7 as $t] + ]; + let target = array![ + [1 as $t, 8 as $t], + [4 as $t, 7 as $t] + ]; + 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); + } + } + } + } + + item! { + #[test] + fn []() { + let a = array![ + [1 as $t, 4 as $t], + [8 as $t, 7 as $t], + [3 as $t, 6 as $t] + ]; + let target = array![ + [1 as $t, 8 as $t, 3 as $t], + [4 as $t, 7 as $t, 6 as $t] + ]; + 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); + } + } + } + } + }; + } + + make_test!(i8); + make_test!(u8); + make_test!(i16); + make_test!(u16); + make_test!(i32); + make_test!(u32); + make_test!(i64); + make_test!(u64); + make_test!(f32); + make_test!(f64); +} diff --git a/argmin-math-ndarray-linalg-tests/src/zero.rs b/argmin-math-ndarray-linalg-tests/src/zero.rs new file mode 100644 index 000000000..9eba19e55 --- /dev/null +++ b/argmin-math-ndarray-linalg-tests/src/zero.rs @@ -0,0 +1,92 @@ +// Copyright 2018-2022 argmin developers +// +// Licensed under the Apache License, Version 2.0 or the MIT license , 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 paste::item; + + macro_rules! make_test { + ($t:ty) => { + // item! { + // #[test] + // fn []() { + // let a = as ArgminZero>::zero(); + // let b: Array1<$t> = array![]; + // assert_eq!(a, b); + // } + // } + + item! { + #[test] + fn []() { + let t: Array1<$t> = array![]; + let a = t.zero_like(); + assert_eq!(t, a); + } + } + + item! { + #[test] + 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); + } + } + } + + // item! { + // #[test] + // fn []() { + // let a = as ArgminZero>::zero(); + // let b: Array2<$t> = Array2::zeros((0, 0)); + // assert_eq!(a, b); + // } + // } + + item! { + #[test] + fn []() { + let t: Array2<$t> = Array2::zeros((0, 0)); + let a = t.zero_like(); + assert_eq!(t, a); + } + } + + item! { + #[test] + fn []() { + 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); + } + } + } + } + }; + } + + make_test!(isize); + make_test!(usize); + make_test!(i8); + make_test!(u8); + make_test!(i16); + make_test!(u16); + make_test!(i32); + make_test!(u32); + make_test!(i64); + make_test!(u64); + make_test!(f32); + make_test!(f64); +} diff --git a/argmin-math/Cargo.toml b/argmin-math/Cargo.toml index 01ee3a987..da6590014 100644 --- a/argmin-math/Cargo.toml +++ b/argmin-math/Cargo.toml @@ -93,19 +93,6 @@ ndarray_v0_14-nolinalg-serde = ["ndarray_v0_14-nolinalg", "ndarray_0_14/serde-1" ndarray_v0_13-nolinalg = ["ndarray_0_13", "num-complex_0_2", "ndarray_all"] ndarray_v0_13-nolinalg-serde = ["ndarray_v0_13-nolinalg", "ndarray_0_13/serde-1"] -# For development and running the tests a backend for `ndarray-linalg` must be chosen. Normally -# one would add those as dev dependencies (the features would then be unified with the regular -# dependencies). However, linking somehow fails when the non-dev `ndarra-linalg` dependency is -# missing (which is the case for the `*-nolinalg*` features of the ndarray backend). To fix that, -# the `_dev_linalg_*` features were introduced. When testing and developing with one of the -# ndarray features with linalg support on, the appropriate `_dev_linalg_*` feature must be turned -# on as well. Note that the version number in `_dev_linalg_*` is always one below the `ndarray` -# version. For instance, for ndarray 0.15, one would use the `_dev_linalg_0_14` feature. -_dev_linalg_latest = ["_dev_linalg_0_16"] -_dev_linalg_0_16 = ["ndarray-linalg_0_16/netlib-static"] -_dev_linalg_0_13 = ["ndarray-linalg_0_13/netlib-static"] -_dev_linalg_0_12 = ["ndarray-linalg_0_12/netlib"] - [badges] maintenance = { status = "actively-developed" } diff --git a/argmin-math/src/lib.rs b/argmin-math/src/lib.rs index d54e98ee6..eba89b020 100644 --- a/argmin-math/src/lib.rs +++ b/argmin-math/src/lib.rs @@ -233,7 +233,7 @@ mod vec; #[allow(unused_imports)] pub use crate::vec::*; -use anyhow::Error; +pub use anyhow::Error; /// Dot/scalar product of `T` and `self` pub trait ArgminDot { diff --git a/argmin-math/src/ndarray_m/add.rs b/argmin-math/src/ndarray_m/add.rs index d1d777c3f..17aaf4b92 100644 --- a/argmin-math/src/ndarray_m/add.rs +++ b/argmin-math/src/ndarray_m/add.rs @@ -61,166 +61,10 @@ make_add!(f64); make_add!(Complex); make_add!(Complex); +// The tests expect the name for the crate containing the tested functions to be argmin_math #[cfg(test)] -mod tests { - use super::*; - use ndarray::array; - use paste::item; - - macro_rules! make_test { - ($t:ty) => { - item! { - #[test] - fn []() { - let a = array![1 as $t, 4 as $t, 8 as $t]; - let b = 34 as $t; - 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); - } - } - } - - item! { - #[test] - fn []() { - let a = array![1 as $t, 4 as $t, 8 as $t]; - let b = 34 as $t; - 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); - } - } - } - - item! { - #[test] - fn []() { - let a = array![1 as $t, 4 as $t, 8 as $t]; - let b = array![41 as $t, 38 as $t, 34 as $t]; - 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); - } - } - } - - item! { - #[test] - #[should_panic] - fn []() { - let a = array![1 as $t, 4 as $t]; - let b = array![41 as $t, 38 as $t, 34 as $t]; - as ArgminAdd, Array1<$t>>>::add(&a, &b); - } - } - - item! { - #[test] - #[should_panic] - fn []() { - let a = array![]; - let b = array![41 as $t, 38 as $t, 34 as $t]; - as ArgminAdd, Array1<$t>>>::add(&a, &b); - } - } - - item! { - #[test] - #[should_panic] - fn []() { - let a = array![41 as $t, 38 as $t, 34 as $t]; - let b = array![]; - as ArgminAdd, Array1<$t>>>::add(&a, &b); - } - } - - item! { - #[test] - fn []() { - let a = array![ - [1 as $t, 4 as $t, 8 as $t], - [2 as $t, 5 as $t, 9 as $t] - ]; - let b = array![ - [41 as $t, 38 as $t, 34 as $t], - [40 as $t, 37 as $t, 33 as $t] - ]; - let target = array![ - [42 as $t, 42 as $t, 42 as $t], - [42 as $t, 42 as $t, 42 as $t] - ]; - 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); - } - } - } - } - - item! { - #[test] - fn []() { - let a = array![ - [1 as $t, 4 as $t, 8 as $t], - [2 as $t, 5 as $t, 9 as $t] - ]; - let b = 2 as $t; - let target = array![ - [3 as $t, 6 as $t, 10 as $t], - [4 as $t, 7 as $t, 11 as $t] - ]; - 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); - } - } - } - } - - item! { - #[test] - #[should_panic] - fn []() { - let a = array![ - [1 as $t, 4 as $t, 8 as $t], - [2 as $t, 5 as $t, 9 as $t] - ]; - let b = array![ - [41 as $t, 38 as $t], - ]; - as ArgminAdd, Array2<$t>>>::add(&a, &b); - } - } - - item! { - #[test] - #[should_panic] - fn []() { - let a = array![ - [1 as $t, 4 as $t, 8 as $t], - [2 as $t, 5 as $t, 9 as $t] - ]; - let b = array![[]]; - as ArgminAdd, Array2<$t>>>::add(&a, &b); - } - } - }; - } - - make_test!(i8); - make_test!(u8); - make_test!(i16); - make_test!(u16); - make_test!(i32); - make_test!(u32); - make_test!(i64); - make_test!(u64); - make_test!(f32); - make_test!(f64); -} +use crate as argmin_math; +include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../argmin-math-ndarray-linalg-tests/src/add.rs" +)); diff --git a/argmin-math/src/ndarray_m/conj.rs b/argmin-math/src/ndarray_m/conj.rs index 3e7169e20..b72a12161 100644 --- a/argmin-math/src/ndarray_m/conj.rs +++ b/argmin-math/src/ndarray_m/conj.rs @@ -49,93 +49,10 @@ make_conj!(Complex); make_conj!(Complex); make_conj!(Complex); +// The tests expect the name for the crate containing the tested functions to be argmin_math #[cfg(test)] -mod tests { - use super::*; - use ndarray::array; - use paste::item; - - macro_rules! make_test { - ($t:ty) => { - item! { - #[test] - fn []() { - let a = Array1::from(vec![ - Complex::new(1 as $t, 2 as $t), - Complex::new(4 as $t, -3 as $t), - Complex::new(8 as $t, 0 as $t) - ]); - let b = vec![ - Complex::new(1 as $t, -2 as $t), - Complex::new(4 as $t, 3 as $t), - Complex::new(8 as $t, 0 as $t) - ]; - 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); - } - } - } - - item! { - #[test] - fn []() { - let a = array![ - [Complex::new(1 as $t, 2 as $t), Complex::new(4 as $t, -5 as $t)], - [Complex::new(3 as $t, -5 as $t), Complex::new(8 as $t, 3 as $t)], - [Complex::new(4 as $t, -2 as $t), Complex::new(9 as $t, -9 as $t)], - ]; - let b = array![ - [Complex::new(1 as $t, -2 as $t), Complex::new(4 as $t, 5 as $t)], - [Complex::new(3 as $t, 5 as $t), Complex::new(8 as $t, -3 as $t)], - [Complex::new(4 as $t, 2 as $t), Complex::new(9 as $t, 9 as $t)], - ]; - 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); - } - } - } - } - - item! { - #[test] - fn []() { - 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); - } - } - } - - item! { - #[test] - fn []() { - let a = array![ - [1 as $t, 4 as $t, 8 as $t], - [2 as $t, 5 as $t, 9 as $t], - ]; - 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); - } - } - } - } - }; - } - - make_test!(isize); - make_test!(i8); - make_test!(i16); - make_test!(i32); - make_test!(i64); - make_test!(f32); - make_test!(f64); -} +use crate as argmin_math; +include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../argmin-math-ndarray-linalg-tests/src/conj.rs" +)); diff --git a/argmin-math/src/ndarray_m/div.rs b/argmin-math/src/ndarray_m/div.rs index 8b87c54c5..fe7a8c95d 100644 --- a/argmin-math/src/ndarray_m/div.rs +++ b/argmin-math/src/ndarray_m/div.rs @@ -54,145 +54,10 @@ make_div!(f64); make_div!(Complex); make_div!(Complex); +// The tests expect the name for the crate containing the tested functions to be argmin_math #[cfg(test)] -mod tests { - use super::*; - use ndarray::array; - use paste::item; - - macro_rules! make_test { - ($t:ty) => { - item! { - #[test] - fn []() { - let a = array![4 as $t, 16 as $t, 8 as $t]; - let b = 2 as $t; - 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); - } - } - } - - item! { - #[test] - fn []() { - let a = array![2 as $t, 4 as $t, 8 as $t]; - let b = 32 as $t; - 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); - } - } - } - - item! { - #[test] - fn []() { - let a = array![4 as $t, 9 as $t, 8 as $t]; - let b = array![2 as $t, 3 as $t, 4 as $t]; - 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); - } - } - } - - item! { - #[test] - #[should_panic] - fn []() { - let a = array![1 as $t, 4 as $t]; - let b = array![41 as $t, 38 as $t, 34 as $t]; - as ArgminDiv, Array1<$t>>>::div(&a, &b); - } - } - - item! { - #[test] - #[should_panic] - fn []() { - let a = array![]; - let b = array![41 as $t, 38 as $t, 34 as $t]; - as ArgminDiv, Array1<$t>>>::div(&a, &b); - } - } - - item! { - #[test] - #[should_panic] - fn []() { - let a = array![41 as $t, 38 as $t, 34 as $t]; - let b = array![]; - as ArgminDiv, Array1<$t>>>::div(&a, &b); - } - } - - item! { - #[test] - fn []() { - let a = array![ - [4 as $t, 12 as $t, 8 as $t], - [9 as $t, 20 as $t, 45 as $t] - ]; - let b = array![ - [2 as $t, 3 as $t, 4 as $t], - [3 as $t, 4 as $t, 5 as $t] - ]; - let target = array![ - [2 as $t, 4 as $t, 2 as $t], - [3 as $t, 5 as $t, 9 as $t] - ]; - 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); - } - } - } - } - - item! { - #[test] - #[should_panic] - fn []() { - let a = array![ - [1 as $t, 4 as $t, 8 as $t], - [2 as $t, 5 as $t, 9 as $t] - ]; - let b = array![ - [41 as $t, 38 as $t], - ]; - as ArgminDiv, Array2<$t>>>::div(&a, &b); - } - } - - item! { - #[test] - #[should_panic] - fn []() { - let a = array![ - [1 as $t, 4 as $t, 8 as $t], - [2 as $t, 5 as $t, 9 as $t] - ]; - let b = array![[]]; - as ArgminDiv, Array2<$t>>>::div(&a, &b); - } - } - }; - } - - make_test!(i8); - make_test!(u8); - make_test!(i16); - make_test!(u16); - make_test!(i32); - make_test!(u32); - make_test!(i64); - make_test!(u64); - make_test!(f32); - make_test!(f64); -} +use crate as argmin_math; +include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../argmin-math-ndarray-linalg-tests/src/div.rs" +)); diff --git a/argmin-math/src/ndarray_m/dot.rs b/argmin-math/src/ndarray_m/dot.rs index 05fa5cc9a..c32845b38 100644 --- a/argmin-math/src/ndarray_m/dot.rs +++ b/argmin-math/src/ndarray_m/dot.rs @@ -142,362 +142,10 @@ make_dot_ndarray!(usize); make_dot_ndarray!(f32); make_dot_ndarray!(f64); +// The tests expect the name for the crate containing the tested functions to be argmin_math #[cfg(test)] -mod tests { - use super::*; - use ndarray::array; - use paste::item; - - macro_rules! make_test { - ($t:ty) => { - item! { - #[test] - fn []() { - 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); - } - } - - item! { - #[test] - fn []() { - let a = array![1 as $t, 2 as $t, 3 as $t]; - let b = 2 as $t; - let product: Array1<$t> = - 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); - } - } - } - - item! { - #[test] - fn []() { - let a = array![1 as $t, 2 as $t, 3 as $t]; - let b = 2 as $t; - let product: Array1<$t> = - <$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); - } - } - } - - item! { - #[test] - fn []() { - 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 = array![ - [4 as $t, 5 as $t, 6 as $t], - [8 as $t, 10 as $t, 12 as $t], - [12 as $t, 15 as $t, 18 as $t] - ]; - let product: Array2<$t> = - 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); - } - } - } - } - - item! { - #[test] - fn []() { - let a = array![ - [1 as $t, 2 as $t, 3 as $t], - [4 as $t, 5 as $t, 6 as $t], - [7 as $t, 8 as $t, 9 as $t] - ]; - let b = array![1 as $t, 2 as $t, 3 as $t]; - let res = array![14 as $t, 32 as $t, 50 as $t]; - 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); - } - } - } - - item! { - #[test] - fn []() { - let a = array![ - [1 as $t, 2 as $t, 3 as $t], - [4 as $t, 5 as $t, 6 as $t], - [3 as $t, 2 as $t, 1 as $t] - ]; - let b = array![ - [3 as $t, 2 as $t, 1 as $t], - [6 as $t, 5 as $t, 4 as $t], - [2 as $t, 4 as $t, 3 as $t] - ]; - let res = array![ - [21 as $t, 24 as $t, 18 as $t], - [54 as $t, 57 as $t, 42 as $t], - [23 as $t, 20 as $t, 14 as $t] - ]; - let product: Array2<$t> = - 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); - } - } - } - } - - item! { - #[test] - fn []() { - let a = array![ - [1 as $t, 2 as $t, 3 as $t], - [4 as $t, 5 as $t, 6 as $t], - [3 as $t, 2 as $t, 1 as $t] - ]; - let res = array![ - [2 as $t, 4 as $t, 6 as $t], - [8 as $t, 10 as $t, 12 as $t], - [6 as $t, 4 as $t, 2 as $t] - ]; - let product: Array2<$t> = - 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); - } - } - } - } - - item! { - #[test] - fn []() { - let a = array![ - [1 as $t, 2 as $t, 3 as $t], - [4 as $t, 5 as $t, 6 as $t], - [3 as $t, 2 as $t, 1 as $t] - ]; - let res = array![ - [2 as $t, 4 as $t, 6 as $t], - [8 as $t, 10 as $t, 12 as $t], - [6 as $t, 4 as $t, 2 as $t] - ]; - let product: Array2<$t> = - <$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); - } - } - } - } - - item! { - #[test] - fn []() { - let a = array![ - Complex::new(2 as $t, 2 as $t), - Complex::new(5 as $t, 2 as $t), - Complex::new(3 as $t, 2 as $t), - ]; - let b = array![ - Complex::new(5 as $t, 3 as $t), - Complex::new(2 as $t, 4 as $t), - Complex::new(8 as $t, 4 as $t), - ]; - 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); - } - } - - item! { - #[test] - fn []() { - let a = array![ - Complex::new(2 as $t, 2 as $t), - Complex::new(5 as $t, 2 as $t), - Complex::new(3 as $t, 2 as $t), - ]; - let b = Complex::new(4 as $t, 2 as $t); - let product: Array1> = - > 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); - } - } - } - - item! { - #[test] - fn []() { - let a = array![ - Complex::new(2 as $t, 2 as $t), - Complex::new(5 as $t, 2 as $t), - Complex::new(3 as $t, 2 as $t), - ]; - let b = Complex::new(4 as $t, 2 as $t); - let product: Array1> = - 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); - } - } - } - - item! { - #[test] - fn []() { - let a = array![ - Complex::new(2 as $t, 2 as $t), - Complex::new(5 as $t, 2 as $t), - ]; - let b = array![ - Complex::new(5 as $t, 1 as $t), - Complex::new(2 as $t, 1 as $t), - ]; - let res = array![ - [a[0]*b[0], a[0]*b[1]], - [a[1]*b[0], a[1]*b[1]], - ]; - let product: Array2> = - > 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); - } - } - } - } - - item! { - #[test] - fn []() { - let a = array![ - [Complex::new(2 as $t, 2 as $t), Complex::new(5 as $t, 2 as $t)], - [Complex::new(2 as $t, 2 as $t), Complex::new(5 as $t, 2 as $t)], - ]; - let b = array![ - Complex::new(5 as $t, 1 as $t), - Complex::new(2 as $t, 1 as $t), - ]; - let res = array![ - a[(0, 0)] * b[0] + a[(0, 1)] * b[1], - a[(1, 0)] * b[0] + a[(1, 1)] * b[1], - ]; - 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); - } - } - } - - item! { - #[test] - fn []() { - let a = array![ - [Complex::new(2 as $t, 1 as $t), Complex::new(5 as $t, 2 as $t)], - [Complex::new(4 as $t, 2 as $t), Complex::new(7 as $t, 1 as $t)], - ]; - let b = array![ - [Complex::new(2 as $t, 2 as $t), Complex::new(5 as $t, 1 as $t)], - [Complex::new(3 as $t, 1 as $t), Complex::new(4 as $t, 2 as $t)], - ]; - let res = array![ - [ - a[(0, 0)] * b[(0, 0)] + a[(0, 1)] * b[(1, 0)], - a[(0, 0)] * b[(0, 1)] + a[(0, 1)] * b[(1, 1)] - ], - [ - a[(1, 0)] * b[(0, 0)] + a[(1, 1)] * b[(1, 0)], - a[(1, 0)] * b[(0, 1)] + a[(1, 1)] * b[(1, 1)] - ], - ]; - let product: Array2> = - > 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); - } - } - } - } - - item! { - #[test] - fn []() { - let a = array![ - [Complex::new(2 as $t, 1 as $t), Complex::new(5 as $t, 2 as $t)], - [Complex::new(4 as $t, 2 as $t), Complex::new(7 as $t, 1 as $t)], - ]; - let b = Complex::new(4 as $t, 1 as $t); - let res = array![ - [a[(0, 0)] * b, a[(0, 1)] * b], - [a[(1, 0)] * b, a[(1, 1)] * b] - ]; - let product: Array2> = - > 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); - } - } - } - } - - item! { - #[test] - fn []() { - let a = array![ - [Complex::new(2 as $t, 1 as $t), Complex::new(5 as $t, 2 as $t)], - [Complex::new(4 as $t, 2 as $t), Complex::new(7 as $t, 1 as $t)], - ]; - let b = Complex::new(4 as $t, 1 as $t); - let res = array![ - [a[(0, 0)] * b, a[(0, 1)] * b], - [a[(1, 0)] * b, a[(1, 1)] * b], - ]; - let product: Array2> = - 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); - } - } - } - } - }; - } - - make_test!(i8); - make_test!(u8); - make_test!(i16); - make_test!(u16); - make_test!(i32); - make_test!(u32); - make_test!(i64); - make_test!(u64); - make_test!(isize); - make_test!(usize); - make_test!(f32); - make_test!(f64); -} +use crate as argmin_math; +include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../argmin-math-ndarray-linalg-tests/src/dot.rs" +)); diff --git a/argmin-math/src/ndarray_m/eye.rs b/argmin-math/src/ndarray_m/eye.rs index 4c3df76cb..ffa2a1bfd 100644 --- a/argmin-math/src/ndarray_m/eye.rs +++ b/argmin-math/src/ndarray_m/eye.rs @@ -39,78 +39,10 @@ make_eye!(u64); make_eye!(f32); make_eye!(f64); +// The tests expect the name for the crate containing the tested functions to be argmin_math #[cfg(test)] -mod tests { - use super::*; - use ndarray::array; - use paste::item; - - macro_rules! make_test { - ($t:ty) => { - item! { - #[test] - fn []() { - let e: Array2<$t> = as ArgminEye>::eye(3); - let res = array![ - [1 as $t, 0 as $t, 0 as $t], - [0 as $t, 1 as $t, 0 as $t], - [0 as $t, 0 as $t, 1 as $t] - ]; - for i in 0..3 { - for j in 0..3 { - assert!((((res[(i, j)] - e[(i, j)]) as f64).abs()) < std::f64::EPSILON); - } - } - } - } - - item! { - #[test] - fn []() { - let a = array![ - [0 as $t, 2 as $t, 6 as $t], - [3 as $t, 2 as $t, 7 as $t], - [9 as $t, 8 as $t, 1 as $t] - ]; - let e: Array2<$t> = a.eye_like(); - let res = array![ - [1 as $t, 0 as $t, 0 as $t], - [0 as $t, 1 as $t, 0 as $t], - [0 as $t, 0 as $t, 1 as $t] - ]; - for i in 0..3 { - for j in 0..3 { - assert!((((res[(i, j)] - e[(i, j)]) as f64).abs()) < std::f64::EPSILON); - } - } - } - } - - item! { - #[test] - #[should_panic] - #[allow(unused)] - fn []() { - let a = array![ - [0 as $t, 2 as $t, 6 as $t], - [3 as $t, 2 as $t, 7 as $t], - ]; - let e: Array2<$t> = a.eye_like(); - } - } - }; - } - - make_test!(isize); - make_test!(usize); - make_test!(i8); - make_test!(u8); - make_test!(i16); - make_test!(u16); - make_test!(i32); - make_test!(u32); - make_test!(i64); - make_test!(u64); - make_test!(f32); - make_test!(f64); -} +use crate as argmin_math; +include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../argmin-math-ndarray-linalg-tests/src/eye.rs" +)); diff --git a/argmin-math/src/ndarray_m/inv.rs b/argmin-math/src/ndarray_m/inv.rs index 1eea7a051..ac02f7f32 100644 --- a/argmin-math/src/ndarray_m/inv.rs +++ b/argmin-math/src/ndarray_m/inv.rs @@ -38,49 +38,10 @@ make_inv!(f64); make_inv!(Complex); make_inv!(Complex); +// The tests expect the name for the crate containing the tested functions to be argmin_math #[cfg(test)] -mod tests { - use super::*; - use ndarray::array; - use paste::item; - - macro_rules! make_test { - ($t:ty) => { - item! { - #[test] - fn []() { - let a = array![ - [2 as $t, 5 as $t], - [1 as $t, 3 as $t], - ]; - let target = array![ - [3 as $t, -5 as $t], - [-1 as $t, 2 as $t], - ]; - 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); - } - } - } - } - - item! { - #[test] - fn []() { - 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); - } - } - }; - } - - make_test!(f32); - make_test!(f64); -} +use crate as argmin_math; +include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../argmin-math-ndarray-linalg-tests/src/inv.rs" +)); diff --git a/argmin-math/src/ndarray_m/l1norm.rs b/argmin-math/src/ndarray_m/l1norm.rs index d7d002875..a1d29bd15 100644 --- a/argmin-math/src/ndarray_m/l1norm.rs +++ b/argmin-math/src/ndarray_m/l1norm.rs @@ -67,78 +67,10 @@ make_l1norm_complex!(Complex, usize); make_l1norm_complex!(Complex, f32); make_l1norm_complex!(Complex, f64); +// The tests expect the name for the crate containing the tested functions to be argmin_math #[cfg(test)] -mod tests { - use super::*; - use ndarray::{array, Array1}; - use paste::item; - - macro_rules! make_test { - ($t:ty) => { - item! { - #[test] - fn []() { - 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); - } - } - - item! { - #[test] - fn []() { - 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); - } - } - }; - } - - macro_rules! make_test_signed { - ($t:ty) => { - item! { - #[test] - fn []() { - 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); - } - } - - item! { - #[test] - fn []() { - 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); - } - } - }; - } - - make_test!(isize); - make_test!(usize); - make_test!(i8); - make_test!(u8); - make_test!(i16); - make_test!(u16); - make_test!(i32); - make_test!(u32); - make_test!(i64); - make_test!(u64); - make_test!(f32); - make_test!(f64); - - make_test_signed!(isize); - make_test_signed!(i8); - make_test_signed!(i16); - make_test_signed!(i32); - make_test_signed!(i64); - make_test_signed!(f32); - make_test_signed!(f64); -} +use crate as argmin_math; +include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../argmin-math-ndarray-linalg-tests/src/l1norm.rs" +)); diff --git a/argmin-math/src/ndarray_m/l2norm.rs b/argmin-math/src/ndarray_m/l2norm.rs index d43c18971..33e774d91 100644 --- a/argmin-math/src/ndarray_m/l2norm.rs +++ b/argmin-math/src/ndarray_m/l2norm.rs @@ -79,78 +79,10 @@ make_norm_complex!(Complex, usize); make_norm_complex!(Complex, f32); make_norm_complex!(Complex, f64); +// The tests expect the name for the crate containing the tested functions to be argmin_math #[cfg(test)] -mod tests { - use super::*; - use ndarray::{array, Array1}; - use paste::item; - - macro_rules! make_test { - ($t:ty) => { - item! { - #[test] - fn []() { - 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); - } - } - - item! { - #[test] - fn []() { - 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); - } - } - }; - } - - macro_rules! make_test_signed { - ($t:ty) => { - item! { - #[test] - fn []() { - 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); - } - } - - item! { - #[test] - fn []() { - 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); - } - } - }; - } - - make_test!(isize); - make_test!(usize); - make_test!(i8); - make_test!(u8); - make_test!(i16); - make_test!(u16); - make_test!(i32); - make_test!(u32); - make_test!(i64); - make_test!(u64); - make_test!(f32); - make_test!(f64); - - make_test_signed!(isize); - make_test_signed!(i8); - make_test_signed!(i16); - make_test_signed!(i32); - make_test_signed!(i64); - make_test_signed!(f32); - make_test_signed!(f64); -} +use crate as argmin_math; +include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../argmin-math-ndarray-linalg-tests/src/l2norm.rs" +)); diff --git a/argmin-math/src/ndarray_m/minmax.rs b/argmin-math/src/ndarray_m/minmax.rs index 2738d5218..ef3ab66df 100644 --- a/argmin-math/src/ndarray_m/minmax.rs +++ b/argmin-math/src/ndarray_m/minmax.rs @@ -79,72 +79,10 @@ make_minmax!(u64); make_minmax!(f32); make_minmax!(f64); +// The tests expect the name for the crate containing the tested functions to be argmin_math #[cfg(test)] -mod tests { - use super::*; - use ndarray::array; - use paste::item; - - macro_rules! make_test { - ($t:ty) => { - item! { - #[test] - fn []() { - let a = array![1 as $t, 4 as $t, 8 as $t]; - let b = array![2 as $t, 3 as $t, 4 as $t]; - let target_max = array![2 as $t, 4 as $t, 8 as $t]; - let target_min = array![1 as $t, 3 as $t, 4 as $t]; - 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); - } - } - } - - item! { - #[test] - fn []() { - let a = array![ - [1 as $t, 4 as $t, 8 as $t], - [2 as $t, 5 as $t, 9 as $t] - ]; - let b = array![ - [2 as $t, 3 as $t, 4 as $t], - [3 as $t, 4 as $t, 5 as $t] - ]; - let target_max = array![ - [2 as $t, 4 as $t, 8 as $t], - [3 as $t, 5 as $t, 9 as $t] - ]; - let target_min = array![ - [1 as $t, 3 as $t, 4 as $t], - [2 as $t, 4 as $t, 5 as $t] - ]; - let res_max = as ArgminMinMax>::max(&a, &b); - 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); - } - } - } - } - }; - } - - make_test!(i8); - make_test!(u8); - make_test!(i16); - make_test!(u16); - make_test!(i32); - make_test!(u32); - make_test!(i64); - make_test!(u64); - make_test!(isize); - make_test!(usize); - make_test!(f32); - make_test!(f64); -} +use crate as argmin_math; +include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../argmin-math-ndarray-linalg-tests/src/minmax.rs" +)); diff --git a/argmin-math/src/ndarray_m/mul.rs b/argmin-math/src/ndarray_m/mul.rs index d81b11fae..1d2d7fa7e 100644 --- a/argmin-math/src/ndarray_m/mul.rs +++ b/argmin-math/src/ndarray_m/mul.rs @@ -263,401 +263,10 @@ make_complex_integer_mul!(u64); make_complex_integer_mul!(isize); make_complex_integer_mul!(usize); +// The tests expect the name for the crate containing the tested functions to be argmin_math #[cfg(test)] -mod tests { - use super::*; - use ndarray::array; - use paste::item; - - macro_rules! make_test { - ($t:ty) => { - item! { - #[test] - fn []() { - let a = array![1 as $t, 4 as $t, 8 as $t]; - let b = 2 as $t; - 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); - } - } - } - - item! { - #[test] - fn []() { - let a = array![ - Complex::new(5 as $t, 3 as $t), - Complex::new(8 as $t, 2 as $t) - ]; - let b = Complex::new(2 as $t, 3 as $t); - 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); - } - } - } - - item! { - #[test] - fn []() { - let a = array![ - Complex::new(5 as $t, 3 as $t), - Complex::new(8 as $t, 2 as $t) - ]; - let b = 2 as $t; - 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); - } - } - } - - item! { - #[test] - fn []() { - let a = array![1 as $t, 4 as $t, 8 as $t]; - let b = 2 as $t; - 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); - } - } - } - - item! { - #[test] - fn []() { - let a = array![ - Complex::new(5 as $t, 3 as $t), - Complex::new(8 as $t, 2 as $t) - ]; - let b = Complex::new(2 as $t, 3 as $t); - 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); - } - } - } - - item! { - #[test] - fn []() { - let a = array![ - Complex::new(5 as $t, 3 as $t), - Complex::new(8 as $t, 2 as $t) - ]; - let b = 2 as $t; - 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); - } - } - } - - item! { - #[test] - fn []() { - let a = array![1 as $t, 4 as $t, 8 as $t]; - let b = array![2 as $t, 3 as $t, 4 as $t]; - 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); - } - } - } - - item! { - #[test] - fn []() { - let a = array![ - Complex::new(5 as $t, 3 as $t), - Complex::new(8 as $t, 2 as $t) - ]; - let b = array![ - Complex::new(2 as $t, 3 as $t), - Complex::new(1 as $t, 2 as $t) - ]; - 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); - } - } - } - - item! { - #[test] - #[should_panic] - fn []() { - let a = array![1 as $t, 4 as $t]; - let b = array![41 as $t, 38 as $t, 34 as $t]; - as ArgminMul, Array1<$t>>>::mul(&a, &b); - } - } - - item! { - #[test] - #[should_panic] - fn []() { - let a = array![]; - let b = array![41 as $t, 38 as $t, 34 as $t]; - as ArgminMul, Array1<$t>>>::mul(&a, &b); - } - } - - item! { - #[test] - #[should_panic] - fn []() { - let a = array![41 as $t, 38 as $t, 34 as $t]; - let b = array![]; - as ArgminMul, Array1<$t>>>::mul(&a, &b); - } - } - - item! { - #[test] - fn []() { - let a = array![ - [1 as $t, 4 as $t, 8 as $t], - [2 as $t, 5 as $t, 9 as $t] - ]; - let b = array![ - [2 as $t, 3 as $t, 4 as $t], - [3 as $t, 4 as $t, 5 as $t] - ]; - let target = array![ - [2 as $t, 12 as $t, 32 as $t], - [6 as $t, 20 as $t, 45 as $t] - ]; - 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); - } - } - } - } - - item! { - #[test] - fn []() { - let a = array![ - [Complex::new(5 as $t, 3 as $t), Complex::new(8 as $t, 2 as $t)], - [Complex::new(4 as $t, 2 as $t), Complex::new(7 as $t, 1 as $t)], - [Complex::new(3 as $t, 1 as $t), Complex::new(6 as $t, 2 as $t)], - ]; - let b = array![ - [Complex::new(5 as $t, 3 as $t), Complex::new(8 as $t, 2 as $t)], - [Complex::new(4 as $t, 2 as $t), Complex::new(7 as $t, 1 as $t)], - [Complex::new(3 as $t, 1 as $t), Complex::new(6 as $t, 2 as $t)], - ]; - let target = array![ - [a[(0, 0)] * b[(0, 0)], a[(0, 1)] * b[(0, 1)]], - [a[(1, 0)] * b[(1, 0)], a[(1, 1)] * b[(1, 1)]], - [a[(2, 0)] * b[(2, 0)], a[(2, 1)] * b[(2, 1)]], - ]; - 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); - } - } - } - } - - item! { - #[test] - #[should_panic] - fn []() { - let a = array![ - [1 as $t, 4 as $t, 8 as $t], - [2 as $t, 5 as $t, 9 as $t] - ]; - let b = array![ - [41 as $t, 38 as $t], - ]; - as ArgminMul, Array2<$t>>>::mul(&a, &b); - } - } - - item! { - #[test] - #[should_panic] - fn []() { - let a = array![ - [1 as $t, 4 as $t, 8 as $t], - [2 as $t, 5 as $t, 9 as $t] - ]; - let b = array![[]]; - as ArgminMul, Array2<$t>>>::mul(&a, &b); - } - } - - item! { - #[test] - fn []() { - let a = array![ - [1 as $t, 4 as $t, 8 as $t], - [2 as $t, 5 as $t, 9 as $t] - ]; - let b = 2 as $t; - let target = array![ - [2 as $t, 8 as $t, 16 as $t], - [4 as $t, 10 as $t, 18 as $t] - ]; - 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); - } - } - } - } - - item! { - #[test] - fn []() { - let a = array![ - [Complex::new(5 as $t, 3 as $t), Complex::new(8 as $t, 2 as $t)], - [Complex::new(4 as $t, 2 as $t), Complex::new(7 as $t, 1 as $t)], - [Complex::new(3 as $t, 1 as $t), Complex::new(6 as $t, 2 as $t)], - ]; - let b = Complex::new(3 as $t, 2 as $t); - let target = array![ - [a[(0, 0)] * b, a[(0, 1)] * b], - [a[(1, 0)] * b, a[(1, 1)] * b], - [a[(2, 0)] * b, a[(2, 1)] * b], - ]; - 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); - } - } - } - } - - item! { - #[test] - fn []() { - let a = array![ - [Complex::new(5 as $t, 3 as $t), Complex::new(8 as $t, 2 as $t)], - [Complex::new(4 as $t, 2 as $t), Complex::new(7 as $t, 1 as $t)], - [Complex::new(3 as $t, 1 as $t), Complex::new(6 as $t, 2 as $t)], - ]; - let b = 3 as $t; - let target = array![ - [a[(0, 0)] * b, a[(0, 1)] * b], - [a[(1, 0)] * b, a[(1, 1)] * b], - [a[(2, 0)] * b, a[(2, 1)] * b], - ]; - 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); - } - } - } - } - - item! { - #[test] - fn []() { - let b = array![ - [1 as $t, 4 as $t, 8 as $t], - [2 as $t, 5 as $t, 9 as $t] - ]; - let a = 2 as $t; - let target = array![ - [2 as $t, 8 as $t, 16 as $t], - [4 as $t, 10 as $t, 18 as $t] - ]; - 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); - } - } - } - } - - item! { - #[test] - fn []() { - let a = array![ - [Complex::new(5 as $t, 3 as $t), Complex::new(8 as $t, 2 as $t)], - [Complex::new(4 as $t, 2 as $t), Complex::new(7 as $t, 1 as $t)], - [Complex::new(3 as $t, 1 as $t), Complex::new(6 as $t, 2 as $t)], - ]; - let b = Complex::new(3 as $t, 2 as $t); - let target = array![ - [a[(0, 0)] * b, a[(0, 1)] * b], - [a[(1, 0)] * b, a[(1, 1)] * b], - [a[(2, 0)] * b, a[(2, 1)] * b], - ]; - 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); - } - } - } - } - - item! { - #[test] - fn []() { - let a = array![ - [Complex::new(5 as $t, 3 as $t), Complex::new(8 as $t, 2 as $t)], - [Complex::new(4 as $t, 2 as $t), Complex::new(7 as $t, 1 as $t)], - [Complex::new(3 as $t, 1 as $t), Complex::new(6 as $t, 2 as $t)], - ]; - let b = 3 as $t; - let target = array![ - [a[(0, 0)] * b, a[(0, 1)] * b], - [a[(1, 0)] * b, a[(1, 1)] * b], - [a[(2, 0)] * b, a[(2, 1)] * b], - ]; - 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); - } - } - } - } - }; - } - - make_test!(i8); - make_test!(u8); - make_test!(i16); - make_test!(u16); - make_test!(i32); - make_test!(u32); - make_test!(i64); - make_test!(u64); - make_test!(isize); - make_test!(usize); - make_test!(f32); - make_test!(f64); -} +use crate as argmin_math; +include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../argmin-math-ndarray-linalg-tests/src/mul.rs" +)); diff --git a/argmin-math/src/ndarray_m/random.rs b/argmin-math/src/ndarray_m/random.rs index a13b45c00..f0f78926b 100644 --- a/argmin-math/src/ndarray_m/random.rs +++ b/argmin-math/src/ndarray_m/random.rs @@ -73,60 +73,10 @@ make_random!(u64); make_random!(f32); make_random!(f64); +// The tests expect the name for the crate containing the tested functions to be argmin_math #[cfg(test)] -mod tests { - use super::*; - use ndarray::{array, Array1, Array2}; - use paste::item; - - macro_rules! make_test { - ($t:ty) => { - item! { - #[test] - fn []() { - let a = array![1 as $t, 2 as $t, 4 as $t]; - let b = array![2 as $t, 3 as $t, 5 as $t]; - let random = Array1::<$t>::rand_from_range(&a, &b); - for i in 0..3usize { - assert!(random[i] >= a[i]); - assert!(random[i] <= b[i]); - } - } - } - - item! { - #[test] - fn []() { - let a = array![ - [1 as $t, 2 as $t, 4 as $t], - [2 as $t, 3 as $t, 5 as $t] - ]; - let b = array![ - [2 as $t, 3 as $t, 5 as $t], - [3 as $t, 4 as $t, 6 as $t] - ]; - let random = Array2::<$t>::rand_from_range(&a, &b); - for i in 0..3 { - for j in 0..2 { - assert!(random[(j, i)] >= a[(j, i)]); - assert!(random[(j, i)] <= b[(j, i)]); - } - } - } - } - }; - } - - make_test!(i8); - make_test!(u8); - make_test!(i16); - make_test!(u16); - make_test!(i32); - make_test!(u32); - make_test!(i64); - make_test!(u64); - make_test!(isize); - make_test!(usize); - make_test!(f32); - make_test!(f64); -} +use crate as argmin_math; +include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../argmin-math-ndarray-linalg-tests/src/random.rs" +)); diff --git a/argmin-math/src/ndarray_m/scaledadd.rs b/argmin-math/src/ndarray_m/scaledadd.rs index 648bc1f1d..1e30cf825 100644 --- a/argmin-math/src/ndarray_m/scaledadd.rs +++ b/argmin-math/src/ndarray_m/scaledadd.rs @@ -5,162 +5,10 @@ // http://opensource.org/licenses/MIT>, at your option. This file may not be // copied, modified, or distributed except according to those terms. +// The tests expect the name for the crate containing the tested functions to be argmin_math #[cfg(test)] -mod tests { - use crate::ArgminScaledAdd; - use ndarray::{array, Array1, Array2}; - use paste::item; - - macro_rules! make_test { - ($t:ty) => { - item! { - #[test] - fn []() { - let a = array![1 as $t, 2 as $t, 3 as $t]; - let b = 2 as $t; - let c = array![4 as $t, 5 as $t, 6 as $t]; - 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); - } - } - } - - item! { - #[test] - #[should_panic] - fn []() { - let a = array![1 as $t, 2 as $t, 3 as $t]; - let b = 2 as $t; - let c = array![4 as $t, 5 as $t]; - as ArgminScaledAdd, $t, Array1<$t>>>::scaled_add(&a, &b, &c); - } - } - - item! { - #[test] - #[should_panic] - fn []() { - let a = array![1 as $t, 2 as $t]; - let b = 2 as $t; - let c = array![4 as $t, 5 as $t, 6 as $t]; - as ArgminScaledAdd, $t, Array1<$t>>>::scaled_add(&a, &b, &c); - } - } - - item! { - #[test] - fn []() { - let a = array![1 as $t, 2 as $t, 3 as $t]; - let b = array![3 as $t, 2 as $t, 1 as $t]; - let c = array![4 as $t, 5 as $t, 6 as $t]; - 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); - } - } - } - - item! { - #[test] - #[should_panic] - fn []() { - let a = array![1 as $t, 2 as $t]; - let b = array![3 as $t, 2 as $t, 1 as $t]; - let c = array![4 as $t, 5 as $t, 6 as $t]; - as ArgminScaledAdd, Array1<$t>, Array1<$t>>>::scaled_add(&a, &b, &c); - } - } - - item! { - #[test] - #[should_panic] - fn []() { - let a = array![1 as $t, 2 as $t, 3 as $t]; - let b = array![3 as $t, 2 as $t]; - let c = array![4 as $t, 5 as $t, 6 as $t]; - as ArgminScaledAdd, Array1<$t>, Array1<$t>>>::scaled_add(&a, &b, &c); - } - } - - item! { - #[test] - #[should_panic] - fn []() { - let a = array![1 as $t, 2 as $t, 3 as $t]; - let b = array![3 as $t, 2 as $t, 1 as $t]; - let c = array![4 as $t, 5 as $t]; - as ArgminScaledAdd, Array1<$t>, Array1<$t>>>::scaled_add(&a, &b, &c); - } - } - - item! { - #[test] - fn []() { - let a = array![ - [1 as $t, 2 as $t], - [3 as $t, 4 as $t], - ]; - let b = array![ - [4 as $t, 3 as $t], - [2 as $t, 1 as $t], - ]; - let c = array![ - [1 as $t, 2 as $t], - [2 as $t, 1 as $t], - ]; - let res = as ArgminScaledAdd, Array2<$t>, Array2<$t>>>::scaled_add(&a, &b, &c); - let target = array![ - [5 as $t, 8 as $t], - [7 as $t, 5 as $t], - ]; - for i in 0..2 { - for j in 0..2 { - assert!((((res[(i, j)] - target[(i, j)]) as f64).abs()) < std::f64::EPSILON); - } - } - } - } - - item! { - #[test] - fn []() { - let a = array![ - [1 as $t, 2 as $t], - [3 as $t, 4 as $t], - ]; - let b = 2 as $t; - let c = array![ - [1 as $t, 2 as $t], - [2 as $t, 1 as $t], - ]; - let res = as ArgminScaledAdd, $t, Array2<$t>>>::scaled_add(&a, &b, &c); - let target = array![ - [3 as $t, 6 as $t], - [7 as $t, 6 as $t], - ]; - for i in 0..2 { - for j in 0..2 { - assert!((((res[(i, j)] - target[(i, j)]) as f64).abs()) < std::f64::EPSILON); - } - } - } - } - }; - } - - // make_test!(isize); - // make_test!(usize); - make_test!(i8); - make_test!(u8); - make_test!(i16); - make_test!(u16); - make_test!(i32); - make_test!(u32); - make_test!(i64); - make_test!(u64); - make_test!(f32); - make_test!(f64); -} +use crate as argmin_math; +include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../argmin-math-ndarray-linalg-tests/src/scaledadd.rs" +)); diff --git a/argmin-math/src/ndarray_m/scaledsub.rs b/argmin-math/src/ndarray_m/scaledsub.rs index 723accce4..d06324fab 100644 --- a/argmin-math/src/ndarray_m/scaledsub.rs +++ b/argmin-math/src/ndarray_m/scaledsub.rs @@ -5,160 +5,10 @@ // http://opensource.org/licenses/MIT>, at your option. This file may not be // copied, modified, or distributed except according to those terms. +// The tests expect the name for the crate containing the tested functions to be argmin_math #[cfg(test)] -mod tests { - use crate::ArgminScaledSub; - use ndarray::{array, Array1, Array2}; - use paste::item; - - macro_rules! make_test { - ($t:ty) => { - item! { - #[test] - fn []() { - let a = array![10 as $t, 20 as $t, 30 as $t]; - let b = 2 as $t; - let c = array![4 as $t, 5 as $t, 6 as $t]; - 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); - } - } - } - - item! { - #[test] - #[should_panic] - fn []() { - let a = array![1 as $t, 2 as $t, 3 as $t]; - let b = 2 as $t; - let c = array![4 as $t, 5 as $t]; - as ArgminScaledSub, $t, Array1<$t>>>::scaled_sub(&a, &b, &c); - } - } - - item! { - #[test] - #[should_panic] - fn []() { - let a = array![1 as $t, 2 as $t]; - let b = 2 as $t; - let c = array![4 as $t, 5 as $t, 6 as $t]; - as ArgminScaledSub, $t, Array1<$t>>>::scaled_sub(&a, &b, &c); - } - } - - item! { - #[test] - fn []() { - let a = array![20 as $t, 20 as $t, 30 as $t]; - let b = array![3 as $t, 2 as $t, 1 as $t]; - let c = array![4 as $t, 5 as $t, 6 as $t]; - 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); - } - } - } - - item! { - #[test] - #[should_panic] - fn []() { - let a = array![1 as $t, 2 as $t]; - let b = array![3 as $t, 2 as $t, 1 as $t]; - let c = array![4 as $t, 5 as $t, 6 as $t]; - as ArgminScaledSub, Array1<$t>, Array1<$t>>>::scaled_sub(&a, &b, &c); - } - } - - item! { - #[test] - #[should_panic] - fn []() { - let a = array![1 as $t, 2 as $t, 3 as $t]; - let b = array![3 as $t, 2 as $t]; - let c = array![4 as $t, 5 as $t, 6 as $t]; - as ArgminScaledSub, Array1<$t>, Array1<$t>>>::scaled_sub(&a, &b, &c); - } - } - - item! { - #[test] - #[should_panic] - fn []() { - let a = array![1 as $t, 2 as $t, 3 as $t]; - let b = array![3 as $t, 2 as $t, 1 as $t]; - let c = array![4 as $t, 5 as $t]; - as ArgminScaledSub, Array1<$t>, Array1<$t>>>::scaled_sub(&a, &b, &c); - } - } - - item! { - #[test] - fn []() { - let a = array![ - [10 as $t, 20 as $t], - [30 as $t, 40 as $t], - ]; - let b = array![ - [4 as $t, 3 as $t], - [2 as $t, 1 as $t], - ]; - let c = array![ - [1 as $t, 2 as $t], - [2 as $t, 1 as $t], - ]; - let res = as ArgminScaledSub, Array2<$t>, Array2<$t>>>::scaled_sub(&a, &b, &c); - let target = array![ - [6 as $t, 14 as $t], - [26 as $t, 39 as $t], - ]; - for i in 0..2 { - for j in 0..2 { - assert!((((res[(i, j)] - target[(i, j)]) as f64).abs()) < std::f64::EPSILON); - } - } - } - } - - item! { - #[test] - fn []() { - let a = array![ - [10 as $t, 20 as $t], - [30 as $t, 40 as $t], - ]; - let b = 2 as $t; - let c = array![ - [1 as $t, 2 as $t], - [2 as $t, 1 as $t], - ]; - let res = as ArgminScaledSub, $t, Array2<$t>>>::scaled_sub(&a, &b, &c); - let target = array![ - [8 as $t, 16 as $t], - [26 as $t, 38 as $t], - ]; - for i in 0..2 { - for j in 0..2 { - assert!((((res[(i, j)] - target[(i, j)]) as f64).abs()) < std::f64::EPSILON); - } - } - } - } - }; - } - - make_test!(i8); - make_test!(u8); - make_test!(i16); - make_test!(u16); - make_test!(i32); - make_test!(u32); - make_test!(i64); - make_test!(u64); - make_test!(f32); - make_test!(f64); -} +use crate as argmin_math; +include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../argmin-math-ndarray-linalg-tests/src/scaledsub.rs" +)); diff --git a/argmin-math/src/ndarray_m/signum.rs b/argmin-math/src/ndarray_m/signum.rs index 9ea6474ea..c41875479 100644 --- a/argmin-math/src/ndarray_m/signum.rs +++ b/argmin-math/src/ndarray_m/signum.rs @@ -82,121 +82,10 @@ make_signum_complex!(Complex); make_signum_complex!(Complex); make_signum_complex!(Complex); +// The tests expect the name for the crate containing the tested functions to be argmin_math #[cfg(test)] -mod tests { - use super::*; - use ndarray::array; - use paste::item; - - macro_rules! make_test { - ($t:ty) => { - item! { - #[test] - fn []() { - let x = Array1::from(vec![ - Complex::new(1 as $t, 2 as $t), - Complex::new(4 as $t, -3 as $t), - Complex::new(-8 as $t, 4 as $t), - Complex::new(-8 as $t, -1 as $t), - ]); - let y = Array1::from(vec![ - Complex::new(1 as $t, 1 as $t), - Complex::new(1 as $t, -1 as $t), - Complex::new(-1 as $t, 1 as $t), - Complex::new(-1 as $t, -1 as $t), - ]); - let res = > as ArgminSignum>::signum(x); - for i in 0..4 { - let tmp = y[i] - res[i]; - let norm = ((tmp.re * tmp.re + tmp.im * tmp.im) as f64).sqrt(); - assert!(norm < std::f64::EPSILON); - } - } - } - - item! { - #[test] - fn []() { - let x = Array1::from(vec![1 as $t, -4 as $t, 8 as $t]); - let y = Array1::from(vec![1 as $t, -1 as $t, 1 as $t]); - let res = as ArgminSignum>::signum(x); - for i in 0..3 { - let diff = (y[i] - res[i]).abs() as f64; - assert!(diff < std::f64::EPSILON); - } - } - } - - item! { - #[test] - fn []() { - let x = array![ - [ - Complex::new(1 as $t, 2 as $t), - Complex::new(4 as $t, -3 as $t), - Complex::new(-8 as $t, 4 as $t), - Complex::new(-8 as $t, -1 as $t), - ], - [ - Complex::new(-1 as $t, -2 as $t), - Complex::new(4 as $t, 3 as $t), - Complex::new(-8 as $t, -4 as $t), - Complex::new(8 as $t, -1 as $t), - ] - ]; - let y = array![ - [ - Complex::new(1 as $t, 1 as $t), - Complex::new(1 as $t, -1 as $t), - Complex::new(-1 as $t, 1 as $t), - Complex::new(-1 as $t, -1 as $t), - ], - [ - Complex::new(-1 as $t, -1 as $t), - Complex::new(1 as $t, 1 as $t), - Complex::new(-1 as $t, -1 as $t), - Complex::new(1 as $t, -1 as $t), - ] - ]; - let res = > as ArgminSignum>::signum(x); - for j in 0..2 { - for i in 0..4 { - let tmp = y[(j, i)] - res[(j, i)]; - let norm = ((tmp.re * tmp.re + tmp.im * tmp.im) as f64).sqrt(); - assert!(norm < std::f64::EPSILON); - } - } - } - } - - item! { - #[test] - fn []() { - let x = array![ - [1 as $t, -4 as $t, 8 as $t], - [-2 as $t, 2 as $t, -8 as $t] - ]; - let y = array![ - [1 as $t, -1 as $t, 1 as $t], - [-1 as $t, 1 as $t, -1 as $t] - ]; - let res = as ArgminSignum>::signum(x); - for j in 0..2 { - for i in 0..3 { - let diff = (y[(j, i)] - res[(j, i)]).abs() as f64; - assert!(diff < std::f64::EPSILON); - } - } - } - } - }; - } - - make_test!(isize); - make_test!(i8); - make_test!(i16); - make_test!(i32); - make_test!(i64); - make_test!(f32); - make_test!(f64); -} +use crate as argmin_math; +include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../argmin-math-ndarray-linalg-tests/src/signum.rs" +)); diff --git a/argmin-math/src/ndarray_m/sub.rs b/argmin-math/src/ndarray_m/sub.rs index 4c0e3f351..99022b9d1 100644 --- a/argmin-math/src/ndarray_m/sub.rs +++ b/argmin-math/src/ndarray_m/sub.rs @@ -61,166 +61,10 @@ make_sub!(f64); make_sub!(Complex); make_sub!(Complex); +// The tests expect the name for the crate containing the tested functions to be argmin_math #[cfg(test)] -mod tests { - use super::*; - use ndarray::array; - use paste::item; - - macro_rules! make_test { - ($t:ty) => { - item! { - #[test] - fn []() { - let a = array![36 as $t, 39 as $t, 43 as $t]; - let b = 1 as $t; - 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); - } - } - } - - item! { - #[test] - fn []() { - let a = array![1 as $t, 4 as $t, 8 as $t]; - let b = 34 as $t; - 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); - } - } - } - - item! { - #[test] - fn []() { - let a = array![41 as $t, 38 as $t, 34 as $t]; - let b = array![1 as $t, 4 as $t, 8 as $t]; - 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); - } - } - } - - item! { - #[test] - #[should_panic] - fn []() { - let a = array![41 as $t, 38 as $t, 34 as $t]; - let b = array![1 as $t, 4 as $t]; - as ArgminSub, Array1<$t>>>::sub(&a, &b); - } - } - - item! { - #[test] - #[should_panic] - fn []() { - let a = array![]; - let b = array![41 as $t, 38 as $t, 34 as $t]; - as ArgminSub, Array1<$t>>>::sub(&a, &b); - } - } - - item! { - #[test] - #[should_panic] - fn []() { - let a = array![41 as $t, 38 as $t, 34 as $t]; - let b = array![]; - as ArgminSub, Array1<$t>>>::sub(&a, &b); - } - } - - item! { - #[test] - fn []() { - let a = array![ - [43 as $t, 46 as $t, 50 as $t], - [44 as $t, 47 as $t, 51 as $t] - ]; - let b = array![ - [1 as $t, 4 as $t, 8 as $t], - [2 as $t, 5 as $t, 9 as $t] - ]; - let target = array![ - [42 as $t, 42 as $t, 42 as $t], - [42 as $t, 42 as $t, 42 as $t] - ]; - 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); - } - } - } - } - - item! { - #[test] - fn []() { - let a = array![ - [43 as $t, 46 as $t, 50 as $t], - [44 as $t, 47 as $t, 51 as $t] - ]; - let b = 2 as $t; - let target = array![ - [41 as $t, 44 as $t, 48 as $t], - [42 as $t, 45 as $t, 49 as $t] - ]; - 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); - } - } - } - } - - item! { - #[test] - #[should_panic] - fn []() { - let a = array![ - [41 as $t, 38 as $t], - ]; - let b = array![ - [1 as $t, 4 as $t, 8 as $t], - [2 as $t, 5 as $t, 9 as $t] - ]; - as ArgminSub, Array2<$t>>>::sub(&a, &b); - } - } - - item! { - #[test] - #[should_panic] - fn []() { - let a = array![ - [1 as $t, 4 as $t, 8 as $t], - [2 as $t, 5 as $t, 9 as $t] - ]; - let b = array![[]]; - as ArgminSub, Array2<$t>>>::sub(&a, &b); - } - } - }; - } - - make_test!(i8); - make_test!(u8); - make_test!(i16); - make_test!(u16); - make_test!(i32); - make_test!(u32); - make_test!(i64); - make_test!(u64); - make_test!(f32); - make_test!(f64); -} +use crate as argmin_math; +include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../argmin-math-ndarray-linalg-tests/src/sub.rs" +)); diff --git a/argmin-math/src/ndarray_m/transpose.rs b/argmin-math/src/ndarray_m/transpose.rs index 5eeb09446..ab84a788e 100644 --- a/argmin-math/src/ndarray_m/transpose.rs +++ b/argmin-math/src/ndarray_m/transpose.rs @@ -43,78 +43,10 @@ make_add!(f64); make_add!(Complex); make_add!(Complex); +// The tests expect the name for the crate containing the tested functions to be argmin_math #[cfg(test)] -mod tests { - use super::*; - use ndarray::array; - use paste::item; - - macro_rules! make_test { - ($t:ty) => { - item! { - // not sure if this is such a smart test :D - #[test] - fn []() { - let a = array![1 as $t, 4 as $t]; - 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); - } - } - } - - item! { - #[test] - fn []() { - let a = array![ - [1 as $t, 4 as $t], - [8 as $t, 7 as $t] - ]; - let target = array![ - [1 as $t, 8 as $t], - [4 as $t, 7 as $t] - ]; - 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); - } - } - } - } - - item! { - #[test] - fn []() { - let a = array![ - [1 as $t, 4 as $t], - [8 as $t, 7 as $t], - [3 as $t, 6 as $t] - ]; - let target = array![ - [1 as $t, 8 as $t, 3 as $t], - [4 as $t, 7 as $t, 6 as $t] - ]; - 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); - } - } - } - } - }; - } - - make_test!(i8); - make_test!(u8); - make_test!(i16); - make_test!(u16); - make_test!(i32); - make_test!(u32); - make_test!(i64); - make_test!(u64); - make_test!(f32); - make_test!(f64); -} +use crate as argmin_math; +include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../argmin-math-ndarray-linalg-tests/src/transpose.rs" +)); diff --git a/argmin-math/src/ndarray_m/zero.rs b/argmin-math/src/ndarray_m/zero.rs index b912fce67..68c11ed0a 100644 --- a/argmin-math/src/ndarray_m/zero.rs +++ b/argmin-math/src/ndarray_m/zero.rs @@ -38,84 +38,10 @@ where // } } +// The tests expect the name for the crate containing the tested functions to be argmin_math #[cfg(test)] -mod tests { - use super::*; - use ndarray::{array, Array1, Array2}; - use paste::item; - - macro_rules! make_test { - ($t:ty) => { - // item! { - // #[test] - // fn []() { - // let a = as ArgminZero>::zero(); - // let b: Array1<$t> = array![]; - // assert_eq!(a, b); - // } - // } - - item! { - #[test] - fn []() { - let t: Array1<$t> = array![]; - let a = t.zero_like(); - assert_eq!(t, a); - } - } - - item! { - #[test] - 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); - } - } - } - - // item! { - // #[test] - // fn []() { - // let a = as ArgminZero>::zero(); - // let b: Array2<$t> = Array2::zeros((0, 0)); - // assert_eq!(a, b); - // } - // } - - item! { - #[test] - fn []() { - let t: Array2<$t> = Array2::zeros((0, 0)); - let a = t.zero_like(); - assert_eq!(t, a); - } - } - - item! { - #[test] - fn []() { - 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); - } - } - } - } - }; - } - - make_test!(isize); - make_test!(usize); - make_test!(i8); - make_test!(u8); - make_test!(i16); - make_test!(u16); - make_test!(i32); - make_test!(u32); - make_test!(i64); - make_test!(u64); - make_test!(f32); - make_test!(f64); -} +use crate as argmin_math; +include!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../argmin-math-ndarray-linalg-tests/src/zero.rs" +)); diff --git a/argmin/Cargo.toml b/argmin/Cargo.toml index ac5d30b85..b29c44115 100644 --- a/argmin/Cargo.toml +++ b/argmin/Cargo.toml @@ -51,7 +51,7 @@ default = ["slog-logger", "serde1"] wasm-bindgen = ["instant/wasm-bindgen", "getrandom/js"] slog-logger = ["slog", "slog-term", "slog-async"] serde1 = ["serde", "serde_json", "rand/serde1", "bincode", "slog-json", "rand_xoshiro/serde1"] -_ndarrayl = ["argmin-math/ndarray_latest-serde", "argmin-math/_dev_linalg_latest"] +_ndarrayl = ["argmin-math/ndarray_latest-serde"] _nalgebral = ["argmin-math/nalgebra_latest-serde"] # When adding new features, please consider adding them to either `full` (for users) # or `_full_dev` (only for local development, testing and computing test coverage).