gpt4 book ai didi

multidimensional-array - 如何编写一个将 ndarray Array 或 ArrayView 作为输入的通用函数?

转载 作者:行者123 更新时间:2023-12-03 11:45:35 25 4
gpt4 key购买 nike

我正在使用 ndarray 编写一组数学函数,我想在任何类型的 ArrayBase 上执行这些函数。但是,我无法指定所涉及的特征/类型。

此基本函数适用于 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

最佳答案

如果您查看 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()
}

这正是编译器在提示时的意思:

  = 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`

关于multidimensional-array - 如何编写一个将 ndarray Array 或 ArrayView 作为输入的通用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61758934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com