nalgebra:为通用 MatrixMN 实现一个函数

nalgebra: Implementing a function for a generic MatrixMN

我正在尝试为通用正方形 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 的优点。

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

  • 复制实现与您类似功能的 impl 块的签名,例如here 中的 DefaultAllocator: Allocator<N, R, R> 行允许摆脱许多限制
  • 而不是 Scalar,如果它是你计算的浮点数,使用 RealField 更容易,它给你 Scalar 加上许多其他有用的属性(比如 [= identity() 函数需要 15=] 和 Zero)
  • 关注编译器错误——它建议我添加一个 use std::ops::Add,最终它成功了。

这是代码,playground:

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)
}