1

我正在尝试为通用方形 MatrixMN 实现 exp 函数

pub fn exp<N, R>(m: &MatrixMN<N, R, R>, k: usize) -> MatrixMN<N, R, R>
where
    N: Scalar + One + Zero,
    R: DimName + DimNameAdd<R>,
    <R as DimName>::Value: Mul<<R as DimName>::Value>,
    <<R as DimName>::Value as Mul<<R as DimName>::Value>>::Output: generic_array::ArrayLength<N>,
{
    let mut i = MatrixMN::<N, R, R>::identity();
    i.add(&m)
}

但我不断收到这样的错误。

error[E0599]: no method named `add` found for struct `nalgebra::base::matrix::Matrix<N, R, R, nalgebra::base::array_storage::ArrayStorage<N, R, R>>` in the current scope
  --> src/state_extrapolation.rs:24:7
   |
24 |     i.add(&m)
   |       ^^^ method not found in `nalgebra::base::matrix::Matrix<N, R, R, nalgebra::base::array_storage::ArrayStorage<N, R, R>>`
   |
   = note: the method `add` exists but the following trait bounds were not satisfied:
           `&mut nalgebra::base::matrix::Matrix<N, R, R, nalgebra::base::array_storage::ArrayStorage<N, R, R>> : nalgebra::base::dimension::DimNameAdd<_>`
           `&nalgebra::base::matrix::Matrix<N, R, R, nalgebra::base::array_storage::ArrayStorage<N, R, R>> : nalgebra::base::dimension::DimNameAdd<_>`
           `nalgebra::base::matrix::Matrix<N, R, R, nalgebra::base::array_storage::ArrayStorage<N, R, R>> : nalgebra::base::dimension::DimNameAdd<_>`

有没有更好的方法将泛型矩阵传递给函数?

我也试过这样的东西

pub fn exp2<M>(m: &M, k: usize) -> M
where
    M: nalgebra::base::Matrix<_, _, _, _>,
{
    let mut i = M::identity();

    i.add(&m)
}

但无法为 M 提出好的特征。

4

1 回答 1

2

当使事物完全通用时,很容易迷失在特征中。我的建议是:

  • 复制实现与您的类似功能的 impl 块的签名,例如,此处DefaultAllocator: Allocator<N, R, R>的行允许摆脱许多约束
  • 而不是Scalar,如果它将是您将计算的浮点数,它更易于使用RealField,它为您提供了Scalar许多其他有用的属性(如函数One所需Zeroidentity()
  • 遵循编译器错误——它建议我添加一个use std::ops::Add,这使它最终工作。

这是代码,操场

use nalgebra::{
    base::allocator::Allocator, DefaultAllocator, DimName, DimNameAdd, MatrixN, RealField,
};
use std::ops::Add;

fn exp<N, R>(m: &MatrixN<N, R>, k: usize) -> MatrixN<N, R>
where
    N: RealField,
    R: DimName + DimNameAdd<R>,
    DefaultAllocator: Allocator<N, R, R>,
{
    let i = MatrixN::<N, R>::identity();
    m.add(i)
}
于 2020-03-27T12:15:56.563 回答