From a241240762784f1fc484c06367b884101c809d30 Mon Sep 17 00:00:00 2001 From: bluss Date: Sat, 27 Mar 2021 13:11:20 +0100 Subject: [PATCH 1/3] FIX: Simplify .is_square() slightly --- src/impl_2d.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/impl_2d.rs b/src/impl_2d.rs index 64d4860cf..6e2cfa2c7 100644 --- a/src/impl_2d.rs +++ b/src/impl_2d.rs @@ -118,6 +118,7 @@ where /// assert!(!array.is_square()); /// ``` pub fn is_square(&self) -> bool { - self.nrows() == self.ncols() + let (m, n) = self.dim(); + m == n } } From be13f3d2b5f9cac5d590dfe66397027a74524a2f Mon Sep 17 00:00:00 2001 From: bluss Date: Sat, 27 Mar 2021 13:11:20 +0100 Subject: [PATCH 2/3] FIX: Update Axes example, clarify docs and adjust reexports Update the example to use fields instead of method calls. (The deprecation warnings in examples are never visible to us.) Use pub(crate) more correctly here, for the items that are not pub. --- src/dimension/axes.rs | 17 +++++++++++------ src/dimension/mod.rs | 3 ++- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/dimension/axes.rs b/src/dimension/axes.rs index a678e78ca..d6f1c7280 100644 --- a/src/dimension/axes.rs +++ b/src/dimension/axes.rs @@ -1,7 +1,7 @@ use crate::{Axis, Dimension, Ix, Ixs}; /// Create a new Axes iterator -pub fn axes_of<'a, D>(d: &'a D, strides: &'a D) -> Axes<'a, D> +pub(crate) fn axes_of<'a, D>(d: &'a D, strides: &'a D) -> Axes<'a, D> where D: Dimension, { @@ -15,9 +15,10 @@ where /// An iterator over the length and stride of each axis of an array. /// -/// See [`.axes()`](../struct.ArrayBase.html#method.axes) for more information. +/// This iterator is created from the array method +/// [`.axes()`](crate::ArrayBase::axes). /// -/// Iterator element type is `AxisDescription`. +/// Iterator element type is [`AxisDescription`]. /// /// # Examples /// @@ -27,10 +28,14 @@ where /// /// let a = Array3::::zeros((3, 5, 4)); /// +/// // find the largest axis in the array +/// // check the axis index and its length +/// /// let largest_axis = a.axes() -/// .max_by_key(|ax| ax.len()) -/// .unwrap().axis(); -/// assert_eq!(largest_axis, Axis(1)); +/// .max_by_key(|ax| ax.len) +/// .unwrap(); +/// assert_eq!(largest_axis.axis, Axis(1)); +/// assert_eq!(largest_axis.len, 5); /// ``` #[derive(Debug)] pub struct Axes<'a, D> { diff --git a/src/dimension/mod.rs b/src/dimension/mod.rs index fb062513e..f4f46e764 100644 --- a/src/dimension/mod.rs +++ b/src/dimension/mod.rs @@ -11,7 +11,8 @@ use crate::slice::SliceArg; use crate::{Ix, Ixs, Slice, SliceInfoElem}; use num_integer::div_floor; -pub use self::axes::{axes_of, Axes, AxisDescription}; +pub use self::axes::{Axes, AxisDescription}; +pub(crate) use self::axes::axes_of; pub use self::axis::Axis; pub use self::broadcast::DimMax; pub use self::conversion::IntoDimension; From 7be7fd586ef36226040b9d3d544106c068619097 Mon Sep 17 00:00:00 2001 From: bluss Date: Sat, 27 Mar 2021 13:30:03 +0100 Subject: [PATCH 3/3] DOC: Add comment on Axis about why we don't have conversion traits --- src/dimension/axis.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/dimension/axis.rs b/src/dimension/axis.rs index 42a1ee12c..f4625d2da 100644 --- a/src/dimension/axis.rs +++ b/src/dimension/axis.rs @@ -8,11 +8,21 @@ /// An axis index. /// -/// An axis one of an array’s “dimensions”; an *n*-dimensional array has *n* axes. -/// Axis *0* is the array’s outermost axis and *n*-1 is the innermost. +/// An axis one of an array’s “dimensions”; an *n*-dimensional array has *n* +/// axes. Axis *0* is the array’s outermost axis and *n*-1 is the innermost. /// /// All array axis arguments use this type to make the code easier to write /// correctly and easier to understand. +/// +/// For example: in a method like `index_axis(axis, index)` the code becomes +/// self-explanatory when it's called like `.index_axis(Axis(1), i)`; it's +/// evident which integer is the axis number and which is the index. +/// +/// Note: This type does **not** implement From/Into usize and similar trait +/// based conversions, because we want to preserve code readability and quality. +/// +/// `Axis(1)` in itself is a very clear code style and the style that should be +/// avoided is code like `1.into()`. #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] pub struct Axis(pub usize);