2016-03-04 67 views
0

我刚刚开始使用特征,但出于某种奇怪的原因,我正在努力应该很简单。下面的代码是我想要执行的一些类似计算的简化版本(在Ax = b中解决x)。无法访问本征线性系统解决方案的元素

输入:

auto N = 10; 
auto A = Matrix<Float, Dynamic, Dynamic>::Identity(N, N); 
auto b = Matrix<Float, Dynamic, 1>::Constant(N, 1, 1); 
std::cout << "A: " << std::endl 
      << A << std::endl 
      << "b: " << std::endl 
      << b << std::endl; 
auto x = A.fullPivLu().solve(b); 
std::cout << "x(" << x.rows() << ", " << x.cols() 
      << "): " << std::endl << x << std::endl; 

输出:

A: 
1 0 0 0 0 0 0 0 0 0 
0 1 0 0 0 0 0 0 0 0 
0 0 1 0 0 0 0 0 0 0 
0 0 0 1 0 0 0 0 0 0 
0 0 0 0 1 0 0 0 0 0 
0 0 0 0 0 1 0 0 0 0 
0 0 0 0 0 0 1 0 0 0 
0 0 0 0 0 0 0 1 0 0 
0 0 0 0 0 0 0 0 1 0 
0 0 0 0 0 0 0 0 0 1 
b: 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
x(10, 1): 
mouse: /home/jansen/devel/build/external/eigen/include/eigen3/Eigen/src/Core/Block.h:119: Eigen::Block<Eigen::Matrix<double, -1, -1, 0, -1, -1>, 1, -1, false>::Block(XprType &, Index) [XprType = Eigen::Matrix<double, -1, -1, 0, -1, -1>, BlockRows = 1, BlockCols = -1, InnerPanel = false]: Assertion `(i>=0) && (((BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) && i<xpr.rows()) ||((BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) && i<xpr.cols()))' failed. 
[1] 21192 abort (core dumped) ./src/mouse 

A和B以及形成与解x即使有合适的尺寸,但每当我尝试访问x的元素,我得到一个断言失败。从断言中我推断出现了某种出界错误,但我无法弄清楚为什么?

+0

你想访问什么元素?您是否使用正确的索引基础(看起来像是基于零的索引)。 – 1201ProgramAlarm

回答

3

请不要滥用auto与表达模板库,请参阅此page。通常情况下,你的情况,x不是Matrix<>对象,而是一个抽象的对象说A\b来计算......解决办法是这样的:

Matrix<Float, Dynamic, 1> x = A.fullPivLu().solve(b);