From 5a25defa3579e74947c3ffed3f7cba5d5fa59049 Mon Sep 17 00:00:00 2001 From: bluss Date: Thu, 8 Apr 2021 22:14:20 +0200 Subject: [PATCH 1/2] TEST: Add tests for column/row matrix layouts These tests - w.r.t C/F preference, failed when they were introduced. --- src/layout/mod.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/layout/mod.rs b/src/layout/mod.rs index 625fc7ff2..d4271dd77 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -170,6 +170,34 @@ mod tests { } } + #[test] + fn no_layouts() { + let a = M::zeros((5, 5)); + let b = M::zeros((5, 5).f()); + + // 2D row/column matrixes + let arow = a.slice(s![0..1, ..]); + let acol = a.slice(s![.., 0..1]); + let brow = b.slice(s![0..1, ..]); + let bcol = b.slice(s![.., 0..1]); + assert_layouts!(arow, CORDER, FORDER); + assert_not_layouts!(acol, CORDER, CPREFER, FORDER, FPREFER); + assert_layouts!(bcol, CORDER, FORDER); + assert_not_layouts!(brow, CORDER, CPREFER, FORDER, FPREFER); + + // 2D row/column matrixes - now made with insert axis + for &axis in &[Axis(0), Axis(1)] { + let arow = a.slice(s![0, ..]).insert_axis(axis); + let acol = a.slice(s![.., 0]).insert_axis(axis); + let brow = b.slice(s![0, ..]).insert_axis(axis); + let bcol = b.slice(s![.., 0]).insert_axis(axis); + assert_layouts!(arow, CORDER, FORDER); + assert_not_layouts!(acol, CORDER, CPREFER, FORDER, FPREFER); + assert_layouts!(bcol, CORDER, FORDER); + assert_not_layouts!(brow, CORDER, CPREFER, FORDER, FPREFER); + } + } + #[test] fn skip_layouts() { let a = M::zeros((5, 5)); From ef51f56f27d2b39d4ebf2cff0e947e011d4f672d Mon Sep 17 00:00:00 2001 From: bluss Date: Thu, 8 Apr 2021 21:53:00 +0200 Subject: [PATCH 2/2] FIX: For stride == 1 axes, require len > 1 to determine C/F preference The issue was that otherwise, this array: shape: [1, 256] strides: [1, 256] Which was "correctly" determined to be F-preference because it's sliced out of an f-order array. In reality it is discontiguous; every element is 256 steps from the next. This should have layout None, the same as this array would have (ignoring 1-len axes). shape: [256] strides: [256] --- src/zip/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/zip/mod.rs b/src/zip/mod.rs index 38dbeb579..5f9b15c5a 100644 --- a/src/zip/mod.rs +++ b/src/zip/mod.rs @@ -69,9 +69,9 @@ where } else if n > 1 && self.raw_view().reversed_axes().is_standard_layout() { Layout::f() } else if n > 1 { - if self.stride_of(Axis(0)) == 1 { + if self.len_of(Axis(0)) > 1 && self.stride_of(Axis(0)) == 1 { Layout::fpref() - } else if self.stride_of(Axis(n - 1)) == 1 { + } else if self.len_of(Axis(n - 1)) > 1 && self.stride_of(Axis(n - 1)) == 1 { Layout::cpref() } else { Layout::none()