Skip to content

Commit 617769c

Browse files
committed
Improve docs related to subviews
1 parent b308eb8 commit 617769c

File tree

3 files changed

+40
-34
lines changed

3 files changed

+40
-34
lines changed

src/impl_methods.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -542,8 +542,8 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
542542
}
543543
}
544544

545-
/// Along `axis`, select the subview `index` and return a
546-
/// view with that axis removed.
545+
/// Returns a view restricted to `index` along the axis, with the axis
546+
/// removed.
547547
///
548548
/// See [*Subviews*](#subviews) for full documentation.
549549
///
@@ -570,8 +570,8 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
570570
self.view().index_axis_move(axis, index)
571571
}
572572

573-
/// Along `axis`, select the subview `index` and return a read-write view
574-
/// with the axis removed.
573+
/// Returns a mutable view restricted to `index` along the axis, with the
574+
/// axis removed.
575575
///
576576
/// **Panics** if `axis` or `index` is out of bounds.
577577
///
@@ -602,10 +602,11 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
602602
self.view_mut().index_axis_move(axis, index)
603603
}
604604

605-
/// Along `axis`, select the subview `index` and return `self`
606-
/// with that axis removed.
605+
/// Collapses the array to `index` along the axis and removes the axis.
607606
///
608607
/// See [`.index_axis()`](#method.index_axis) and [*Subviews*](#subviews) for full documentation.
608+
///
609+
/// **Panics** if `axis` or `index` is out of bounds.
609610
pub fn index_axis_move(mut self, axis: Axis, index: usize) -> ArrayBase<S, D::Smaller>
610611
where
611612
D: RemoveAxis,
@@ -614,10 +615,9 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
614615
self.remove_axis(axis)
615616
}
616617

