Thanks to visit codestin.com
Credit goes to docs.rs

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> ArrayRef<A, Ix2>
14{
15    /// Return an array view of row `index`.
16    ///
17    /// **Panics** if `index` is out of bounds.
18    ///
19    /// ```
20    /// use ndarray::array;
21    /// let array = array![[1., 2.], [3., 4.]];
22    /// assert_eq!(array.row(0), array![1., 2.]);
23    /// ```
24    #[track_caller]
25    pub fn row(&self, index: Ix) -> ArrayView1<'_, A>
26    {
27        self.index_axis(Axis(0), index)
28    }
29
30    /// Return a mutable array view of row `index`.
31    ///
32    /// **Panics** if `index` is out of bounds.
33    ///
34    /// ```
35    /// use ndarray::array;
36    /// let mut array = array![[1., 2.], [3., 4.]];
37    /// array.row_mut(0)[1] = 5.;
38    /// assert_eq!(array, array![[1., 5.], [3., 4.]]);
39    /// ```
40    #[track_caller]
41    pub fn row_mut(&mut self, index: Ix) -> ArrayViewMut1<'_, A>
42    {
43        self.index_axis_mut(Axis(0), index)
44    }
45}
46
47impl<A> LayoutRef<A, Ix2>
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
72impl<A> ArrayRef<A, Ix2>
73{
74    /// Return an array view of column `index`.
75    ///
76    /// **Panics** if `index` is out of bounds.
77    ///
78    /// ```
79    /// use ndarray::array;
80    /// let array = array![[1., 2.], [3., 4.]];
81    /// assert_eq!(array.column(0), array![1., 3.]);
82    /// ```
83    #[track_caller]
84    pub fn column(&self, index: Ix) -> ArrayView1<'_, A>
85    {
86        self.index_axis(Axis(1), index)
87    }
88
89    /// Return a mutable array view of column `index`.
90    ///
91    /// **Panics** if `index` is out of bounds.
92    ///
93    /// ```
94    /// use ndarray::array;
95    /// let mut array = array![[1., 2.], [3., 4.]];
96    /// array.column_mut(0)[1] = 5.;
97    /// assert_eq!(array, array![[1., 2.], [5., 4.]]);
98    /// ```
99    #[track_caller]
100    pub fn column_mut(&mut self, index: Ix) -> ArrayViewMut1<'_, A>
101    {
102        self.index_axis_mut(Axis(1), index)
103    }
104}
105
106impl<A> LayoutRef<A, Ix2>
107{
108    /// Return the number of columns (length of `Axis(1)`) in the two-dimensional array.
109    ///
110    /// ```
111    /// use ndarray::{array, Axis};
112    ///
113    /// let array = array![[1., 2.],
114    ///                    [3., 4.],
115    ///                    [5., 6.]];
116    /// assert_eq!(array.ncols(), 2);
117    ///
118    /// // equivalent ways of getting the dimensions
119    /// // get nrows, ncols by using dim:
120    /// let (m, n) = array.dim();
121    /// assert_eq!(n, array.ncols());
122    /// // get length of any particular axis with .len_of()
123    /// assert_eq!(n, array.len_of(Axis(1)));
124    /// ```
125    pub fn ncols(&self) -> usize
126    {
127        self.len_of(Axis(1))
128    }
129
130    /// Return true if the array is square, false otherwise.
131    ///
132    /// # Examples
133    /// Square:
134    /// ```
135    /// use ndarray::array;
136    /// let array = array![[1., 2.], [3., 4.]];
137    /// assert!(array.is_square());
138    /// ```
139    /// Not square:
140    /// ```
141    /// use ndarray::array;
142    /// let array = array![[1., 2., 5.], [3., 4., 6.]];
143    /// assert!(!array.is_square());
144    /// ```
145    pub fn is_square(&self) -> bool
146    {
147        let (m, n) = self.dim();
148        m == n
149    }
150}
151
152impl<S: RawData> ArrayBase<S, Ix2>
153{
154    /// Return the number of rows (length of `Axis(0)`) in the two-dimensional array.
155    ///
156    /// ```
157    /// use ndarray::{array, Axis};
158    ///
159    /// let array = array![[1., 2.],
160    ///                    [3., 4.],
161    ///                    [5., 6.]];
162    /// assert_eq!(array.nrows(), 3);
163    ///
164    /// // equivalent ways of getting the dimensions
165    /// // get nrows, ncols by using dim:
166    /// let (m, n) = array.dim();
167    /// assert_eq!(m, array.nrows());
168    /// // get length of any particular axis with .len_of()
169    /// assert_eq!(m, array.len_of(Axis(0)));
170    /// ```
171    pub fn nrows(&self) -> usize
172    {
173        self.as_layout_ref().nrows()
174    }
175
176    /// Return the number of columns (length of `Axis(1)`) in the two-dimensional array.
177    ///
178    /// ```
179    /// use ndarray::{array, Axis};
180    ///
181    /// let array = array![[1., 2.],
182    ///                    [3., 4.],
183    ///                    [5., 6.]];
184    /// assert_eq!(array.ncols(), 2);
185    ///
186    /// // equivalent ways of getting the dimensions
187    /// // get nrows, ncols by using dim:
188    /// let (m, n) = array.dim();
189    /// assert_eq!(n, array.ncols());
190    /// // get length of any particular axis with .len_of()
191    /// assert_eq!(n, array.len_of(Axis(1)));
192    /// ```
193    pub fn ncols(&self) -> usize
194    {
195        self.as_layout_ref().ncols()
196    }
197
198    /// Return true if the array is square, false otherwise.
199    ///
200    /// # Examples
201    /// Square:
202    /// ```
203    /// use ndarray::array;
204    /// let array = array![[1., 2.], [3., 4.]];
205    /// assert!(array.is_square());
206    /// ```
207    /// Not square:
208    /// ```
209    /// use ndarray::array;
210    /// let array = array![[1., 2., 5.], [3., 4., 6.]];
211    /// assert!(!array.is_square());
212    /// ```
213    pub fn is_square(&self) -> bool
214    {
215        self.as_layout_ref().is_square()
216    }
217}