Skip to content

Commit 328389e

Browse files
committed
add array of ones similar to numpy for convenience
1 parent 2f69398 commit 328389e

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/impl_constructors.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,16 @@ impl<S, A, D> ArrayBase<S, D>
190190
Self::from_elem(shape, A::zero())
191191
}
192192

193+
/// Create an array with ones, shape `shape`.
194+
///
195+
/// **Panics** if the number of elements in `shape` would overflow usize.
196+
pub fn ones<Sh>(shape: Sh) -> Self
197+
where A: Clone + One,
198+
Sh: ShapeBuilder<Dim=D>,
199+
{
200+
Self::from_elem(shape, A::one())
201+
}
202+
193203
/// Create an array with default values, shape `shape`
194204
///
195205
/// **Panics** if the number of elements in `shape` would overflow usize.
@@ -228,13 +238,13 @@ impl<S, A, D> ArrayBase<S, D>
228238
/// Create an array with the given shape from a vector. (No cloning of
229239
/// elements needed.)
230240
///
231-
/// ----
241+
/// ----
232242
///
233243
/// For a contiguous c- or f-order shape, the following applies:
234244
///
235245
/// **Errors** if `shape` does not correspond to the number of elements in `v`.
236246
///
237-
/// ----
247+
/// ----
238248
///
239249
/// For custom strides, the following applies:
240250
///

tests/array-construct.rs

+8
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ fn deny_wraparound_from_vec() {
142142
assert!(six.is_err());
143143
}
144144

145+
#[test]
146+
fn test_ones() {
147+
let mut a = Array::<f32, _>::zeros((2, 3, 4));
148+
a.fill(1.0);
149+
let b = Array::<f32, _>::ones((2, 3, 4));
150+
assert_eq!(a, b);
151+
}
152+
145153
#[should_panic]
146154
#[test]
147155
fn deny_wraparound_zeros() {

0 commit comments

Comments
 (0)