Skip to content

Commit b10b09c

Browse files
VasanthakumarVjturner314
authored andcommitted
Add axis windows method
Co-authored-by: Jim Turner <github@turner.link>
1 parent 187cea9 commit b10b09c

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/impl_methods.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,50 @@ where
13991399
Windows::new(self.view(), window_size)
14001400
}
14011401

1402+
/// Returns a producer which traverses over all windows of a given length along an axis.
1403+
///
1404+
/// The windows are all distinct, possibly-overlapping views. The shape of each window
1405+
/// is the shape of `self`, with the length of `axis` replaced with `window_size`.
1406+
///
1407+
/// **Panics** if `axis` is out-of-bounds or if `window_size` is zero.
1408+
///
1409+
/// ```
1410+
/// use ndarray::{Array3, Axis, s};
1411+
///
1412+
/// let arr = Array3::from_shape_fn([4, 5, 2], |(i, j, k)| i * 100 + j * 10 + k);
1413+
/// let correct = vec![
1414+
/// arr.slice(s![.., 0..3, ..]),
1415+
/// arr.slice(s![.., 1..4, ..]),
1416+
/// arr.slice(s![.., 2..5, ..]),
1417+
/// ];
1418+
/// for (window, correct) in arr.axis_windows(Axis(1), 3).into_iter().zip(&correct) {
1419+
/// assert_eq!(window, correct);
1420+
/// assert_eq!(window.shape(), &[4, 3, 2]);
1421+
/// }
1422+
/// ```
1423+
pub fn axis_windows(&self, axis: Axis, window_size: usize) -> Windows<'_, A, D>
1424+
where
1425+
S: Data,
1426+
{
1427+
let axis_index = axis.index();
1428+
1429+
ndassert!(
1430+
axis_index < self.ndim(),
1431+
concat!(
1432+
"Window axis {} does not match array dimension {} ",
1433+
"(with array of shape {:?})"
1434+
),
1435+
axis_index,
1436+
self.ndim(),
1437+
self.shape()
1438+
);
1439+
1440+
let mut size = self.raw_dim();
1441+
size[axis_index] = window_size;
1442+
1443+
Windows::new(self.view(), size)
1444+
}
1445+
14021446
// Return (length, stride) for diagonal
14031447
fn diag_params(&self) -> (Ix, Ixs) {
14041448
/* empty shape has len 1 */

0 commit comments

Comments
 (0)