Skip to main content

nuvec_core/
view.rs

1//! View geometry: shapes, strides and offsets of arrays that share another array's data.
2//!
3//! Pure functions of `(shape, strides)`, used both when a view is recorded (to know its
4//! layout without evaluating) and when it is evaluated.
5
6use smallvec::SmallVec;
7
8use crate::shape::{self, Shape, Strides};
9use crate::{Error, Result};
10
11/// One slice specification per dimension: `start:stop:step` with Python semantics.
12///
13/// `None` bounds mean "from the beginning/end" in the step's direction. Negative indices
14/// count from the end, and out-of-range bounds are clamped.
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub struct SliceArg {
17    /// First index, or `None` for the start in the step's direction.
18    pub start: Option<isize>,
19    /// One past the last index, or `None` for the end in the step's direction.
20    pub stop: Option<isize>,
21    /// Distance between selected indices; must not be zero.
22    pub step: isize,
23}
24
25impl SliceArg {
26    /// The whole dimension (`::`).
27    pub const fn full() -> Self {
28        Self {
29            start: None,
30            stop: None,
31            step: 1,
32        }
33    }
34
35    /// `start:stop:step`.
36    pub const fn new(start: Option<isize>, stop: Option<isize>, step: isize) -> Self {
37        Self { start, stop, step }
38    }
39}
40
41impl From<std::ops::Range<isize>> for SliceArg {
42    fn from(r: std::ops::Range<isize>) -> Self {
43        Self::new(Some(r.start), Some(r.end), 1)
44    }
45}
46
47impl From<std::ops::RangeFrom<isize>> for SliceArg {
48    fn from(r: std::ops::RangeFrom<isize>) -> Self {
49        Self::new(Some(r.start), None, 1)
50    }
51}
52
53impl From<std::ops::RangeTo<isize>> for SliceArg {
54    fn from(r: std::ops::RangeTo<isize>) -> Self {
55        Self::new(None, Some(r.end), 1)
56    }
57}
58
59impl From<std::ops::RangeFull> for SliceArg {
60    fn from(_: std::ops::RangeFull) -> Self {
61        Self::full()
62    }
63}
64
65/// A single index `i`, keeping the dimension with size 1 (`i:i+1`, or the last element for
66/// `-1`).
67impl From<isize> for SliceArg {
68    fn from(i: isize) -> Self {
69        // `i + 1` past `isize::MAX` is the end, as `-1 + 1` is.
70        Self::new(Some(i), if i == -1 { None } else { i.checked_add(1) }, 1)
71    }
72}
73
74/// A normalised slice of one dimension.
75#[derive(Debug, Clone, Copy, PartialEq, Eq)]
76pub(crate) struct DimSlice {
77    pub(crate) start: usize,
78    pub(crate) step: isize,
79    pub(crate) len: usize,
80}
81
82/// Normalises one slice per dimension like Python's `slice.indices`.
83pub(crate) fn normalize_slice(shape: &[usize], args: &[SliceArg]) -> Result<Vec<DimSlice>> {
84    if args.len() != shape.len() {
85        return Err(Error::InvalidArgument(format!(
86            "slice needs one specification per dimension: got {} for shape {shape:?}",
87            args.len()
88        )));
89    }
90    shape
91        .iter()
92        .zip(args)
93        .map(|(&n, arg)| {
94            if arg.step == 0 {
95                return Err(Error::InvalidArgument("slice step must not be zero".into()));
96            }
97            let n = n as isize;
98            let (lower, upper) = if arg.step > 0 { (0, n) } else { (-1, n - 1) };
99            let clamp = |i: isize| {
100                let i = if i < 0 { i + n } else { i };
101                i.clamp(lower, upper)
102            };
103            let start = arg
104                .start
105                .map_or(if arg.step > 0 { lower } else { upper }, clamp);
106            let stop = arg
107                .stop
108                .map_or(if arg.step > 0 { upper } else { lower }, clamp);
109            let span = if arg.step > 0 {
110                stop - start
111            } else {
112                start - stop
113            };
114            let len = if span > 0 {
115                (span + arg.step.abs() - 1) / arg.step.abs()
116            } else {
117                0
118            };
119            Ok(if len == 0 {
120                DimSlice {
121                    start: 0,
122                    step: 1,
123                    len: 0,
124                }
125            } else {
126                DimSlice {
127                    start: start as usize,
128                    step: arg.step,
129                    len: len as usize,
130                }
131            })
132        })
133        .collect()
134}
135
136/// Strides and offset delta of a slice of a layout with `strides`.
137pub(crate) fn slice_geometry(strides: &[isize], slices: &[DimSlice]) -> (Strides, isize) {
138    // The step of a dimension of at most one index is never applied, and may be any
139    // value (a validated slice of more indices stays within the buffer).
140    let out = strides
141        .iter()
142        .zip(slices)
143        .map(|(&s, d)| if d.len <= 1 { s } else { s * d.step })
144        .collect();
145    let delta = strides
146        .iter()
147        .zip(slices)
148        .map(|(&s, d)| s * d.start as isize)
149        .sum();
150    (out, delta)
151}
152
153/// The broadcast shape of `a` and `b` (NumPy rules), or a shape mismatch for `op`.
154pub(crate) fn broadcast_shapes(op: &'static str, a: &[usize], b: &[usize]) -> Result<Shape> {
155    let ndim = a.len().max(b.len());
156    let dim = |s: &[usize], i: usize| {
157        if i + s.len() < ndim {
158            1
159        } else {
160            s[i + s.len() - ndim]
161        }
162    };
163    (0..ndim)
164        .map(|i| match (dim(a, i), dim(b, i)) {
165            (x, y) if x == y => Ok(x),
166            (1, y) => Ok(y),
167            (x, 1) => Ok(x),
168            _ => Err(Error::shape_mismatch(op, a, b)),
169        })
170        .collect()
171}
172
173/// Strides that present a layout of `shape`/`strides` as the larger shape `out`, or an
174/// error if `shape` cannot broadcast to `out`.
175pub(crate) fn broadcast_strides(
176    shape: &[usize],
177    strides: &[isize],
178    out: &[usize],
179) -> Result<Strides> {
180    let mismatch = || Error::shape_mismatch("broadcast_to", shape, out);
181    if shape.len() > out.len() {
182        return Err(mismatch());
183    }
184    let lead = out.len() - shape.len();
185    (0..out.len())
186        .map(|i| {
187            if i < lead {
188                return Ok(0);
189            }
190            match (shape[i - lead], out[i]) {
191                (x, y) if x == y => Ok(strides[i - lead]),
192                (1, _) => Ok(0),
193                _ => Err(mismatch()),
194            }
195        })
196        .collect()
197}
198
199/// Strides for viewing a layout of `shape`/`strides` as `new_shape` without copying, or
200/// `None` if the data must be copied. NumPy's `_attempt_nocopy_reshape` (C order).
201///
202/// Requires both shapes to have the same element count.
203pub(crate) fn nocopy_reshape(
204    shape: &[usize],
205    strides: &[isize],
206    new_shape: &[usize],
207) -> Option<Strides> {
208    if shape.contains(&0) || new_shape.iter().all(|&d| d == 1) {
209        return Some(shape::row_contiguous_strides(new_shape));
210    }
211    let (old_dims, old_strides): (Vec<usize>, Vec<isize>) = shape
212        .iter()
213        .zip(strides)
214        .filter(|(d, _)| **d != 1)
215        .map(|(&d, &s)| (d, s))
216        .unzip();
217    let mut out: Strides = SmallVec::from_elem(1, new_shape.len());
218    let (mut oi, mut oj, mut ni, mut nj) = (0, 1, 0, 1);
219    while ni < new_shape.len() && oi < old_dims.len() {
220        let mut np = new_shape[ni];
221        let mut op = old_dims[oi];
222        while np != op {
223            if np < op {
224                np *= new_shape[nj];
225                nj += 1;
226            } else {
227                op *= old_dims[oj];
228                oj += 1;
229            }
230        }
231        // The merged old dimensions must be contiguous among themselves.
232        for ok in oi..oj - 1 {
233            if old_strides[ok] != old_dims[ok + 1] as isize * old_strides[ok + 1] {
234                return None;
235            }
236        }
237        out[nj - 1] = old_strides[oj - 1];
238        for nk in (ni + 1..nj).rev() {
239            out[nk - 1] = out[nk] * new_shape[nk] as isize;
240        }
241        ni = nj;
242        nj += 1;
243        oi = oj;
244        oj += 1;
245    }
246    Some(out)
247}
248
249/// Element count of `requested` with at most one `-1` inferred to give `size` elements.
250pub(crate) fn infer_shape(size: usize, requested: &[isize]) -> Result<Shape> {
251    let invalid = || Error::invalid_shape("reshape", requested.iter().map(|&d| d as i64));
252    if requested.iter().filter(|&&d| d == -1).count() > 1 || requested.iter().any(|&d| d < -1) {
253        return Err(invalid());
254    }
255    let known = requested
256        .iter()
257        .filter(|&&d| d >= 0)
258        .try_fold(1usize, |acc, &d| acc.checked_mul(d as usize))
259        .ok_or_else(invalid)?;
260    let inferred = if requested.contains(&-1) {
261        if known == 0 || !size.is_multiple_of(known) {
262            return Err(invalid());
263        }
264        size / known
265    } else if known == size {
266        0
267    } else {
268        return Err(invalid());
269    };
270    Ok(requested
271        .iter()
272        .map(|&d| if d == -1 { inferred } else { d as usize })
273        .collect())
274}
275
276/// Shape and strides with axes reordered: output axis `i` is input axis `axes[i]`.
277pub(crate) fn permute(
278    shape: &[usize],
279    strides: &[isize],
280    axes: &[usize],
281) -> Result<(Shape, Strides)> {
282    let mut seen = vec![false; shape.len()];
283    let valid = axes.len() == shape.len()
284        && axes
285            .iter()
286            .all(|&a| a < shape.len() && !std::mem::replace(&mut seen[a], true));
287    if !valid {
288        return Err(Error::InvalidArgument(format!(
289            "transpose axes {axes:?} are not a permutation of {} dimensions",
290            shape.len()
291        )));
292    }
293    Ok((
294        axes.iter().map(|&a| shape[a]).collect(),
295        axes.iter().map(|&a| strides[a]).collect(),
296    ))
297}
298
299/// `axis` as an index into `ndim` dimensions; negative axes count from the end.
300pub(crate) fn normalize_axis(axis: isize, ndim: usize) -> Result<usize> {
301    let a = if axis < 0 { axis + ndim as isize } else { axis };
302    if (0..ndim as isize).contains(&a) {
303        Ok(a as usize)
304    } else {
305        Err(Error::invalid_axis("axis", axis as i64, ndim))
306    }
307}
308
309#[cfg(test)]
310mod tests {
311    use super::*;
312
313    fn slices(n: usize, start: Option<isize>, stop: Option<isize>, step: isize) -> DimSlice {
314        normalize_slice(&[n], &[SliceArg::new(start, stop, step)]).unwrap()[0]
315    }
316
317    #[test]
318    fn python_slice_semantics() {
319        let d = |start, step, len| DimSlice { start, step, len };
320        assert_eq!(slices(5, None, None, 1), d(0, 1, 5));
321        assert_eq!(slices(5, None, None, -1), d(4, -1, 5));
322        assert_eq!(slices(5, Some(-2), None, 1), d(3, 1, 2));
323        assert_eq!(slices(5, Some(1), Some(100), 2), d(1, 2, 2));
324        assert_eq!(slices(5, None, Some(-6), -2), d(4, -2, 3));
325        assert_eq!(slices(5, Some(3), Some(1), 1).len, 0);
326        assert_eq!(slices(5, Some(0), Some(5), 10), d(0, 10, 1));
327        assert!(normalize_slice(&[5], &[SliceArg::new(None, None, 0)]).is_err());
328        assert!(normalize_slice(&[5, 2], &[SliceArg::full()]).is_err());
329    }
330
331    #[test]
332    fn broadcasting() {
333        assert_eq!(
334            broadcast_shapes("t", &[3, 1], &[4]).unwrap().as_slice(),
335            &[3, 4]
336        );
337        assert_eq!(
338            broadcast_shapes("t", &[], &[2, 0]).unwrap().as_slice(),
339            &[2, 0]
340        );
341        assert!(broadcast_shapes("t", &[2, 3], &[4]).is_err());
342        assert_eq!(
343            broadcast_strides(&[3, 1], &[1, 1], &[2, 3, 4])
344                .unwrap()
345                .as_slice(),
346            &[0, 1, 0]
347        );
348        assert!(broadcast_strides(&[3], &[1], &[4]).is_err());
349    }
350
351    #[test]
352    fn reshape_views() {
353        // Contiguous: always a view.
354        assert_eq!(
355            nocopy_reshape(&[2, 3], &[3, 1], &[3, 2])
356                .unwrap()
357                .as_slice(),
358            &[2, 1]
359        );
360        assert_eq!(
361            nocopy_reshape(&[2, 3, 4], &[12, 4, 1], &[6, 4])
362                .unwrap()
363                .as_slice(),
364            &[4, 1]
365        );
366        // Transposed 2x3 -> 6: needs a copy.
367        assert!(nocopy_reshape(&[3, 2], &[1, 3], &[6]).is_none());
368        // Transposed, but splitting one dimension is fine.
369        assert_eq!(
370            nocopy_reshape(&[4, 2], &[1, 4], &[2, 2, 2])
371                .unwrap()
372                .as_slice(),
373            &[2, 1, 4]
374        );
375        // Unit dimensions are free (the stride of a size-1 dimension is arbitrary).
376        let s = nocopy_reshape(&[3, 2], &[1, 3], &[3, 1, 2]).unwrap();
377        assert_eq!((s[0], s[2]), (1, 3));
378    }
379
380    #[test]
381    fn shape_inference_and_axes() {
382        assert_eq!(infer_shape(6, &[-1, 2]).unwrap().as_slice(), &[3, 2]);
383        assert!(infer_shape(6, &[-1, -1]).is_err());
384        assert!(infer_shape(6, &[4, -1]).is_err());
385        assert!(infer_shape(0, &[0, -1]).is_err());
386        assert!(infer_shape(6, &[5]).is_err());
387        assert_eq!(normalize_axis(-1, 3).unwrap(), 2);
388        assert!(normalize_axis(3, 3).is_err());
389        assert!(permute(&[2, 3], &[3, 1], &[0, 0]).is_err());
390        let (s, st) = permute(&[2, 3, 4], &[12, 4, 1], &[2, 0, 1]).unwrap();
391        assert_eq!(
392            (s.as_slice(), st.as_slice()),
393            (&[4, 2, 3][..], &[1, 12, 4][..])
394        );
395    }
396}