aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Gael Guennebaud <g.gael@free.fr>2016-08-29 12:06:37 +0200
committerGravatar Gael Guennebaud <g.gael@free.fr>2016-08-29 12:06:37 +0200
commit7e029d1d6ec0eba495a31c72a328ee8160338e44 (patch)
treeebbaae9b73c903102c94ab1ef6ee01882ddcac0c
parenta93e354d9282cc76de899db412c79f730ff58345 (diff)
bug #1271: add SparseMatrix::coeffs() methods returning a 1D view of the non zero coefficients.
-rw-r--r--Eigen/src/SparseCore/SparseCompressedBase.h19
-rw-r--r--doc/snippets/SparseMatrix_coeffs.cpp9
-rw-r--r--test/sparse_basic.cpp10
3 files changed, 38 insertions, 0 deletions
diff --git a/Eigen/src/SparseCore/SparseCompressedBase.h b/Eigen/src/SparseCore/SparseCompressedBase.h
index 15854a73b..55ad91f46 100644
--- a/Eigen/src/SparseCore/SparseCompressedBase.h
+++ b/Eigen/src/SparseCore/SparseCompressedBase.h
@@ -106,6 +106,25 @@ class SparseCompressedBase
/** \returns whether \c *this is in compressed form. */
inline bool isCompressed() const { return innerNonZeroPtr()==0; }
+ /** \returns a read-only view of the stored coefficients as a 1D array expression.
+ *
+ * \warning this method is for \b compressed \b storage \b only, and it will trigger an assertion otherwise.
+ *
+ * \sa valuePtr(), isCompressed() */
+ const Map<const Array<Scalar,Dynamic,1> > coeffs() const { eigen_assert(isCompressed()); return Array<Scalar,Dynamic,1>::Map(valuePtr(),nonZeros()); }
+
+ /** \returns a read-write view of the stored coefficients as a 1D array expression
+ *
+ * \warning this method is for \b compressed \b storage \b only, and it will trigger an assertion otherwise.
+ *
+ * Here is an example:
+ * \include SparseMatrix_coeffs.cpp
+ * and the output is:
+ * \include SparseMatrix_coeffs.out
+ *
+ * \sa valuePtr(), isCompressed() */
+ Map<Array<Scalar,Dynamic,1> > coeffs() { eigen_assert(isCompressed()); return Array<Scalar,Dynamic,1>::Map(valuePtr(),nonZeros()); }
+
protected:
/** Default constructor. Do nothing. */
SparseCompressedBase() {}
diff --git a/doc/snippets/SparseMatrix_coeffs.cpp b/doc/snippets/SparseMatrix_coeffs.cpp
new file mode 100644
index 000000000..f71a69b07
--- /dev/null
+++ b/doc/snippets/SparseMatrix_coeffs.cpp
@@ -0,0 +1,9 @@
+SparseMatrix<double> A(3,3);
+A.insert(1,2) = 0;
+A.insert(0,1) = 1;
+A.insert(2,0) = 2;
+A.makeCompressed();
+cout << "The matrix A is:" << endl << MatrixXd(A) << endl;
+cout << "it has " << A.nonZeros() << " stored non zero coefficients that are: " << A.coeffs().transpose() << endl;
+A.coeffs() += 10;
+cout << "After adding 10 to every stored non zero coefficient, the matrix A is:" << endl << MatrixXd(A) << endl;
diff --git a/test/sparse_basic.cpp b/test/sparse_basic.cpp
index 10309b3a9..7b5f3eb38 100644
--- a/test/sparse_basic.cpp
+++ b/test/sparse_basic.cpp
@@ -207,6 +207,16 @@ template<typename SparseMatrixType> void sparse_basic(const SparseMatrixType& re
VERIFY_IS_APPROX((m1 = m1.transpose()), (refM1 = refM1.transpose().eval()));
VERIFY_IS_APPROX((m1 = -m1.transpose()), (refM1 = -refM1.transpose().eval()));
VERIFY_IS_APPROX((m1 += -m1), (refM1 += -refM1));
+
+ if(m1.isCompressed())
+ {
+ VERIFY_IS_APPROX(m1.coeffs().sum(), m1.sum());
+ m1.coeffs() += s1;
+ for(Index j = 0; j<m1.outerSize(); ++j)
+ for(typename SparseMatrixType::InnerIterator it(m1,j); it; ++it)
+ refM1(it.row(), it.col()) += s1;
+ VERIFY_IS_APPROX(m1, refM1);
+ }
}
// test transpose