下面是我的代码,矩阵是一个填充的2 x 2
稀疏矩阵:
int size = 2
std::vector<Eigen::VectorXd> eachRow(size);
for(unsigned int i = 0 ; i < size ; ++i)
{
Eigen::VectorXd Row(2);
Row = matrix.row(i);
eachRow.emplace_back(Row);
}
但是当我调用mosek函数将线性项放到优化器时我得到了断言错误
int row_index = 0;
for(int j=0 ; j < size ; ++j)
r = MSK_putcj(task, j, eachRow[row_index][j]); // MSK_putcj(task, int, double)
错误讯息:
征:: DenseCoeffsBase ::标量&安培; Eigen :: DenseCoeffsBase :: operator()(Eigen :: Index)[with Derived = Eigen :: Matrix; Eigen :: DenseCoeffsBase :: Scalar = double; Eigen :: Index = long int]:断言`index&gt; = 0&amp;&amp;指数&lt; size()'失败。
int row_index = 0;
Eigen::VectorXd Vec = eachRow[row_index];
for(int j=0 ; j < size ; ++j) r = MSK_putcj(task, j, Vec[j]); // MSK_putcj(task, int, double)
当我运行这样的代码时没有错误,但我不知道为什么。
我的矩阵由下面的代码构成,它是一个逆矩阵
Eigen::SparseMatrix<double> Mat(2, 2), matrix(2, 2), I(2, 2);
I.setIdentity();
std::vector<triplet> tripletList;
tripletList.emplace_back(triplet(0, 0, 1));
tripletList.emplace_back(triplet(0, 1, 2));
tripletList.emplace_back(triplet(1, 0, 2));
tripletList.emplace_back(triplet(1, 1, 5));
Mat.setFromTriplets(tripletList.begin(), tripletList.end());
Eigen::SimplicialLLT < Eigen::SparseMatrix<double> >
解算器;
solver.compute(MAT);
matrix = solver.solve(I); // I is the identity matrix with the same dimension