diff --git a/src/impl_constructors.rs b/src/impl_constructors.rs index ce35a79f4..a8b17c6a1 100644 --- a/src/impl_constructors.rs +++ b/src/impl_constructors.rs @@ -190,6 +190,16 @@ impl ArrayBase Self::from_elem(shape, A::zero()) } + /// Create an array with ones, shape `shape`. + /// + /// **Panics** if the number of elements in `shape` would overflow usize. + pub fn ones(shape: Sh) -> Self + where A: Clone + One, + Sh: ShapeBuilder, + { + Self::from_elem(shape, A::one()) + } + /// Create an array with default values, shape `shape` /// /// **Panics** if the number of elements in `shape` would overflow usize. @@ -228,13 +238,13 @@ impl ArrayBase /// Create an array with the given shape from a vector. (No cloning of /// elements needed.) /// - /// ---- + /// ---- /// /// For a contiguous c- or f-order shape, the following applies: /// /// **Errors** if `shape` does not correspond to the number of elements in `v`. /// - /// ---- + /// ---- /// /// For custom strides, the following applies: /// diff --git a/tests/array-construct.rs b/tests/array-construct.rs index abb0d6ca8..463eae980 100644 --- a/tests/array-construct.rs +++ b/tests/array-construct.rs @@ -142,6 +142,14 @@ fn deny_wraparound_from_vec() { assert!(six.is_err()); } +#[test] +fn test_ones() { + let mut a = Array::::zeros((2, 3, 4)); + a.fill(1.0); + let b = Array::::ones((2, 3, 4)); + assert_eq!(a, b); +} + #[should_panic] #[test] fn deny_wraparound_zeros() {