【问题标题】:Rust: "multiple applicable items in scope" error for ndarray dot product on sliceRust:切片上 ndarray 点积的“范围内有多个适用项目”错误
【发布时间】:2020-04-20 14:21:10
【问题描述】:

我想我在问这个问题时最终想出了一个解决方案,但我想我还是会留下这个问题......

我正在使用 ndarray 板条箱处理 n 维矩阵,我需要获取多个非连续切片的点积。但是,我遇到了一个问题,因为在获取点积时编译器似乎无法确定我的切片是 1D 还是 2D,而且我不确定如何指定此信息。

这是一个简化的例子:

use ndarray::prelude::*;

fn main() {

    let a = array![[1, 2, 3], [1, 2, 3]];
    println!("{:?}", &a);

    let b = array![[1, 2, 3], [1, 2, 3]];
    println!("{:?}", &b);

    let a_slice = a.slice(s![0, ..]);
    let b_slice = b.slice(s![0, ..]);

    println!("{:?}", &a_slice.dot(&b_slice));

}

错误信息有点混乱,考虑到涉及的抽象结构,我不知道如何让点积使用正确的候选:

error[E0034]: multiple applicable items in scope
  --> src/main.rs:14:31
   |
14 |     println!("{:?}", &a_slice.dot(&b_slice));
   |                               ^^^ multiple `dot` found
   |
   = note: candidate #1 is defined in an impl for the type `ndarray::ArrayBase<_, ndarray::dimension::dim::Dim<[usize; 1]>>`
   = note: candidate #2 is defined in an impl for the type `ndarray::ArrayBase<_, ndarray::dimension::dim::Dim<[usize; 2]>>`

我如何提供必要的信息?

【问题讨论】:

    标签: multidimensional-array rust


    【解决方案1】:

    尽管错误消息提到ArrayBase,但这里重要的信息实际上是切片的尺寸。因此,使用ArrayView 提供显式尺寸就可以了:

    use ndarray::prelude::*;
    
    fn main() {
    
        let a = array![[1, 2, 3], [1, 2, 3]];
        println!("{:?}", &a);
    
        let b = array![[1, 2, 3], [1, 2, 3]];
        println!("{:?}", &b);
    
        let a_slice: ArrayView<_, Ix1> = a.slice(s![0, ..]);
        let b_slice: ArrayView<_, Ix1> = b.slice(s![0, ..]);
    
        println!("{:?}", &a_slice.dot(&b_slice));
    
    }
    

    【讨论】:

      猜你喜欢
      • 2021-05-31
      • 1970-01-01
      • 2019-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多