gpt4 book ai didi

rust - 为什么 Rust 在构造 nalgebra::MatrixN 时无法找出要使用的正确 `from_iterator`?

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

我有一个结构定义为:

use nalgebra::{
allocator::Allocator, DefaultAllocator, Dim, DimName, MatrixN, RowVectorN, VectorN, U1,
}; // 0.22.0

pub struct Filter<Order: Dim + DimName>
where
DefaultAllocator:
Allocator<f64, Order, Order> + Allocator<f64, Order> + Allocator<f64, U1, Order>,
{
a: MatrixN<f64, Order>,
b: VectorN<f64, Order>,
c: RowVectorN<f64, Order>,
d: f64,
x: VectorN<f64, Order>,
}
在我的单元测试模块中,我想构造一个 Filter 的实例。 :
mod tests {
use super::*;
use nalgebra::U2;

fn a_filter() -> Filter<U2> {
let a: MatrixN<f64, U2> = MatrixN::from_iterator([1., -0., 1., 0.].into_iter());

Filter {
a,
b: VectorN::zeros(),
c: RowVectorN::zeros(),
d: 0.,
x: VectorN::zeros()
}
}
}
调用 MatrixN::from_iterator()导致编译错误。对 ::zeros() 的调用也会出现类似的错误。 :
error[E0034]: multiple applicable items in scope
--> src/lib.rs:22:44
|
22 | let a: MatrixN<f64, U2> = MatrixN::from_iterator([1., -0., 1., 0.].into_iter());
| ^^^^^^^^^^^^^ multiple `from_iterator` found
|
= note: candidate #1 is defined in an impl for the type `nalgebra::Matrix<N, R, C, <nalgebra::DefaultAllocator as nalgebra::allocator::Allocator<N, R, C>>::Buffer>`
= note: candidate #2 is defined in an impl for the type `nalgebra::Matrix<N, R, nalgebra::Dynamic, <nalgebra::DefaultAllocator as nalgebra::allocator::Allocator<N, R, nalgebra::Dynamic>>::Buffer>`
= note: candidate #3 is defined in an impl for the type `nalgebra::Matrix<N, nalgebra::Dynamic, C, <nalgebra::DefaultAllocator as nalgebra::allocator::Allocator<N, nalgebra::Dynamic, C>>::Buffer>`
= note: candidate #4 is defined in an impl for the type `nalgebra::Matrix<N, nalgebra::Dynamic, nalgebra::Dynamic, <nalgebra::DefaultAllocator as nalgebra::allocator::Allocator<N, nalgebra::Dynamic, nalgebra::Dynamic>>::Buffer>`

error[E0034]: multiple applicable items in scope
--> src/lib.rs:26:25
|
26 | b: VectorN::zeros(),
| ^^^^^ multiple `zeros` found
|
= note: candidate #1 is defined in an impl for the type `nalgebra::Matrix<N, R, C, <nalgebra::DefaultAllocator as nalgebra::allocator::Allocator<N, R, C>>::Buffer>`
= note: candidate #2 is defined in an impl for the type `nalgebra::Matrix<N, R, nalgebra::Dynamic, <nalgebra::DefaultAllocator as nalgebra::allocator::Allocator<N, R, nalgebra::Dynamic>>::Buffer>`
= note: candidate #3 is defined in an impl for the type `nalgebra::Matrix<N, nalgebra::Dynamic, C, <nalgebra::DefaultAllocator as nalgebra::allocator::Allocator<N, nalgebra::Dynamic, C>>::Buffer>`
= note: candidate #4 is defined in an impl for the type `nalgebra::Matrix<N, nalgebra::Dynamic, nalgebra::Dynamic, <nalgebra::DefaultAllocator as nalgebra::allocator::Allocator<N, nalgebra::Dynamic, nalgebra::Dynamic>>::Buffer>`

error[E0034]: multiple applicable items in scope
--> src/lib.rs:27:28
|
27 | c: RowVectorN::zeros(),
| ^^^^^ multiple `zeros` found
|
= note: candidate #1 is defined in an impl for the type `nalgebra::Matrix<N, R, C, <nalgebra::DefaultAllocator as nalgebra::allocator::Allocator<N, R, C>>::Buffer>`
= note: candidate #2 is defined in an impl for the type `nalgebra::Matrix<N, R, nalgebra::Dynamic, <nalgebra::DefaultAllocator as nalgebra::allocator::Allocator<N, R, nalgebra::Dynamic>>::Buffer>`
= note: candidate #3 is defined in an impl for the type `nalgebra::Matrix<N, nalgebra::Dynamic, C, <nalgebra::DefaultAllocator as nalgebra::allocator::Allocator<N, nalgebra::Dynamic, C>>::Buffer>`
= note: candidate #4 is defined in an impl for the type `nalgebra::Matrix<N, nalgebra::Dynamic, nalgebra::Dynamic, <nalgebra::DefaultAllocator as nalgebra::allocator::Allocator<N, nalgebra::Dynamic, nalgebra::Dynamic>>::Buffer>`

