Below is my code, matrix is a filled 2 x 2
sparse matrix:
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);
}
But when i called mosek function to put linear terms to the optimizer i got the assertion error
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)
Error message:
Eigen::DenseCoeffsBase::Scalar& Eigen::DenseCoeffsBase::operator()(Eigen::Index) [with Derived = Eigen::Matrix; Eigen::DenseCoeffsBase::Scalar = double; Eigen::Index = long int]: Assertion `index >= 0 && index < size()' failed.
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)
It will be no errors when I run the code like this, but I don't know why.
My matrix is constructed by the code below, it is an inverse matrix
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;
solver.compute(Mat);
matrix = solver.solve(I); // I is the identity matrix with the same dimension