Search code examples
blocksparse-matrixeigeneigen3

How to instantiate an InnerIterator over a Sparse Block in Eigen 3.3.4?


I have a piece of code that works fine in Eigen 3.2 but is not valid in Eigen 3.3.4 anymore. Here is the code:

// Temporary Eigen blocks
Eigen::Block<const Eigen::SparseMatrix<double> > 
tmpAPotentialBlock(A.block(startPotential, startPotential, sizePotential,sizePotential)), 
tmpAFlowBlock(A.block(startFlow, startPotential, sizeFlow, sizePotential));

for (Eigen::SparseMatrix<double>::Index k=0; k<sizePotential; ++k) {
  // Iterator to the first term of the column k of the potential block and the flow block.
  Eigen::Block<const Eigen::SparseMatrix<double> >::InnerIterator itAPotential(tmpAPotentialBlock,k),
                                                                itAFlow(tmpAFlowBlock,k);
  ...
}

Basically the problem is that InnerIterator is no longer defined for blocks or at least sparse blocks.

I understand that you now need to use an evaluator to define this. Does anyone know what the new syntax would be ?


Solution

  • You need to write:

    Eigen::InnerIterator<SpBlock> it(tmp,k)
    

    Here is a self-contained C++11 example:

    using SpMat = Eigen::SparseMatrix<double>;
    using SpBlock = Eigen::Block<const SpMat>;
    SpMat A;
    Index i, s;
    SpBlock tmp(A, i, i, s, s);
    
    for (Eigen::Index k=0; k<s; ++k) {
        Eigen::InnerIterator<SpBlock> it(tmp,k);
        /* ... */
    }
    

    that can be make prettier in C++17:

    Eigen::SparseMatrix<double> A;
    Index i, s;
    auto tmp = A.block(i, i, s, s);
    
    for (Eigen::Index k=0; k<s; ++k) {
        Eigen::InnerIterator it(tmp,k);
        /* ... */
    }