将本征矩阵的下乔斯基因子作为函数参数传入

问题描述 投票:0回答:1

我想先说一下,我是一个C++新手,所以请大家在评论和建议中多多包涵。

我正在尝试重构一些代码。我执行的其中一个操作涉及到将一个(备忘的) Eigen::LLT<Eigen::MatrixXd> 类型的对象,并对其进行一些计算。

我想把这个计算重构成一个较小的函数,但我在传递这个函数时遇到了麻烦。Eigen::LLT<Eigen::MatrixXd> 类型作为参数,同时遵守(我对)中的建议。Eigen文件在这里.

我尝试了以下方法。

#define _USE_MATH_DEFINES
#include <Eigen/LU>
#include <Eigen/Dense>
#include <math.h>
#include <unsupported/Eigen/SpecialFunctions>

Eigen::MatrixXd conditionalCov(
    Eigen::Ref<const Eigen::MatrixXd> kxstarxstar,
    Eigen::Ref<const Eigen::LLT<Eigen::MatrixXd>> lxx,
    Eigen::Ref<const Eigen::MatrixXd> kxxstar
    )
{
    return (
        kxstarxstar.array() - (kxxstar.transpose() * lxx.solve(kxxstar)).array()
        ).matrix().selfadjointView<Eigen::Lower>();
}

但由于类型定义的原因,无法编译 lxxEigen::Ref<const Eigen::LLT<Eigen::MatrixXd>>,是错误的。GCC说。

[<path_to_file>] error: ‘IsVectorAtCompileTime’ is not a member of ‘const Eigen::LLT<Eigen::Matrix<double, -1, -1>, 1>’
        5 |     Eigen::Ref<const Eigen::LLT<Eigen::MatrixXd>> lxx,

应该是什么类型的 lxx (下乔斯基因子)在这里是为了避免在调用函数时创建临时矩阵?

c++ eigen eigen3
1个回答
0
投票

只要通过 LLT 对象作为一个标准的C++常量引用。

传递 kxstarxstarkxxstarEigen::Ref 也只有当你打算传递其他矩阵的子块时才有意义。如果没有,就把它们作为 const Eigen::MatrixXd &. 如果你想通过他们作为 Eigen::Ref建议将其本身作为 const &:

Eigen::MatrixXd conditionalCov(
    const Eigen::Ref<const Eigen::MatrixXd>& kxstarxstar,
    const Eigen::LLT<Eigen::MatrixXd> &lxx,
    const Eigen::Ref<const Eigen::MatrixXd>& kxxstar
    )

注:在你的函数中,不需要在矩阵和数组类型之间进行转换,只需按以下方式进行转换,就可以得到同样的结果。

return (
    kxstarxstar - kxxstar.transpose() * lxx.solve(kxxstar)
    ).selfadjointView<Eigen::Lower>();
© www.soinside.com 2019 - 2024. All rights reserved.