1

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
andy power
  • 11
  • 2
  • Please provide a [mcve]. Especially, show how `matrix` gets initialized. (And you can remove the `MSK_putcj` call -- unless the error happens inside that function) – chtz Aug 08 '17 at 13:52
  • 1
    Why are you using a sparse matrix if you are copying it to a set of dense vector? – ggael Aug 08 '17 at 18:41
  • Also make sure that `matrix.cols() == size` where `size` is the one of the second code snippet. – ggael Aug 08 '17 at 18:43
  • Hi my matrix is constructed by the bellow code, it is an inverse matrixEigen::SparseMatrix Mat(dim, dim); std::vector 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 > solver;solver.compute(Mat); matrix = solver.solve(I); // I is the identity – andy power Aug 09 '17 at 03:36
  • Your MCVE still requires lots of guessing (e.g., what are the types of `matrix` or `I`). Please read the link above again, on how to provide a good [mcve]. – chtz Aug 11 '17 at 14:18

0 Answers0