[eigen] Setting diagonal of a sparse matrix

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


How should one efficiently set the diagonal of an Eigen sparse matrix ?

The following program does not compile if I set
    USE_SPARSE_DIAGONAL 1

# include <Eigen/SparseCore>
# include <iostream>

# define USE_SPARSE_DIAGONAL 0
int main()
{   using Eigen::Dynamic;
Eigen::Matrix<double, 3, 3> D = Eigen::Matrix<double, 3, 3>::Identity();
    Eigen::SparseMatrix<double> S(3, 3);
    D.diagonal() = D.diagonal() / 2.0;
# if USE_SPARSE_DIAGONAL
    S.diagonal() = D.diagonal();
# else
    for(size_t i = 0; i < 3; i++)
        S.coeffRef(i, i) = D(i, i);
# endif
    std::cout << "S =\n" << S << "\n";
}
~



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/