Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions crates/blas-tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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"
);
45 changes: 5 additions & 40 deletions crates/blas-tests/tests/oper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -353,30 +343,15 @@ 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]
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] {
Expand Down Expand Up @@ -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] {
Expand Down
22 changes: 16 additions & 6 deletions crates/numeric-tests/tests/accuracy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}

Expand Down Expand Up @@ -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
);
}
}

Expand Down
38 changes: 9 additions & 29 deletions src/array_approx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,14 @@ macro_rules! impl_approx_traits {
A::default_max_relative()
}

fn relative_eq(
&self,
other: &ArrayRef<B, D>,
epsilon: A::Epsilon,
max_relative: A::Epsilon,
) -> bool {
fn relative_eq(&self, other: &ArrayRef<B, D>, 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()))
}
}

Expand All @@ -118,12 +113,7 @@ macro_rules! impl_approx_traits {
A::default_max_relative()
}

fn relative_eq(
&self,
other: &ArrayBase<S2, D>,
epsilon: A::Epsilon,
max_relative: A::Epsilon,
) -> bool {
fn relative_eq(&self, other: &ArrayBase<S2, D>, epsilon: A::Epsilon, max_relative: A::Epsilon) -> bool {
(**self).relative_eq(other, epsilon, max_relative)
}
}
Expand All @@ -139,12 +129,7 @@ macro_rules! impl_approx_traits {
A::default_max_ulps()
}

fn ulps_eq(
&self,
other: &ArrayRef<B, D>,
epsilon: A::Epsilon,
max_ulps: u32,
) -> bool {
fn ulps_eq(&self, other: &ArrayRef<B, D>, epsilon: A::Epsilon, max_ulps: u32) -> bool {
if self.shape() != other.shape() {
return false;
}
Expand All @@ -168,12 +153,7 @@ macro_rules! impl_approx_traits {
A::default_max_ulps()
}

fn ulps_eq(
&self,
other: &ArrayBase<S2, D>,
epsilon: A::Epsilon,
max_ulps: u32,
) -> bool {
fn ulps_eq(&self, other: &ArrayBase<S2, D>, epsilon: A::Epsilon, max_ulps: u32) -> bool {
(**self).ulps_eq(other, epsilon, max_ulps)
}
}
Expand All @@ -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]
Expand Down
25 changes: 4 additions & 21 deletions src/arrayformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,7 @@ impl<A: fmt::Debug, D: Dimension> fmt::Debug for ArrayRef<A, D>
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())?,
Expand Down Expand Up @@ -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<Item = impl fmt::Display>) -> String
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down
10 changes: 2 additions & 8 deletions src/arraytraits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,10 +580,7 @@ where Slice: AsMut<[A]>
{
let xs = slice.as_mut();
if mem::size_of::<A>() == 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()) }
}
Expand Down Expand Up @@ -619,10 +616,7 @@ impl<'a, A, const N: usize> From<&'a mut [[A; N]]> for ArrayViewMut<'a, A, Ix2>
if size_of::<A>() == 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
Expand Down
66 changes: 11 additions & 55 deletions src/dimension/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)));
Expand All @@ -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]));
}
}
Loading