diff options
-rw-r--r-- | Eigen/src/SparseCholesky/SimplicialCholesky.h | 2 | ||||
-rw-r--r-- | Eigen/src/SparseCore/SparseMatrix.h | 25 | ||||
-rw-r--r-- | test/sparse_basic.cpp | 8 |
3 files changed, 34 insertions, 1 deletions
diff --git a/Eigen/src/SparseCholesky/SimplicialCholesky.h b/Eigen/src/SparseCholesky/SimplicialCholesky.h index edc83a018..4aa366343 100644 --- a/Eigen/src/SparseCholesky/SimplicialCholesky.h +++ b/Eigen/src/SparseCholesky/SimplicialCholesky.h @@ -398,7 +398,7 @@ public: /** \returns the determinant of the underlying matrix from the current factorization */ Scalar determinant() const { - Scalar detL = Diagonal<const CholMatrixType>(Base::m_matrix).prod(); + Scalar detL = Base::m_matrix.diagonal().prod(); return internal::abs2(detL); } }; diff --git a/Eigen/src/SparseCore/SparseMatrix.h b/Eigen/src/SparseCore/SparseMatrix.h index d4530e4ce..f5c5a5be3 100644 --- a/Eigen/src/SparseCore/SparseMatrix.h +++ b/Eigen/src/SparseCore/SparseMatrix.h @@ -70,6 +70,28 @@ struct traits<SparseMatrix<_Scalar, _Options, _Index> > }; }; +template<typename _Scalar, int _Options, typename _Index, int DiagIndex> +struct traits<Diagonal<const SparseMatrix<_Scalar, _Options, _Index>, DiagIndex> > +{ + typedef SparseMatrix<_Scalar, _Options, _Index> MatrixType; + typedef typename nested<MatrixType>::type MatrixTypeNested; + typedef typename remove_reference<MatrixTypeNested>::type _MatrixTypeNested; + + typedef _Scalar Scalar; + typedef Dense StorageKind; + typedef _Index Index; + typedef MatrixXpr XprKind; + + enum { + RowsAtCompileTime = Dynamic, + ColsAtCompileTime = 1, + MaxRowsAtCompileTime = Dynamic, + MaxColsAtCompileTime = 1, + Flags = 0, + CoeffReadCost = _MatrixTypeNested::CoeffReadCost*10 + }; +}; + } // end namespace internal template<typename _Scalar, int _Options, typename _Index> @@ -514,6 +536,9 @@ class SparseMatrix m_data.resize(size); } + /** \returns a const expression of the diagonal coefficients */ + const Diagonal<const SparseMatrix> diagonal() const { return *this; } + /** Default constructor yielding an empty \c 0 \c x \c 0 matrix */ inline SparseMatrix() : m_outerSize(-1), m_innerSize(0), m_outerIndex(0), m_innerNonZeros(0) diff --git a/test/sparse_basic.cpp b/test/sparse_basic.cpp index 3a9cb61b3..276db65aa 100644 --- a/test/sparse_basic.cpp +++ b/test/sparse_basic.cpp @@ -345,6 +345,14 @@ template<typename SparseMatrixType> void sparse_basic(const SparseMatrixType& re initSparse<Scalar>(density, refMat2, m2); VERIFY_IS_APPROX(m2.eval(), refMat2.sparseView().eval()); } + + // test diagonal + { + DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows); + SparseMatrixType m2(rows, rows); + initSparse<Scalar>(density, refMat2, m2); + VERIFY_IS_APPROX(m2.diagonal(), refMat2.diagonal().eval()); + } } void test_sparse_basic() |