如何创建一个包含更高维度的nalgebra矩阵的结构体?

4
我想创建一个包含 nalgebra::MatrixNstruct,这个矩阵要比原先的 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>>,
}

I get the following error:

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`

尝试按照错误消息进行操作会导致下一个trait错误消息的问题。我查看了nalgebra的API,其中没有包含这样复杂的trait链。例如,to_homogenous方法。我不确定我的方法是否正确。
还有一个trait Dim及其对应的DimAdd和DimSum,但由于nalgebra的这部分并没有真正记录,
我不知道我是否走在正确的道路上,或者我想做的事情是否可能。

这样做会导致进入一个兔子洞,接下来就会出现特征错误信息。我已经查看了 nalgebra 的 API,它不包含如此复杂的特征链。例如 to_homogenous 方法。我甚至不确定我的方法是否正确。 - fuji
1
谢谢您指出这个问题,我已经修复了打字错误(na::Matrix -> na::MatrixN)。编译现在会导致上述错误。不幸的是,似乎 Rust playground 不支持 nalgebra。 - fuji
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>,
}

这个线程可能会有一些有趣的见解。这些方法的复杂性通常归结于缺乏const generics - E net4

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接