Skip to content

Commit ebda894

Browse files
committed
DOC: Use box drawing art for illustrating an example for ArrayView::split_at
This is a proof of concept. Box drawings are relatively easy to produce, easy to distribute and maintain in all places rustdoc renders, readable in plain text and when rendered, and they have a consistent style. The biggest positive is that it enables us to produce many illustrations. They have the drawback that they restrict us to mostly 2-D (but that makes for easier to understand illustrations anyway), and it doesn't look very impressive.
1 parent ea82bf4 commit ebda894

File tree

1 file changed

+66
-3
lines changed

1 file changed

+66
-3
lines changed

src/impl_views.rs

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,74 @@ where
123123
/// split and one view after the split.
124124
///
125125
/// **Panics** if `axis` or `index` is out of bounds.
126+
///
127+
/// **Examples:**
128+
/// ```rust
129+
/// # use ndarray::prelude::*;
130+
/// let a = array![[0, 1, 2, 3],
131+
/// [4, 5, 6, 7],
132+
/// [8, 9, 0, 1]];
126133
///
127-
/// Below, an illustration of `.split_at(Axis(2), 2)` on
128-
/// an array with shape 3 × 5 × 5.
134+
/// ```
135+
/// The array `a` has two axes and shape 3 × 4:
136+
/// ```text
137+
/// ─> Axis(1)
138+
/// ┌───┬───┬───┬───┐ 0
139+
/// │ │ 0 │ 1 │ 2 │ 3 │
140+
/// v ├───┼───┼───┼───┤ 1
141+
/// Axis(0)│ 4 │ 5 │ 6 │ 7 │
142+
/// ├───┼───┼───┼───┤ 2
143+
/// │ 8 │ 9 │ 0 │ 1 │
144+
/// └───┴───┴───┴───┘ 3 ↑
145+
/// 0 1 2 3 4 ← possible split_at indices.
146+
/// ```
147+
///
148+
/// Row indices increase along `Axis(0)`, and column indices increase along
149+
/// `Axis(1)`. Note that we split “before” an element index, and that
150+
/// both 0 and the endpoint are valid split indices.
151+
///
152+
/// **Example 1**: Split `a` along the first axis, in this case the rows, at
153+
/// index 1.<br>
154+
/// This produces views v1 and v2 of shapes 1 × 4 and 2 × 4:
155+
///
156+
/// ```rust
157+
/// # use ndarray::prelude::*;
158+
/// # let a = Array::from_elem((3, 3), 0);
159+
/// let (v1, v2) = a.view().split_at(Axis(0), 1);
160+
/// ```
161+
/// ```text
162+
/// ┌───┬───┬───┬───┐ 0 ↓ indices
163+
/// │ │ 0 │ 1 │ 2 │ 3 │ v1
164+
/// v └───┴───┴───┴───┘ 1
165+
/// Axis(0)┌───┬───┬───┬───┐
166+
/// │ 4 │ 5 │ 6 │ 7 │
167+
/// ├───┼───┼───┼───┤ v2 2
168+
/// │ 8 │ 9 │ 0 │ 1 │
169+
/// └───┴───┴───┴───┘ 3
170+
/// ```
171+
///
172+
/// **Example 2**: Split `a` along the second axis, in this case the
173+
/// columns, at index 2.<br>
174+
/// This produces views u1 and u2 of shapes 3 × 2 and 3 × 2:
175+
///
176+
/// ```rust
177+
/// # use ndarray::prelude::*;
178+
/// # let a = Array::from_elem((3, 3), 0);
179+
/// let (u1, u2) = a.view().split_at(Axis(1), 2);
129180
///
130-
/// <img src="https://rust-ndarray.github.io/ndarray/images/split_at.svg" width="300px" height="271px">
181+
/// ```
182+
/// ```text
183+
/// u1 u2
184+
/// 0 1 2 3 4 indices →
185+
/// ┌───┬───┐ ┌───┬───┐
186+
/// │ 0 │ 1 │ │ 2 │ 3 │
187+
/// ├───┼───┤ ├───┼───┤
188+
/// │ 4 │ 5 │ │ 6 │ 7 │
189+
/// ├───┼───┤ ├───┼───┤
190+
/// │ 8 │ 9 │ │ 0 │ 1 │
191+
/// └───┴───┘ └───┴───┘
192+
/// ─> Axis(1)
193+
/// ```
131194
pub fn split_at(self, axis: Axis, index: Ix) -> (Self, Self) {
132195
unsafe {
133196
let (left, right) = self.into_raw_view().split_at(axis, index);

0 commit comments

Comments
 (0)