Skip to content

Commit

Permalink
Merge pull request #957 from rust-ndarray/neg-stride-windows
Browse files Browse the repository at this point in the history
Fix .windows() producer for negative stride arrays
  • Loading branch information
bluss committed Mar 27, 2021
2 parents 288f131 + e377e82 commit a77825a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/iterators/windows.rs
Expand Up @@ -41,7 +41,7 @@ impl<'a, A, D: Dimension> Windows<'a, A, D> {

unsafe {
Windows {
base: ArrayView::from_shape_ptr(size.strides(a.strides), a.ptr.as_ptr()),
base: ArrayView::new(a.ptr, size, a.strides),
window,
strides: window_strides,
}
Expand Down
36 changes: 36 additions & 0 deletions tests/windows.rs
Expand Up @@ -116,3 +116,39 @@ fn test_window_zip() {
}
}
}

#[test]
fn test_window_neg_stride() {
let array = Array::from_iter(1..10).into_shape((3, 3)).unwrap();

// window neg/pos stride combinations

// Make a 2 x 2 array of the windows of the 3 x 3 array
// and compute test answers from here
let mut answer = Array::from_iter(array.windows((2, 2)).into_iter().map(|a| a.to_owned()))
.into_shape((2, 2)).unwrap();

answer.invert_axis(Axis(1));
answer.map_inplace(|a| a.invert_axis(Axis(1)));

itertools::assert_equal(
array.slice(s![.., ..;-1]).windows((2, 2)),
answer.iter().map(|a| a.view())
);

answer.invert_axis(Axis(0));
answer.map_inplace(|a| a.invert_axis(Axis(0)));

itertools::assert_equal(
array.slice(s![..;-1, ..;-1]).windows((2, 2)),
answer.iter().map(|a| a.view())
);

answer.invert_axis(Axis(1));
answer.map_inplace(|a| a.invert_axis(Axis(1)));

itertools::assert_equal(
array.slice(s![..;-1, ..]).windows((2, 2)),
answer.iter().map(|a| a.view())
);
}

0 comments on commit a77825a

Please sign in to comment.