Reputation: 183
I've been trying to understand how using owned Arrays works. So I tried to write this function and it doesn't compile.
use ndarray::prelude::*;
fn add_arrays<T>(ar1 : &Array1<T>, ar2 : &Array1<T>) -> Array1<T>
where T : Add+Sub+Div+Mul
{
ar1 + ar2
}
But i get this error :
binary operation `+` cannot be applied to type `ndarray::ArrayBase<ndarray::OwnedRepr<T>, ndarray::dimension::dim::Dim<[usize; 1]>>`
note: an implementation of `std::ops::Add` might be missing for `ndarray::ArrayBase<ndarray::OwnedRepr<T>, ndarray::dimension::dim::Dim<[usize; 1]>>`
i'd like to know what should be done to do this
Upvotes: 0
Views: 195
Reputation: 208
You should use LinalgScalar trait
use ndarray::*;
fn add_arrays<T>(ar1: &Array1<T>, ar2: &Array1<T>) -> Array1<T>
where
T: LinalgScalar,
{
ar1 + ar2
}
fn main() {
let a = array![1.0, 2.0];
let b = array![3.0, 4.0];
let c = add_arrays(&a, &b);
println!("{:?}", c);
}
Upvotes: 1