动态矩阵特征分解过程中的误差
Error during decomposition with Eigen with dynamic matrices
我在这里尝试使用Eigen提供的示例,它似乎有效。然而,当我试图改变矩阵类型以支持动态矩阵时,一切都会爆炸(下面的一切与示例中完全一样,但针对矩阵/向量的类型(:
#include <Eigen/Dense>
#include <iostream>
using Matrix2D = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor | Eigen::AutoAlign>;
using Vector = Eigen::Matrix<double, Eigen::Dynamic, 1>;
int main() {
Matrix2D A(3,3);
Vector b(3);
A << 1,2,3, 4,5,6, 7,8,10;
b << 3, 3, 4;
std::cout << "Here is the matrix A:n" << A << std::endl;
std::cout << "Here is the vector b:n" << b << std::endl;
auto x = A.colPivHouseholderQr().solve(b);
std::cout << "The solution is:n" << x << std::endl;
return 0;
}
运行期间的输出
Here is the matrix A:
1 2 3
4 5 6
7 8 10
Here is the vector b:
3
3
4
The solution is:
a.out: eigen33/Eigen/src/Core/Block.h:123: Eigen::Block<XprType, BlockRows, BlockCols, InnerPanel>::Block(XprType&, Eigen::Index) [with XprType = Eigen::Matrix<double, -1, 1>; int BlockRows = 1; int BlockCols = 1; bool InnerPanel = false; Eigen::Index = long int]: Assertion `(i>=0) && ( ((BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) && i<xpr.rows()) ||((BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) && i<xpr.cols()))' failed.
./makeEigen.sh: line 6: 12045 Aborted (core dumped) ./a.out
这样做可能吗?如果可能,我做错了什么?
这是使用auto
很危险的地方之一。
您链接到的示例有:
Vector3f x = A.colPivHouseholderQr().solve(b);
^^^^^^^^
您有:
auto x = A.colPivHouseholderQr().solve(b);
^^^^^
在这种情况下,这是一个非常显著的区别,因为solve()
的返回类型不是Vector3f
。这是一种中间的无法表达的类型——我们正在构建一个表达式模板,以便稍后进行工作。但该表达式模板保留了一堆中间引用,如果您不立即解析它们,这些引用就会挂起。
来自Eigen文档:
简而言之:不要在Eigen的表达式中使用
auto
关键字,除非你100%确定自己在做什么。特别是,不要使用auto关键字来替换Matrix<>
类型。
相关文章:
- 是否删除在对象构造过程中创建的对象
- 如何在鼠标挂钩过程中检测拖动
- clang 插件:在编译过程中修改 AST
- Adafruit 羽毛RFM69HCW在使用过程中会冻结,需要硬重置
- 线程过程中的线程同步问题
- MySQL 连接器/C++.尝试在此过程中停止连接到数据库
- C++ 在编译过程中 strtok 函数 Eclipse 说没有在范围内声明?
- 在调用过程中删除 std::函数
- 动态矩阵特征分解过程中的误差
- cmake在构建过程中使用另一个工具检测标志
- 在模板参数推导过程中丢失限定符
- 升压过程中的管道缓冲区大小
- 如何在插入过程中更改插入的值
- 在PostgreSQL函数或过程中使用C++代码
- Vulkan:设备在缓冲区复制过程中丢失
- 如何解决导入dll过程中Viual Studio中的并排配置错误
- C++:如何在对象构造过程中调用初始值设定项列表之外的成员构造函数
- Node中的子节点列表在C++中的BFS遍历过程中丢失
- 复制文件的最佳方式是什么,以便我可以在复制过程中轻松取消复制?
- chrono::duration_cast在比率相乘过程中的问题