ndarray/
impl_dyn.rs

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.
10use crate::imp_prelude::*;
11
12/// # Methods for Dynamic-Dimensional Arrays
13impl<A, S> ArrayBase<S, IxDyn>
14where 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    pub fn insert_axis_inplace(&mut self, axis: Axis)
33    {
34        assert!(axis.index() <= self.ndim());
35        self.dim = self.dim.insert_axis(axis);
36        self.strides = self.strides.insert_axis(axis);
37    }
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    pub fn index_axis_inplace(&mut self, axis: Axis, index: usize)
56    {
57        self.collapse_axis(axis, index);
58        self.dim = self.dim.remove_axis(axis);
59        self.strides = self.strides.remove_axis(axis);
60    }
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    pub fn squeeze(self) -> Self
84    {
85        let mut out = self;
86        for axis in (0..out.shape().len()).rev() {
87            if out.shape()[axis] == 1 && out.shape().len() > 1 {
88                out = out.remove_axis(Axis(axis));
89            }
90        }
91        out
92    }
93}
94
95#[cfg(test)]
96mod 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}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy