Ndarray generic function traits

I am learning rust and I wanted to create a generic function that takes any ndarray and returns an ndarray of the same type and shape, initialised to zero. I added Zero and NdProducer traits but still it cannot find T::zeros function - any guidance appreciated.

extern crate ndarray;

use ndarray::prelude::*;
use std::ops::Mul;
use num_traits::identities::Zero;
use ndarray::NdProducer;


// A minimal example of a fn that takes any ndarray
// and returns a result of the same type of ndarray:
fn create_zero_ndarray<T>(a: &T) -> T
where
    T: Clone + Mul<Output=T> + Zero + NdProducer
{
    T::zeros(a.raw_dim())
}

fn create_zero_ndarray_3U16(a: &Array3::<u16>) -> Array3::<u16>
{
    Array3::<u16>::zeros(a.raw_dim())
}

fn main() {

    let a = Array3::<u16>::zeros((3, 3, 3));
    println!("r = {:?}",create_zero_ndarray_3U16(&a));
    println!("r = {:?}",create_zero_ndarray(&a));
    
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0599]: no function or associated item named `zeros` found for type parameter `T` in the current scope
  --> src/main.rs:15:8
   |
15 |     T::zeros(a.raw_dim())
   |        ^^^^^ function or associated item not found in `T`
   |
   = help: items from traits can only be used if the type parameter is bounded by the trait
help: the following trait defines an item `zeros`, perhaps you need to restrict type parameter `T` with it:
   |
11 | fn create_zero_ndarray<T: ndarray::Dimension>(a: &T) -> T
   |                        ~~~~~~~~~~~~~~~~~~~~~
help: there is an associated function with a similar name
   |
15 |     T::zero(a.raw_dim())
   |        ~~~~

error[E0277]: the trait bound `ArrayBase<OwnedRepr<u16>, Dim<[usize; 3]>>: num_traits::Zero` is not satisfied
  --> src/main.rs:27:45
   |
27 |     println!("r = {:?}",create_zero_ndarray(&a));
   |                         ------------------- ^^ the trait `num_traits::Zero` is not implemented for `ArrayBase<OwnedRepr<u16>, Dim<[usize; 3]>>`
   |                         |
   |                         required by a bound introduced by this call
   |
note: required by a bound in `create_zero_ndarray`
  --> src/main.rs:13:32
   |
11 | fn create_zero_ndarray<T>(a: &T) -> T
   |    ------------------- required by a bound in this
12 | where
13 |     T: Clone + Mul<Output=T> + Zero + NdProducer
   |                                ^^^^ required by this bound in `create_zero_ndarray`

error[E0277]: the trait bound `ArrayBase<OwnedRepr<u16>, Dim<[usize; 3]>>: NdProducer` is not satisfied
  --> src/main.rs:27:45
   |
27 |     println!("r = {:?}",create_zero_ndarray(&a));
   |                         ------------------- ^^ the trait `NdProducer` is not implemented for `ArrayBase<OwnedRepr<u16>, Dim<[usize; 3]>>`
   |                         |
   |                         required by a bound introduced by this call
   |
   = help: the following implementations were found:
             <ArrayBase<RawViewRepr<*const A>, D> as NdProducer>
             <ArrayBase<RawViewRepr<*mut A>, D> as NdProducer>
             <ArrayBase<ViewRepr<&'a A>, D> as NdProducer>
             <ArrayBase<ViewRepr<&'a mut A>, D> as NdProducer>
note: required by a bound in `create_zero_ndarray`
  --> src/main.rs:13:39
   |
11 | fn create_zero_ndarray<T>(a: &T) -> T
   |    ------------------- required by a bound in this
12 | where
13 |     T: Clone + Mul<Output=T> + Zero + NdProducer
   |                                       ^^^^^^^^^^ required by this bound in `create_zero_ndarray`

Some errors have detailed explanations: E0277, E0599.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `playground` due to 3 previous errors

Rust Playground


Edit: Ah, you said same type, too... then: Rust Playground

Great - thanks!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.