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
15 S: RawData<Elem = A>,
16{
17 /// Return an array view of row `index`.
18 ///
19 /// **Panics** if `index` is out of bounds.
20 ///
21 /// ```
22 /// use ndarray::array;
23 /// let array = array![[1., 2.], [3., 4.]];
24 /// assert_eq!(array.row(0), array![1., 2.]);
25 /// ```
26 pub fn row(&self, index: Ix) -> ArrayView1<'_, A>
27 where
28 S: Data,
29 {
30 self.index_axis(Axis(0), index)
31 }
32
33 /// Return a mutable array view of row `index`.
34 ///
35 /// **Panics** if `index` is out of bounds.
36 ///
37 /// ```
38 /// use ndarray::array;
39 /// let mut array = array![[1., 2.], [3., 4.]];
40 /// array.row_mut(0)[1] = 5.;
41 /// assert_eq!(array, array![[1., 5.], [3., 4.]]);
42 /// ```
43 pub fn row_mut(&mut self, index: Ix) -> ArrayViewMut1<'_, A>
44 where
45 S: DataMut,
46 {
47 self.index_axis_mut(Axis(0), index)
48 }
49
50 /// Return the number of rows (length of `Axis(0)`) in the two-dimensional array.
51 ///
52 /// ```
53 /// use ndarray::array;
54 /// let array = array![[1., 2.], [3., 4.]];
55 /// assert_eq!(array.nrows(), 2usize);
56 /// ```
57 pub fn nrows(&self) -> usize {
58 self.len_of(Axis(0))
59 }
60
61 /// Return an array view of column `index`.
62 ///
63 /// **Panics** if `index` is out of bounds.
64 ///
65 /// ```
66 /// use ndarray::array;
67 /// let array = array![[1., 2.], [3., 4.]];
68 /// assert_eq!(array.column(0), array![1., 3.]);
69 /// ```
70 pub fn column(&self, index: Ix) -> ArrayView1<'_, A>
71 where
72 S: Data,
73 {
74 self.index_axis(Axis(1), index)
75 }
76
77 /// Return a mutable array view of column `index`.
78 ///
79 /// **Panics** if `index` is out of bounds.
80 ///
81 /// ```
82 /// use ndarray::array;
83 /// let mut array = array![[1., 2.], [3., 4.]];
84 /// array.column_mut(0)[1] = 5.;
85 /// assert_eq!(array, array![[1., 2.], [5., 4.]]);
86 /// ```
87 pub fn column_mut(&mut self, index: Ix) -> ArrayViewMut1<'_, A>
88 where
89 S: DataMut,
90 {
91 self.index_axis_mut(Axis(1), index)
92 }
93
94 /// Return the number of columns (length of `Axis(1)`) in the two-dimensional array.
95 ///
96 /// ```
97 /// use ndarray::array;
98 /// let array = array![[1., 2.], [3., 4.]];
99 /// assert_eq!(array.ncols(), 2usize);
100 /// ```
101 pub fn ncols(&self) -> usize {
102 self.len_of(Axis(1))
103 }
104
105 /// Return true if the array is square, false otherwise.
106 ///
107 /// # Examples
108 /// Sqaure:
109 /// ```
110 /// use ndarray::array;
111 /// let array = array![[1., 2.], [3., 4.]];
112 /// assert!(array.is_square());
113 /// ```
114 /// Not sqaure:
115 /// ```
116 /// use ndarray::array;
117 /// let array = array![[1., 2., 5.], [3., 4., 6.]];
118 /// assert!(!array.is_square());
119 /// ```
120 pub fn is_square(&self) -> bool {
121 let (m, n) = self.dim();
122 m == n
123 }
124}