Coverage Report

Created: 2025-11-28 06:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/ndarray-0.16.1/src/impl_dyn.rs
Line
Count
Source
1
// Copyright 2018 bluss and ndarray developers.
2
//
3
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6
// option. This file may not be copied, modified, or distributed
7
// except according to those terms.
8
9
//! Methods for dynamic-dimensional arrays.
10
use crate::imp_prelude::*;
11
12
/// # Methods for Dynamic-Dimensional Arrays
13
impl<A, S> ArrayBase<S, IxDyn>
14
where S: Data<Elem = A>
15
{
16
    /// Insert new array axis of length 1 at `axis`, modifying the shape and
17
    /// strides in-place.
18
    ///
19
    /// **Panics** if the axis is out of bounds.
20
    ///
21
    /// ```
22
    /// use ndarray::{Axis, arr2, arr3};
23
    ///
24
    /// let mut a = arr2(&[[1, 2, 3], [4, 5, 6]]).into_dyn();
25
    /// assert_eq!(a.shape(), &[2, 3]);
26
    ///
27
    /// a.insert_axis_inplace(Axis(1));
28
    /// assert_eq!(a, arr3(&[[[1, 2, 3]], [[4, 5, 6]]]).into_dyn());
29
    /// assert_eq!(a.shape(), &[2, 1, 3]);
30
    /// ```
31
    #[track_caller]
32
0
    pub fn insert_axis_inplace(&mut self, axis: Axis)
33
    {
34
0
        assert!(axis.index() <= self.ndim());
35
0
        self.dim = self.dim.insert_axis(axis);
36
0
        self.strides = self.strides.insert_axis(axis);
37
0
    }
38
39
    /// Collapses the array to `index` along the axis and removes the axis,
40
    /// modifying the shape and strides in-place.
41
    ///
42
    /// **Panics** if `axis` or `index` is out of bounds.
43
    ///
44
    /// ```
45
    /// use ndarray::{Axis, arr1, arr2};
46
    ///
47
    /// let mut a = arr2(&[[1, 2, 3], [4, 5, 6]]).into_dyn();
48
    /// assert_eq!(a.shape(), &[2, 3]);
49
    ///
50
    /// a.index_axis_inplace(Axis(1), 1);
51
    /// assert_eq!(a, arr1(&[2, 5]).into_dyn());
52
    /// assert_eq!(a.shape(), &[2]);
53
    /// ```
54
    #[track_caller]
55
0
    pub fn index_axis_inplace(&mut self, axis: Axis, index: usize)
56
    {
57
0
        self.collapse_axis(axis, index);
58
0
        self.dim = self.dim.remove_axis(axis);
59
0
        self.strides = self.strides.remove_axis(axis);
60
0
    }
61
62
    /// Remove axes of length 1 and return the modified array.
63
    ///
64
    /// If the array has more the one dimension, the result array will always
65
    /// have at least one dimension, even if it has a length of 1.
66
    ///
67
    /// ```
68
    /// use ndarray::{arr1, arr2, arr3};
69
    ///
70
    /// let a = arr3(&[[[1, 2, 3]], [[4, 5, 6]]]).into_dyn();
71
    /// assert_eq!(a.shape(), &[2, 1, 3]);
72
    /// let b = a.squeeze();
73
    /// assert_eq!(b, arr2(&[[1, 2, 3], [4, 5, 6]]).into_dyn());
74
    /// assert_eq!(b.shape(), &[2, 3]);
75
    ///
76
    /// let c = arr2(&[[1]]).into_dyn();
77
    /// assert_eq!(c.shape(), &[1, 1]);
78
    /// let d = c.squeeze();
79
    /// assert_eq!(d, arr1(&[1]).into_dyn());
80
    /// assert_eq!(d.shape(), &[1]);
81
    /// ```
82
    #[track_caller]
83
0
    pub fn squeeze(self) -> Self
84
    {
85
0
        let mut out = self;
86
0
        for axis in (0..out.shape().len()).rev() {
87
0
            if out.shape()[axis] == 1 && out.shape().len() > 1 {
88
0
                out = out.remove_axis(Axis(axis));
89
0
            }
90
        }
91
0
        out
92
0
    }
93
}
94
95
#[cfg(test)]
96
mod tests
97
{
98
    use crate::{arr1, arr2, arr3};
99
100
    #[test]
101
    fn test_squeeze()
102
    {
103
        let a = arr3(&[[[1, 2, 3]], [[4, 5, 6]]]).into_dyn();
104
        assert_eq!(a.shape(), &[2, 1, 3]);
105
106
        let b = a.squeeze();
107
        assert_eq!(b, arr2(&[[1, 2, 3], [4, 5, 6]]).into_dyn());
108
        assert_eq!(b.shape(), &[2, 3]);
109
110
        let c = arr2(&[[1]]).into_dyn();
111
        assert_eq!(c.shape(), &[1, 1]);
112
113
        let d = c.squeeze();
114
        assert_eq!(d, arr1(&[1]).into_dyn());
115
        assert_eq!(d.shape(), &[1]);
116
    }
117
}