mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-04-16 14:49:39 +08:00
bug #1271: add SparseMatrix::coeffs() methods returning a 1D view of the non zero coefficients.
This commit is contained in:
parent
a93e354d92
commit
7e029d1d6e
@ -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() {}
|
||||
|
9
doc/snippets/SparseMatrix_coeffs.cpp
Normal file
9
doc/snippets/SparseMatrix_coeffs.cpp
Normal file
@ -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;
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user