Skip to content

Commit e56ca71

Browse files
authored
Merge pull request #701 from rust-ndarray/rows-cols
Add .nrows(), .ncols() and old names .rows(), .cols() are now deprecated
2 parents e4d2711 + bf14ff7 commit e56ca71

File tree

9 files changed

+35
-23
lines changed

9 files changed

+35
-23
lines changed

examples/bounds_check_elim.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ pub fn test1d_while(a: &Array1<f64>) -> f64 {
8888
#[no_mangle]
8989
pub fn test2d_ranges(a: &Array2<f64>) -> f64 {
9090
let mut sum = 0.;
91-
for i in 0..a.rows() {
92-
for j in 0..a.cols() {
91+
for i in 0..a.nrows() {
92+
for j in 0..a.ncols() {
9393
sum += a[[i, j]];
9494
}
9595
}
@@ -100,9 +100,9 @@ pub fn test2d_ranges(a: &Array2<f64>) -> f64 {
100100
pub fn test2d_whiles(a: &Array2<f64>) -> f64 {
101101
let mut sum = 0.;
102102
let mut i = 0;
103-
while i < a.rows() {
103+
while i < a.nrows() {
104104
let mut j = 0;
105-
while j < a.cols() {
105+
while j < a.ncols() {
106106
sum += a[[i, j]];
107107
j += 1;
108108
}

examples/life.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ fn iterate(z: &mut Board, scratch: &mut Board) {
5858
}
5959

6060
fn turn_on_corners(z: &mut Board) {
61-
let n = z.rows();
62-
let m = z.cols();
61+
let n = z.nrows();
62+
let m = z.ncols();
6363
z[[1, 1]] = 1;
6464
z[[1, m - 2]] = 1;
6565
z[[n - 2, 1]] = 1;

src/doc/ndarray_for_numpy_users/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -553,8 +553,8 @@
553553
//!
554554
//! NumPy | `ndarray` | Notes
555555
//! ------|-----------|------
556-
//! `len(a)` or `a.shape[0]` | [`a.rows()`][.rows()] | get the number of rows in a 2-D array
557-
//! `a.shape[1]` | [`a.cols()`][.cols()] | get the number of columns in a 2-D array
556+
//! `len(a)` or `a.shape[0]` | [`a.nrows()`][.nrows()] | get the number of rows in a 2-D array
557+
//! `a.shape[1]` | [`a.ncols()`][.ncols()] | get the number of columns in a 2-D array
558558
//! `a[1]` or `a[1,:]` | [`a.row(1)`][.row()] or [`a.row_mut(1)`][.row_mut()] | view (or mutable view) of row 1 in a 2-D array
559559
//! `a[:,4]` | [`a.column(4)`][.column()] or [`a.column_mut(4)`][.column_mut()] | view (or mutable view) of column 4 in a 2-D array
560560
//! `a.shape[0] == a.shape[1]` | [`a.is_square()`][.is_square()] | check if the array is square
@@ -571,7 +571,7 @@
571571
//! [.assign()]: ../../struct.ArrayBase.html#method.assign
572572
//! [.axis_iter()]: ../../struct.ArrayBase.html#method.axis_iter
573573
//! [azip!]: ../../macro.azip.html
574-
//! [.cols()]: ../../struct.ArrayBase.html#method.cols
574+
//! [.ncols()]: ../../struct.ArrayBase.html#method.ncols
575575
//! [.column()]: ../../struct.ArrayBase.html#method.column
576576
//! [.column_mut()]: ../../struct.ArrayBase.html#method.column_mut
577577
//! [CowArray]: ../../type.CowArray.html
@@ -615,7 +615,7 @@
615615
//! [.reversed_axes()]: ../../struct.ArrayBase.html#method.reversed_axes
616616
//! [.row()]: ../../struct.ArrayBase.html#method.row
617617
//! [.row_mut()]: ../../struct.ArrayBase.html#method.row_mut
618-
//! [.rows()]: ../../struct.ArrayBase.html#method.rows
618+
//! [.nrows()]: ../../struct.ArrayBase.html#method.nrows
619619
//! [s!]: ../../macro.s.html
620620
//! [.sum()]: ../../struct.ArrayBase.html#method.sum
621621
//! [.slice()]: ../../struct.ArrayBase.html#method.slice

src/impl_2d.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,16 @@ where
3535
}
3636

3737
/// Return the number of rows (length of `Axis(0)`) in the two-dimensional array.
38-
pub fn rows(&self) -> usize {
38+
pub fn nrows(&self) -> usize {
3939
self.len_of(Axis(0))
4040
}
4141

42+
/// Return the number of rows (length of `Axis(0)`) in the two-dimensional array.
43+
#[deprecated(note = "Renamed to .nrows(), please use the new name")]
44+
pub fn rows(&self) -> usize {
45+
self.nrows()
46+
}
47+
4248
/// Return an array view of column `index`.
4349
///
4450
/// **Panics** if `index` is out of bounds.
@@ -60,12 +66,18 @@ where
6066
}
6167

6268
/// Return the number of columns (length of `Axis(1)`) in the two-dimensional array.
63-
pub fn cols(&self) -> usize {
69+
pub fn ncols(&self) -> usize {
6470
self.len_of(Axis(1))
6571
}
6672

73+
/// Return the number of columns (length of `Axis(1)`) in the two-dimensional array.
74+
#[deprecated(note = "Renamed to .ncols(), please use the new name")]
75+
pub fn cols(&self) -> usize {
76+
self.ncols()
77+
}
78+
6779
/// Return true if the array is square, false otherwise.
6880
pub fn is_square(&self) -> bool {
69-
self.rows() == self.cols()
81+
self.nrows() == self.ncols()
7082
}
7183
}

src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -404,12 +404,12 @@ pub type Ixs = isize;
404404
///
405405
/// // 2. Use Zip to pair each row in 2D `a` with elements in 1D `b`
406406
/// use ndarray::Zip;
407-
/// let mut b = Array::zeros(a.rows());
407+
/// let mut b = Array::zeros(a.nrows());
408408
///
409409
/// Zip::from(a.genrows())
410410
/// .and(&mut b)
411411
/// .apply(|a_row, b_elt| {
412-
/// *b_elt = a_row[a.cols() - 1] - a_row[0];
412+
/// *b_elt = a_row[a.ncols() - 1] - a_row[0];
413413
/// });
414414
/// ```
415415
///

src/zip/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ impl<'a, A, D: Dimension> NdProducer for ArrayViewMut<'a, A, D> {
448448
///
449449
/// use ndarray::{Array1, Axis};
450450
///
451-
/// let mut totals = Array1::zeros(a.rows());
451+
/// let mut totals = Array1::zeros(a.nrows());
452452
///
453453
/// Zip::from(&mut totals)
454454
/// .and(a.genrows())

src/zip/zipmacro.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
/// //
103103
/// // The row is an array view; use the 'ref' rule on the row, to avoid the
104104
/// // default which is to dereference the produced item.
105-
/// let mut totals = Array1::zeros(a.rows());
105+
/// let mut totals = Array1::zeros(a.nrows());
106106
///
107107
/// azip!(mut totals, ref row (a.genrows()) in {
108108
/// *totals = row.sum();

tests/array.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1852,8 +1852,8 @@ fn test_swap() {
18521852
let mut a = arr2(&[[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
18531853
let b = a.clone();
18541854

1855-
for i in 0..a.rows() {
1856-
for j in i + 1..a.cols() {
1855+
for i in 0..a.nrows() {
1856+
for j in i + 1..a.ncols() {
18571857
a.swap((i, j), (j, i));
18581858
}
18591859
}
@@ -1865,8 +1865,8 @@ fn test_uswap() {
18651865
let mut a = arr2(&[[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
18661866
let b = a.clone();
18671867

1868-
for i in 0..a.rows() {
1869-
for j in i + 1..a.cols() {
1868+
for i in 0..a.nrows() {
1869+
for j in i + 1..a.ncols() {
18701870
unsafe { a.uswap((i, j), (j, i)) };
18711871
}
18721872
}

tests/zst.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ fn test_swap() {
77

88
let b = a.clone();
99

10-
for i in 0..a.rows() {
11-
for j in i + 1..a.cols() {
10+
for i in 0..a.nrows() {
11+
for j in i + 1..a.ncols() {
1212
a.swap((i, j), (j, i));
1313
}
1414
}

0 commit comments

Comments
 (0)