我想使用 Eigen::Ref 来使用 Eigen::Matrix 参数来实现非模板函数。我的问题是,在这些函数中,我可能必须调整 Eigen::Ref 引用的矩阵的大小。我知道,一般而言,不应调整 Eigen::Ref 的大小,因为它可以映射到表达式或矩阵块,但就我而言,我确信 Eigen::Ref 背后是 Eigen::Matrix。
为了说明这一点:
#include "Eigen/Dense"
void add(Eigen::Ref<Eigen::MatrixXd> M, const Eigen::Ref<const Eigen::MatrixXd> &A, const Eigen::Ref<const Eigen::MatrixXd> &B) {
M=A+B;
}
int main() {
Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor, 32, 32> M(2,3);
Eigen::Matrix<double, 2, 2> A;
Eigen::Matrix<double, 2, 2> B;
add(M,A,B);
}
在运行时给出:
void Eigen::DenseBase<Derived>::resize(Eigen::Index, Eigen::Index) [with Derived = Eigen::Ref<Eigen::Matrix<double, -1, -1> >; Eigen::Index = long int]: Assertion `rows == this->rows() && cols == this->cols() && "DenseBase::resize() does not actually allow to resize."' failed.
我试图欺骗它:
void add(Eigen::Ref<Eigen::MatrixXd> M, const Eigen::Ref<const Eigen::MatrixXd> &A, const Eigen::Ref<const Eigen::MatrixXd> &B) {
Eigen::Ref<Eigen::Matrix<double,2,2>> MM(M);
MM=A+B;
}
但我在运行时得到:
Eigen::internal::variable_if_dynamic<T, Value>::variable_if_dynamic(T) [with T = long int; int Value = 2]: Assertion `v == T(Value)' failed.
那么,我能做些什么来处理这个问题呢?在 Eigen 文档中,使用 MatrixBase 作为参数的模板函数解决了调整大小的问题,但对于 Eigen::Ref?