Skip to content

Commit 288f131

Browse files
authored
Merge pull request #956 from rust-ndarray/axis-axes-cleanup
Elaborate doc for Axis and fix an example for Axes
2 parents 1f65811 + 7be7fd5 commit 288f131

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed

src/dimension/axes.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{Axis, Dimension, Ix, Ixs};
22

33
/// Create a new Axes iterator
4-
pub fn axes_of<'a, D>(d: &'a D, strides: &'a D) -> Axes<'a, D>
4+
pub(crate) fn axes_of<'a, D>(d: &'a D, strides: &'a D) -> Axes<'a, D>
55
where
66
D: Dimension,
77
{
@@ -15,9 +15,10 @@ where
1515

1616
/// An iterator over the length and stride of each axis of an array.
1717
///
18-
/// See [`.axes()`](../struct.ArrayBase.html#method.axes) for more information.
18+
/// This iterator is created from the array method
19+
/// [`.axes()`](crate::ArrayBase::axes).
1920
///
20-
/// Iterator element type is `AxisDescription`.
21+
/// Iterator element type is [`AxisDescription`].
2122
///
2223
/// # Examples
2324
///
@@ -27,10 +28,14 @@ where
2728
///
2829
/// let a = Array3::<f32>::zeros((3, 5, 4));
2930
///
31+
/// // find the largest axis in the array
32+
/// // check the axis index and its length
33+
///
3034
/// let largest_axis = a.axes()
31-
/// .max_by_key(|ax| ax.len())
32-
/// .unwrap().axis();
33-
/// assert_eq!(largest_axis, Axis(1));
35+
/// .max_by_key(|ax| ax.len)
36+
/// .unwrap();
37+
/// assert_eq!(largest_axis.axis, Axis(1));
38+
/// assert_eq!(largest_axis.len, 5);
3439
/// ```
3540
#[derive(Debug)]
3641
pub struct Axes<'a, D> {

src/dimension/axis.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,21 @@
88

99
/// An axis index.
1010
///
11-
/// An axis one of an array’s “dimensions”; an *n*-dimensional array has *n* axes.
12-
/// Axis *0* is the array’s outermost axis and *n*-1 is the innermost.
11+
/// An axis one of an array’s “dimensions”; an *n*-dimensional array has *n*
12+
/// axes. Axis *0* is the array’s outermost axis and *n*-1 is the innermost.
1313
///
1414
/// All array axis arguments use this type to make the code easier to write
1515
/// correctly and easier to understand.
16+
///
17+
/// For example: in a method like `index_axis(axis, index)` the code becomes
18+
/// self-explanatory when it's called like `.index_axis(Axis(1), i)`; it's
19+
/// evident which integer is the axis number and which is the index.
20+
///
21+
/// Note: This type does **not** implement From/Into usize and similar trait
22+
/// based conversions, because we want to preserve code readability and quality.
23+
///
24+
/// `Axis(1)` in itself is a very clear code style and the style that should be
25+
/// avoided is code like `1.into()`.
1626
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
1727
pub struct Axis(pub usize);
1828

src/dimension/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ use crate::slice::SliceArg;
1111
use crate::{Ix, Ixs, Slice, SliceInfoElem};
1212
use num_integer::div_floor;
1313

14-
pub use self::axes::{axes_of, Axes, AxisDescription};
14+
pub use self::axes::{Axes, AxisDescription};
15+
pub(crate) use self::axes::axes_of;
1516
pub use self::axis::Axis;
1617
pub use self::broadcast::DimMax;
1718
pub use self::conversion::IntoDimension;

src/impl_2d.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ where
118118
/// assert!(!array.is_square());
119119
/// ```
120120
pub fn is_square(&self) -> bool {
121-
self.nrows() == self.ncols()
121+
let (m, n) = self.dim();
122+
m == n
122123
}
123124
}

0 commit comments

Comments
 (0)