rust-ndarray / ndarray

ndarray: an N-dimensional array with array views, multidimensional slicing, and efficient operations
https://docs.rs/ndarray/
Apache License 2.0
3.59k stars 307 forks source link

Document element type returned by `Array::zeros` #1217

Open dstansby opened 2 years ago

dstansby commented 2 years ago

Doing something like let xs = Array::zeros((100, 3));, it's not clear from the API or documentation for zeros what the return type is. Ie. are the elements integer, f64, f32 etc.? It would be good to document this, alongside the right way to construct arrays of zeros with different element datatypes.

adamreichold commented 2 years ago

The above does not compile exactly due to this:

error[E0283]: type annotations needed for `ArrayBase<OwnedRepr<A>, Dim<[usize; 2]>>`
   --> src/main.rs:2:9
    |
2   |     let xs = ndarray::Array::zeros((2, 3));
    |         ^^   --------------------- type must be known at this point
    |
    = note: cannot satisfy `_: Clone`
note: required by a bound in `ndarray::impl_constructors::<impl ArrayBase<S, D>>::zeros`
   --> /home/adam/.cargo/registry/src/github.com-1ecc6299db9ec823/ndarray-0.15.6/src/impl_constructors.rs:342:12
    |
342 |         A: Clone + Zero,
    |            ^^^^^ required by this bound in `ndarray::impl_constructors::<impl ArrayBase<S, D>>::zeros`
help: consider specifying the type argument in the function call
    |
2   |     let xs = ndarray::Array::zeros::<Sh>((2, 3));
    |                                   ++++++

For more information about this error, try `rustc --explain E0283`.

Meaning that one needs to constrain the element type somehow in any case, e.g.

let xs = ndarray::Array::<f64, _>::zeros((2, 3));

or

let xs: ndarray::Array<f64, _> = ndarray::Array::zeros((2, 3));
bluss commented 2 years ago

Would be good to document. The docs already show the A parameter. zeros is generic, the user selects what element type to use, and that type must implement Zero.