4

我想创建一个struct包含更大nalgebra::MatrixN的a:U1

extern crate nalgebra as na;

use na::{DimName, DimNameAdd, DimNameSum, MatrixN, U1};

pub struct Homogenous<D: DimName>
where
    D: DimNameAdd<U1>,
{
    mat: na::MatrixN<f32, DimNameSum<D, U1>>,
}

我收到以下错误:

error[E0277]: cannot multiply `<<D as na::DimNameAdd<na::U1>>::Output as na::DimName>::Value` to `<<D as na::DimNameAdd<na::U1>>::Output as na::DimName>::Value`
 --> src/main.rs:9:5
  |
9 |     mat: na::MatrixN<f32, DimNameSum<D, U1>>,
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no implementation for `<<D as na::DimNameAdd<na::U1>>::Output as na::DimName>::Value * <<D as na::DimNameAdd<na::U1>>::Output as na::DimName>::Value`
  |
  = help: the trait `std::ops::Mul` is not implemented for `<<D as na::DimNameAdd<na::U1>>::Output as na::DimName>::Value`
  = help: consider adding a `where <<D as na::DimNameAdd<na::U1>>::Output as na::DimName>::Value: std::ops::Mul` bound
  = note: required because of the requirements on the impl of `na::allocator::Allocator<f32, <D as na::DimNameAdd<na::U1>>::Output, <D as na::DimNameAdd<na::U1>>::Output>` for `na::DefaultAllocator`

试图遵循错误消息会导致下一个特征错误消息的兔子洞。我看过 nalgebra 的 API,它不包含如此复杂的特征链。例如to_homogenous方法。我不确定我的方法是否正确。

还有Dim对应的特质DimAddDimSum,但是由于那部分nalgebra没有真正记录,

我不知道我是否走在正确的道路上——或者我想做的事情是否可能。

4

1 回答 1

5

这篇文章为我指明了正确的方向。这样做的方法nalgebra有点复杂:

extern crate nalgebra as na;

use crate::na::{Dim, DimName, DimNameAdd, DimNameSum, MatrixN, U1, DefaultAllocator};
use crate::na::allocator::Allocator;

pub struct Homogenous<D: Dim + DimName>
where
    D: DimNameAdd<U1>,
    DefaultAllocator: Allocator<f32, DimNameSum<D, U1>, DimNameSum<D, U1>>,
{
    mat: MatrixN<f32, DimNameSum<D, U1>>,
}

希望这些类型的泛型操作在 Rust 和 nalgebra 的未来版本中变得更符合人体工程学,因为这些相当繁琐的类型注释需要经常重复。

顺便说一句,仅将这些泛型类型存储在结构中仍然需要DefaultAllocator

extern crate nalgebra as na;

use crate::na::{Dim, DimName, MatrixN, DefaultAllocator};
use crate::na::allocator::Allocator;

pub struct Homogenous<D: Dim + DimName>
where
    DefaultAllocator: Allocator<f32, D, D>,
{
    mat: MatrixN<f32, D>,
}
于 2018-11-12T09:07:13.627 回答