aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Gael Guennebaud <g.gael@free.fr>2015-10-25 22:01:58 +0100
committerGravatar Gael Guennebaud <g.gael@free.fr>2015-10-25 22:01:58 +0100
commit8a211bb1a96ae7b0512a719e23058660af1bad2b (patch)
tree5f648f504600adbb287f0ce1a2831aa892a4d6a0
parentac6b2266b939200dfdcbe415b6ca592293f6d401 (diff)
bug #1088: fix setIdenity for non-compressed sparse-matrix
-rw-r--r--Eigen/src/SparseCore/SparseMatrix.h5
-rw-r--r--test/sparse_basic.cpp14
2 files changed, 18 insertions, 1 deletions
diff --git a/Eigen/src/SparseCore/SparseMatrix.h b/Eigen/src/SparseCore/SparseMatrix.h
index b27061f9e..272f1d7b4 100644
--- a/Eigen/src/SparseCore/SparseMatrix.h
+++ b/Eigen/src/SparseCore/SparseMatrix.h
@@ -729,7 +729,8 @@ class SparseMatrix
m_data.swap(other.m_data);
}
- /** Sets *this to the identity matrix */
+ /** Sets *this to the identity matrix.
+ * This function also turns the matrix into compressed mode, and drop any reserved memory. */
inline void setIdentity()
{
eigen_assert(rows() == cols() && "ONLY FOR SQUARED MATRICES");
@@ -737,6 +738,8 @@ class SparseMatrix
Eigen::Map<IndexVector>(&this->m_data.index(0), rows()).setLinSpaced(0, StorageIndex(rows()-1));
Eigen::Map<ScalarVector>(&this->m_data.value(0), rows()).setOnes();
Eigen::Map<IndexVector>(this->m_outerIndex, rows()+1).setLinSpaced(0, StorageIndex(rows()));
+ std::free(m_innerNonZeros);
+ m_innerNonZeros = 0;
}
inline SparseMatrix& operator=(const SparseMatrix& other)
{
diff --git a/test/sparse_basic.cpp b/test/sparse_basic.cpp
index 993f7840c..e8ebd7000 100644
--- a/test/sparse_basic.cpp
+++ b/test/sparse_basic.cpp
@@ -434,6 +434,20 @@ template<typename SparseMatrixType> void sparse_basic(const SparseMatrixType& re
SparseMatrixType m1(rows, rows);
m1.setIdentity();
VERIFY_IS_APPROX(m1, refMat1);
+ for(int k=0; k<rows*rows/4; ++k)
+ {
+ Index i = internal::random<Index>(0,rows-1);
+ Index j = internal::random<Index>(0,rows-1);
+ Index v = internal::random<Scalar>();
+ m1.coeffRef(i,j) = v;
+ refMat1.coeffRef(i,j) = v;
+ VERIFY_IS_APPROX(m1, refMat1);
+ if(internal::random<Index>(0,10)<2)
+ m1.makeCompressed();
+ }
+ m1.setIdentity();
+ refMat1.setIdentity();
+ VERIFY_IS_APPROX(m1, refMat1);
}
}