Macro ndarray::s[][src]

macro_rules! s {
    (@parse [$($stack:tt)*] $r:expr;$s:expr) => { ... };
    (@parse [$($stack:tt)*] $r:expr) => { ... };
    (@parse [$($stack:tt)*] $r:expr;$s:expr ,) => { ... };
    (@parse [$($stack:tt)*] $r:expr ,) => { ... };
    (@parse [$($stack:tt)*] $r:expr;$s:expr, $($t:tt)*) => { ... };
    (@parse [$($stack:tt)*] $r:expr, $($t:tt)*) => { ... };
    (@step $r:expr, $s:expr) => { ... };
    ($($t:tt)*) => { ... };
}
Expand description

Slice argument constructor.

s![] takes a list of ranges, separated by comma, with optional strides that are separated from the range by a semicolon. It is converted into a slice argument with type &[Si; N].

Each range uses signed indices, where a negative value is counted from the end of the axis. Strides are also signed and may be negative, but must not be zero.

The syntax is s![ [ axis-slice [, axis-slice [ , … ] ] ] ]. Where axis-slice is either i .. j or i .. j ; step, and i is the start index, j end index and step the element step size (which defaults to 1). The number of axis-slice must match the number of axes in the array.

For example s![0..4;2, 1..5] is a slice of rows 0..4 with step size 2, and columns 1..5 with default step size 1. The slice would have shape [2, 4].

If an array has two axes, the slice argument is passed as type &[Si; 2]. The macro expansion of s![a..b;c, d..e] is equivalent to &[Si(a, Some(b), c), Si(d, Some(e), 1)].

#[macro_use]
extern crate ndarray;

use ndarray::{Array2, ArrayView2};

fn laplacian(v: &ArrayView2<f32>) -> Array2<f32> {
    -4. * &v.slice(s![1..-1, 1..-1])
    + v.slice(s![ ..-2, 1..-1])
    + v.slice(s![1..-1,  ..-2])
    + v.slice(s![1..-1, 2..  ])
    + v.slice(s![2..  , 1..-1])
}