@@ -83,17 +83,53 @@ where
83
83
}
84
84
85
85
/// Return the shape of the array as it stored in the array.
86
+ ///
87
+ /// This is primarily useful for passing to other `ArrayBase`
88
+ /// functions, such as when creating another array of the same
89
+ /// shape and dimensionality.
90
+ ///
91
+ /// ```
92
+ /// use ndarray::Array;
93
+ ///
94
+ /// let a = Array::from_elem((2, 3), 5.);
95
+ ///
96
+ /// // Create an array of zeros that's the same shape and dimensionality as `a`.
97
+ /// let b = Array::<f64, _>::zeros(a.raw_dim());
98
+ /// ```
86
99
pub fn raw_dim ( & self ) -> D {
87
100
self . dim . clone ( )
88
101
}
89
102
90
103
/// Return the shape of the array as a slice.
91
- pub fn shape ( & self ) -> & [ Ix ] {
104
+ ///
105
+ /// Note that you probably don't want to use this to create an array of the
106
+ /// same shape as another array because creating an array with e.g.
107
+ /// [`Array::zeros()`](ArrayBase::zeros) using a shape of type `&[usize]`
108
+ /// results in a dynamic-dimensional array. If you want to create an array
109
+ /// that has the same shape and dimensionality as another array, use
110
+ /// [`.raw_dim()`](ArrayBase::raw_dim) instead:
111
+ ///
112
+ /// ```rust
113
+ /// use ndarray::{Array, Array2};
114
+ ///
115
+ /// let a = Array2::<i32>::zeros((3, 4));
116
+ /// let shape = a.shape();
117
+ /// assert_eq!(shape, &[3, 4]);
118
+ ///
119
+ /// // Since `a.shape()` returned `&[usize]`, we get an `ArrayD` instance:
120
+ /// let b = Array::zeros(shape);
121
+ /// assert_eq!(a.clone().into_dyn(), b);
122
+ ///
123
+ /// // To get the same dimension type, use `.raw_dim()` instead:
124
+ /// let c = Array::zeros(a.raw_dim());
125
+ /// assert_eq!(a, c);
126
+ /// ```
127
+ pub fn shape ( & self ) -> & [ usize ] {
92
128
self . dim . slice ( )
93
129
}
94
130
95
- /// Return the strides of the array as a slice
96
- pub fn strides ( & self ) -> & [ Ixs ] {
131
+ /// Return the strides of the array as a slice.
132
+ pub fn strides ( & self ) -> & [ isize ] {
97
133
let s = self . strides . slice ( ) ;
98
134
// reinterpret unsigned integer as signed
99
135
unsafe {
0 commit comments