617-
/// Collapse the axis into length one, selecting the subview at the given
618-
/// `index` along the axis.
618+
/// Selects `index` along the axis, collapsing the axis into length one.
619619
///
620-
/// **Panics** if `index` is past the length of the axis.
620+
/// **Panics** if `axis` or `index` is out of bounds.
621621
pub fn collapse_axis(&mut self, axis: Axis, index: usize) {
622622
dimension::do_collapse_axis(
623623
&mut self.dim,

src/lib.rs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -452,10 +452,11 @@ pub type Ixs = isize;
452452
///
453453
/// [`&SliceInfo`]: struct.SliceInfo.html
454454
///
455-
/// If a range is used, the axis is preserved. If an index is used, a subview
456-
/// is taken with respect to the axis. See [*Subviews*](#subviews) for more
457-
/// information about subviews. Note that [`.slice_inplace()`] behaves like
458-
/// [`.collapse_axis()`] by preserving the number of dimensions.
455+
/// If a range is used, the axis is preserved. If an index is used, that index
456+
/// is selected and the axis is removed; this selects a subview. See
457+
/// [*Subviews*](#subviews) for more information about subviews. Note that
458+
/// [`.slice_inplace()`] behaves like [`.collapse_axis()`] by preserving the
459+
/// number of dimensions.
459460
///
460461
/// [`.slice()`]: #method.slice
461462
/// [`.slice_mut()`]: #method.slice_mut
@@ -504,7 +505,7 @@ pub type Ixs = isize;
504505
/// assert_eq!(d, e);
505506
/// assert_eq!(d.shape(), &[2, 1, 3]);
506507
///
507-
/// // Let’s create a slice while taking a subview with
508+
/// // Let’s create a slice while selecting a subview with
508509
/// //
509510
/// // - Both submatrices of the greatest dimension: `..`
510511
/// // - The last row in each submatrix, removing that axis: `-1`
@@ -520,17 +521,29 @@ pub type Ixs = isize;
520521
/// ## Subviews
521522
///
522523
/// Subview methods allow you to restrict the array view while removing one
523-
/// axis from the array. Subview methods include [`.index_axis()`],
524-
/// [`.index_axis_mut()`], [`.index_axis_move()`], and [`.collapse_axis()`]. You
525-
/// can also take a subview by using a single index instead of a range when
526-
/// slicing.
527-
///
528-
/// Subview takes two arguments: `axis` and `index`.
529-
///
524+
/// axis from the array. Methods for selecting individual subviews include
525+
/// [`.index_axis()`], [`.index_axis_mut()`], and [`.index_axis_move()`]. You
526+
/// can also select a subview by using a single index instead of a range when
527+
/// slicing. Some other methods, such as [`.fold_axis()`], [`.axis_iter()`],
528+
/// [`.axis_iter_mut()`], [`.outer_iter()`], and [`.outer_iter_mut()`] operate
529+
/// on all the subviews along an axis.
530+
///
531+
/// A related method is [`.collapse_axis()`], which modifies the view in the
532+
/// same way as [`.index_axis()`] except for removing the collapsed axis, since
533+
/// it operates *in place*. The length of the axis becomes 1.
534+
///
535+
/// Methods for selecting an individual subview take two arguments: `axis` and
536+
/// `index`.
537+
///
538+
/// [`.axis_iter()`]: #method.axis_iter
539+
/// [`.axis_iter_mut()`]: #method.axis_iter_mut
540+
/// [`.fold_axis()`]: #method.fold_axis
530541
/// [`.index_axis()`]: #method.index_axis
531542
/// [`.index_axis_mut()`]: #method.index_axis_mut
532543
/// [`.index_axis_move()`]: #method.index_axis_move
533544
/// [`.collapse_axis()`]: #method.collapse_axis
545+
/// [`.outer_iter()`]: #method.outer_iter
546+
/// [`.outer_iter_mut()`]: #method.outer_iter_mut
534547
///
535548
/// ```
536549
/// #[macro_use(s)] extern crate ndarray;
@@ -574,14 +587,6 @@ pub type Ixs = isize;
574587
/// # }
575588
/// ```
576589
///
577-
/// [`.collapse_axis()`] modifies the view in the same way as [`.index_axis()`],
578-
/// but since it is *in place*, it cannot remove the collapsed axis. It becomes
579-
/// an axis of length 1.
580-
///
581-
/// `.outer_iter()` is an iterator of every subview along the zeroth (outer)
582-
/// axis, while `.axis_iter()` is an iterator of every subview along a
583-
/// specific axis.
584-
///
585590
/// ## Arithmetic Operations
586591
///
587592
/// Arrays support all arithmetic operations the same way: they apply elementwise.

src/slice.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -465,12 +465,13 @@ impl_slicenextdim_larger!((), Slice);
465465
/// The syntax is `s![` *[ axis-slice-or-index [, axis-slice-or-index [ , ... ]
466466
/// ] ]* `]`, where *axis-slice-or-index* is any of the following:
467467
///
468-
/// * *index*: an index to use for taking a subview with respect to that axis
469-
/// * *range*: a range with step size 1 to use for slicing that axis
470-
/// * *range* `;` *step*: a range with step size *step* to use for slicing that axis
471-
/// * *slice*: a [`Slice`] instance to use for slicing that axis
468+
/// * *index*: an index to use for taking a subview with respect to that axis.
469+
/// (The index is selected and the axis is removed.)
470+
/// * *range*: a range with step size 1 to use for slicing that axis.
471+
/// * *range* `;` *step*: a range with step size *step* to use for slicing that axis.
472+
/// * *slice*: a [`Slice`] instance to use for slicing that axis.
472473
/// * *slice* `;` *step*: a range constructed from the start and end of a [`Slice`]
473-
/// instance, with new step size *step*, to use for slicing that axis
474+
/// instance, with new step size *step*, to use for slicing that axis.
474475
///
475476
/// [`Slice`]: struct.Slice.html
476477
///

0 commit comments

Comments
 (0)