Skip to content

Commit a77825a

Browse files
authored
Merge pull request #957 from rust-ndarray/neg-stride-windows
Fix .windows() producer for negative stride arrays
2 parents 288f131 + e377e82 commit a77825a

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/iterators/windows.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl<'a, A, D: Dimension> Windows<'a, A, D> {
4141

4242
unsafe {
4343
Windows {
44-
base: ArrayView::from_shape_ptr(size.strides(a.strides), a.ptr.as_ptr()),
44+
base: ArrayView::new(a.ptr, size, a.strides),
4545
window,
4646
strides: window_strides,
4747
}

tests/windows.rs

+36
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,39 @@ fn test_window_zip() {
116116
}
117117
}
118118
}
119+
120+
#[test]
121+
fn test_window_neg_stride() {
122+
let array = Array::from_iter(1..10).into_shape((3, 3)).unwrap();
123+
124+
// window neg/pos stride combinations
125+
126+
// Make a 2 x 2 array of the windows of the 3 x 3 array
127+
// and compute test answers from here
128+
let mut answer = Array::from_iter(array.windows((2, 2)).into_iter().map(|a| a.to_owned()))
129+
.into_shape((2, 2)).unwrap();
130+
131+
answer.invert_axis(Axis(1));
132+
answer.map_inplace(|a| a.invert_axis(Axis(1)));
133+
134+
itertools::assert_equal(
135+
array.slice(s![.., ..;-1]).windows((2, 2)),
136+
answer.iter().map(|a| a.view())
137+
);
138+
139+
answer.invert_axis(Axis(0));
140+
answer.map_inplace(|a| a.invert_axis(Axis(0)));
141+
142+
itertools::assert_equal(
143+
array.slice(s![..;-1, ..;-1]).windows((2, 2)),
144+
answer.iter().map(|a| a.view())
145+
);
146+
147+
answer.invert_axis(Axis(1));
148+
answer.map_inplace(|a| a.invert_axis(Axis(1)));
149+
150+
itertools::assert_equal(
151+
array.slice(s![..;-1, ..]).windows((2, 2)),
152+
answer.iter().map(|a| a.view())
153+
);
154+
}

0 commit comments

Comments
 (0)