Skip to content

Commit 6559364

Browse files
committed
DOC: Add example for IxDyn / ArrayD
1 parent 0744748 commit 6559364

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/aliases.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,32 @@ pub type Ix5 = Dim<[Ix; 5]>;
5353
/// six-dimensional
5454
pub type Ix6 = Dim<[Ix; 6]>;
5555
/// dynamic-dimensional
56+
///
57+
/// `Vec<Ix>` and `&[usize]` implement `IntoDimension` to produce `IxDyn`;
58+
/// use them to create arrays with a dynamic number of axes.
59+
///
60+
/// ```
61+
/// use ndarray::ArrayD;
62+
///
63+
/// // Create a 5 × 6 × 3 × 4 array using the dynamic dimension type
64+
/// let mut a = ArrayD::<f64>::zeros(vec![5, 6, 3, 4]);
65+
/// // Create a 1 × 3 × 4 array using the dynamic dimension type
66+
/// let mut b = ArrayD::<f64>::zeros(vec![1, 3, 4]);
67+
///
68+
/// // We can use broadcasting to add arrays of compatible shapes together:
69+
/// a += &b;
70+
///
71+
/// // We can index into a, b using fixed size arrays:
72+
/// a[[0, 0, 0, 0]] = 0.;
73+
/// b[[0, 2, 3]] = a[[0, 0, 2, 3]];
74+
///
75+
/// // Note: It will panic at runtime if the number of indices given does
76+
/// // not match the array.
77+
///
78+
/// // We can keep them in the same vector because both the arrays have
79+
/// // the same type `Array<f64, IxDyn>` a.k.a `ArrayD<f64>`:
80+
/// let arrays = vec![a, b];
81+
/// ```
5682
pub type IxDyn = Dim<Vec<Ix>>;
5783

5884
/// zero-dimensional array

0 commit comments

Comments
 (0)