From 8e050bd68163a64e0252eed66e215baafb6c11ca Mon Sep 17 00:00:00 2001 From: Desire NUENTSA Date: Wed, 22 May 2013 10:43:12 +0200 Subject: [PATCH] Optimize Sparse setIdentity and add a unit test --- Eigen/src/SparseCore/SparseMatrix.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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) {