From 9a4b7998cfc743796850875914d94376ca0bc18e Mon Sep 17 00:00:00 2001 From: Gael Guennebaud Date: Thu, 15 Jan 2009 14:16:41 +0000 Subject: Sparse module: add row/col methods to the iterators --- Eigen/src/Sparse/CoreIterators.h | 234 +-------------------------------- Eigen/src/Sparse/MappedSparseMatrix.h | 5 +- Eigen/src/Sparse/SparseCwiseBinaryOp.h | 8 ++ Eigen/src/Sparse/SparseCwiseUnaryOp.h | 2 + Eigen/src/Sparse/SparseMatrix.h | 35 ++--- Eigen/src/Sparse/SparseVector.h | 6 +- 6 files changed, 41 insertions(+), 249 deletions(-) (limited to 'Eigen/src/Sparse') diff --git a/Eigen/src/Sparse/CoreIterators.h b/Eigen/src/Sparse/CoreIterators.h index d57eac287..f1520a585 100644 --- a/Eigen/src/Sparse/CoreIterators.h +++ b/Eigen/src/Sparse/CoreIterators.h @@ -33,12 +33,12 @@ * * todo */ -// template InnerIterator; // generic version for dense matrix and expressions template class MatrixBase::InnerIterator { typedef typename Derived::Scalar Scalar; + enum { IsRowMajor = (Derived::Flags&RowMajorBit)==RowMajorBit }; public: EIGEN_STRONG_INLINE InnerIterator(const Derived& expr, int outer) : m_expression(expr), m_inner(0), m_outer(outer), m_end(expr.rows()) @@ -46,13 +46,15 @@ template class MatrixBase::InnerIterator EIGEN_STRONG_INLINE Scalar value() const { - return (Derived::Flags&RowMajorBit) ? m_expression.coeff(m_outer, m_inner) - : m_expression.coeff(m_inner, m_outer); + return (IsRowMajor) ? m_expression.coeff(m_outer, m_inner) + : m_expression.coeff(m_inner, m_outer); } EIGEN_STRONG_INLINE InnerIterator& operator++() { m_inner++; return *this; } EIGEN_STRONG_INLINE int index() const { return m_inner; } + inline int row() const { return IsRowMajor ? m_outer : index(); } + inline int col() const { return IsRowMajor ? index() : m_outer; } EIGEN_STRONG_INLINE operator bool() const { return m_inner < m_end && m_inner>=0; } @@ -63,230 +65,4 @@ template class MatrixBase::InnerIterator const int m_end; }; - -/* -template -class Block::InnerIterator -{ - typedef typename Block::Scalar Scalar; - typedef typename ei_traits::_MatrixTypeNested _MatrixTypeNested; - typedef typename _MatrixTypeNested::InnerIterator MatrixTypeIterator; - public: - - EIGEN_STRONG_INLINE InnerIterator(const Block& block, int outer) - : m_iter(block.m_matrix,(Block::Flags&RowMajor) ? block.m_startRow.value() + outer : block.m_startCol.value() + outer), - m_start( (Block::Flags&RowMajor) ? block.m_startCol.value() : block.m_startRow.value()), - m_end(m_start + ((Block::Flags&RowMajor) ? block.m_blockCols.value() : block.m_blockRows.value())), - m_offset( (Block::Flags&RowMajor) ? block.m_startCol.value() : block.m_startRow.value()) - { - while (m_iter.index()>=0 && m_iter.index() -class Block::InnerIterator -{ - typedef typename Block::Scalar Scalar; - typedef typename ei_traits::_MatrixTypeNested _MatrixTypeNested; - typedef typename _MatrixTypeNested::InnerIterator MatrixTypeIterator; - public: - - EIGEN_STRONG_INLINE InnerIterator(const Block& block, int outer) - : m_iter(block.m_matrix,(Block::Flags&RowMajor) ? block.m_startRow.value() + outer : block.m_startCol.value() + outer), - m_start( (Block::Flags&RowMajor) ? block.m_startCol.value() : block.m_startRow.value()), - m_end(m_start + ((Block::Flags&RowMajor) ? block.m_blockCols.value() : block.m_blockRows.value())), - m_offset( (Block::Flags&RowMajor) ? block.m_startCol.value() : block.m_startRow.value()) - { - while (m_iter.index()>=0 && m_iter.index() struct ei_is_scalar_product { enum { ret = false }; }; -template struct ei_is_scalar_product > { enum { ret = true }; }; - -template -class CwiseBinaryOpInnerIterator; - -template -class CwiseBinaryOp::InnerIterator - : public CwiseBinaryOpInnerIterator::InnerIterator> -{ - typedef CwiseBinaryOpInnerIterator< - BinaryOp,Lhs,Rhs, typename CwiseBinaryOp::InnerIterator> Base; - public: - typedef typename CwiseBinaryOp::Scalar Scalar; - typedef typename ei_traits::_LhsNested _LhsNested; - typedef typename _LhsNested::InnerIterator LhsIterator; - typedef typename ei_traits::_RhsNested _RhsNested; - typedef typename _RhsNested::InnerIterator RhsIterator; -// public: - EIGEN_STRONG_INLINE InnerIterator(const CwiseBinaryOp& binOp, int outer) - : Base(binOp.m_lhs,binOp.m_rhs,binOp.m_functor,outer) - {} -};*/ -/* -template -class CwiseBinaryOpInnerIterator -{ - typedef CwiseBinaryOp ExpressionType; - typedef typename ExpressionType::Scalar Scalar; - typedef typename ei_traits::_LhsNested _LhsNested; -// typedef typename ei_traits::LhsIterator LhsIterator; - typedef typename ei_traits::_RhsNested _RhsNested; -// typedef typename ei_traits::RhsIterator RhsIterator; -// typedef typename ei_traits::_LhsNested _LhsNested; - typedef typename _LhsNested::InnerIterator LhsIterator; -// typedef typename ei_traits::_RhsNested _RhsNested; - typedef typename _RhsNested::InnerIterator RhsIterator; -// enum { IsProduct = ei_is_scalar_product::ret }; - public: - - EIGEN_STRONG_INLINE CwiseBinaryOpInnerIterator(const _LhsNested& lhs, const _RhsNested& rhs, - const BinaryOp& functor, int outer) - : m_lhsIter(lhs,outer), m_rhsIter(rhs,outer), m_functor(functor), m_id(-1) - { - this->operator++(); - } - - EIGEN_STRONG_INLINE Derived& operator++() - { - if (m_lhsIter && m_rhsIter && (m_lhsIter.index() == m_rhsIter.index())) - { - m_id = m_lhsIter.index(); - m_value = m_functor(m_lhsIter.value(), m_rhsIter.value()); - ++m_lhsIter; - ++m_rhsIter; - } - else if (m_lhsIter && (!m_rhsIter || (m_lhsIter.index() < m_rhsIter.index()))) - { - m_id = m_lhsIter.index(); - m_value = m_functor(m_lhsIter.value(), Scalar(0)); - ++m_lhsIter; - } - else if (m_rhsIter && (!m_lhsIter || (m_lhsIter.index() > m_rhsIter.index()))) - { - m_id = m_rhsIter.index(); - m_value = m_functor(Scalar(0), m_rhsIter.value()); - ++m_rhsIter; - } - else - { - m_id = -1; - } - return *static_cast(this); - } - - EIGEN_STRONG_INLINE Scalar value() const { return m_value; } - - EIGEN_STRONG_INLINE int index() const { return m_id; } - - EIGEN_STRONG_INLINE operator bool() const { return m_id>=0; } - - protected: - LhsIterator m_lhsIter; - RhsIterator m_rhsIter; - const BinaryOp& m_functor; - Scalar m_value; - int m_id; -}; - -template -class CwiseBinaryOpInnerIterator,Lhs,Rhs,Derived> -{ - typedef typename CwiseBinaryOp::Scalar Scalar; - typedef typename ei_traits::_LhsNested _LhsNested; - typedef typename _LhsNested::InnerIterator LhsIterator; - typedef typename ei_traits::_RhsNested _RhsNested; - typedef typename _RhsNested::InnerIterator RhsIterator; - public: - - EIGEN_STRONG_INLINE CwiseBinaryOpInnerIterator(const CwiseBinaryOp& binOp, int outer) - : m_lhsIter(binOp.m_lhs,outer), m_rhsIter(binOp.m_rhs,outer), m_functor(binOp.m_functor)//, m_id(-1) - { - //this->operator++(); - while (m_lhsIter && m_rhsIter && m_lhsIter.index() != m_rhsIter.index()) - { - if (m_lhsIter.index() < m_rhsIter.index()) - ++m_lhsIter; - else - ++m_rhsIter; - } - } - - EIGEN_STRONG_INLINE Derived& operator++() - { -// m_id = -1; - asm("#beginwhile"); - while (m_lhsIter && m_rhsIter) - { - if (m_lhsIter.index() == m_rhsIter.index()) - { -// m_id = m_lhsIter.index(); - //m_value = m_functor(m_lhsIter.value(), m_rhsIter.value()); - ++m_lhsIter; - ++m_rhsIter; - break; - } - else if (m_lhsIter.index() < m_rhsIter.index()) - ++m_lhsIter; - else - ++m_rhsIter; - } - asm("#endwhile"); - return *static_cast(this); - } - - EIGEN_STRONG_INLINE Scalar value() const { return m_functor(m_lhsIter.value(), m_rhsIter.value()); } - - EIGEN_STRONG_INLINE int index() const { return m_lhsIter.index(); } - - EIGEN_STRONG_INLINE operator bool() const { return m_lhsIter && m_rhsIter; } - - protected: - LhsIterator m_lhsIter; - RhsIterator m_rhsIter; - const BinaryOp& m_functor; -// Scalar m_value; -// int m_id; -};*/ - #endif // EIGEN_COREITERATORS_H diff --git a/Eigen/src/Sparse/MappedSparseMatrix.h b/Eigen/src/Sparse/MappedSparseMatrix.h index 55f76b41b..b4a12cf0d 100644 --- a/Eigen/src/Sparse/MappedSparseMatrix.h +++ b/Eigen/src/Sparse/MappedSparseMatrix.h @@ -140,7 +140,7 @@ class MappedSparseMatrix::InnerIterator { public: InnerIterator(const MappedSparseMatrix& mat, int outer) - : m_matrix(mat), m_id(mat._outerIndexPtr[outer]), m_start(m_id), m_end(mat._outerIndexPtr[outer+1]) + : m_matrix(mat), m_outer(outer), m_id(mat._outerIndexPtr[outer]), m_start(m_id), m_end(mat._outerIndexPtr[outer+1]) {} template @@ -155,11 +155,14 @@ class MappedSparseMatrix::InnerIterator inline Scalar& valueRef() { return const_cast(m_matrix._valuePtr[m_id]); } inline int index() const { return m_matrix._innerIndexPtr(m_id); } + inline int row() const { return IsRowMajor ? m_outer : index(); } + inline int col() const { return IsRowMajor ? index() : m_outer; } inline operator bool() const { return (m_id < m_end) && (m_id>=m_start); } protected: const MappedSparseMatrix& m_matrix; + const int m_outer; int m_id; const int m_start; const int m_end; diff --git a/Eigen/src/Sparse/SparseCwiseBinaryOp.h b/Eigen/src/Sparse/SparseCwiseBinaryOp.h index bdd19018b..1fc307b41 100644 --- a/Eigen/src/Sparse/SparseCwiseBinaryOp.h +++ b/Eigen/src/Sparse/SparseCwiseBinaryOp.h @@ -190,6 +190,8 @@ class ei_sparse_cwise_binary_op_inner_iterator_selector=0; } @@ -243,6 +245,8 @@ class ei_sparse_cwise_binary_op_inner_iterator_selector, EIGEN_STRONG_INLINE Scalar value() const { return m_functor(m_lhsIter.value(), m_rhsIter.value()); } EIGEN_STRONG_INLINE int index() const { return m_lhsIter.index(); } + EIGEN_STRONG_INLINE int row() const { return m_lhsIter.row(); } + EIGEN_STRONG_INLINE int col() const { return m_lhsIter.col(); } EIGEN_STRONG_INLINE operator bool() const { return (m_lhsIter && m_rhsIter); } @@ -279,6 +283,8 @@ class ei_sparse_cwise_binary_op_inner_iterator_selector, m_xpr.rhs().coeff(IsRowMajor?m_outer:m_lhsIter.index(),IsRowMajor?m_lhsIter.index():m_outer)); } EIGEN_STRONG_INLINE int index() const { return m_lhsIter.index(); } + EIGEN_STRONG_INLINE int row() const { return m_lhsIter.row(); } + EIGEN_STRONG_INLINE int col() const { return m_lhsIter.col(); } EIGEN_STRONG_INLINE operator bool() const { return m_lhsIter; } @@ -315,6 +321,8 @@ class ei_sparse_cwise_binary_op_inner_iterator_selector, { return m_functor(m_xpr.lhs().coeff(IsRowMajor?m_outer:m_rhsIter.index(),IsRowMajor?m_rhsIter.index():m_outer), m_rhsIter.value()); } EIGEN_STRONG_INLINE int index() const { return m_rhsIter.index(); } + EIGEN_STRONG_INLINE int row() const { return m_rhsIter.row(); } + EIGEN_STRONG_INLINE int col() const { return m_rhsIter.col(); } EIGEN_STRONG_INLINE operator bool() const { return m_rhsIter; } diff --git a/Eigen/src/Sparse/SparseCwiseUnaryOp.h b/Eigen/src/Sparse/SparseCwiseUnaryOp.h index e66baa0df..ff7c9c73f 100644 --- a/Eigen/src/Sparse/SparseCwiseUnaryOp.h +++ b/Eigen/src/Sparse/SparseCwiseUnaryOp.h @@ -82,6 +82,8 @@ class SparseCwiseUnaryOp::InnerIterator EIGEN_STRONG_INLINE Scalar value() const { return m_functor(m_iter.value()); } EIGEN_STRONG_INLINE int index() const { return m_iter.index(); } + EIGEN_STRONG_INLINE int row() const { return m_iter.row(); } + EIGEN_STRONG_INLINE int col() const { return m_iter.col(); } EIGEN_STRONG_INLINE operator bool() const { return m_iter; } diff --git a/Eigen/src/Sparse/SparseMatrix.h b/Eigen/src/Sparse/SparseMatrix.h index 4f077424a..d3b504839 100644 --- a/Eigen/src/Sparse/SparseMatrix.h +++ b/Eigen/src/Sparse/SparseMatrix.h @@ -61,8 +61,8 @@ class SparseMatrix protected: - enum { RowMajor = Base::IsRowMajor }; - typedef SparseMatrix TransposedSparseMatrix; + enum { IsRowMajor = Base::IsRowMajor }; + typedef SparseMatrix TransposedSparseMatrix; int m_outerSize; int m_innerSize; @@ -72,8 +72,8 @@ class SparseMatrix public: - inline int rows() const { return RowMajor ? m_outerSize : m_innerSize; } - inline int cols() const { return RowMajor ? m_innerSize : m_outerSize; } + inline int rows() const { return IsRowMajor ? m_outerSize : m_innerSize; } + inline int cols() const { return IsRowMajor ? m_innerSize : m_outerSize; } inline int innerSize() const { return m_innerSize; } inline int outerSize() const { return m_outerSize; } inline int innerNonZeros(int j) const { return m_outerIndex[j+1]-m_outerIndex[j]; } @@ -89,8 +89,8 @@ class SparseMatrix inline Scalar coeff(int row, int col) const { - const int outer = RowMajor ? row : col; - const int inner = RowMajor ? col : row; + const int outer = IsRowMajor ? row : col; + const int inner = IsRowMajor ? col : row; int start = m_outerIndex[outer]; int end = m_outerIndex[outer+1]; @@ -108,8 +108,8 @@ class SparseMatrix inline Scalar& coeffRef(int row, int col) { - const int outer = RowMajor ? row : col; - const int inner = RowMajor ? col : row; + const int outer = IsRowMajor ? row : col; + const int inner = IsRowMajor ? col : row; int start = m_outerIndex[outer]; int end = m_outerIndex[outer+1]; @@ -154,8 +154,8 @@ class SparseMatrix */ inline Scalar& fill(int row, int col) { - const int outer = RowMajor ? row : col; - const int inner = RowMajor ? col : row; + const int outer = IsRowMajor ? row : col; + const int inner = IsRowMajor ? col : row; if (m_outerIndex[outer+1]==0) { @@ -180,8 +180,8 @@ class SparseMatrix */ inline Scalar& fillrand(int row, int col) { - const int outer = RowMajor ? row : col; - const int inner = RowMajor ? col : row; + const int outer = IsRowMajor ? row : col; + const int inner = IsRowMajor ? col : row; if (m_outerIndex[outer+1]==0) { @@ -236,8 +236,8 @@ class SparseMatrix void resize(int rows, int cols) { // std::cerr << this << " resize " << rows << "x" << cols << "\n"; - const int outerSize = RowMajor ? rows : cols; - m_innerSize = RowMajor ? cols : rows; + const int outerSize = IsRowMajor ? rows : cols; + m_innerSize = IsRowMajor ? cols : rows; m_data.clear(); if (m_outerSize != outerSize) { @@ -390,12 +390,12 @@ class SparseMatrix::InnerIterator { public: InnerIterator(const SparseMatrix& mat, int outer) - : m_matrix(mat), m_id(mat.m_outerIndex[outer]), m_start(m_id), m_end(mat.m_outerIndex[outer+1]) + : m_matrix(mat), m_outer(outer), m_id(mat.m_outerIndex[outer]), m_start(m_id), m_end(mat.m_outerIndex[outer+1]) {} template InnerIterator(const Flagged& mat, int outer) - : m_matrix(mat._expression()), m_id(m_matrix.m_outerIndex[outer]), + : m_matrix(mat._expression()), m_outer(outer), m_id(m_matrix.m_outerIndex[outer]), m_start(m_id), m_end(m_matrix.m_outerIndex[outer+1]) {} @@ -405,11 +405,14 @@ class SparseMatrix::InnerIterator inline Scalar& valueRef() { return const_cast(m_matrix.m_data.value(m_id)); } inline int index() const { return m_matrix.m_data.index(m_id); } + inline int row() const { return IsRowMajor ? m_outer : index(); } + inline int col() const { return IsRowMajor ? index() : m_outer; } inline operator bool() const { return (m_id < m_end) && (m_id>=m_start); } protected: const SparseMatrix& m_matrix; + const int m_outer; int m_id; const int m_start; const int m_end; diff --git a/Eigen/src/Sparse/SparseVector.h b/Eigen/src/Sparse/SparseVector.h index f0bd888f3..901332e08 100644 --- a/Eigen/src/Sparse/SparseVector.h +++ b/Eigen/src/Sparse/SparseVector.h @@ -64,9 +64,7 @@ class SparseVector public: typedef SparseMatrixBase SparseBase; - enum { - IsColVector = ei_traits::IsColVector - }; + enum { IsColVector = ei_traits::IsColVector }; CompressedStorage m_data; int m_size; @@ -319,6 +317,8 @@ class SparseVector::InnerIterator inline Scalar& valueRef() { return const_cast(m_vector.m_data.value(m_id)); } inline int index() const { return m_vector.m_data.index(m_id); } + inline int row() const { return IsColVector ? index() : 0; } + inline int col() const { return IsColVector ? 0 : index(); } inline operator bool() const { return (m_id < m_end); } -- cgit v1.2.3