Skip to content

Commit

Permalink
add array of ones similar to numpy for convenience
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsanmok committed Feb 19, 2018
1 parent 2f69398 commit 328389e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/impl_constructors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,16 @@ impl<S, A, D> ArrayBase<S, D>
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<Sh>(shape: Sh) -> Self
where A: Clone + One,
Sh: ShapeBuilder<Dim=D>,
{
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.
Expand Down Expand Up @@ -228,13 +238,13 @@ impl<S, A, D> ArrayBase<S, D>
/// 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:
///
Expand Down
8 changes: 8 additions & 0 deletions tests/array-construct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ fn deny_wraparound_from_vec() {
assert!(six.is_err());
}

#[test]
fn test_ones() {
let mut a = Array::<f32, _>::zeros((2, 3, 4));
a.fill(1.0);
let b = Array::<f32, _>::ones((2, 3, 4));
assert_eq!(a, b);
}

#[should_panic]
#[test]
fn deny_wraparound_zeros() {
Expand Down

0 comments on commit 328389e

Please sign in to comment.