8
8
9
9
//! Constructor methods for ndarray
10
10
//!
11
- use libnum;
11
+ //!
12
+
13
+ #![ allow( deprecated) ] // from_shape_vec
14
+
15
+ use libnum:: { Zero , One , Float } ;
12
16
13
17
use imp_prelude:: * ;
18
+ use { Shape , StrideShape } ;
14
19
use dimension;
15
20
use linspace;
16
21
use error:: { self , ShapeError , ErrorKind } ;
@@ -30,7 +35,7 @@ impl<S> ArrayBase<S, Ix>
30
35
/// let array = OwnedArray::from_vec(vec![1., 2., 3., 4.]);
31
36
/// ```
32
37
pub fn from_vec ( v : Vec < S :: Elem > ) -> ArrayBase < S , Ix > {
33
- unsafe { Self :: from_vec_dim_unchecked ( v. len ( ) as Ix , v) }
38
+ unsafe { Self :: from_shape_vec_unchecked ( v. len ( ) as Ix , v) }
34
39
}
35
40
36
41
/// Create a one-dimensional array from an iterable.
@@ -58,7 +63,7 @@ impl<S> ArrayBase<S, Ix>
58
63
/// ```
59
64
pub fn linspace < F > ( start : F , end : F , n : usize ) -> ArrayBase < S , Ix >
60
65
where S : Data < Elem =F > ,
61
- F : libnum :: Float ,
66
+ F : Float ,
62
67
{
63
68
Self :: from_vec ( :: iterators:: to_vec ( linspace:: linspace ( start, end, n) ) )
64
69
}
@@ -74,7 +79,7 @@ impl<S> ArrayBase<S, Ix>
74
79
/// ```
75
80
pub fn range < F > ( start : F , end : F , step : F ) -> ArrayBase < S , Ix >
76
81
where S : Data < Elem =F > ,
77
- F : libnum :: Float ,
82
+ F : Float ,
78
83
{
79
84
Self :: from_vec ( :: iterators:: to_vec ( linspace:: range ( start, end, step) ) )
80
85
}
@@ -89,7 +94,7 @@ impl<S, A> ArrayBase<S, (Ix, Ix)>
89
94
/// **Panics** if `n * n` would overflow usize.
90
95
pub fn eye ( n : Ix ) -> ArrayBase < S , ( Ix , Ix ) >
91
96
where S : DataMut ,
92
- A : Clone + libnum :: Zero + libnum :: One ,
97
+ A : Clone + Zero + One ,
93
98
{
94
99
let mut eye = Self :: zeros ( ( n, n) ) ;
95
100
for a_ii in eye. diag_mut ( ) {
@@ -113,13 +118,12 @@ impl<S, A, D> ArrayBase<S, D>
113
118
where S : DataOwned < Elem =A > ,
114
119
D : Dimension ,
115
120
{
116
- /// Create an array with copies of `elem`, dimension `dim `.
121
+ /// Create an array with copies of `elem`, shape `shape `.
117
122
///
118
- /// **Panics** if the number of elements in `dim ` would overflow usize.
123
+ /// **Panics** if the number of elements in `shape ` would overflow usize.
119
124
///
120
125
/// ```
121
- /// use ndarray::OwnedArray;
122
- /// use ndarray::arr3;
126
+ /// use ndarray::{OwnedArray, arr3, ShapeBuilder};
123
127
///
124
128
/// let a = OwnedArray::from_elem((2, 2, 2), 1.);
125
129
///
@@ -130,89 +134,128 @@ impl<S, A, D> ArrayBase<S, D>
130
134
/// [1., 1.]]])
131
135
/// );
132
136
/// assert!(a.strides() == &[4, 2, 1]);
137
+ ///
138
+ /// let b = OwnedArray::from_elem((2, 2, 2).f(), 1.);
139
+ /// assert!(b.strides() == &[1, 2, 4]);
133
140
/// ```
134
- pub fn from_elem ( dim : D , elem : A ) -> ArrayBase < S , D >
135
- where A : Clone
141
+ pub fn from_elem < Sh > ( shape : Sh , elem : A ) -> ArrayBase < S , D >
142
+ where A : Clone ,
143
+ Sh : Into < Shape < D > > ,
136
144
{
137
145
// Note: We don't need to check the case of a size between
138
146
// isize::MAX -> usize::MAX; in this case, the vec constructor itself
139
147
// panics.
140
- let size = size_checked_unwrap ! ( dim) ;
148
+ let shape = shape. into ( ) ;
149
+ let size = size_checked_unwrap ! ( shape. dim) ;
141
150
let v = vec ! [ elem; size] ;
142
- unsafe { Self :: from_vec_dim_unchecked ( dim , v) }
151
+ unsafe { Self :: from_shape_vec_unchecked ( shape , v) }
143
152
}
144
153
145
- /// Create an array with copies of `elem`, dimension `dim` and fortran
146
- /// memory order.
147
- ///
148
- /// **Panics** if the number of elements would overflow usize.
154
+ /// Create an array with zeros, shape `shape`.
149
155
///
150
- /// ```
151
- /// use ndarray::OwnedArray;
152
- ///
153
- /// let a = OwnedArray::from_elem_f((2, 2, 2), 1.);
154
- /// assert!(a.strides() == &[1, 2, 4]);
155
- /// ```
156
- pub fn from_elem_f ( dim : D , elem : A ) -> ArrayBase < S , D >
157
- where A : Clone
156
+ /// **Panics** if the number of elements in `shape` would overflow usize.
157
+ pub fn zeros < Sh > ( shape : Sh ) -> ArrayBase < S , D >
158
+ where A : Clone + Zero ,
159
+ Sh : Into < Shape < D > > ,
158
160
{
159
- let size = size_checked_unwrap ! ( dim) ;
160
- let v = vec ! [ elem; size] ;
161
- unsafe { Self :: from_vec_dim_unchecked_f ( dim, v) }
161
+ Self :: from_elem ( shape, A :: zero ( ) )
162
162
}
163
163
164
- /// Create an array with zeros, dimension `dim`.
164
+ /// Create an array with default values, shape `shape`
165
165
///
166
- /// **Panics** if the number of elements in `dim` would overflow usize.
167
- pub fn zeros ( dim : D ) -> ArrayBase < S , D >
168
- where A : Clone + libnum:: Zero
166
+ /// **Panics** if the number of elements in `shape` would overflow usize.
167
+ pub fn default < Sh > ( shape : Sh ) -> ArrayBase < S , D >
168
+ where A : Default ,
169
+ Sh : Into < Shape < D > > ,
169
170
{
170
- Self :: from_elem ( dim, libnum:: zero ( ) )
171
+ let shape = shape. into ( ) ;
172
+ let v = ( 0 ..shape. dim . size ( ) ) . map ( |_| A :: default ( ) ) . collect ( ) ;
173
+ unsafe { Self :: from_shape_vec_unchecked ( shape, v) }
171
174
}
172
175
173
- /// Create an array with zeros, dimension `dim` and fortran memory order.
176
+ /// Create an array with the given shape from a vector. (No cloning of
177
+ /// elements needed.)
174
178
///
175
- /// **Panics** if the number of elements in `dim` would overflow usize.
176
- pub fn zeros_f ( dim : D ) -> ArrayBase < S , D >
177
- where A : Clone + libnum:: Zero
179
+ /// ----
180
+ ///
181
+ /// For a contiguous c- or f-order shape, the following applies:
182
+ ///
183
+ /// **Errors** if `shape` does not correspond to the number of elements in `v`.
184
+ ///
185
+ /// ----
186
+ ///
187
+ /// For custom strides, the following applies:
188
+ ///
189
+ /// **Errors** if strides and dimensions can point out of bounds of `v`.<br>
190
+ /// **Errors** if strides allow multiple indices to point to the same element.
191
+ pub fn from_shape_vec < Sh > ( shape : Sh , v : Vec < A > ) -> Result < ArrayBase < S , D > , ShapeError >
192
+ where Sh : Into < StrideShape < D > > ,
178
193
{
179
- Self :: from_elem_f ( dim, libnum:: zero ( ) )
194
+ // eliminate the type parameter Sh as soon as possible
195
+ Self :: from_shape_vec_impl ( shape. into ( ) , v)
180
196
}
181
197
182
- /// Create an array with default values, dimension `dim`.
183
- ///
184
- /// **Panics** if the number of elements in `dim` would overflow usize.
185
- pub fn default ( dim : D ) -> ArrayBase < S , D >
186
- where A : Default
198
+ fn from_shape_vec_impl ( shape : StrideShape < D > , v : Vec < A > ) -> Result < ArrayBase < S , D > , ShapeError >
187
199
{
188
- let v = ( 0 ..dim. size ( ) ) . map ( |_| A :: default ( ) ) . collect ( ) ;
189
- unsafe { Self :: from_vec_dim_unchecked ( dim, v) }
200
+ if shape. custom {
201
+ Self :: from_vec_dim_stride ( shape. dim , shape. strides , v)
202
+ } else {
203
+ let dim = shape. dim ;
204
+ let strides = shape. strides ;
205
+ if dim. size_checked ( ) != Some ( v. len ( ) ) {
206
+ return Err ( error:: incompatible_shapes ( & v. len ( ) , & dim) ) ;
207
+ }
208
+ unsafe { Ok ( Self :: from_vec_dim_stride_unchecked ( dim, strides, v) ) }
209
+ }
190
210
}
191
211
192
- /// Create an array from a vector (no copying needed).
212
+ /// Create an array from a vector and interpret it according to the
213
+ /// provided dimensions and strides. (No cloning of elements needed.)
193
214
///
194
- /// **Errors** if `dim` does not correspond to the number of elements in `v`.
215
+ /// Unsafe because dimension and strides are unchecked.
216
+ pub unsafe fn from_shape_vec_unchecked < Sh > ( shape : Sh , v : Vec < A > ) -> ArrayBase < S , D >
217
+ where Sh : Into < StrideShape < D > > ,
218
+ {
219
+ let shape = shape. into ( ) ;
220
+ Self :: from_vec_dim_stride_unchecked ( shape. dim , shape. strides , v)
221
+ }
222
+
223
+ #[ cfg_attr( has_deprecated, deprecated( note="Use from_elem instead." ) ) ]
224
+ /// ***Deprecated: Use from_elem instead***
225
+ pub fn from_elem_f ( dim : D , elem : A ) -> ArrayBase < S , D >
226
+ where A : Clone
227
+ {
228
+ Self :: from_elem ( dim. f ( ) , elem)
229
+ }
230
+
231
+ #[ cfg_attr( has_deprecated, deprecated( note="Use zeros instead." ) ) ]
232
+ /// ***Deprecated: Use zeros instead***
233
+ pub fn zeros_f ( dim : D ) -> ArrayBase < S , D >
234
+ where A : Clone + Zero
235
+ {
236
+ Self :: from_elem_f ( dim, A :: zero ( ) )
237
+ }
238
+
239
+ #[ cfg_attr( has_deprecated, deprecated( note="Use from_shape_vec instead." ) ) ]
240
+ /// ***Deprecated: Use from_shape_vec instead***
195
241
pub fn from_vec_dim ( dim : D , v : Vec < A > ) -> Result < ArrayBase < S , D > , ShapeError > {
196
242
if dim. size_checked ( ) != Some ( v. len ( ) ) {
197
243
return Err ( error:: incompatible_shapes ( & v. len ( ) , & dim) ) ;
198
244
}
199
245
unsafe { Ok ( Self :: from_vec_dim_unchecked ( dim, v) ) }
200
246
}
201
247
202
- /// Create an array from a vector (no copying needed) using fortran
203
- /// memory order to interpret the data.
204
- ///
205
- /// **Errors** if `dim` does not correspond to the number of elements in `v`.
248
+ #[ cfg_attr( has_deprecated, deprecated( note="Use from_shape_vec instead." ) ) ]
249
+ /// ***Deprecated: Use from_shape_vec instead***
206
250
pub fn from_vec_dim_f ( dim : D , v : Vec < A > ) -> Result < ArrayBase < S , D > , ShapeError > {
207
251
if dim. size_checked ( ) != Some ( v. len ( ) ) {
208
252
return Err ( error:: incompatible_shapes ( & v. len ( ) , & dim) ) ;
209
253
}
210
254
unsafe { Ok ( Self :: from_vec_dim_unchecked_f ( dim, v) ) }
211
255
}
212
256
213
- /// Create an array from a vector (no copying needed).
214
- ///
215
- /// Unsafe because dimension is unchecked, and must be correct.
257
+ #[ cfg_attr( has_deprecated, deprecated( note="Use from_shape_vec_unchecked instead." ) ) ]
258
+ /// ***Deprecated: Use from_shape_vec_unchecked instead***
216
259
pub unsafe fn from_vec_dim_unchecked ( dim : D , mut v : Vec < A > ) -> ArrayBase < S , D > {
217
260
debug_assert ! ( dim. size_checked( ) == Some ( v. len( ) ) ) ;
218
261
ArrayBase {
@@ -223,24 +266,16 @@ impl<S, A, D> ArrayBase<S, D>
223
266
}
224
267
}
225
268
226
- /// Create an array from a vector (with no copying needed),
227
- /// using fortran memory order to interpret the data.
228
- ///
229
- /// Unsafe because dimension is unchecked, and must be correct.
269
+ #[ cfg_attr( has_deprecated, deprecated( note="Use from_shape_vec_unchecked instead." ) ) ]
270
+ /// ***Deprecated: Use from_shape_vec_unchecked instead***
230
271
pub unsafe fn from_vec_dim_unchecked_f ( dim : D , v : Vec < A > ) -> ArrayBase < S , D > {
231
272
debug_assert ! ( dim. size_checked( ) == Some ( v. len( ) ) ) ;
232
273
let strides = dim. fortran_strides ( ) ;
233
274
Self :: from_vec_dim_stride_unchecked ( dim, strides, v)
234
275
}
235
276
236
- /// Create an array from a vector and interpret it according to the
237
- /// provided dimensions and strides. No allocation needed.
238
- ///
239
- /// Checks whether `dim` and `strides` are compatible with the vector's
240
- /// length, returning an `Err` if not compatible.
241
- ///
242
- /// **Errors** if strides and dimensions can point out of bounds of `v`.<br>
243
- /// **Errors** if strides allow multiple indices to point to the same element.
277
+ #[ cfg_attr( has_deprecated, deprecated( note="Use from_shape_vec instead." ) ) ]
278
+ /// ***Deprecated: Use from_shape_vec instead***
244
279
pub fn from_vec_dim_stride ( dim : D , strides : D , v : Vec < A > )
245
280
-> Result < ArrayBase < S , D > , ShapeError >
246
281
{
@@ -251,10 +286,9 @@ impl<S, A, D> ArrayBase<S, D>
251
286
} )
252
287
}
253
288
254
- /// Create an array from a vector and interpret it according to the
255
- /// provided dimensions and strides. No allocation needed.
289
+ # [ cfg_attr ( has_deprecated , deprecated ( note= "Use from_shape_vec_unchecked instead." ) ) ]
290
+ /// ***Deprecated: Use from_shape_vec_unchecked instead***
256
291
///
257
- /// Unsafe because dimension and strides are unchecked.
258
292
pub unsafe fn from_vec_dim_stride_unchecked ( dim : D , strides : D , mut v : Vec < A > )
259
293
-> ArrayBase < S , D >
260
294
{
0 commit comments