gpt4 book ai didi

c++ - Eigen :访问 ProductBase 系数

转载 作者:太空宇宙 更新时间:2023-11-04 11:51:11 27 4
gpt4 key购买 nike

以下代码片段死于 Eigen 断言:

MatrixXd L;
VectorXd x, b;
...
ASSERT_MATRIX_EQ(L*x, b);

与,

template <typename DerivedL, typename DerivedR>
void ASSERT_MATRIX_EQ(const Eigen::DenseBase<DerivedL>& A, const Eigen::DenseBase<DerivedR>& B, double tol=1e-7) {
ASSERT_EQ(A.rows(), B.rows());
ASSERT_EQ(A.cols(), B.cols());
for(int i=0; i < A.rows(); i++) {
for(int j=0; j < A.cols(); j++) {
ASSERT_NEAR(A(i,j), B(i,j), tol);
}
}
}

它死于错误:

test_leq: /usr/include/eigen3/Eigen/src/Core/ProductBase.h:154: typename Base::CoeffReturnType Eigen::ProductBase<Eigen::GeneralProduct<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, 4>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >::coeff(Index, Index) const: Assertion `this->rows() == 1 && this->cols() == 1' failed.

在调用 A(i,j) 时. (不过,我可以调用 cout << A << endl; 就好了。)

第 154 行,ProductBase.h奇怪的是有断言

    // restrict coeff accessors to 1x1 expressions. No need to care about mutators here since this isnt a Lvalue expression
typename Base::CoeffReturnType coeff(Index row, Index col) const
{
#ifdef EIGEN2_SUPPORT
return lhs().row(row).cwiseProduct(rhs().col(col).transpose()).sum();
#else
EIGEN_STATIC_ASSERT_SIZE_1x1(Derived)
eigen_assert(this->rows() == 1 && this->cols() == 1);
return derived().coeff(row,col);
#endif
}

我正在关注 Eigen's guide用于编写通用矩阵函数。如何正确编写此通用函数?

编辑:也很高兴知道为什么 ProductBase需要一个 1x1 矩阵。

最佳答案

A thread在 Eigen 邮件列表上表示 ProductBase 上的系数访问被有意禁用。现在的解决方案是避免像 foobar(A*x) 这样的表达式。

关于c++ - Eigen :访问 ProductBase 系数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18169460/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com