我如何编写一个通用函数,以ndarray Array或ArrayView作为输入?

问题描述 投票:-1回答:1

我正在写一组数学函数,使用了 ndarray 我想在任何类型的产品上执行 ArrayBase. 然而,我在指定相关的traitstypes时遇到了麻烦。

这个基本功能可以在以下两种情况下工作 OwnedReprViewRepr 数据。

use ndarray::{prelude::*, Data}; // 0.13.1

fn sum_owned(x: Array<f64, Ix1>) -> f64 {
    x.sum()
}

fn sum_view(x: ArrayView<f64, Ix1>) -> f64 {
    x.sum()
}

fn main() {
    let a = Array::from_shape_vec((4,), vec![1.0, 2.0, 3.0, 4.0]).unwrap();
    println!("{:?}", sum_owned(a.clone()));

    let b = a.slice(s![..]);
    println!("{:?}", sum_view(b));

    // Complains that OwnedRepr is not ViewRepr
    //println!("{:?}", sum_view(a.clone()));
}

我可以理解为什么被注释掉的部分不能编译 但我对通用性的理解还不够透彻,无法写出更多的... 通用性。

下面是我试过的。

use ndarray::prelude::*;
use ndarray::Data;

fn sum_general<S>(x: ArrayBase<S, Ix1>) -> f64
where
    S: Data,
{
    x.sum()
}

编译器的错误表明 Data 是不够具体,但我就是不能很好地解析它,弄不清应该怎么解决。

error[E0277]: the trait bound `<S as ndarray::data_traits::RawData>::Elem: std::clone::Clone` is not satisfied
 --> src/lib.rs:8:7
  |
6 |     S: Data,
  |             - help: consider further restricting the associated type: `, <S as ndarray::data_traits::RawData>::Elem: std::clone::Clone`
7 | {
8 |     x.sum()
  |       ^^^ the trait `std::clone::Clone` is not implemented for `<S as ndarray::data_traits::RawData>::Elem`

error[E0277]: the trait bound `<S as ndarray::data_traits::RawData>::Elem: num_traits::identities::Zero` is not satisfied
 --> src/lib.rs:8:7
  |
6 |     S: Data,
  |             - help: consider further restricting the associated type: `, <S as ndarray::data_traits::RawData>::Elem: num_traits::identities::Zero`
7 | {
8 |     x.sum()
  |       ^^^ the trait `num_traits::identities::Zero` is not implemented for `<S as ndarray::data_traits::RawData>::Elem`

error[E0308]: mismatched types
 --> src/lib.rs:8:5
  |
4 | fn sum_general<S>(x: ArrayBase<S, Ix1>) -> f64
  |                                            --- expected `f64` because of return type
...
8 |     x.sum()
  |     ^^^^^^^ expected `f64`, found associated type
  |
  = note:         expected type `f64`
          found associated type `<S as ndarray::data_traits::RawData>::Elem`
  = note: consider constraining the associated type `<S as ndarray::data_traits::RawData>::Elem` to `f64`
  = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
multidimensional-array rust
1个回答
1
投票

如果你看一下 ndarray::ArrayBase::sum 函数。

impl<A, S, D> ArrayBase<S, D>
where
    S: Data<Elem = A>,
    D: Dimension,
{
    pub fn sum(&self) -> A
    where
       A: Clone + Add<Output = A> + Zero
    {
         // etc.
    }
}

很明显,在你的情况下 A = f64D = Ix1但你仍然需要指定约束条件,即 S: Data<Elem = f64>. 因此,当编译器建议:

use ndarray::prelude::*;
use ndarray::Data;

fn sum_general<S>(x: ArrayBase<S, Ix1>) -> f64
where
    S: Data<Elem = f64>,
{
    x.sum()
}

这正是编译器建议的意思。

  = 注:预期类型`f64`发现关联类型`<S作为ndarray::data_traits::RawData>:Elem` = 注:考虑将关联类型`<S作为ndarray::data_traits::RawData>:Elem`约束为`f64`。
© www.soinside.com 2019 - 2024. All rights reserved.