ndarray/
impl_2d.rs

1// Copyright 2014-2016 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 two-dimensional arrays.
10use crate::imp_prelude::*;
11
12/// # Methods For 2-D Arrays
13impl<A, S> ArrayBase<S, Ix2>
14where S: RawData<Elem = A>
15{
16    /// Return an array view of row `index`.
17    ///
18    /// **Panics** if `index` is out of bounds.
19    ///
20    /// ```
21    /// use ndarray::array;
22    /// let array = array![[1., 2.], [3., 4.]];
23    /// assert_eq!(array.row(0), array![1., 2.]);
24    /// ```
25    #[track_caller]
26    pub fn row(&self, index: Ix) -> ArrayView1<'_, A>
27    where S: Data
28    {
29        self.index_axis(Axis(0), index)
30    }
31
32    /// Return a mutable array view of row `index`.
33    ///
34    /// **Panics** if `index` is out of bounds.
35    ///
36    /// ```
37    /// use ndarray::array;
38    /// let mut array = array![[1., 2.], [3., 4.]];
39    /// array.row_mut(0)[1] = 5.;
40    /// assert_eq!(array, array![[1., 5.], [3., 4.]]);
41    /// ```
42    #[track_caller]
43    pub fn row_mut(&mut self, index: Ix) -> ArrayViewMut1<'_, A>
44    where S: DataMut
45    {
46        self.index_axis_mut(Axis(0), index)
47    }
48
49    /// Return the number of rows (length of `Axis(0)`) in the two-dimensional array.
50    ///
51    /// ```
52    /// use ndarray::{array, Axis};
53    ///
54    /// let array = array![[1., 2.],
55    ///                    [3., 4.],
56    ///                    [5., 6.]];
57    /// assert_eq!(array.nrows(), 3);
58    ///
59    /// // equivalent ways of getting the dimensions
60    /// // get nrows, ncols by using dim:
61    /// let (m, n) = array.dim();
62    /// assert_eq!(m, array.nrows());
63    /// // get length of any particular axis with .len_of()
64    /// assert_eq!(m, array.len_of(Axis(0)));
65    /// ```
66    pub fn nrows(&self) -> usize
67    {
68        self.len_of(Axis(0))
69    }
70
71    /// Return an array view of column `index`.
72    ///
73    /// **Panics** if `index` is out of bounds.
74    ///
75    /// ```
76    /// use ndarray::array;
77    /// let array = array![[1., 2.], [3., 4.]];
78    /// assert_eq!(array.column(0), array![1., 3.]);
79    /// ```
80    #[track_caller]
81    pub fn column(&self, index: Ix) -> ArrayView1<'_, A>
82    where S: Data
83    {
84        self.index_axis(Axis(1), index)
85    }
86
87    /// Return a mutable array view of column `index`.
88    ///
89    /// **Panics** if `index` is out of bounds.
90    ///
91    /// ```
92    /// use ndarray::array;
93    /// let mut array = array![[1., 2.], [3., 4.]];
94    /// array.column_mut(0)[1] = 5.;
95    /// assert_eq!(array, array![[1., 2.], [5., 4.]]);
96    /// ```
97    #[track_caller]
98    pub fn column_mut(&mut self, index: Ix) -> ArrayViewMut1<'_, A>
99    where S: DataMut
100    {
101        self.index_axis_mut(Axis(1), index)
102    }
103
104    /// Return the number of columns (length of `Axis(1)`) in the two-dimensional array.
105    ///
106    /// ```
107    /// use ndarray::{array, Axis};
108    ///
109    /// let array = array![[1., 2.],
110    ///                    [3., 4.],
111    ///                    [5., 6.]];
112    /// assert_eq!(array.ncols(), 2);
113    ///
114    /// // equivalent ways of getting the dimensions
115    /// // get nrows, ncols by using dim:
116    /// let (m, n) = array.dim();
117    /// assert_eq!(n, array.ncols());
118    /// // get length of any particular axis with .len_of()
119    /// assert_eq!(n, array.len_of(Axis(1)));
120    /// ```
121    pub fn ncols(&self) -> usize
122    {
123        self.len_of(Axis(1))
124    }
125
126    /// Return true if the array is square, false otherwise.
127    ///
128    /// # Examples
129    /// Square:
130    /// ```
131    /// use ndarray::array;
132    /// let array = array![[1., 2.], [3., 4.]];
133    /// assert!(array.is_square());
134    /// ```
135    /// Not square:
136    /// ```
137    /// use ndarray::array;
138    /// let array = array![[1., 2., 5.], [3., 4., 6.]];
139    /// assert!(!array.is_square());
140    /// ```
141    pub fn is_square(&self) -> bool
142    {
143        let (m, n) = self.dim();
144        m == n
145    }
146}