I am trying to implement a whole bunch of generic functions, using ndarray, that all should
- take in an array size the same way
Array::zeros()
does, so as either a usize
or an n-tuple of usize
- return an array with the given size
I can do this for, say, Array1
, but since i'm translating MATLAB code I really need it to work for any size, so I guess something like -> Array<T, IxDyn>
?
I've looked at things like shape: S
with S : Into<IxDyn>
, but so far all my attempts have been rejected by the compiler.
What have you tried so far? Have you looked at the implementation of Array::zeros
?
If you just want to copy its behavior, why write a new function at all? Just use Array::zeros()
directly.
1 Like
Thank you for your response, i just mentioned Array::zeros()
as a simple example of the kind of objects that are usually passed to represent an array dimension. A big difference is that Array::zeros()
being a method of ArrayBase uses shape: Sh
with Sh: ShapeBuilder<Dim = D>
where D
is given by the ArrayBase. I'm tyring to have a completely standalone function that returns an Array, I'm not trying to extend the functionality of ArrayBase. I hope that made sense, I'm pretty new to rust.
You can reference the associated type of the ShapeBuilder
trait:
use ndarray::{Array, DataOwned, ShapeBuilder};
use num::Zero;
fn new_array<D, S>(shape: S) -> Array<D, S::Dim>
where
D: DataOwned + Clone + Zero,
S: ShapeBuilder,
{
Array::zeros(shape)
}
1 Like
Thank you very much, I just had not understood how to use ShapeBuilder, the example let me implement all the versions i needed.