diff --git a/Eigen/src/SparseCore/SparseMatrix.h b/Eigen/src/SparseCore/SparseMatrix.h index d93dffb48..072578132 100644 --- a/Eigen/src/SparseCore/SparseMatrix.h +++ b/Eigen/src/SparseCore/SparseMatrix.h @@ -677,9 +677,10 @@ class SparseMatrix inline void setIdentity() { eigen_assert(rows() == cols() && "ONLY FOR SQUARED MATRICES"); - this->setZero(); - for (int j = 0; j < rows(); j++) - this->insert(j,j) = Scalar(1.0); + this->m_data.resize(rows()); + Eigen::Map >(&this->m_data.index(0), rows()).setLinSpaced(0, rows()-1); + Eigen::Map >(&this->m_data.value(0), rows()).setOnes(); + Eigen::Map >(this->m_outerIndex, rows()+1).setLinSpaced(0, rows()); } inline SparseMatrix& operator=(const SparseMatrix& other) {