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, Axis};
54 ///
55 /// let array = array![[1., 2.],
56 /// [3., 4.],
57 /// [5., 6.]];
58 /// assert_eq!(array.nrows(), 3);
59 ///
60 /// // equivalent ways of getting the dimensions
61 /// // get nrows, ncols by using dim:
62 /// let (m, n) = array.dim();
63 /// assert_eq!(m, array.nrows());
64 /// // get length of any particular axis with .len_of()
65 /// assert_eq!(m, array.len_of(Axis(0)));
66 /// ```
67 pub fn nrows(&self) -> usize {
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 pub fn column(&self, index: Ix) -> ArrayView1<'_, A>
81 where
82 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 pub fn column_mut(&mut self, index: Ix) -> ArrayViewMut1<'_, A>
98 where
99 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 self.len_of(Axis(1))
123 }
124
125 /// Return true if the array is square, false otherwise.
126 ///
127 /// # Examples
128 /// Square:
129 /// ```
130 /// use ndarray::array;
131 /// let array = array![[1., 2.], [3., 4.]];
132 /// assert!(array.is_square());
133 /// ```
134 /// Not square:
135 /// ```
136 /// use ndarray::array;
137 /// let array = array![[1., 2., 5.], [3., 4., 6.]];
138 /// assert!(!array.is_square());
139 /// ```
140 pub fn is_square(&self) -> bool {
141 let (m, n) = self.dim();
142 m == n
143 }
144}