error[E0034]: multiple applicable items in scope
--> src/lib.rs:29:25
|
29 | x: VectorN::zeros(),
| ^^^^^ multiple `zeros` found
|
= note: candidate #1 is defined in an impl for the type `nalgebra::Matrix<N, R, C, <nalgebra::DefaultAllocator as nalgebra::allocator::Allocator<N, R, C>>::Buffer>`
= note: candidate #2 is defined in an impl for the type `nalgebra::Matrix<N, R, nalgebra::Dynamic, <nalgebra::DefaultAllocator as nalgebra::allocator::Allocator<N, R, nalgebra::Dynamic>>::Buffer>`
= note: candidate #3 is defined in an impl for the type `nalgebra::Matrix<N, nalgebra::Dynamic, C, <nalgebra::DefaultAllocator as nalgebra::allocator::Allocator<N, nalgebra::Dynamic, C>>::Buffer>`
= note: candidate #4 is defined in an impl for the type `nalgebra::Matrix<N, nalgebra::Dynamic, nalgebra::Dynamic, <nalgebra::DefaultAllocator as nalgebra::allocator::Allocator<N, nalgebra::Dynamic, nalgebra::Dynamic>>::Buffer>`

我不清楚为什么 Rust 无法找出正确的 from_iterator使用。是因为我必须指定 DefaultAllocatorFilter 的定义中?如果是这样,最干净的解决方法是什么?

最佳答案

TL;博士; 一个 workaround对于您的代码:

fn a_filter() -> Filter<U2> {
let a = MatrixN::<f64, U2>::from_iterator([1.0, -0., 1., 0.].iter().copied());

Filter {
a,
b: VectorN::<f64, U2>::zeros(),
c: RowVectorN::<f64, U2>::zeros(),
d: 0.,
x: VectorN::<f64, U2>::zeros()
}
}
您必须在 zeros() 的调用中指定顺序而不是从返回类型中猜测它。 from_iterator还有一个问题是 array::into_iter() 不返回值的迭代器,而是对值的引用,就像 iter() (是的,这是令人困惑的,并且有一个 lint,IIRC),所以你需要添加一个 copied()获得正确的迭代器类型。

这是显示错误的最少代码 ( playground):
use nalgebra::{
VectorN, U3
};
fn main() {
let x: VectorN<f64, U3> = VectorN::zeros();
}
错误是:
error[E0034]: multiple applicable items in scope
--> src/main.rs:5:40
|
5 | let x: VectorN<f64, U3> = VectorN::zeros();
| ^^^^^ multiple `zeros` found
|
= note: candidate #1 is defined in an impl for the type `nalgebra::Matrix<N, R, C, <nalgebra::DefaultAllocator as nalgebra::allocator::Allocator<N, R, C>>::Buffer>`
= note: candidate #2 is defined in an impl for the type `nalgebra::Matrix<N, R, nalgebra::Dynamic, <nalgebra::DefaultAllocator as nalgebra::allocator::Allocator<N, R, nalgebra::Dynamic>>::Buffer>`
= note: candidate #3 is defined in an impl for the type `nalgebra::Matrix<N, nalgebra::Dynamic, C, <nalgebra::DefaultAllocator as nalgebra::allocator::Allocator<N, nalgebra::Dynamic, C>>::Buffer>`
= note: candidate #4 is defined in an impl for the type `nalgebra::Matrix<N, nalgebra::Dynamic, nalgebra::Dynamic, <nalgebra::DefaultAllocator as nalgebra::allocator::Allocator<N, nalgebra::Dynamic, nalgebra::Dynamic>>::Buffer>`
如果您仔细查看候选人,他们只是以下人员的全名:
candidate #1: Matrix<N, R, C, _>
candidate #2: Matrix<N, R, Dynamic, _>`
candidate #3: Matrix<N, Dynamic, C, _>`
candidate #4: Matrix<N, Dynamic, Dynamic, _>`
所以他们与 Dynamic 有关系事情...如果您查看 documentation它不是很有帮助,但是如果您查看 zeros() 中的示例功能它有一个提示:
let v = Vector3::<f32>::zeros();
let dv = DVector::<f32>::zeros(3);
let m = Matrix2x3::<f32>::zeros();
let dm = DMatrix::<f32>::zeros(2, 3);
等等...不是需要0个参数吗?嗯,看代码,虽然文档没有说,但是貌似 zeros()接受尽可能多的参数 Dynamic在泛型类型中。和 Vector3<T>Vector<T, U3> 的别名, DVector<T>Vector<T, Dynamic> 的别名等等。
您可能仍然认为编译器无论如何都可以推导出函数,因为 zero() 的每个版本的返回类型和参数编号都不同。 ,但这不是编译器的工作方式:Rust 中没有函数重载。如果一个函数名解析为多个函数,这是一个错误。
考虑这个显示类似错误的更简单的代码:
struct Foo<T> {
t: T,
}

impl Foo<u8> {
fn new() -> u8 {
0
}
}

impl Foo<u32> {
fn new() -> u32 {
0
}
}

fn main() {
let t: u8 = Foo::new(); // error!
}
请注意,如果两个函数返回相同或不同的类型,甚至它们是否具有相同数量的参数,这都无关紧要,这始终是编译器错误。
我的代码的解决方法是:
fn main() {
let t: u8 = Foo::<u8>::new();
}

关于rust - 为什么 Rust 在构造 nalgebra::MatrixN 时无法找出要使用的正确 `from_iterator`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63908237/

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