From 7b9198d78733ce8f07cb68cbf4be06338ca93db8 Mon Sep 17 00:00:00 2001 From: Craig Watson Date: Fri, 1 May 2026 11:34:23 -0700 Subject: [PATCH 1/3] Take generic ArrayBase in concatenate and stack --- src/stacking.rs | 10 ++++++---- tests/stacking.rs | 6 +++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/stacking.rs b/src/stacking.rs index 8737d6f60..b2a23f85c 100644 --- a/src/stacking.rs +++ b/src/stacking.rs @@ -33,8 +33,9 @@ use crate::imp_prelude::*; /// [3., 3.]])) /// ); /// ``` -pub fn concatenate(axis: Axis, arrays: &[ArrayView]) -> Result, ShapeError> +pub fn concatenate(axis: Axis, arrays: &[ArrayBase]) -> Result, ShapeError> where + S: Data, A: Clone, D: RemoveAxis, { @@ -66,7 +67,7 @@ where }; for array in arrays { - res.append(axis, array.clone())?; + res.append(axis, array.view())?; } debug_assert_eq!(res.len_of(axis), stacked_dim); Ok(res) @@ -96,8 +97,9 @@ where /// ); /// # } /// ``` -pub fn stack(axis: Axis, arrays: &[ArrayView]) -> Result, ShapeError> +pub fn stack(axis: Axis, arrays: &[ArrayBase]) -> Result, ShapeError> where + S: Data, A: Clone, D: Dimension, D::Larger: RemoveAxis, @@ -129,7 +131,7 @@ where }; for array in arrays { - res.append(axis, array.clone().insert_axis(axis))?; + res.append(axis, array.view().insert_axis(axis))?; } debug_assert_eq!(res.len_of(axis), arrays.len()); diff --git a/tests/stacking.rs b/tests/stacking.rs index bdfe478b4..be98d1697 100644 --- a/tests/stacking.rs +++ b/tests/stacking.rs @@ -1,4 +1,4 @@ -use ndarray::{arr2, arr3, aview1, aview2, concatenate, stack, Array2, Axis, ErrorKind, Ix1}; +use ndarray::{arr2, arr3, aview1, aview2, concatenate, stack, Array2, Axis, ErrorKind, Ix1, ViewRepr}; #[test] fn concatenating() @@ -29,7 +29,7 @@ fn concatenating() let res = ndarray::concatenate(Axis(2), &[a.view(), c.view()]); assert_eq!(res.unwrap_err().kind(), ErrorKind::OutOfBounds); - let res: Result, _> = ndarray::concatenate(Axis(0), &[]); + let res: Result, _> = ndarray::concatenate::, _, _>(Axis(0), &[]); assert_eq!(res.unwrap_err().kind(), ErrorKind::Unsupported); } @@ -50,6 +50,6 @@ fn stacking() let res = ndarray::stack(Axis(3), &[a.view(), a.view()]); assert_eq!(res.unwrap_err().kind(), ErrorKind::OutOfBounds); - let res: Result, _> = ndarray::stack::<_, Ix1>(Axis(0), &[]); + let res: Result, _> = ndarray::stack::, Ix1, _>(Axis(0), &[]); assert_eq!(res.unwrap_err().kind(), ErrorKind::Unsupported); } From 91ae90ac1241bb7b32c23ad1dbde8fb092f1af54 Mon Sep 17 00:00:00 2001 From: Craig Watson Date: Fri, 1 May 2026 13:32:00 -0700 Subject: [PATCH 2/3] Simplify trait bounds --- src/stacking.rs | 13 ++++++------- tests/stacking.rs | 18 ++++++------------ 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/src/stacking.rs b/src/stacking.rs index b2a23f85c..eb90d8524 100644 --- a/src/stacking.rs +++ b/src/stacking.rs @@ -33,10 +33,10 @@ use crate::imp_prelude::*; /// [3., 3.]])) /// ); /// ``` -pub fn concatenate(axis: Axis, arrays: &[ArrayBase]) -> Result, ShapeError> +pub fn concatenate(axis: Axis, arrays: &[ArrayBase]) -> Result, ShapeError> where - S: Data, - A: Clone, + S: Data, + S::Elem: Clone, D: RemoveAxis, { if arrays.is_empty() { @@ -97,12 +97,11 @@ where /// ); /// # } /// ``` -pub fn stack(axis: Axis, arrays: &[ArrayBase]) -> Result, ShapeError> +pub fn stack(axis: Axis, arrays: &[ArrayBase]) -> Result, ShapeError> where - S: Data, - A: Clone, + S: Data, + S::Elem: Clone, D: Dimension, - D::Larger: RemoveAxis, { if arrays.is_empty() { return Err(from_kind(ErrorKind::Unsupported)); diff --git a/tests/stacking.rs b/tests/stacking.rs index be98d1697..7dfceacfc 100644 --- a/tests/stacking.rs +++ b/tests/stacking.rs @@ -1,24 +1,19 @@ use ndarray::{arr2, arr3, aview1, aview2, concatenate, stack, Array2, Axis, ErrorKind, Ix1, ViewRepr}; #[test] -fn concatenating() -{ +fn concatenating() { let a = arr2(&[[2., 2.], [3., 3.]]); let b = ndarray::concatenate(Axis(0), &[a.view(), a.view()]).unwrap(); assert_eq!(b, arr2(&[[2., 2.], [3., 3.], [2., 2.], [3., 3.]])); let c = concatenate![Axis(0), a, b]; - assert_eq!( - c, - arr2(&[[2., 2.], [3., 3.], [2., 2.], [3., 3.], [2., 2.], [3., 3.]]) - ); + assert_eq!(c, arr2(&[[2., 2.], [3., 3.], [2., 2.], [3., 3.], [2., 2.], [3., 3.]])); let d = concatenate![Axis(0), a.row(0), &[9., 9.]]; assert_eq!(d, aview1(&[2., 2., 9., 9.])); let d = concatenate![Axis(1), a.row(0).insert_axis(Axis(1)), aview1(&[9., 9.]).insert_axis(Axis(1))]; - assert_eq!(d, aview2(&[[2., 9.], - [2., 9.]])); + assert_eq!(d, aview2(&[[2., 9.], [2., 9.]])); let d = concatenate![Axis(0), a.row(0).insert_axis(Axis(1)), aview1(&[9., 9.]).insert_axis(Axis(1))]; assert_eq!(d, aview2(&[[2.], [2.], [9.], [9.]])); @@ -29,13 +24,12 @@ fn concatenating() let res = ndarray::concatenate(Axis(2), &[a.view(), c.view()]); assert_eq!(res.unwrap_err().kind(), ErrorKind::OutOfBounds); - let res: Result, _> = ndarray::concatenate::, _, _>(Axis(0), &[]); + let res: Result, _> = ndarray::concatenate::, _>(Axis(0), &[]); assert_eq!(res.unwrap_err().kind(), ErrorKind::Unsupported); } #[test] -fn stacking() -{ +fn stacking() { let a = arr2(&[[2., 2.], [3., 3.]]); let b = ndarray::stack(Axis(0), &[a.view(), a.view()]).unwrap(); assert_eq!(b, arr3(&[[[2., 2.], [3., 3.]], [[2., 2.], [3., 3.]]])); @@ -50,6 +44,6 @@ fn stacking() let res = ndarray::stack(Axis(3), &[a.view(), a.view()]); assert_eq!(res.unwrap_err().kind(), ErrorKind::OutOfBounds); - let res: Result, _> = ndarray::stack::, Ix1, _>(Axis(0), &[]); + let res: Result, _> = ndarray::stack::, Ix1>(Axis(0), &[]); assert_eq!(res.unwrap_err().kind(), ErrorKind::Unsupported); } From a7dcc6908392ab7c7e3a2b9857040d9460730ea9 Mon Sep 17 00:00:00 2001 From: Craig Watson Date: Sat, 6 Jun 2026 19:45:29 -0700 Subject: [PATCH 3/3] formatting --- crates/blas-tests/src/lib.rs | 6 +- crates/blas-tests/tests/oper.rs | 45 +---- crates/numeric-tests/tests/accuracy.rs | 22 +- src/array_approx.rs | 38 +--- src/arrayformat.rs | 25 +-- src/arraytraits.rs | 10 +- src/dimension/mod.rs | 66 +----- src/dimension/ndindex.rs | 32 +-- src/dimension/reshape.rs | 6 +- src/free_functions.rs | 109 +++++----- src/impl_methods.rs | 5 +- src/impl_ops.rs | 60 +----- src/impl_owned_array.rs | 12 +- src/impl_raw_views.rs | 38 +--- src/iterators/chunks.rs | 10 +- src/iterators/into_iter.rs | 8 +- src/iterators/mod.rs | 24 +-- src/iterators/windows.rs | 12 +- src/linalg/impl_linalg.rs | 75 ++++--- src/macro_utils.rs | 22 +- src/numeric/impl_float_maths.rs | 2 +- src/slice.rs | 3 +- src/zip/mod.rs | 13 +- tests/append.rs | 147 ++++---------- tests/array-construct.rs | 8 +- tests/array.rs | 269 +++++-------------------- tests/complex.rs | 5 +- tests/dimension.rs | 38 +--- tests/iterators.rs | 27 +-- tests/numeric.rs | 91 ++------- tests/oper.rs | 53 +---- tests/par_zip.rs | 3 +- tests/reshape.rs | 48 +++-- tests/stacking.rs | 6 +- tests/windows.rs | 98 ++++----- 35 files changed, 437 insertions(+), 999 deletions(-) diff --git a/crates/blas-tests/src/lib.rs b/crates/blas-tests/src/lib.rs index fc031eedb..ad453c8e2 100644 --- a/crates/blas-tests/src/lib.rs +++ b/crates/blas-tests/src/lib.rs @@ -1,4 +1,6 @@ #[cfg(not(feature = "blas-src"))] -compile_error!("Missing backend: could not compile. +compile_error!( + "Missing backend: could not compile. Help: For this testing crate, select one of the blas backend features, for example \ - openblas-system"); + openblas-system" +); diff --git a/crates/blas-tests/tests/oper.rs b/crates/blas-tests/tests/oper.rs index f604ae091..79d276baf 100644 --- a/crates/blas-tests/tests/oper.rs +++ b/crates/blas-tests/tests/oper.rs @@ -315,12 +315,7 @@ fn gemm_c64_1_f() let mut y = range_mat_complex64(m, 1); let answer = reference_mat_mul(&a, &x) + &y; general_mat_mul(Complex64::new(1.0, 0.), &a, &x, Complex64::new(1.0, 0.), &mut y); - assert_relative_eq!( - y.mapv(|i| i.norm_sqr()), - answer.mapv(|i| i.norm_sqr()), - epsilon = 1e-12, - max_relative = 1e-7 - ); + assert_relative_eq!(y.mapv(|i| i.norm_sqr()), answer.mapv(|i| i.norm_sqr()), epsilon = 1e-12, max_relative = 1e-7); } #[test] @@ -333,12 +328,7 @@ fn gemm_c32_1_f() let mut y = range_mat_complex(m, 1); let answer = reference_mat_mul(&a, &x) + &y; general_mat_mul(Complex32::new(1.0, 0.), &a, &x, Complex32::new(1.0, 0.), &mut y); - assert_relative_eq!( - y.mapv(|i| i.norm_sqr()), - answer.mapv(|i| i.norm_sqr()), - epsilon = 1e-12, - max_relative = 1e-7 - ); + assert_relative_eq!(y.mapv(|i| i.norm_sqr()), answer.mapv(|i| i.norm_sqr()), epsilon = 1e-12, max_relative = 1e-7); } #[test] @@ -353,12 +343,7 @@ fn gemm_c64_actually_complex() let beta = Complex64::new(1.0, 1.0); let answer = alpha * reference_mat_mul(&a, &b) + beta * &y; general_mat_mul(alpha.clone(), &a, &b, beta.clone(), &mut y); - assert_relative_eq!( - y.mapv(|i| i.norm_sqr()), - answer.mapv(|i| i.norm_sqr()), - epsilon = 1e-12, - max_relative = 1e-7 - ); + assert_relative_eq!(y.mapv(|i| i.norm_sqr()), answer.mapv(|i| i.norm_sqr()), epsilon = 1e-12, max_relative = 1e-7); } #[test] @@ -366,17 +351,7 @@ fn gen_mat_vec_mul() { let alpha = -2.3; let beta = 3.14; - let sizes = vec![ - (4, 4), - (8, 8), - (17, 15), - (4, 17), - (17, 3), - (19, 18), - (16, 17), - (15, 16), - (67, 63), - ]; + let sizes = vec![(4, 4), (8, 8), (17, 15), (4, 17), (17, 3), (19, 18), (16, 17), (15, 16), (67, 63)]; // test different strides for &s1 in &[1, 2, -1, -2] { for &s2 in &[1, 2, -1, -2] { @@ -408,17 +383,7 @@ fn gen_mat_vec_mul() #[test] fn vec_mat_mul() { - let sizes = vec![ - (4, 4), - (8, 8), - (17, 15), - (4, 17), - (17, 3), - (19, 18), - (16, 17), - (15, 16), - (67, 63), - ]; + let sizes = vec![(4, 4), (8, 8), (17, 15), (4, 17), (17, 3), (19, 18), (16, 17), (15, 16), (67, 63)]; // test different strides for &s1 in &[1, 2, -1, -2] { for &s2 in &[1, 2, -1, -2] { diff --git a/crates/numeric-tests/tests/accuracy.rs b/crates/numeric-tests/tests/accuracy.rs index db10d57cd..ff8a84080 100644 --- a/crates/numeric-tests/tests/accuracy.rs +++ b/crates/numeric-tests/tests/accuracy.rs @@ -216,9 +216,14 @@ where let diff = &c - &reference; let max_diff = diff.iter().copied().fold(A::zero(), A::max); let max_elt = reference.iter().copied().fold(A::zero(), A::max); - println!("Max elt diff={:?}, max={:?}, ratio={:.4e}", max_diff, max_elt, (max_diff/max_elt).as_()); - assert!((max_diff / max_elt).as_() < limit, - "Expected relative norm diff < {:e}, found {:?} / {:?}", limit, max_diff, max_elt); + println!("Max elt diff={:?}, max={:?}, ratio={:.4e}", max_diff, max_elt, (max_diff / max_elt).as_()); + assert!( + (max_diff / max_elt).as_() < limit, + "Expected relative norm diff < {:e}, found {:?} / {:?}", + limit, + max_diff, + max_elt + ); } } @@ -249,9 +254,14 @@ where let max_elt = |elt: &Complex<_>| A::max(A::abs(elt.re), A::abs(elt.im)); let max_diff = diff.iter().map(max_elt).fold(A::zero(), A::max); let max_elt = reference.iter().map(max_elt).fold(A::zero(), A::max); - println!("Max elt diff={:?}, max={:?}, ratio={:.4e}", max_diff, max_elt, (max_diff/max_elt).as_()); - assert!((max_diff / max_elt).as_() < limit, - "Expected relative norm diff < {:e}, found {:?} / {:?}", limit, max_diff, max_elt); + println!("Max elt diff={:?}, max={:?}, ratio={:.4e}", max_diff, max_elt, (max_diff / max_elt).as_()); + assert!( + (max_diff / max_elt).as_() < limit, + "Expected relative norm diff < {:e}, found {:?} / {:?}", + limit, + max_diff, + max_elt + ); } } diff --git a/src/array_approx.rs b/src/array_approx.rs index 93d1bd0ac..413700b79 100644 --- a/src/array_approx.rs +++ b/src/array_approx.rs @@ -89,19 +89,14 @@ macro_rules! impl_approx_traits { A::default_max_relative() } - fn relative_eq( - &self, - other: &ArrayRef, - epsilon: A::Epsilon, - max_relative: A::Epsilon, - ) -> bool { + fn relative_eq(&self, other: &ArrayRef, epsilon: A::Epsilon, max_relative: A::Epsilon) -> bool { if self.shape() != other.shape() { return false; } - Zip::from(self).and(other).all(move |a, b| { - A::relative_eq(a, b, epsilon.clone(), max_relative.clone()) - }) + Zip::from(self) + .and(other) + .all(move |a, b| A::relative_eq(a, b, epsilon.clone(), max_relative.clone())) } } @@ -118,12 +113,7 @@ macro_rules! impl_approx_traits { A::default_max_relative() } - fn relative_eq( - &self, - other: &ArrayBase, - epsilon: A::Epsilon, - max_relative: A::Epsilon, - ) -> bool { + fn relative_eq(&self, other: &ArrayBase, epsilon: A::Epsilon, max_relative: A::Epsilon) -> bool { (**self).relative_eq(other, epsilon, max_relative) } } @@ -139,12 +129,7 @@ macro_rules! impl_approx_traits { A::default_max_ulps() } - fn ulps_eq( - &self, - other: &ArrayRef, - epsilon: A::Epsilon, - max_ulps: u32, - ) -> bool { + fn ulps_eq(&self, other: &ArrayRef, epsilon: A::Epsilon, max_ulps: u32) -> bool { if self.shape() != other.shape() { return false; } @@ -168,12 +153,7 @@ macro_rules! impl_approx_traits { A::default_max_ulps() } - fn ulps_eq( - &self, - other: &ArrayBase, - epsilon: A::Epsilon, - max_ulps: u32, - ) -> bool { + fn ulps_eq(&self, other: &ArrayBase, epsilon: A::Epsilon, max_ulps: u32) -> bool { (**self).ulps_eq(other, epsilon, max_ulps) } } @@ -183,8 +163,8 @@ macro_rules! impl_approx_traits { use crate::prelude::*; use alloc::vec; use $approx::{ - assert_abs_diff_eq, assert_abs_diff_ne, assert_relative_eq, assert_relative_ne, - assert_ulps_eq, assert_ulps_ne, + assert_abs_diff_eq, assert_abs_diff_ne, assert_relative_eq, assert_relative_ne, assert_ulps_eq, + assert_ulps_ne, }; #[test] diff --git a/src/arrayformat.rs b/src/arrayformat.rs index 7e5e1b1c9..f6322afde 100644 --- a/src/arrayformat.rs +++ b/src/arrayformat.rs @@ -221,13 +221,7 @@ impl fmt::Debug for ArrayRef format_array(self, f, <_>::fmt, &fmt_opt)?; // Add extra information for Debug - write!( - f, - ", shape={:?}, strides={:?}, layout={:?}", - self.shape(), - self.strides(), - self.view().layout(), - )?; + write!(f, ", shape={:?}, strides={:?}, layout={:?}", self.shape(), self.strides(), self.view().layout(),)?; match D::NDIM { Some(ndim) => write!(f, ", const ndim={}", ndim)?, None => write!(f, ", dynamic ndim={}", self.ndim())?, @@ -355,12 +349,7 @@ mod formatting_with_omit fn assert_str_eq(expected: &str, actual: &str) { // use assert to avoid printing the strings twice on failure - assert!( - expected == actual, - "formatting assertion failed\nexpected:\n{}\nactual:\n{}\n", - expected, - actual, - ); + assert!(expected == actual, "formatting assertion failed\nexpected:\n{}\nactual:\n{}\n", expected, actual,); } fn ellipsize(limit: usize, sep: &str, elements: impl IntoIterator) -> String @@ -456,10 +445,7 @@ mod formatting_with_omit let a = Array2::from_elem((ARRAY_MANY_ELEMENT_LIMIT / 10, 10), 1); let actual = format!("{}", a); let row = format!("{}", a.row(0)); - let expected = format!( - "[{}]", - ellipsize(AXIS_LIMIT_COL, ",\n ", (0..a.nrows()).map(|_| &row)) - ); + let expected = format!("[{}]", ellipsize(AXIS_LIMIT_COL, ",\n ", (0..a.nrows()).map(|_| &row))); assert_str_eq(&expected, &actual); } @@ -480,10 +466,7 @@ mod formatting_with_omit let a = Array2::from_elem((AXIS_2D_OVERFLOW_LIMIT + overflow, AXIS_2D_OVERFLOW_LIMIT + overflow), 1); let actual = format!("{}", a); let row = format!("[{}]", ellipsize(AXIS_LIMIT_ROW, ", ", a.row(0))); - let expected = format!( - "[{}]", - ellipsize(AXIS_LIMIT_COL, ",\n ", (0..a.nrows()).map(|_| &row)) - ); + let expected = format!("[{}]", ellipsize(AXIS_LIMIT_COL, ",\n ", (0..a.nrows()).map(|_| &row))); assert_str_eq(&expected, &actual); } diff --git a/src/arraytraits.rs b/src/arraytraits.rs index 8b214ac9d..b3b980dc3 100644 --- a/src/arraytraits.rs +++ b/src/arraytraits.rs @@ -580,10 +580,7 @@ where Slice: AsMut<[A]> { let xs = slice.as_mut(); if mem::size_of::() == 0 { - assert!( - xs.len() <= isize::MAX as usize, - "Slice length must fit in `isize`.", - ); + assert!(xs.len() <= isize::MAX as usize, "Slice length must fit in `isize`.",); } unsafe { Self::from_shape_ptr(xs.len(), xs.as_mut_ptr()) } } @@ -619,10 +616,7 @@ impl<'a, A, const N: usize> From<&'a mut [[A; N]]> for ArrayViewMut<'a, A, Ix2> if size_of::() == 0 { dimension::size_of_shape_checked(&dim).expect("Product of non-zero axis lengths must not overflow isize."); } else if N == 0 { - assert!( - xs.len() <= isize::MAX as usize, - "Product of non-zero axis lengths must not overflow isize.", - ); + assert!(xs.len() <= isize::MAX as usize, "Product of non-zero axis lengths must not overflow isize.",); } // `cols * rows` is guaranteed to fit in `isize` because we checked that it fits in diff --git a/src/dimension/mod.rs b/src/dimension/mod.rs index 4731da1b5..2f63e30de 100644 --- a/src/dimension/mod.rs +++ b/src/dimension/mod.rs @@ -410,18 +410,8 @@ fn to_abs_slice(axis_len: usize, slice: Slice) -> (usize, usize, isize) if end < start { end = start; } - ndassert!( - start <= axis_len, - "Slice begin {} is past end of axis of length {}", - start, - axis_len, - ); - ndassert!( - end <= axis_len, - "Slice end {} is past end of axis of length {}", - end, - axis_len, - ); + ndassert!(start <= axis_len, "Slice begin {} is past end of axis of length {}", start, axis_len,); + ndassert!(end <= axis_len, "Slice end {} is past end of axis of length {}", end, axis_len,); ndassert!(step != 0, "Slice stride must not be zero"); (start, end, step) } @@ -1100,14 +1090,8 @@ mod test assert_eq!(slice_min_max(10, Slice::new(-8, Some(8), -3)), Some((4, 7))); assert_eq!(slice_min_max(10, Slice::new(1, Some(-2), -3)), Some((1, 7))); assert_eq!(slice_min_max(10, Slice::new(2, Some(-2), -3)), Some((4, 7))); - assert_eq!( - slice_min_max(10, Slice::new(-9, Some(-2), -3)), - Some((1, 7)) - ); - assert_eq!( - slice_min_max(10, Slice::new(-8, Some(-2), -3)), - Some((4, 7)) - ); + assert_eq!(slice_min_max(10, Slice::new(-9, Some(-2), -3)), Some((1, 7))); + assert_eq!(slice_min_max(10, Slice::new(-8, Some(-2), -3)), Some((4, 7))); assert_eq!(slice_min_max(9, Slice::new(2, None, -3)), Some((2, 8))); assert_eq!(slice_min_max(9, Slice::new(-7, None, -3)), Some((2, 8))); assert_eq!(slice_min_max(9, Slice::new(3, None, -3)), Some((5, 8))); @@ -1117,46 +1101,18 @@ mod test #[test] fn slices_intersect_true() { - assert!(slices_intersect( - &Dim([4, 5]), - s![NewAxis, .., NewAxis, ..], - s![.., NewAxis, .., NewAxis] - )); - assert!(slices_intersect( - &Dim([4, 5]), - s![NewAxis, 0, ..], - s![0, ..] - )); - assert!(slices_intersect( - &Dim([4, 5]), - s![..;2, ..], - s![..;3, NewAxis, ..] - )); - assert!(slices_intersect( - &Dim([4, 5]), - s![.., ..;2], - s![.., 1..;3, NewAxis] - )); + assert!(slices_intersect(&Dim([4, 5]), s![NewAxis, .., NewAxis, ..], s![.., NewAxis, .., NewAxis])); + assert!(slices_intersect(&Dim([4, 5]), s![NewAxis, 0, ..], s![0, ..])); + assert!(slices_intersect(&Dim([4, 5]), s![..;2, ..], s![..;3, NewAxis, ..])); + assert!(slices_intersect(&Dim([4, 5]), s![.., ..;2], s![.., 1..;3, NewAxis])); assert!(slices_intersect(&Dim([4, 10]), s![.., ..;9], s![.., 3..;6])); } #[test] fn slices_intersect_false() { - assert!(!slices_intersect( - &Dim([4, 5]), - s![..;2, ..], - s![NewAxis, 1..;2, ..] - )); - assert!(!slices_intersect( - &Dim([4, 5]), - s![..;2, NewAxis, ..], - s![1..;3, ..] - )); - assert!(!slices_intersect( - &Dim([4, 5]), - s![.., ..;9], - s![.., 3..;6, NewAxis] - )); + assert!(!slices_intersect(&Dim([4, 5]), s![..;2, ..], s![NewAxis, 1..;2, ..])); + assert!(!slices_intersect(&Dim([4, 5]), s![..;2, NewAxis, ..], s![1..;3, ..])); + assert!(!slices_intersect(&Dim([4, 5]), s![.., ..;9], s![.., 3..;6, NewAxis])); } } diff --git a/src/dimension/ndindex.rs b/src/dimension/ndindex.rs index ca2a3ea69..1426f8552 100644 --- a/src/dimension/ndindex.rs +++ b/src/dimension/ndindex.rs @@ -197,26 +197,14 @@ unsafe impl NdIndex for Dim<[Ix; N]> #[inline] fn index_checked(&self, dim: &IxDyn, strides: &IxDyn) -> Option { - debug_assert_eq!( - strides.ndim(), - N, - "Attempted to index with {:?} in array with {} axes", - self, - strides.ndim() - ); + debug_assert_eq!(strides.ndim(), N, "Attempted to index with {:?} in array with {} axes", self, strides.ndim()); stride_offset_checked(dim.ix(), strides.ix(), self.ix()) } #[inline] fn index_unchecked(&self, strides: &IxDyn) -> isize { - debug_assert_eq!( - strides.ndim(), - N, - "Attempted to index with {:?} in array with {} axes", - self, - strides.ndim() - ); + debug_assert_eq!(strides.ndim(), N, "Attempted to index with {:?} in array with {} axes", self, strides.ndim()); (0..N) .map(|i| stride_offset(get!(self, i), get!(strides, i))) .sum() @@ -229,26 +217,14 @@ unsafe impl NdIndex for [Ix; N] #[inline] fn index_checked(&self, dim: &IxDyn, strides: &IxDyn) -> Option { - debug_assert_eq!( - strides.ndim(), - N, - "Attempted to index with {:?} in array with {} axes", - self, - strides.ndim() - ); + debug_assert_eq!(strides.ndim(), N, "Attempted to index with {:?} in array with {} axes", self, strides.ndim()); stride_offset_checked(dim.ix(), strides.ix(), self) } #[inline] fn index_unchecked(&self, strides: &IxDyn) -> isize { - debug_assert_eq!( - strides.ndim(), - N, - "Attempted to index with {:?} in array with {} axes", - self, - strides.ndim() - ); + debug_assert_eq!(strides.ndim(), N, "Attempted to index with {:?} in array with {} axes", self, strides.ndim()); (0..N) .map(|i| stride_offset(self[i], get!(strides, i))) .sum() diff --git a/src/dimension/reshape.rs b/src/dimension/reshape.rs index abcec4993..707777200 100644 --- a/src/dimension/reshape.rs +++ b/src/dimension/reshape.rs @@ -153,14 +153,12 @@ fn test_reshape() macro_rules! test_reshape { (fail $order:ident from $from:expr, $stride:expr, to $to:expr) => { let res = reshape_dim(&Dim($from), &Dim($stride), &Dim($to), Order::$order); - println!("Reshape {:?} {:?} to {:?}, order {:?}\n => {:?}", - $from, $stride, $to, Order::$order, res); + println!("Reshape {:?} {:?} to {:?}, order {:?}\n => {:?}", $from, $stride, $to, Order::$order, res); let _res = res.expect_err("Expected failed reshape"); }; (ok $order:ident from $from:expr, $stride:expr, to $to:expr, $to_stride:expr) => {{ let res = reshape_dim(&Dim($from), &Dim($stride), &Dim($to), Order::$order); - println!("Reshape {:?} {:?} to {:?}, order {:?}\n => {:?}", - $from, $stride, $to, Order::$order, res); + println!("Reshape {:?} {:?} to {:?}, order {:?}\n => {:?}", $from, $stride, $to, Order::$order, res); println!("default stride for from dim: {:?}", Dim($from).default_strides()); println!("default stride for to dim: {:?}", Dim($to).default_strides()); let res = res.expect("Expected successful reshape"); diff --git a/src/free_functions.rs b/src/free_functions.rs index 4ad69f2c3..31905cf2d 100644 --- a/src/free_functions.rs +++ b/src/free_functions.rs @@ -142,10 +142,7 @@ pub const fn aview0(x: &A) -> ArrayView0<'_, A> pub const fn aview1(xs: &[A]) -> ArrayView1<'_, A> { if size_of::() == 0 { - assert!( - xs.len() <= isize::MAX as usize, - "Slice length must fit in `isize`.", - ); + assert!(xs.len() <= isize::MAX as usize, "Slice length must fit in `isize`.",); } ArrayBase { data: ViewRepr::new(), @@ -183,19 +180,14 @@ pub const fn aview2(xs: &[[A; N]]) -> ArrayView2<'_, A> if size_of::() == 0 { if let Some(n_elems) = rows.checked_mul(cols) { assert!( - rows <= isize::MAX as usize - && cols <= isize::MAX as usize - && n_elems <= isize::MAX as usize, + rows <= isize::MAX as usize && cols <= isize::MAX as usize && n_elems <= isize::MAX as usize, "Product of non-zero axis lengths must not overflow isize.", ); } else { panic!("Overflow in number of elements."); } } else if N == 0 { - assert!( - rows <= isize::MAX as usize, - "Product of non-zero axis lengths must not overflow isize.", - ); + assert!(rows <= isize::MAX as usize, "Product of non-zero axis lengths must not overflow isize.",); } // Safe because references are always non-null. let ptr = unsafe { NonNull::new_unchecked(xs.as_ptr() as *mut A) }; @@ -563,7 +555,16 @@ mod meshgrid_impl fn meshgrid(arrays: Self, indexing: MeshIndex) -> Self::Output { - meshgrid_body!(6, indexing, (arrays.0, 0), (arrays.1, 1), (arrays.2, 2), (arrays.3, 3), (arrays.4, 4), (arrays.5, 5)) + meshgrid_body!( + 6, + indexing, + (arrays.0, 0), + (arrays.1, 1), + (arrays.2, 2), + (arrays.3, 3), + (arrays.4, 4), + (arrays.5, 5) + ) } } @@ -683,41 +684,59 @@ mod tests let y = array![4, 5, 6, 7]; let z = array![-1, -2]; let (xx, yy, zz) = meshgrid((&x, &y, &z), MeshIndex::XY); - assert_eq!(xx, array![ - [[1, 1], [2, 2], [3, 3]], - [[1, 1], [2, 2], [3, 3]], - [[1, 1], [2, 2], [3, 3]], - [[1, 1], [2, 2], [3, 3]], - ]); - assert_eq!(yy, array![ - [[4, 4], [4, 4], [4, 4]], - [[5, 5], [5, 5], [5, 5]], - [[6, 6], [6, 6], [6, 6]], - [[7, 7], [7, 7], [7, 7]], - ]); - assert_eq!(zz, array![ - [[-1, -2], [-1, -2], [-1, -2]], - [[-1, -2], [-1, -2], [-1, -2]], - [[-1, -2], [-1, -2], [-1, -2]], - [[-1, -2], [-1, -2], [-1, -2]], - ]); + assert_eq!( + xx, + array![ + [[1, 1], [2, 2], [3, 3]], + [[1, 1], [2, 2], [3, 3]], + [[1, 1], [2, 2], [3, 3]], + [[1, 1], [2, 2], [3, 3]], + ] + ); + assert_eq!( + yy, + array![ + [[4, 4], [4, 4], [4, 4]], + [[5, 5], [5, 5], [5, 5]], + [[6, 6], [6, 6], [6, 6]], + [[7, 7], [7, 7], [7, 7]], + ] + ); + assert_eq!( + zz, + array![ + [[-1, -2], [-1, -2], [-1, -2]], + [[-1, -2], [-1, -2], [-1, -2]], + [[-1, -2], [-1, -2], [-1, -2]], + [[-1, -2], [-1, -2], [-1, -2]], + ] + ); let (xx, yy, zz) = meshgrid((&x, &y, &z), MeshIndex::IJ); - assert_eq!(xx, array![ - [[1, 1], [1, 1], [1, 1], [1, 1]], - [[2, 2], [2, 2], [2, 2], [2, 2]], - [[3, 3], [3, 3], [3, 3], [3, 3]], - ]); - assert_eq!(yy, array![ - [[4, 4], [5, 5], [6, 6], [7, 7]], - [[4, 4], [5, 5], [6, 6], [7, 7]], - [[4, 4], [5, 5], [6, 6], [7, 7]], - ]); - assert_eq!(zz, array![ - [[-1, -2], [-1, -2], [-1, -2], [-1, -2]], - [[-1, -2], [-1, -2], [-1, -2], [-1, -2]], - [[-1, -2], [-1, -2], [-1, -2], [-1, -2]], - ]); + assert_eq!( + xx, + array![ + [[1, 1], [1, 1], [1, 1], [1, 1]], + [[2, 2], [2, 2], [2, 2], [2, 2]], + [[3, 3], [3, 3], [3, 3], [3, 3]], + ] + ); + assert_eq!( + yy, + array![ + [[4, 4], [5, 5], [6, 6], [7, 7]], + [[4, 4], [5, 5], [6, 6], [7, 7]], + [[4, 4], [5, 5], [6, 6], [7, 7]], + ] + ); + assert_eq!( + zz, + array![ + [[-1, -2], [-1, -2], [-1, -2], [-1, -2]], + [[-1, -2], [-1, -2], [-1, -2], [-1, -2]], + [[-1, -2], [-1, -2], [-1, -2], [-1, -2]], + ] + ); } #[test] diff --git a/src/impl_methods.rs b/src/impl_methods.rs index 8046f4d1f..54c0555d3 100644 --- a/src/impl_methods.rs +++ b/src/impl_methods.rs @@ -1550,10 +1550,7 @@ impl ArrayRef self.shape() ); - ndassert!( - stride_size >0, - "Stride size must be greater than zero" - ); + ndassert!(stride_size > 0, "Stride size must be greater than zero"); AxisWindows::new_with_stride(self.view(), axis, window_size, stride_size) } diff --git a/src/impl_ops.rs b/src/impl_ops.rs index 53f49cc43..fa57a9cc0 100644 --- a/src/impl_ops.rs +++ b/src/impl_ops.rs @@ -661,54 +661,14 @@ mod assign_ops }; } - impl_assign_op!( - AddAssign, - add_assign, - "Perform `self += rhs` as elementwise addition (in place).\n" - ); - impl_assign_op!( - SubAssign, - sub_assign, - "Perform `self -= rhs` as elementwise subtraction (in place).\n" - ); - impl_assign_op!( - MulAssign, - mul_assign, - "Perform `self *= rhs` as elementwise multiplication (in place).\n" - ); - impl_assign_op!( - DivAssign, - div_assign, - "Perform `self /= rhs` as elementwise division (in place).\n" - ); - impl_assign_op!( - RemAssign, - rem_assign, - "Perform `self %= rhs` as elementwise remainder (in place).\n" - ); - impl_assign_op!( - BitAndAssign, - bitand_assign, - "Perform `self &= rhs` as elementwise bit and (in place).\n" - ); - impl_assign_op!( - BitOrAssign, - bitor_assign, - "Perform `self |= rhs` as elementwise bit or (in place).\n" - ); - impl_assign_op!( - BitXorAssign, - bitxor_assign, - "Perform `self ^= rhs` as elementwise bit xor (in place).\n" - ); - impl_assign_op!( - ShlAssign, - shl_assign, - "Perform `self <<= rhs` as elementwise left shift (in place).\n" - ); - impl_assign_op!( - ShrAssign, - shr_assign, - "Perform `self >>= rhs` as elementwise right shift (in place).\n" - ); + impl_assign_op!(AddAssign, add_assign, "Perform `self += rhs` as elementwise addition (in place).\n"); + impl_assign_op!(SubAssign, sub_assign, "Perform `self -= rhs` as elementwise subtraction (in place).\n"); + impl_assign_op!(MulAssign, mul_assign, "Perform `self *= rhs` as elementwise multiplication (in place).\n"); + impl_assign_op!(DivAssign, div_assign, "Perform `self /= rhs` as elementwise division (in place).\n"); + impl_assign_op!(RemAssign, rem_assign, "Perform `self %= rhs` as elementwise remainder (in place).\n"); + impl_assign_op!(BitAndAssign, bitand_assign, "Perform `self &= rhs` as elementwise bit and (in place).\n"); + impl_assign_op!(BitOrAssign, bitor_assign, "Perform `self |= rhs` as elementwise bit or (in place).\n"); + impl_assign_op!(BitXorAssign, bitxor_assign, "Perform `self ^= rhs` as elementwise bit xor (in place).\n"); + impl_assign_op!(ShlAssign, shl_assign, "Perform `self <<= rhs` as elementwise left shift (in place).\n"); + impl_assign_op!(ShrAssign, shr_assign, "Perform `self >>= rhs` as elementwise right shift (in place).\n"); } diff --git a/src/impl_owned_array.rs b/src/impl_owned_array.rs index d05c080df..71ce2d823 100644 --- a/src/impl_owned_array.rs +++ b/src/impl_owned_array.rs @@ -745,9 +745,12 @@ where D: Dimension if tail_view.ndim() > 1 { sort_axes_in_default_order_tandem(&mut tail_view, &mut array); - debug_assert!(tail_view.is_standard_layout(), - "not std layout dim: {:?}, strides: {:?}", - tail_view.shape(), RawArrayViewMut::strides(&tail_view)); + debug_assert!( + tail_view.is_standard_layout(), + "not std layout dim: {:?}, strides: {:?}", + tail_view.shape(), + RawArrayViewMut::strides(&tail_view) + ); } // Keep track of currently filled length of `self.data` and update it @@ -934,8 +937,7 @@ pub(crate) unsafe fn drop_unreachable_raw( dropped_elements += 1; } - assert_eq!(data_len, dropped_elements + self_len, - "Internal error: inconsistency in move_into"); + assert_eq!(data_len, dropped_elements + self_len, "Internal error: inconsistency in move_into"); } /// Sort axes to standard order, i.e Axis(0) has biggest stride and Axis(n - 1) least stride diff --git a/src/impl_raw_views.rs b/src/impl_raw_views.rs index 2423b9343..a2a23f885 100644 --- a/src/impl_raw_views.rs +++ b/src/impl_raw_views.rs @@ -97,10 +97,7 @@ where D: Dimension #[inline] pub unsafe fn deref_into_view<'a>(self) -> ArrayView<'a, A, D> { - debug_assert!( - is_aligned(self.parts.ptr.as_ptr()), - "The pointer must be aligned." - ); + debug_assert!(is_aligned(self.parts.ptr.as_ptr()), "The pointer must be aligned."); ArrayView::new(self.parts.ptr, self.parts.dim, self.parts.strides) } @@ -147,11 +144,7 @@ where D: Dimension #[track_caller] pub fn cast(self) -> RawArrayView { - assert_eq!( - mem::size_of::(), - mem::size_of::(), - "size mismatch in raw view cast" - ); + assert_eq!(mem::size_of::(), mem::size_of::(), "size mismatch in raw view cast"); let ptr = self.parts.ptr.cast::(); unsafe { RawArrayView::new(ptr, self.parts.dim, self.parts.strides) } } @@ -166,10 +159,7 @@ where D: Dimension { // Check that the size and alignment of `Complex` are as expected. // These assertions should always pass, for arbitrary `T`. - assert_eq!( - mem::size_of::>(), - mem::size_of::().checked_mul(2).unwrap() - ); + assert_eq!(mem::size_of::>(), mem::size_of::().checked_mul(2).unwrap()); assert_eq!(mem::align_of::>(), mem::align_of::()); let dim = self.parts.dim.clone(); @@ -294,8 +284,10 @@ where D: Dimension if let Strides::Custom(strides) = &shape.strides { dimension::strides_non_negative(strides).unwrap(); dimension::max_abs_offset_check_overflow::(&dim, strides).unwrap(); - assert!(!dimension::dim_stride_overlap(&dim, strides), - "The strides must not allow any element to be referenced by two different indices"); + assert!( + !dimension::dim_stride_overlap(&dim, strides), + "The strides must not allow any element to be referenced by two different indices" + ); } else { dimension::size_of_shape_checked(&dim).unwrap(); } @@ -322,10 +314,7 @@ where D: Dimension #[inline] pub unsafe fn deref_into_view<'a>(self) -> ArrayView<'a, A, D> { - debug_assert!( - is_aligned(self.parts.ptr.as_ptr()), - "The pointer must be aligned." - ); + debug_assert!(is_aligned(self.parts.ptr.as_ptr()), "The pointer must be aligned."); ArrayView::new(self.parts.ptr, self.parts.dim, self.parts.strides) } @@ -340,10 +329,7 @@ where D: Dimension #[inline] pub unsafe fn deref_into_view_mut<'a>(self) -> ArrayViewMut<'a, A, D> { - debug_assert!( - is_aligned(self.parts.ptr.as_ptr()), - "The pointer must be aligned." - ); + debug_assert!(is_aligned(self.parts.ptr.as_ptr()), "The pointer must be aligned."); ArrayViewMut::new(self.parts.ptr, self.parts.dim, self.parts.strides) } @@ -377,11 +363,7 @@ where D: Dimension #[track_caller] pub fn cast(self) -> RawArrayViewMut { - assert_eq!( - mem::size_of::(), - mem::size_of::(), - "size mismatch in raw view cast" - ); + assert_eq!(mem::size_of::(), mem::size_of::(), "size mismatch in raw view cast"); let ptr = self.parts.ptr.cast::(); unsafe { RawArrayViewMut::new(ptr, self.parts.dim, self.parts.strides) } } diff --git a/src/iterators/chunks.rs b/src/iterators/chunks.rs index 178ead7e0..9430718d0 100644 --- a/src/iterators/chunks.rs +++ b/src/iterators/chunks.rs @@ -50,10 +50,7 @@ impl<'a, A, D: Dimension> ExactChunks<'a, A, D> let chunk = chunk.into_dimension(); ndassert!( a.ndim() == chunk.ndim(), - concat!( - "Chunk dimension {} does not match array dimension {} ", - "(with array of shape {:?})" - ), + concat!("Chunk dimension {} does not match array dimension {} ", "(with array of shape {:?})"), chunk.ndim(), a.ndim(), a.shape() @@ -149,10 +146,7 @@ impl<'a, A, D: Dimension> ExactChunksMut<'a, A, D> let chunk = chunk.into_dimension(); ndassert!( a.ndim() == chunk.ndim(), - concat!( - "Chunk dimension {} does not match array dimension {} ", - "(with array of shape {:?})" - ), + concat!("Chunk dimension {} does not match array dimension {} ", "(with array of shape {:?})"), chunk.ndim(), a.ndim(), a.shape() diff --git a/src/iterators/into_iter.rs b/src/iterators/into_iter.rs index cacafd2f2..7ee260e52 100644 --- a/src/iterators/into_iter.rs +++ b/src/iterators/into_iter.rs @@ -93,8 +93,12 @@ where D: Dimension unsafe { let data_ptr = self.array_data.as_nonnull_mut(); let view = RawArrayViewMut::new(self.array_head_ptr, self.inner.dim.clone(), self.inner.strides.clone()); - debug_assert!(self.inner.dim.size() < self.data_len, "data_len {} and dim size {}", - self.data_len, self.inner.dim.size()); + debug_assert!( + self.inner.dim.size() < self.data_len, + "data_len {} and dim size {}", + self.data_len, + self.inner.dim.size() + ); drop_unreachable_raw(view, data_ptr, self.data_len); } } diff --git a/src/iterators/mod.rs b/src/iterators/mod.rs index abca3579d..9ede7832f 100644 --- a/src/iterators/mod.rs +++ b/src/iterators/mod.rs @@ -886,13 +886,7 @@ impl AxisIterCore #[inline] unsafe fn offset(&self, index: usize) -> *mut A { - debug_assert!( - index < self.end, - "index={}, end={}, stride={}", - index, - self.end, - self.stride - ); + debug_assert!(index < self.end, "index={}, end={}, stride={}", index, self.end, self.stride); self.ptr.offset(index as isize * self.stride) } @@ -1404,21 +1398,9 @@ macro_rules! chunk_iter_impl { { fn get_subview(&self, index: usize, ptr: *mut A) -> $array<'a, A, D> { if index != self.partial_chunk_index { - unsafe { - $array::new_( - ptr, - self.iter.inner_dim.clone(), - self.iter.inner_strides.clone(), - ) - } + unsafe { $array::new_(ptr, self.iter.inner_dim.clone(), self.iter.inner_strides.clone()) } } else { - unsafe { - $array::new_( - ptr, - self.partial_chunk_dim.clone(), - self.iter.inner_strides.clone(), - ) - } + unsafe { $array::new_(ptr, self.partial_chunk_dim.clone(), self.iter.inner_strides.clone()) } } } diff --git a/src/iterators/windows.rs b/src/iterators/windows.rs index e6fccce46..878a279bb 100644 --- a/src/iterators/windows.rs +++ b/src/iterators/windows.rs @@ -229,7 +229,7 @@ impl<'a, A, D: Dimension> NdProducer for AxisWindows<'a, A, D> ) } - private_impl!{} + private_impl! {} } impl<'a, A, D> IntoIterator for AxisWindows<'a, A, D> @@ -256,10 +256,7 @@ where D: Dimension { ndassert!( a.ndim() == window.ndim(), - concat!( - "Window dimension {} does not match array dimension {} ", - "(with array of shape {:?})" - ), + concat!("Window dimension {} does not match array dimension {} ", "(with array of shape {:?})"), window.ndim(), a.ndim(), a.shape() @@ -267,10 +264,7 @@ where D: Dimension ndassert!( a.ndim() == strides.ndim(), - concat!( - "Stride dimension {} does not match array dimension {} ", - "(with array of shape {:?})" - ), + concat!("Stride dimension {} does not match array dimension {} ", "(with array of shape {:?})"), strides.ndim(), a.ndim(), a.shape() diff --git a/src/linalg/impl_linalg.rs b/src/linalg/impl_linalg.rs index 81c942bc3..c29b99b75 100644 --- a/src/linalg/impl_linalg.rs +++ b/src/linalg/impl_linalg.rs @@ -111,15 +111,8 @@ impl ArrayRef unsafe { let (lhs_ptr, n, incx) = blas_1d_params(self._ptr().as_ptr(), self.len(), self.strides()[0]); - let (rhs_ptr, _, incy) = - blas_1d_params(rhs._ptr().as_ptr(), rhs.len(), rhs.strides()[0]); - let ret = blas_sys::$func( - n, - lhs_ptr as *const $ty, - incx, - rhs_ptr as *const $ty, - incy, - ); + let (rhs_ptr, _, incy) = blas_1d_params(rhs._ptr().as_ptr(), rhs.len(), rhs.strides()[0]); + let ret = blas_sys::$func(n, lhs_ptr as *const $ty, incx, rhs_ptr as *const $ty, incy); return cast_as::<$ty, A>(&ret); } } @@ -183,8 +176,7 @@ macro_rules! impl_dots { { type Output = as Dot>>::Output; - fn dot(&self, rhs: &ArrayBase) -> Self::Output - { + fn dot(&self, rhs: &ArrayBase) -> Self::Output { Dot::dot(&**self, &**rhs) } } @@ -196,8 +188,7 @@ macro_rules! impl_dots { { type Output = as Dot>>::Output; - fn dot(&self, rhs: &ArrayRef) -> Self::Output - { + fn dot(&self, rhs: &ArrayRef) -> Self::Output { (**self).dot(rhs) } } @@ -209,8 +200,7 @@ macro_rules! impl_dots { { type Output = as Dot>>::Output; - fn dot(&self, rhs: &ArrayBase) -> Self::Output - { + fn dot(&self, rhs: &ArrayBase) -> Self::Output { self.dot(&**rhs) } } @@ -340,18 +330,17 @@ fn dot_shape_error(m: usize, k: usize, k2: usize, n: usize) -> ! Some(len) if len <= isize::MAX as usize => {} _ => panic!("ndarray: shape {} × {} overflows isize", m, n), } - panic!( - "ndarray: inputs {} × {} and {} × {} are not compatible for matrix multiplication", - m, k, k2, n - ); + panic!("ndarray: inputs {} × {} and {} × {} are not compatible for matrix multiplication", m, k, k2, n); } #[cold] #[inline(never)] fn general_dot_shape_error(m: usize, k: usize, k2: usize, n: usize, c1: usize, c2: usize) -> ! { - panic!("ndarray: inputs {} × {}, {} × {}, and output {} × {} are not compatible for matrix multiplication", - m, k, k2, n, c1, c2); + panic!( + "ndarray: inputs {} × {}, {} × {}, and output {} × {} are not compatible for matrix multiplication", + m, k, k2, n, c1, c2 + ); } /// Perform the matrix multiplication of the rectangular array `self` and @@ -467,17 +456,17 @@ where A: LinalgScalar cblas_layout, a_trans, b_trans, - m as blas_index, // m, rows of Op(a) - n as blas_index, // n, cols of Op(b) - k as blas_index, // k, cols of Op(a) - gemm_scalar_cast!($ty, alpha), // alpha - a._ptr().as_ptr() as *const _, // a - lda, // lda - b._ptr().as_ptr() as *const _, // b - ldb, // ldb - gemm_scalar_cast!($ty, beta), // beta - c._ptr().as_ptr() as *mut _, // c - ldc, // ldc + m as blas_index, // m, rows of Op(a) + n as blas_index, // n, cols of Op(b) + k as blas_index, // k, cols of Op(a) + gemm_scalar_cast!($ty, alpha), // alpha + a._ptr().as_ptr() as *const _, // a + lda, // lda + b._ptr().as_ptr() as *const _, // b + ldb, // ldb + gemm_scalar_cast!($ty, beta), // beta + c._ptr().as_ptr() as *mut _, // c + ldc, // ldc ); } return; @@ -705,15 +694,15 @@ unsafe fn general_mat_vec_mul_impl( blas_sys::$gemv( cblas_layout, a_trans, - m as blas_index, // m, rows of Op(a) - k as blas_index, // n, cols of Op(a) - cast_as(&alpha), // alpha + m as blas_index, // m, rows of Op(a) + k as blas_index, // n, cols of Op(a) + cast_as(&alpha), // alpha a._ptr().as_ptr() as *const _, // a - a_stride, // lda - x_ptr as *const _, // x + a_stride, // lda + x_ptr as *const _, // x x_stride, - cast_as(&beta), // beta - y_ptr as *mut _, // y + cast_as(&beta), // beta + y_ptr as *mut _, // y y_stride, ); return; @@ -783,8 +772,12 @@ fn same_type() -> bool // **Panics** if `A` and `B` are not the same type fn cast_as(a: &A) -> B { - assert!(same_type::(), "expect type {} and {} to match", - std::any::type_name::(), std::any::type_name::()); + assert!( + same_type::(), + "expect type {} and {} to match", + std::any::type_name::(), + std::any::type_name::() + ); unsafe { ::std::ptr::read(a as *const _ as *const B) } } diff --git a/src/macro_utils.rs b/src/macro_utils.rs index 34c700e65..545070561 100644 --- a/src/macro_utils.rs +++ b/src/macro_utils.rs @@ -61,12 +61,11 @@ macro_rules! expand_if { #[cfg(debug_assertions)] macro_rules! debug_bounds_check { ($self_:ident, $index:expr) => { - if $index.index_checked(&$self_._dim(), &$self_._strides()).is_none() { - panic!( - "ndarray: index {:?} is out of bounds for array of shape {:?}", - $index, - $self_.shape() - ); + if $index + .index_checked(&$self_._dim(), &$self_._strides()) + .is_none() + { + panic!("ndarray: index {:?} is out of bounds for array of shape {:?}", $index, $self_.shape()); } }; } @@ -79,12 +78,11 @@ macro_rules! debug_bounds_check { #[cfg(debug_assertions)] macro_rules! debug_bounds_check_ref { ($self_:ident, $index:expr) => { - if $index.index_checked(&$self_._dim(), &$self_._strides()).is_none() { - panic!( - "ndarray: index {:?} is out of bounds for array of shape {:?}", - $index, - $self_.shape() - ); + if $index + .index_checked(&$self_._dim(), &$self_._strides()) + .is_none() + { + panic!("ndarray: index {:?} is out of bounds for array of shape {:?}", $index, $self_.shape()); } }; } diff --git a/src/numeric/impl_float_maths.rs b/src/numeric/impl_float_maths.rs index 6d6ebce52..5b4b60d0b 100644 --- a/src/numeric/impl_float_maths.rs +++ b/src/numeric/impl_float_maths.rs @@ -314,7 +314,7 @@ mod angle_tests fn test_complex_numbers_radians() { let arr = Array::from_vec(vec![ - Complex::new(1.0f64, 0.0), // 0 + Complex::new(1.0f64, 0.0), // 0 Complex::new(0.0, 1.0), // π/2 Complex::new(-1.0, 0.0), // π Complex::new(0.0, -1.0), // -π/2 diff --git a/src/slice.rs b/src/slice.rs index e2ce1e727..916486db6 100644 --- a/src/slice.rs +++ b/src/slice.rs @@ -588,8 +588,7 @@ where macro_rules! impl_tryfrom_array_for_sliceinfo { ($len:expr) => { - impl TryFrom<[SliceInfoElem; $len]> - for SliceInfo<[SliceInfoElem; $len], Din, Dout> + impl TryFrom<[SliceInfoElem; $len]> for SliceInfo<[SliceInfoElem; $len], Din, Dout> where Din: Dimension, Dout: Dimension, diff --git a/src/zip/mod.rs b/src/zip/mod.rs index b01ae04ff..40e9cebd5 100644 --- a/src/zip/mod.rs +++ b/src/zip/mod.rs @@ -443,11 +443,16 @@ where #[inline] pub(crate) fn debug_assert_c_order(self) -> Self { - debug_assert!(self.layout.is(Layout::CORDER) || self.layout_tendency >= 0 || - self.dimension.slice().iter().filter(|&&d| d > 1).count() <= 1, - "Assertion failed: traversal is not c-order or 1D for \ + debug_assert!( + self.layout.is(Layout::CORDER) + || self.layout_tendency >= 0 + || self.dimension.slice().iter().filter(|&&d| d > 1).count() <= 1, + "Assertion failed: traversal is not c-order or 1D for \ layout {:?}, tendency {}, dimension {:?}", - self.layout, self.layout_tendency, self.dimension); + self.layout, + self.layout_tendency, + self.dimension + ); self } } diff --git a/tests/append.rs b/tests/append.rs index 40beb0f92..3637f9f52 100644 --- a/tests/append.rs +++ b/tests/append.rs @@ -9,19 +9,12 @@ fn push_row() a.push_row(aview1(&[4., 5., 6., 7.])).unwrap(); assert_eq!(a.shape(), &[2, 4]); - assert_eq!(a, - array![[0., 1., 2., 3.], - [4., 5., 6., 7.]]); - - assert_eq!(a.push_row(aview1(&[1.])), - Err(ShapeError::from_kind(ErrorKind::IncompatibleShape))); - assert_eq!(a.push_column(aview1(&[1.])), - Err(ShapeError::from_kind(ErrorKind::IncompatibleShape))); - assert_eq!(a.push_column(aview1(&[1., 2.])), - Ok(())); - assert_eq!(a, - array![[0., 1., 2., 3., 1.], - [4., 5., 6., 7., 2.]]); + assert_eq!(a, array![[0., 1., 2., 3.], [4., 5., 6., 7.]]); + + assert_eq!(a.push_row(aview1(&[1.])), Err(ShapeError::from_kind(ErrorKind::IncompatibleShape))); + assert_eq!(a.push_column(aview1(&[1.])), Err(ShapeError::from_kind(ErrorKind::IncompatibleShape))); + assert_eq!(a.push_column(aview1(&[1., 2.])), Ok(())); + assert_eq!(a, array![[0., 1., 2., 3., 1.], [4., 5., 6., 7., 2.]]); } #[test] @@ -32,17 +25,13 @@ fn push_row_wrong_layout() a.push_row(aview1(&[4., 5., 6., 7.])).unwrap(); assert_eq!(a.shape(), &[2, 4]); - assert_eq!(a, - array![[0., 1., 2., 3.], - [4., 5., 6., 7.]]); + assert_eq!(a, array![[0., 1., 2., 3.], [4., 5., 6., 7.]]); assert_eq!(a.strides(), &[4, 1]); // Changing the memory layout to fit the next append let mut a2 = a.clone(); a2.push_column(aview1(&[1., 2.])).unwrap(); - assert_eq!(a2, - array![[0., 1., 2., 3., 1.], - [4., 5., 6., 7., 2.]]); + assert_eq!(a2, array![[0., 1., 2., 3., 1.], [4., 5., 6., 7., 2.]]); assert_eq!(a2.strides(), &[1, 2]); // Clone the array @@ -52,9 +41,7 @@ fn push_row_wrong_layout() let mut b = Array::zeros(dim); b.append(Axis(1), a.view()).unwrap(); assert_eq!(b.push_column(aview1(&[1., 2.])), Ok(())); - assert_eq!(b, - array![[0., 1., 2., 3., 1.], - [4., 5., 6., 7., 2.]]); + assert_eq!(b, array![[0., 1., 2., 3., 1.], [4., 5., 6., 7., 2.]]); } #[test] @@ -65,9 +52,7 @@ fn push_row_neg_stride_1() a.push_row(aview1(&[4., 5., 6., 7.])).unwrap(); assert_eq!(a.shape(), &[2, 4]); - assert_eq!(a, - array![[0., 1., 2., 3.], - [4., 5., 6., 7.]]); + assert_eq!(a, array![[0., 1., 2., 3.], [4., 5., 6., 7.]]); assert_eq!(a.strides(), &[4, 1]); a.invert_axis(Axis(0)); @@ -77,27 +62,19 @@ fn push_row_neg_stride_1() println!("a = {:?}", a); println!("a2 = {:?}", a2); a2.push_column(aview1(&[1., 2.])).unwrap(); - assert_eq!(a2, - array![[4., 5., 6., 7., 1.], - [0., 1., 2., 3., 2.]]); + assert_eq!(a2, array![[4., 5., 6., 7., 1.], [0., 1., 2., 3., 2.]]); assert_eq!(a2.strides(), &[1, 2]); a.invert_axis(Axis(1)); let mut a3 = a.clone(); a3.push_row(aview1(&[4., 5., 6., 7.])).unwrap(); - assert_eq!(a3, - array![[7., 6., 5., 4.], - [3., 2., 1., 0.], - [4., 5., 6., 7.]]); + assert_eq!(a3, array![[7., 6., 5., 4.], [3., 2., 1., 0.], [4., 5., 6., 7.]]); assert_eq!(a3.strides(), &[4, 1]); a.invert_axis(Axis(0)); let mut a4 = a.clone(); a4.push_row(aview1(&[4., 5., 6., 7.])).unwrap(); - assert_eq!(a4, - array![[3., 2., 1., 0.], - [7., 6., 5., 4.], - [4., 5., 6., 7.]]); + assert_eq!(a4, array![[3., 2., 1., 0.], [7., 6., 5., 4.], [4., 5., 6., 7.]]); assert_eq!(a4.strides(), &[4, -1]); } @@ -109,9 +86,7 @@ fn push_row_neg_stride_2() a.push_row(aview1(&[4., 5., 6., 7.])).unwrap(); assert_eq!(a.shape(), &[2, 4]); - assert_eq!(a, - array![[0., 1., 2., 3.], - [4., 5., 6., 7.]]); + assert_eq!(a, array![[0., 1., 2., 3.], [4., 5., 6., 7.]]); assert_eq!(a.strides(), &[4, 1]); a.invert_axis(Axis(1)); @@ -121,27 +96,19 @@ fn push_row_neg_stride_2() println!("a = {:?}", a); println!("a2 = {:?}", a2); a2.push_column(aview1(&[1., 2.])).unwrap(); - assert_eq!(a2, - array![[3., 2., 1., 0., 1.], - [7., 6., 5., 4., 2.]]); + assert_eq!(a2, array![[3., 2., 1., 0., 1.], [7., 6., 5., 4., 2.]]); assert_eq!(a2.strides(), &[1, 2]); a.invert_axis(Axis(0)); let mut a3 = a.clone(); a3.push_row(aview1(&[4., 5., 6., 7.])).unwrap(); - assert_eq!(a3, - array![[7., 6., 5., 4.], - [3., 2., 1., 0.], - [4., 5., 6., 7.]]); + assert_eq!(a3, array![[7., 6., 5., 4.], [3., 2., 1., 0.], [4., 5., 6., 7.]]); assert_eq!(a3.strides(), &[4, 1]); a.invert_axis(Axis(1)); let mut a4 = a.clone(); a4.push_row(aview1(&[4., 5., 6., 7.])).unwrap(); - assert_eq!(a4, - array![[4., 5., 6., 7.], - [0., 1., 2., 3.], - [4., 5., 6., 7.]]); + assert_eq!(a4, array![[4., 5., 6., 7.], [0., 1., 2., 3.], [4., 5., 6., 7.]]); assert_eq!(a4.strides(), &[4, 1]); } @@ -150,18 +117,10 @@ fn push_row_error() { let mut a = Array::zeros((3, 4)); - assert_eq!(a.push_row(aview1(&[1.])), - Err(ShapeError::from_kind(ErrorKind::IncompatibleShape))); - assert_eq!(a.push_column(aview1(&[1.])), - Err(ShapeError::from_kind(ErrorKind::IncompatibleShape))); - assert_eq!(a.push_column(aview1(&[1., 2., 3.])), - Ok(())); - assert_eq!(a.t(), - array![[0., 0., 0.], - [0., 0., 0.], - [0., 0., 0.], - [0., 0., 0.], - [1., 2., 3.]]); + assert_eq!(a.push_row(aview1(&[1.])), Err(ShapeError::from_kind(ErrorKind::IncompatibleShape))); + assert_eq!(a.push_column(aview1(&[1.])), Err(ShapeError::from_kind(ErrorKind::IncompatibleShape))); + assert_eq!(a.push_column(aview1(&[1., 2., 3.])), Ok(())); + assert_eq!(a.t(), array![[0., 0., 0.], [0., 0., 0.], [0., 0., 0.], [0., 0., 0.], [1., 2., 3.]]); } #[test] @@ -172,21 +131,12 @@ fn push_row_existing() a.push_row(aview1(&[4., 5., 6., 7.])).unwrap(); assert_eq!(a.shape(), &[3, 4]); - assert_eq!(a, - array![[0., 0., 0., 0.], - [0., 1., 2., 3.], - [4., 5., 6., 7.]]); - - assert_eq!(a.push_row(aview1(&[1.])), - Err(ShapeError::from_kind(ErrorKind::IncompatibleShape))); - assert_eq!(a.push_column(aview1(&[1.])), - Err(ShapeError::from_kind(ErrorKind::IncompatibleShape))); - assert_eq!(a.push_column(aview1(&[1., 2., 3.])), - Ok(())); - assert_eq!(a, - array![[0., 0., 0., 0., 1.], - [0., 1., 2., 3., 2.], - [4., 5., 6., 7., 3.]]); + assert_eq!(a, array![[0., 0., 0., 0.], [0., 1., 2., 3.], [4., 5., 6., 7.]]); + + assert_eq!(a.push_row(aview1(&[1.])), Err(ShapeError::from_kind(ErrorKind::IncompatibleShape))); + assert_eq!(a.push_column(aview1(&[1.])), Err(ShapeError::from_kind(ErrorKind::IncompatibleShape))); + assert_eq!(a.push_column(aview1(&[1., 2., 3.])), Ok(())); + assert_eq!(a, array![[0., 0., 0., 0., 1.], [0., 1., 2., 3., 2.], [4., 5., 6., 7., 3.]]); } #[test] @@ -196,15 +146,12 @@ fn push_row_col_len_1() let mut a = Array::zeros((1, 1)); a.push_row(aview1(&[1.])).unwrap(); // shape 2 x 1 a.push_column(aview1(&[2., 3.])).unwrap(); // shape 2 x 2 - assert_eq!(a.push_row(aview1(&[1.])), - Err(ShapeError::from_kind(ErrorKind::IncompatibleShape))); + assert_eq!(a.push_row(aview1(&[1.])), Err(ShapeError::from_kind(ErrorKind::IncompatibleShape))); //assert_eq!(a.push_row(aview1(&[1., 2.])), Err(ShapeError::from_kind(ErrorKind::IncompatibleLayout))); a.push_column(aview1(&[4., 5.])).unwrap(); // shape 2 x 3 assert_eq!(a.shape(), &[2, 3]); - assert_eq!(a, - array![[0., 2., 4.], - [1., 3., 5.]]); + assert_eq!(a, array![[0., 2., 4.], [1., 3., 5.]]); } #[test] @@ -215,9 +162,7 @@ fn push_column() a.push_column(aview1(&[4., 5., 6., 7.])).unwrap(); assert_eq!(a.shape(), &[4, 2]); - assert_eq!(a.t(), - array![[0., 1., 2., 3.], - [4., 5., 6., 7.]]); + assert_eq!(a.t(), array![[0., 1., 2., 3.], [4., 5., 6., 7.]]); } #[test] @@ -231,18 +176,12 @@ fn append_array1() //a.push_column(aview1(&[4., 5., 6., 7.])).unwrap(); //assert_eq!(a.shape(), &[4, 2]); - assert_eq!(a, - array![[0., 1., 2., 3.], - [4., 5., 6., 7.]]); + assert_eq!(a, array![[0., 1., 2., 3.], [4., 5., 6., 7.]]); a.append(Axis(0), aview2(&[[5., 5., 4., 4.], [3., 3., 2., 2.]])) .unwrap(); println!("{:?}", a); - assert_eq!(a, - array![[0., 1., 2., 3.], - [4., 5., 6., 7.], - [5., 5., 4., 4.], - [3., 3., 2., 2.]]); + assert_eq!(a, array![[0., 1., 2., 3.], [4., 5., 6., 7.], [5., 5., 4., 4.], [3., 3., 2., 2.]]); } #[test] @@ -270,21 +209,13 @@ fn append_array_3d() println!("Send {:?} to append", av); a.append(Axis(1), av).unwrap(); println!("{:?}", a); - assert_eq!(a, - array![[[0, 1], - [51, 52], - [55, 56], - [71, 72], - [75, 76], - [81, 82], - [85, 86]], - [[2, 3], - [53, 54], - [57, 58], - [73, 74], - [77, 78], - [83, 84], - [87, 88]]]); + assert_eq!( + a, + array![ + [[0, 1], [51, 52], [55, 56], [71, 72], [75, 76], [81, 82], [85, 86]], + [[2, 3], [53, 54], [57, 58], [73, 74], [77, 78], [83, 84], [87, 88]] + ] + ); } #[test] diff --git a/tests/array-construct.rs b/tests/array-construct.rs index ec8cedf3f..bf78e5cd1 100644 --- a/tests/array-construct.rs +++ b/tests/array-construct.rs @@ -176,8 +176,7 @@ fn test_from_shape_with_neg_stride() let v = s[..12].to_vec(); let v_ptr = v.as_ptr(); let a = Array::from_shape_vec((2, 1, 2).strides((1, -4isize as usize, 2)), v).unwrap(); - assert_eq!(a, arr3(&[[[0, 2]], - [[1, 3]]])); + assert_eq!(a, arr3(&[[[0, 2]], [[1, 3]]])); assert_eq!(a.as_ptr(), v_ptr); } @@ -189,10 +188,7 @@ fn test_from_shape_2_2_2_with_neg_stride() let v = s[..12].to_vec(); let v_ptr = v.as_ptr(); let a = Array::from_shape_vec((2, 2, 2).strides((1, -4isize as usize, 2)), v).unwrap(); - assert_eq!(a, arr3(&[[[4, 6], - [0, 2]], - [[5, 7], - [1, 3]]])); + assert_eq!(a, arr3(&[[[4, 6], [0, 2]], [[5, 7], [1, 3]]])); assert_eq!(a.as_ptr(), v_ptr.wrapping_add(4)); } diff --git a/tests/array.rs b/tests/array.rs index 391f88b95..6f735f9a5 100644 --- a/tests/array.rs +++ b/tests/array.rs @@ -427,10 +427,7 @@ fn test_multislice() .into_shape_with_order((8, 6)) .unwrap(); - assert_eq!( - (arr.clone().view_mut(),), - arr.multi_slice_mut((s![.., ..],)), - ); + assert_eq!((arr.clone().view_mut(),), arr.multi_slice_mut((s![.., ..],)),); assert_eq!(arr.multi_slice_mut(()), ()); do_test!(&mut arr, s![0, ..]); do_test!(&mut arr, s![0, ..], s![1, ..]); @@ -1298,12 +1295,9 @@ macro_rules! assert_matches { ($value:expr, $pat:pat) => { match $value { $pat => {} - ref err => panic!( - "assertion failed: `{}` matches `{}` found: {:?}", - stringify!($value), - stringify!($pat), - err - ), + ref err => { + panic!("assertion failed: `{}` matches `{}` found: {:?}", stringify!($value), stringify!($pat), err) + } } }; } @@ -1322,10 +1316,7 @@ fn from_vec_dim_stride_0d() let one = [1.]; let two = [1., 2.]; // too few elements - assert_matches!( - Array::from_shape_vec(().strides(()), empty.to_vec()), - Err(_) - ); + assert_matches!(Array::from_shape_vec(().strides(()), empty.to_vec()), Err(_)); // exact number of elements assert_matches!(Array::from_shape_vec(().strides(()), one.to_vec()), Ok(_)); // too many are ok @@ -1356,10 +1347,7 @@ fn from_vec_dim_stride_2d_3() let a = arr3(&[[[1]], [[2]], [[3]]]); let d = a.raw_dim(); let s = d.default_strides(); - assert_matches!( - Array::from_shape_vec(d.strides(s), a.as_slice().unwrap().to_vec()), - Ok(_) - ); + assert_matches!(Array::from_shape_vec(d.strides(s), a.as_slice().unwrap().to_vec()), Ok(_)); } #[test] @@ -1368,10 +1356,7 @@ fn from_vec_dim_stride_2d_4() let a = arr3(&[[[1]], [[2]], [[3]]]); let d = a.raw_dim(); let s = d.fortran_strides(); - assert_matches!( - Array::from_shape_vec(d.strides(s), a.as_slice().unwrap().to_vec()), - Ok(_) - ); + assert_matches!(Array::from_shape_vec(d.strides(s), a.as_slice().unwrap().to_vec()), Ok(_)); } #[test] @@ -1380,10 +1365,7 @@ fn from_vec_dim_stride_2d_5() let a = arr3(&[[[1, 2, 3]]]); let d = a.raw_dim(); let s = d.fortran_strides(); - assert_matches!( - Array::from_shape_vec(d.strides(s), a.as_slice().unwrap().to_vec()), - Ok(_) - ); + assert_matches!(Array::from_shape_vec(d.strides(s), a.as_slice().unwrap().to_vec()), Ok(_)); } #[test] @@ -1447,10 +1429,7 @@ fn views() a.clone()[(0, 0)] = 99; assert_eq!(b[(0, 0)], 1); - assert_eq!( - a.view().into_iter().cloned().collect::>(), - vec![1, 2, 3, 4] - ); + assert_eq!(a.view().into_iter().cloned().collect::>(), vec![1, 2, 3, 4]); } #[test] @@ -1587,83 +1566,25 @@ fn insert_axis() test_insert!(arr1(&[1, 2, 3]), 1, arr2(&[[1], [2], [3]])); assert!(::std::panic::catch_unwind(|| arr1(&[1, 2, 3]).insert_axis(Axis(2))).is_err()); - test_insert!( - arr2(&[[1, 2, 3], [4, 5, 6]]), - 0, - arr3(&[[[1, 2, 3], [4, 5, 6]]]) - ); - test_insert!( - arr2(&[[1, 2, 3], [4, 5, 6]]), - 1, - arr3(&[[[1, 2, 3]], [[4, 5, 6]]]) - ); - test_insert!( - arr2(&[[1, 2, 3], [4, 5, 6]]), - 2, - arr3(&[[[1], [2], [3]], [[4], [5], [6]]]) - ); - assert!( - ::std::panic::catch_unwind(|| arr2(&[[1, 2, 3], [4, 5, 6]]).insert_axis(Axis(3))).is_err() - ); + test_insert!(arr2(&[[1, 2, 3], [4, 5, 6]]), 0, arr3(&[[[1, 2, 3], [4, 5, 6]]])); + test_insert!(arr2(&[[1, 2, 3], [4, 5, 6]]), 1, arr3(&[[[1, 2, 3]], [[4, 5, 6]]])); + test_insert!(arr2(&[[1, 2, 3], [4, 5, 6]]), 2, arr3(&[[[1], [2], [3]], [[4], [5], [6]]])); + assert!(::std::panic::catch_unwind(|| arr2(&[[1, 2, 3], [4, 5, 6]]).insert_axis(Axis(3))).is_err()); - test_insert!( - Array3::::zeros((3, 4, 5)), - 0, - Array4::::zeros((1, 3, 4, 5)) - ); - test_insert!( - Array3::::zeros((3, 4, 5)), - 1, - Array4::::zeros((3, 1, 4, 5)) - ); - test_insert!( - Array3::::zeros((3, 4, 5)), - 3, - Array4::::zeros((3, 4, 5, 1)) - ); - assert!( - ::std::panic::catch_unwind(|| Array3::::zeros((3, 4, 5)).insert_axis(Axis(4))).is_err() - ); + test_insert!(Array3::::zeros((3, 4, 5)), 0, Array4::::zeros((1, 3, 4, 5))); + test_insert!(Array3::::zeros((3, 4, 5)), 1, Array4::::zeros((3, 1, 4, 5))); + test_insert!(Array3::::zeros((3, 4, 5)), 3, Array4::::zeros((3, 4, 5, 1))); + assert!(::std::panic::catch_unwind(|| Array3::::zeros((3, 4, 5)).insert_axis(Axis(4))).is_err()); - test_insert!( - Array6::::zeros((2, 3, 4, 3, 2, 3)), - 0, - ArrayD::::zeros(vec![1, 2, 3, 4, 3, 2, 3]) - ); - test_insert!( - Array6::::zeros((2, 3, 4, 3, 2, 3)), - 3, - ArrayD::::zeros(vec![2, 3, 4, 1, 3, 2, 3]) - ); - test_insert!( - Array6::::zeros((2, 3, 4, 3, 2, 3)), - 6, - ArrayD::::zeros(vec![2, 3, 4, 3, 2, 3, 1]) - ); - assert!(::std::panic::catch_unwind( - || Array6::::zeros((2, 3, 4, 3, 2, 3)).insert_axis(Axis(7)) - ) - .is_err()); + test_insert!(Array6::::zeros((2, 3, 4, 3, 2, 3)), 0, ArrayD::::zeros(vec![1, 2, 3, 4, 3, 2, 3])); + test_insert!(Array6::::zeros((2, 3, 4, 3, 2, 3)), 3, ArrayD::::zeros(vec![2, 3, 4, 1, 3, 2, 3])); + test_insert!(Array6::::zeros((2, 3, 4, 3, 2, 3)), 6, ArrayD::::zeros(vec![2, 3, 4, 3, 2, 3, 1])); + assert!(::std::panic::catch_unwind(|| Array6::::zeros((2, 3, 4, 3, 2, 3)).insert_axis(Axis(7))).is_err()); - test_insert!( - ArrayD::::zeros(vec![3, 4, 5]), - 0, - ArrayD::::zeros(vec![1, 3, 4, 5]) - ); - test_insert!( - ArrayD::::zeros(vec![3, 4, 5]), - 1, - ArrayD::::zeros(vec![3, 1, 4, 5]) - ); - test_insert!( - ArrayD::::zeros(vec![3, 4, 5]), - 3, - ArrayD::::zeros(vec![3, 4, 5, 1]) - ); - assert!( - ::std::panic::catch_unwind(|| ArrayD::::zeros(vec![3, 4, 5]).insert_axis(Axis(4))) - .is_err() - ); + test_insert!(ArrayD::::zeros(vec![3, 4, 5]), 0, ArrayD::::zeros(vec![1, 3, 4, 5])); + test_insert!(ArrayD::::zeros(vec![3, 4, 5]), 1, ArrayD::::zeros(vec![3, 1, 4, 5])); + test_insert!(ArrayD::::zeros(vec![3, 4, 5]), 3, ArrayD::::zeros(vec![3, 4, 5, 1])); + assert!(::std::panic::catch_unwind(|| ArrayD::::zeros(vec![3, 4, 5]).insert_axis(Axis(4))).is_err()); } #[test] @@ -1675,43 +1596,21 @@ fn insert_axis_f() assert!(res.t().is_standard_layout()); }); - test_insert_f!( - Array0::from_shape_vec(().f(), vec![1]).unwrap(), - 0, - arr1(&[1]) - ); - assert!( - ::std::panic::catch_unwind(|| Array0::from_shape_vec(().f(), vec![1]) - .unwrap() - .insert_axis(Axis(1))) - .is_err() - ); + test_insert_f!(Array0::from_shape_vec(().f(), vec![1]).unwrap(), 0, arr1(&[1])); + assert!(::std::panic::catch_unwind(|| Array0::from_shape_vec(().f(), vec![1]) + .unwrap() + .insert_axis(Axis(1))) + .is_err()); test_insert_f!(Array1::::zeros((3).f()), 0, Array2::::zeros((1, 3))); test_insert_f!(Array1::::zeros((3).f()), 1, Array2::::zeros((3, 1))); - assert!( - ::std::panic::catch_unwind(|| Array1::::zeros((3).f()).insert_axis(Axis(2))).is_err() - ); + assert!(::std::panic::catch_unwind(|| Array1::::zeros((3).f()).insert_axis(Axis(2))).is_err()); - test_insert_f!( - Array3::::zeros((3, 4, 5).f()), - 1, - Array4::::zeros((3, 1, 4, 5)) - ); - assert!( - ::std::panic::catch_unwind(|| Array3::::zeros((3, 4, 5).f()).insert_axis(Axis(4))) - .is_err() - ); + test_insert_f!(Array3::::zeros((3, 4, 5).f()), 1, Array4::::zeros((3, 1, 4, 5))); + assert!(::std::panic::catch_unwind(|| Array3::::zeros((3, 4, 5).f()).insert_axis(Axis(4))).is_err()); - test_insert_f!( - ArrayD::::zeros(vec![3, 4, 5].f()), - 1, - ArrayD::::zeros(vec![3, 1, 4, 5]) - ); - assert!(::std::panic::catch_unwind( - || ArrayD::::zeros(vec![3, 4, 5].f()).insert_axis(Axis(4)) - ) - .is_err()); + test_insert_f!(ArrayD::::zeros(vec![3, 4, 5].f()), 1, ArrayD::::zeros(vec![3, 1, 4, 5])); + assert!(::std::panic::catch_unwind(|| ArrayD::::zeros(vec![3, 4, 5].f()).insert_axis(Axis(4))).is_err()); } #[test] @@ -1719,18 +1618,9 @@ fn insert_axis_view() { let a = array![[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]]; - assert_eq!( - a.index_axis(Axis(1), 0).insert_axis(Axis(0)), - array![[[1, 2], [5, 6], [9, 10]]] - ); - assert_eq!( - a.index_axis(Axis(1), 0).insert_axis(Axis(1)), - array![[[1, 2]], [[5, 6]], [[9, 10]]] - ); - assert_eq!( - a.index_axis(Axis(1), 0).insert_axis(Axis(2)), - array![[[1], [2]], [[5], [6]], [[9], [10]]] - ); + assert_eq!(a.index_axis(Axis(1), 0).insert_axis(Axis(0)), array![[[1, 2], [5, 6], [9, 10]]]); + assert_eq!(a.index_axis(Axis(1), 0).insert_axis(Axis(1)), array![[[1, 2]], [[5, 6]], [[9, 10]]]); + assert_eq!(a.index_axis(Axis(1), 0).insert_axis(Axis(2)), array![[[1], [2]], [[5], [6]], [[9], [10]]]); } #[test] @@ -1747,14 +1637,8 @@ fn arithmetic_broadcast() let a = arr2(&[[2], [3], [4]]); let b = arr1(&[5, 6, 7]); assert_eq!(&a + &b, arr2(&[[7, 8, 9], [8, 9, 10], [9, 10, 11]])); - assert_eq!( - a.clone() - &b, - arr2(&[[-3, -4, -5], [-2, -3, -4], [-1, -2, -3]]) - ); - assert_eq!( - a.clone() * b.clone(), - arr2(&[[10, 12, 14], [15, 18, 21], [20, 24, 28]]) - ); + assert_eq!(a.clone() - &b, arr2(&[[-3, -4, -5], [-2, -3, -4], [-1, -2, -3]])); + assert_eq!(a.clone() * b.clone(), arr2(&[[10, 12, 14], [15, 18, 21], [20, 24, 28]])); assert_eq!(&b / a, arr2(&[[2, 3, 3], [1, 2, 2], [1, 1, 1]])); // Negative strides and non-contiguous memory @@ -1765,14 +1649,8 @@ fn arithmetic_broadcast() let mut c = s.clone(); c.collapse_axis(Axis(2), 1); let c = c.slice(s![1,..;2,..]); - assert_eq!( - &a.to_owned() + &b, - arr3(&[[[11, 15], [20, 24]], [[10, 14], [19, 23]]]) - ); - assert_eq!( - &a + b.into_owned() + c, - arr3(&[[[15, 19], [32, 36]], [[14, 18], [31, 35]]]) - ); + assert_eq!(&a.to_owned() + &b, arr3(&[[[11, 15], [20, 24]], [[10, 14], [19, 23]]])); + assert_eq!(&a + b.into_owned() + c, arr3(&[[[15, 19], [32, 36]], [[14, 18], [31, 35]]])); // shared array let sa = a.to_shared(); @@ -1781,10 +1659,7 @@ fn arithmetic_broadcast() let sb2 = sb.to_shared(); let sc = c.to_shared(); let sc2 = sc.into_shared(); - assert_eq!( - sa2 + &sb2 + sc2.into_owned(), - arr3(&[[[15, 19], [32, 36]], [[14, 18], [31, 35]]]) - ); + assert_eq!(sa2 + &sb2 + sc2.into_owned(), arr3(&[[[15, 19], [32, 36]], [[14, 18], [31, 35]]])); // Same shape let a = s.slice(s![..;-1, ..;2, ..]); @@ -2012,10 +1887,7 @@ fn map_mut_with_unsharing() let a = rcarr2(&[[0, 5], [1, 6], [2, 7], [3, 8], [4, 9]]).reversed_axes(); assert_eq!(a.shape(), &[2, 5]); assert_eq!(a.strides(), &[1, 2]); - assert_eq!( - a.as_slice_memory_order(), - Some(&[0, 5, 1, 6, 2, 7, 3, 8, 4, 9][..]) - ); + assert_eq!(a.as_slice_memory_order(), Some(&[0, 5, 1, 6, 2, 7, 3, 8, 4, 9][..])); // Shared reference of a portion of `a`. let mut b = a.clone().slice_move(s![.., ..2]); @@ -2129,17 +2001,11 @@ fn test_contiguous_neg_strides() { let s = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]; let a = ArrayView::from_shape((2, 3, 2).strides((1, 4, 2)), &s).unwrap(); - assert_eq!( - a, - arr3(&[[[0, 2], [4, 6], [8, 10]], [[1, 3], [5, 7], [9, 11]]]) - ); + assert_eq!(a, arr3(&[[[0, 2], [4, 6], [8, 10]], [[1, 3], [5, 7], [9, 11]]])); assert!(a.as_slice_memory_order().is_some()); let mut b = a.slice(s![..;1, ..;-1, ..;-1]); - assert_eq!( - b, - arr3(&[[[10, 8], [6, 4], [2, 0]], [[11, 9], [7, 5], [3, 1]]]) - ); + assert_eq!(b, arr3(&[[[10, 8], [6, 4], [2, 0]], [[11, 9], [7, 5], [3, 1]]])); assert!(b.as_slice_memory_order().is_some()); b.swap_axes(1, 2); @@ -2151,10 +2017,7 @@ fn test_contiguous_neg_strides() assert!(b.as_slice_memory_order().is_some()); let mut c = b.reversed_axes(); - assert_eq!( - c, - arr3(&[[[11, 10], [9, 8]], [[7, 6], [5, 4]], [[3, 2], [1, 0]]]) - ); + assert_eq!(c, arr3(&[[[11, 10], [9, 8]], [[7, 6], [5, 4]], [[3, 2], [1, 0]]])); assert!(c.as_slice_memory_order().is_some()); c.merge_axes(Axis(1), Axis(2)); @@ -2459,10 +2322,7 @@ fn array_macros() assert_eq!(a4, arr3(&[[[1, 2], [3, 4]], [[5, 6], [7, 8]]])); let s = String::from("abc"); - let a2s = array![ - [String::from("w"), s], - [String::from("x"), String::from("y")] - ]; + let a2s = array![[String::from("w"), s], [String::from("x"), String::from("y")]]; assert_eq!(a2s[[0, 0]], "w"); assert_eq!(a2s[[0, 1]], "abc"); assert_eq!(a2s[[1, 0]], "x"); @@ -2689,34 +2549,22 @@ fn test_remove_index() a.remove_index(Axis(0), 1); a.remove_index(Axis(1), 2); assert_eq!(a.shape(), &[3, 2]); - assert_eq!(a, - array![[1, 2], - [7, 8], - [10,11]]); + assert_eq!(a, array![[1, 2], [7, 8], [10, 11]]); let mut a = arr2(&[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]); a.invert_axis(Axis(0)); a.remove_index(Axis(0), 1); a.remove_index(Axis(1), 2); assert_eq!(a.shape(), &[3, 2]); - assert_eq!(a, - array![[10,11], - [4, 5], - [1, 2]]); + assert_eq!(a, array![[10, 11], [4, 5], [1, 2]]); a.remove_index(Axis(1), 1); assert_eq!(a.shape(), &[3, 1]); - assert_eq!(a, - array![[10], - [4], - [1]]); + assert_eq!(a, array![[10], [4], [1]]); a.remove_index(Axis(1), 0); assert_eq!(a.shape(), &[3, 0]); - assert_eq!(a, - array![[], - [], - []]); + assert_eq!(a, array![[], [], []]); } #[should_panic(expected = "must be less")] @@ -2734,14 +2582,9 @@ fn test_remove_index_oob2() let mut a = array![[10], [4], [1]]; a.remove_index(Axis(1), 0); assert_eq!(a.shape(), &[3, 0]); - assert_eq!(a, - array![[], - [], - []]); + assert_eq!(a, array![[], [], []]); a.remove_index(Axis(0), 1); // ok - assert_eq!(a, - array![[], - []]); + assert_eq!(a, array![[], []]); a.remove_index(Axis(1), 0); // oob } @@ -2803,8 +2646,8 @@ fn test_split_complex_permuted() let a = Array3::from_shape_fn((3, 4, 5), |(i, j, k)| Complex::new(i * k + j, k)); let permuted = a.view().permuted_axes([1, 0, 2]); let Complex { re, im } = permuted.split_complex(); - assert_eq!(re.get((3,2,4)).unwrap(), &11); - assert_eq!(im.get((3,2,4)).unwrap(), &4); + assert_eq!(re.get((3, 2, 4)).unwrap(), &11); + assert_eq!(im.get((3, 2, 4)).unwrap(), &4); } #[test] diff --git a/tests/complex.rs b/tests/complex.rs index 824e296a4..8cd6c8849 100644 --- a/tests/complex.rs +++ b/tests/complex.rs @@ -18,8 +18,5 @@ fn complex_mat_mul() let r = a.dot(&e); println!("{}", a); assert_eq!(r, a); - assert_eq!( - a.mean_axis(Axis(0)).unwrap(), - arr1(&[c(1.5, 1.), c(2.5, 0.)]) - ); + assert_eq!(a.mean_axis(Axis(0)).unwrap(), arr1(&[c(1.5, 1.), c(2.5, 0.)])); } diff --git a/tests/dimension.rs b/tests/dimension.rs index 53f204c6b..0b0608ba8 100644 --- a/tests/dimension.rs +++ b/tests/dimension.rs @@ -20,10 +20,7 @@ fn insert_axis() assert_eq!(Dim([2, 3, 4]).insert_axis(Axis(2)), Dim([2, 3, 1, 4])); - assert_eq!( - Dim([2, 3, 4, 5, 6, 7]).insert_axis(Axis(2)), - Dim(vec![2, 3, 1, 4, 5, 6, 7]) - ); + assert_eq!(Dim([2, 3, 4, 5, 6, 7]).insert_axis(Axis(2)), Dim(vec![2, 3, 1, 4, 5, 6, 7])); assert_eq!(Dim(vec![]).insert_axis(Axis(0)), Dim(vec![1])); @@ -31,14 +28,8 @@ fn insert_axis() assert_eq!(Dim(vec![2, 3]).insert_axis(Axis(1)), Dim(vec![2, 1, 3])); assert_eq!(Dim(vec![2, 3]).insert_axis(Axis(2)), Dim(vec![2, 3, 1])); - assert_eq!( - Dim(vec![2, 3, 4, 5, 6]).insert_axis(Axis(2)), - Dim(vec![2, 3, 1, 4, 5, 6]) - ); - assert_eq!( - Dim(vec![2, 3, 4, 5, 6, 7]).insert_axis(Axis(2)), - Dim(vec![2, 3, 1, 4, 5, 6, 7]) - ); + assert_eq!(Dim(vec![2, 3, 4, 5, 6]).insert_axis(Axis(2)), Dim(vec![2, 3, 1, 4, 5, 6])); + assert_eq!(Dim(vec![2, 3, 4, 5, 6, 7]).insert_axis(Axis(2)), Dim(vec![2, 3, 1, 4, 5, 6, 7])); } #[test] @@ -133,31 +124,18 @@ fn fastest_varying_order() assert_eq!(order.slice(), &[3, 0, 2, 1]); assert_eq!(Dim([1, 3])._fastest_varying_stride_order(), Dim([0, 1])); - assert_eq!( - Dim([1, -3isize as usize])._fastest_varying_stride_order(), - Dim([0, 1]) - ); + assert_eq!(Dim([1, -3isize as usize])._fastest_varying_stride_order(), Dim([0, 1])); assert_eq!(Dim([7, 2])._fastest_varying_stride_order(), Dim([1, 0])); - assert_eq!( - Dim([-7isize as usize, 2])._fastest_varying_stride_order(), - Dim([1, 0]) - ); - assert_eq!( - Dim([6, 1, 3])._fastest_varying_stride_order(), - Dim([1, 2, 0]) - ); - assert_eq!( - Dim([-6isize as usize, 1, -3isize as usize])._fastest_varying_stride_order(), - Dim([1, 2, 0]) - ); + assert_eq!(Dim([-7isize as usize, 2])._fastest_varying_stride_order(), Dim([1, 0])); + assert_eq!(Dim([6, 1, 3])._fastest_varying_stride_order(), Dim([1, 2, 0])); + assert_eq!(Dim([-6isize as usize, 1, -3isize as usize])._fastest_varying_stride_order(), Dim([1, 2, 0])); // it's important that it produces distinct indices. Prefer the stable order // where 0 is before 1 when they are equal. assert_eq!(Dim([2, 2])._fastest_varying_stride_order(), [0, 1]); assert_eq!(Dim([2, 2, 1])._fastest_varying_stride_order(), [2, 0, 1]); assert_eq!( - Dim([-2isize as usize, -2isize as usize, 3, 1, -2isize as usize]) - ._fastest_varying_stride_order(), + Dim([-2isize as usize, -2isize as usize, 3, 1, -2isize as usize])._fastest_varying_stride_order(), [3, 0, 1, 4, 2] ); } diff --git a/tests/iterators.rs b/tests/iterators.rs index 96b0673aa..1d5976c1e 100644 --- a/tests/iterators.rs +++ b/tests/iterators.rs @@ -147,12 +147,7 @@ fn as_slice() let a = a.into_shape_with_order((8, 1)).unwrap(); assert_slice_correct(&a); let u = a.slice(s![..;2, ..]); - println!( - "u={:?}, shape={:?}, strides={:?}", - u, - u.shape(), - u.strides() - ); + println!("u={:?}, shape={:?}, strides={:?}", u, u.shape(), u.strides()); assert!(u.as_slice().is_none()); } @@ -281,11 +276,10 @@ fn axis_iter() // [[6, 7], // [8, 9], // ... - assert_equal(a.axis_iter(Axis(1)), vec![ - a.index_axis(Axis(1), 0), - a.index_axis(Axis(1), 1), - a.index_axis(Axis(1), 2), - ]); + assert_equal( + a.axis_iter(Axis(1)), + vec![a.index_axis(Axis(1), 0), a.index_axis(Axis(1), 1), a.index_axis(Axis(1), 2)], + ); } #[test] @@ -562,11 +556,7 @@ fn axis_chunks_iter_corner_cases() let it = a.axis_chunks_iter(Axis(0), 8); assert_equal(it, vec![a.view()]); let it = a.axis_chunks_iter(Axis(0), 3); - assert_equal(it, vec![ - array![[7], [6], [5]], - array![[4], [3], [2]], - array![[1], [0]], - ]); + assert_equal(it, vec![array![[7], [6], [5]], array![[4], [3], [2]], array![[1], [0]]]); let b = ArcArray::::zeros((8, 2)); let a = b.slice(s![1..;2,..]); @@ -938,10 +928,7 @@ fn test_rfold() acc.push(*elt); acc }); - assert_eq!( - Array1::from(output), - Array::from_iter((1..10).rev().map(|i| i * 2)) - ); + assert_eq!(Array1::from(output), Array::from_iter((1..10).rev().map(|i| i * 2))); } } diff --git a/tests/numeric.rs b/tests/numeric.rs index b82a3561f..2bb7f5d15 100644 --- a/tests/numeric.rs +++ b/tests/numeric.rs @@ -24,14 +24,12 @@ fn test_mean_with_empty_array_of_floats() fn test_mean_with_array_of_floats() { let a: Array1 = array![ - 0.99889651, 0.0150731, 0.28492482, 0.83819218, 0.48413156, 0.80710412, 0.41762936, - 0.22879429, 0.43997224, 0.23831807, 0.02416466, 0.6269962, 0.47420614, 0.56275487, - 0.78995021, 0.16060581, 0.64635041, 0.34876609, 0.78543249, 0.19938356, 0.34429457, - 0.88072369, 0.17638164, 0.60819363, 0.250392, 0.69912532, 0.78855523, 0.79140914, - 0.85084218, 0.31839879, 0.63381769, 0.22421048, 0.70760302, 0.99216018, 0.80199153, - 0.19239188, 0.61356023, 0.31505352, 0.06120481, 0.66417377, 0.63608897, 0.84959691, - 0.43599069, 0.77867775, 0.88267754, 0.83003623, 0.67016118, 0.67547638, 0.65220036, - 0.68043427 + 0.99889651, 0.0150731, 0.28492482, 0.83819218, 0.48413156, 0.80710412, 0.41762936, 0.22879429, 0.43997224, + 0.23831807, 0.02416466, 0.6269962, 0.47420614, 0.56275487, 0.78995021, 0.16060581, 0.64635041, 0.34876609, + 0.78543249, 0.19938356, 0.34429457, 0.88072369, 0.17638164, 0.60819363, 0.250392, 0.69912532, 0.78855523, + 0.79140914, 0.85084218, 0.31839879, 0.63381769, 0.22421048, 0.70760302, 0.99216018, 0.80199153, 0.19239188, + 0.61356023, 0.31505352, 0.06120481, 0.66417377, 0.63608897, 0.84959691, 0.43599069, 0.77867775, 0.88267754, + 0.83003623, 0.67016118, 0.67547638, 0.65220036, 0.68043427 ]; let exact_mean = 0.5475494054; assert_abs_diff_eq!(a.mean().unwrap(), exact_mean); @@ -60,14 +58,8 @@ fn sum_mean_prod_empty() assert_eq!(Array3::::ones((2, 0, 3)).product(), 1.); assert_eq!(Array1::::ones(0).sum_axis(Axis(0)), arr0(0.)); assert_eq!(Array1::::ones(0).product_axis(Axis(0)), arr0(1.)); - assert_eq!( - Array3::::ones((2, 0, 3)).sum_axis(Axis(1)), - Array::zeros((2, 3)), - ); - assert_eq!( - Array3::::ones((2, 0, 3)).product_axis(Axis(1)), - Array::ones((2, 3)), - ); + assert_eq!(Array3::::ones((2, 0, 3)).sum_axis(Axis(1)), Array::zeros((2, 3)),); + assert_eq!(Array3::::ones((2, 0, 3)).product_axis(Axis(1)), Array::ones((2, 3)),); let a = Array1::::ones(0).mean_axis(Axis(0)); assert_eq!(a, None); let a = Array3::::ones((2, 0, 3)).mean_axis(Axis(1)); @@ -238,16 +230,8 @@ fn var_axis() use ndarray::{aview0, aview2}; let a = array![ - [ - [-9.76, -0.38, 1.59, 6.23], - [-8.57, -9.27, 5.76, 6.01], - [-9.54, 5.09, 3.21, 6.56], - ], - [ - [8.23, -9.63, 3.76, -3.48], - [-5.46, 5.86, -2.81, 1.35], - [-1.08, 4.66, 8.34, -0.73], - ], + [[-9.76, -0.38, 1.59, 6.23], [-8.57, -9.27, 5.76, 6.01], [-9.54, 5.09, 3.21, 6.56],], + [[8.23, -9.63, 3.76, -3.48], [-5.46, 5.86, -2.81, 1.35], [-1.08, 4.66, 8.34, -0.73],], ]; assert_abs_diff_eq!( a.var_axis(Axis(0), 1.5), @@ -268,19 +252,12 @@ fn var_axis() ); assert_abs_diff_eq!( a.var_axis(Axis(2), 2.3), - aview2(&[ - [79.64552941, 129.09663235, 95.98929412], - [109.64952941, 43.28758824, 36.27439706], - ]), + aview2(&[[79.64552941, 129.09663235, 95.98929412], [109.64952941, 43.28758824, 36.27439706],]), epsilon = 1e-8, ); let b = array![[1.1, 2.3, 4.7]]; - assert_abs_diff_eq!( - b.var_axis(Axis(0), 0.), - aview1(&[0., 0., 0.]), - epsilon = 1e-12 - ); + assert_abs_diff_eq!(b.var_axis(Axis(0), 0.), aview1(&[0., 0., 0.]), epsilon = 1e-12); assert_abs_diff_eq!(b.var_axis(Axis(1), 0.), aview1(&[2.24]), epsilon = 1e-12); let c = array![[], []]; @@ -320,32 +297,18 @@ fn std_axis() ); assert_abs_diff_eq!( a.std_axis(Axis(1), 1.7), - aview2(&[ - [0.42698655, 0.48139215, 0.36874991, 0.41458724], - [0.26769097, 0.18941435, 0.30555015, 0.35118674], - ]), + aview2(&[[0.42698655, 0.48139215, 0.36874991, 0.41458724], [0.26769097, 0.18941435, 0.30555015, 0.35118674],]), epsilon = 1e-8, ); assert_abs_diff_eq!( a.std_axis(Axis(2), 2.3), - aview2(&[ - [0.41117907, 0.37130425, 0.35332388], - [0.16905862, 0.25304841, 0.39978276], - ]), + aview2(&[[0.41117907, 0.37130425, 0.35332388], [0.16905862, 0.25304841, 0.39978276],]), epsilon = 1e-8, ); let b = array![[100000., 1., 0.01]]; - assert_abs_diff_eq!( - b.std_axis(Axis(0), 0.), - aview1(&[0., 0., 0.]), - epsilon = 1e-12, - ); - assert_abs_diff_eq!( - b.std_axis(Axis(1), 0.), - aview1(&[47_140.214_021_552_77]), - epsilon = 1e-6, - ); + assert_abs_diff_eq!(b.std_axis(Axis(0), 0.), aview1(&[0., 0., 0.]), epsilon = 1e-12,); + assert_abs_diff_eq!(b.std_axis(Axis(1), 0.), aview1(&[47_140.214_021_552_77]), epsilon = 1e-6,); let c = array![[], []]; assert_eq!(c.std_axis(Axis(0), 0.), aview1(&[])); @@ -420,30 +383,20 @@ fn diff_1d_order1() fn diff_1d_order2() { let data = array![1.0, 2.0, 4.0, 7.0]; - assert_eq!( - data.diff(2, Axis(0)), - data.diff(1, Axis(0)).diff(1, Axis(0)) - ); + assert_eq!(data.diff(2, Axis(0)), data.diff(1, Axis(0)).diff(1, Axis(0))); } #[test] fn diff_1d_order3() { let data = array![1.0, 2.0, 4.0, 7.0]; - assert_eq!( - data.diff(3, Axis(0)), - data.diff(1, Axis(0)).diff(1, Axis(0)).diff(1, Axis(0)) - ); + assert_eq!(data.diff(3, Axis(0)), data.diff(1, Axis(0)).diff(1, Axis(0)).diff(1, Axis(0))); } #[test] fn diff_2d_order1_ax0() { - let data = array![ - [1.0, 2.0, 4.0, 7.0], - [1.0, 3.0, 6.0, 6.0], - [1.5, 3.5, 5.5, 5.5] - ]; + let data = array![[1.0, 2.0, 4.0, 7.0], [1.0, 3.0, 6.0, 6.0], [1.5, 3.5, 5.5, 5.5]]; let expected = array![[0.0, 1.0, 2.0, -1.0], [0.5, 0.5, -0.5, -0.5]]; assert_eq!(data.diff(1, Axis(0)), expected); } @@ -451,11 +404,7 @@ fn diff_2d_order1_ax0() #[test] fn diff_2d_order1_ax1() { - let data = array![ - [1.0, 2.0, 4.0, 7.0], - [1.0, 3.0, 6.0, 6.0], - [1.5, 3.5, 5.5, 5.5] - ]; + let data = array![[1.0, 2.0, 4.0, 7.0], [1.0, 3.0, 6.0, 6.0], [1.5, 3.5, 5.5, 5.5]]; let expected = array![[1.0, 2.0, 3.0], [2.0, 3.0, 0.0], [2.0, 2.0, 0.0]]; assert_eq!(data.diff(1, Axis(1)), expected); } diff --git a/tests/oper.rs b/tests/oper.rs index a6d7054ba..7b08960b3 100644 --- a/tests/oper.rs +++ b/tests/oper.rs @@ -567,10 +567,7 @@ fn scaled_add_3() let cslice: Vec = if n == 1 { vec![Slice::from(..).step_by(s2).into()] } else { - vec![ - Slice::from(..).step_by(s1).into(), - Slice::from(..).step_by(s2).into(), - ] + vec![Slice::from(..).step_by(s1).into(), Slice::from(..).step_by(s2).into()] }; let c = range_mat::(n, q).into_shape_with_order(cdim).unwrap(); @@ -710,17 +707,7 @@ fn gen_mat_vec_mul() let alpha = -2.3; let beta = f64::consts::PI; - let sizes = vec![ - (4, 4), - (8, 8), - (17, 15), - (4, 17), - (17, 3), - (19, 18), - (16, 17), - (15, 16), - (67, 63), - ]; + let sizes = vec![(4, 4), (8, 8), (17, 15), (4, 17), (17, 3), (19, 18), (16, 17), (15, 16), (67, 63)]; // test different strides for &s1 in &[1, 2, -1, -2] { for &s2 in &[1, 2, -1, -2] { @@ -774,17 +761,7 @@ fn vec_mat_mul() .unwrap() } - let sizes = vec![ - (4, 4), - (8, 8), - (17, 15), - (4, 17), - (17, 3), - (19, 18), - (16, 17), - (15, 16), - (67, 63), - ]; + let sizes = vec![(4, 4), (8, 8), (17, 15), (4, 17), (17, 3), (19, 18), (16, 17), (15, 16), (67, 63)]; // test different strides for &s1 in &[1, 2, -1, -2] { for &s2 in &[1, 2, -1, -2] { @@ -820,22 +797,12 @@ fn kron_square_f64() assert_eq!( kron(&a, &b), - arr2(&[ - [0.0, 1.0, 0.0, 0.0], - [1.0, 0.0, 0.0, 0.0], - [0.0, 0.0, 0.0, 1.0], - [0.0, 0.0, 1.0, 0.0] - ]), + arr2(&[[0.0, 1.0, 0.0, 0.0], [1.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 1.0], [0.0, 0.0, 1.0, 0.0]]), ); assert_eq!( kron(&b, &a), - arr2(&[ - [0.0, 0.0, 1.0, 0.0], - [0.0, 0.0, 0.0, 1.0], - [1.0, 0.0, 0.0, 0.0], - [0.0, 1.0, 0.0, 0.0] - ]), + arr2(&[[0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0], [1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0]]), ) } @@ -845,15 +812,9 @@ fn kron_square_i64() let a = arr2(&[[1, 0], [0, 1]]); let b = arr2(&[[0, 1], [1, 0]]); - assert_eq!( - kron(&a, &b), - arr2(&[[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]), - ); + assert_eq!(kron(&a, &b), arr2(&[[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]),); - assert_eq!( - kron(&b, &a), - arr2(&[[0, 0, 1, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 1, 0, 0]]), - ) + assert_eq!(kron(&b, &a), arr2(&[[0, 0, 1, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 1, 0, 0]]),) } #[test] diff --git a/tests/par_zip.rs b/tests/par_zip.rs index 9f10d9fd5..ddcf634ba 100644 --- a/tests/par_zip.rs +++ b/tests/par_zip.rs @@ -125,8 +125,7 @@ fn test_zip_small_collect() assert_abs_diff_eq!(a, &b + &c, epsilon = 1e-6); if m > 1 && n > 1 { - assert_eq!(a.strides(), b.strides(), - "Failure for {}x{} c/f: {:?}", m, n, is_f); + assert_eq!(a.strides(), b.strides(), "Failure for {}x{} c/f: {:?}", m, n, is_f); } } } diff --git a/tests/reshape.rs b/tests/reshape.rs index a13a5c05f..71abe798c 100644 --- a/tests/reshape.rs +++ b/tests/reshape.rs @@ -139,7 +139,10 @@ fn to_shape_add_axis() let u = v.to_shape(((4, 2), Order::RowMajor)).unwrap(); assert!(u.to_shape(((1, 4, 2), Order::RowMajor)).unwrap().is_view()); - assert!(u.to_shape(((1, 4, 2), Order::ColumnMajor)).unwrap().is_view()); + assert!(u + .to_shape(((1, 4, 2), Order::ColumnMajor)) + .unwrap() + .is_view()); } #[test] @@ -202,11 +205,21 @@ fn to_shape_discontig() let v1 = a1.to_shape(((4, 2, 4), order)).unwrap(); assert!(v1.is_view()); let v1 = a1.to_shape(((8, 4), order)).unwrap(); - assert_eq!(v1.is_view(), order == create_order && create_order == Order::C, - "failed for {:?}, {:?}", create_order, order); + assert_eq!( + v1.is_view(), + order == create_order && create_order == Order::C, + "failed for {:?}, {:?}", + create_order, + order + ); let v1 = a1.to_shape(((4, 8), order)).unwrap(); - assert_eq!(v1.is_view(), order == create_order && create_order == Order::F, - "failed for {:?}, {:?}", create_order, order); + assert_eq!( + v1.is_view(), + order == create_order && create_order == Order::F, + "failed for {:?}, {:?}", + create_order, + order + ); let v1 = a1.to_shape((32, order)).unwrap(); assert!(!v1.is_view()); } @@ -225,13 +238,24 @@ fn to_shape_broadcast() for &order in &[Order::C, Order::F] { let v2 = v1.to_shape(((2, 2, 2, 2, 2, 2), order)).unwrap(); - assert_eq!(v2.strides(), match (create_order, order) { - (Order::C, Order::C) => { &[32, 16, 0, 0, 2, 1] } - (Order::C, Order::F) => { &[16, 32, 0, 0, 1, 2] } - (Order::F, Order::C) => { &[2, 1, 0, 0, 32, 16] } - (Order::F, Order::F) => { &[1, 2, 0, 0, 16, 32] } - _other => unreachable!() - }); + assert_eq!( + v2.strides(), + match (create_order, order) { + (Order::C, Order::C) => { + &[32, 16, 0, 0, 2, 1] + } + (Order::C, Order::F) => { + &[16, 32, 0, 0, 1, 2] + } + (Order::F, Order::C) => { + &[2, 1, 0, 0, 32, 16] + } + (Order::F, Order::F) => { + &[1, 2, 0, 0, 16, 32] + } + _other => unreachable!(), + } + ); let v2 = v1.to_shape(((4, 4, 4), order)).unwrap(); assert!(v2.is_view()); diff --git a/tests/stacking.rs b/tests/stacking.rs index 7dfceacfc..88fb1b111 100644 --- a/tests/stacking.rs +++ b/tests/stacking.rs @@ -1,7 +1,8 @@ use ndarray::{arr2, arr3, aview1, aview2, concatenate, stack, Array2, Axis, ErrorKind, Ix1, ViewRepr}; #[test] -fn concatenating() { +fn concatenating() +{ let a = arr2(&[[2., 2.], [3., 3.]]); let b = ndarray::concatenate(Axis(0), &[a.view(), a.view()]).unwrap(); assert_eq!(b, arr2(&[[2., 2.], [3., 3.], [2., 2.], [3., 3.]])); @@ -29,7 +30,8 @@ fn concatenating() { } #[test] -fn stacking() { +fn stacking() +{ let a = arr2(&[[2., 2.], [3., 3.]]); let b = ndarray::stack(Axis(0), &[a.view(), a.view()]).unwrap(); assert_eq!(b, arr3(&[[[2., 2.], [3., 3.]], [[2., 2.], [3., 3.]]])); diff --git a/tests/windows.rs b/tests/windows.rs index 7d0f36990..8c5140558 100644 --- a/tests/windows.rs +++ b/tests/windows.rs @@ -121,12 +121,10 @@ fn windows_iterator_only_one_valid_window_for_oversized_stride() fn windows_iterator_1d_with_stride() { let a = Array::from_iter(10..20).into_shape_with_order(10).unwrap(); - itertools::assert_equal(a.windows_with_stride(4, 2), vec![ - arr1(&[10, 11, 12, 13]), - arr1(&[12, 13, 14, 15]), - arr1(&[14, 15, 16, 17]), - arr1(&[16, 17, 18, 19]), - ]); + itertools::assert_equal( + a.windows_with_stride(4, 2), + vec![arr1(&[10, 11, 12, 13]), arr1(&[12, 13, 14, 15]), arr1(&[14, 15, 16, 17]), arr1(&[16, 17, 18, 19])], + ); } /// Simple test for iterating 2d-arrays via `Windows` with stride. @@ -263,16 +261,8 @@ fn test_axis_windows_3d() .unwrap(); itertools::assert_equal(a.axis_windows(Axis(1), 2), vec![ - arr3(&[ - [[0, 1, 2], [3, 4, 5]], - [[9, 10, 11], [12, 13, 14]], - [[18, 19, 20], [21, 22, 23]], - ]), - arr3(&[ - [[3, 4, 5], [6, 7, 8]], - [[12, 13, 14], [15, 16, 17]], - [[21, 22, 23], [24, 25, 26]], - ]), + arr3(&[[[0, 1, 2], [3, 4, 5]], [[9, 10, 11], [12, 13, 14]], [[18, 19, 20], [21, 22, 23]]]), + arr3(&[[[3, 4, 5], [6, 7, 8]], [[12, 13, 14], [15, 16, 17]], [[21, 22, 23], [24, 25, 26]]]), ]); } @@ -289,7 +279,7 @@ fn tests_axis_windows_3d_zips_with_1d() .for_each(|b, a| { *b = a.sum(); }); - assert_eq!(b,arr1(&[207, 261])); + assert_eq!(b, arr1(&[207, 261])); } /// Test verifies that non existent Axis results in panic @@ -342,16 +332,15 @@ fn test_axis_windows_with_stride_1d() { let a = Array::from_iter(10..20).into_shape_with_order(10).unwrap(); - itertools::assert_equal(a.axis_windows_with_stride(Axis(0), 5, 2), vec![ - arr1(&[10, 11, 12, 13, 14]), - arr1(&[12, 13, 14, 15, 16]), - arr1(&[14, 15, 16, 17, 18]), - ]); + itertools::assert_equal( + a.axis_windows_with_stride(Axis(0), 5, 2), + vec![arr1(&[10, 11, 12, 13, 14]), arr1(&[12, 13, 14, 15, 16]), arr1(&[14, 15, 16, 17, 18])], + ); - itertools::assert_equal(a.axis_windows_with_stride(Axis(0), 5, 3), vec![ - arr1(&[10, 11, 12, 13, 14]), - arr1(&[13, 14, 15, 16, 17]), - ]); + itertools::assert_equal( + a.axis_windows_with_stride(Axis(0), 5, 3), + vec![arr1(&[10, 11, 12, 13, 14]), arr1(&[13, 14, 15, 16, 17])], + ); } /// Simple test for iterating 2d-arrays via `Axis Windows`. @@ -363,21 +352,21 @@ fn test_axis_windows_with_stride_2d() .unwrap(); itertools::assert_equal(a.axis_windows_with_stride(Axis(0), 2, 1), vec![ - arr2(&[[10, 11, 12, 13], [14, 15, 16, 17]]), - arr2(&[[14, 15, 16, 17], [18, 19, 20, 21]]), - arr2(&[[18, 19, 20, 21], [22, 23, 24, 25]]), - arr2(&[[22, 23, 24, 25], [26, 27, 28, 29]]), - ]); - - itertools::assert_equal(a.axis_windows_with_stride(Axis(0), 2, 2), vec![ - arr2(&[[10, 11, 12, 13], [14, 15, 16, 17]]), - arr2(&[[18, 19, 20, 21], [22, 23, 24, 25]]), - ]); - - itertools::assert_equal(a.axis_windows_with_stride(Axis(0), 2, 3), vec![ - arr2(&[[10, 11, 12, 13], [14, 15, 16, 17]]), - arr2(&[[22, 23, 24, 25], [26, 27, 28, 29]]), - ]); + arr2(&[[10, 11, 12, 13], [14, 15, 16, 17]]), + arr2(&[[14, 15, 16, 17], [18, 19, 20, 21]]), + arr2(&[[18, 19, 20, 21], [22, 23, 24, 25]]), + arr2(&[[22, 23, 24, 25], [26, 27, 28, 29]]), + ]); + + itertools::assert_equal( + a.axis_windows_with_stride(Axis(0), 2, 2), + vec![arr2(&[[10, 11, 12, 13], [14, 15, 16, 17]]), arr2(&[[18, 19, 20, 21], [22, 23, 24, 25]])], + ); + + itertools::assert_equal( + a.axis_windows_with_stride(Axis(0), 2, 3), + vec![arr2(&[[10, 11, 12, 13], [14, 15, 16, 17]]), arr2(&[[22, 23, 24, 25], [26, 27, 28, 29]])], + ); } /// Simple test for iterating 3d-arrays via `Axis Windows`. @@ -389,25 +378,14 @@ fn test_axis_windows_with_stride_3d() .unwrap(); itertools::assert_equal(a.axis_windows_with_stride(Axis(1), 2, 1), vec![ - arr3(&[ - [[0, 1, 2], [3, 4, 5]], - [[9, 10, 11], [12, 13, 14]], - [[18, 19, 20], [21, 22, 23]], - ]), - arr3(&[ - [[3, 4, 5], [6, 7, 8]], - [[12, 13, 14], [15, 16, 17]], - [[21, 22, 23], [24, 25, 26]], - ]), + arr3(&[[[0, 1, 2], [3, 4, 5]], [[9, 10, 11], [12, 13, 14]], [[18, 19, 20], [21, 22, 23]]]), + arr3(&[[[3, 4, 5], [6, 7, 8]], [[12, 13, 14], [15, 16, 17]], [[21, 22, 23], [24, 25, 26]]]), ]); - itertools::assert_equal(a.axis_windows_with_stride(Axis(1), 2, 2), vec![ - arr3(&[ - [[0, 1, 2], [3, 4, 5]], - [[9, 10, 11], [12, 13, 14]], - [[18, 19, 20], [21, 22, 23]], - ]), - ]); + itertools::assert_equal( + a.axis_windows_with_stride(Axis(1), 2, 2), + vec![arr3(&[[[0, 1, 2], [3, 4, 5]], [[9, 10, 11], [12, 13, 14]], [[18, 19, 20], [21, 22, 23]]])], + ); } #[test] @@ -424,14 +402,14 @@ fn tests_axis_windows_with_stride_3d_zips_with_1d() .for_each(|b, a| { *b = a.sum(); }); - assert_eq!(b1,arr1(&[207, 261])); + assert_eq!(b1, arr1(&[207, 261])); Zip::from(b2.view_mut()) .and(a.axis_windows_with_stride(Axis(1), 2, 2)) .for_each(|b, a| { *b = a.sum(); }); - assert_eq!(b2,arr1(&[207])); + assert_eq!(b2, arr1(&[207])); } #[test]