diff --git a/Eigen/Sparse b/Eigen/Sparse index f027d3e6c..bca1c4ceb 100644 --- a/Eigen/Sparse +++ b/Eigen/Sparse @@ -111,6 +111,7 @@ struct Sparse {}; #include "src/Sparse/SparseLLT.h" #include "src/Sparse/SparseLDLT.h" #include "src/Sparse/SparseLU.h" +#include "src/Sparse/SparseView.h" #ifdef EIGEN_CHOLMOD_SUPPORT # include "src/Sparse/CholmodSupport.h" diff --git a/Eigen/src/Core/MatrixBase.h b/Eigen/src/Core/MatrixBase.h index cc35800bf..1829ece7a 100644 --- a/Eigen/src/Core/MatrixBase.h +++ b/Eigen/src/Core/MatrixBase.h @@ -215,6 +215,8 @@ template class MatrixBase template SelfAdjointView selfadjointView(); template const SelfAdjointView selfadjointView() const; + const SparseView sparseView(const Scalar m_reference = Scalar(0), + typename NumTraits::Real m_epsilon = NumTraits::dummy_precision()) const; static const IdentityReturnType Identity(); static const IdentityReturnType Identity(Index rows, Index cols); static const BasisReturnType Unit(Index size, Index i); diff --git a/Eigen/src/Core/util/ForwardDeclarations.h b/Eigen/src/Core/util/ForwardDeclarations.h index 6a9a7941c..4c113e9cc 100644 --- a/Eigen/src/Core/util/ForwardDeclarations.h +++ b/Eigen/src/Core/util/ForwardDeclarations.h @@ -86,6 +86,7 @@ template class TriangularBase; template class TriangularView; template class SelfAdjointView; +template class SparseView; template class WithFormat; template struct CommaInitializer; template class ReturnByValue; diff --git a/Eigen/src/Sparse/SparseUtil.h b/Eigen/src/Sparse/SparseUtil.h index f780f4087..deaf70bc8 100644 --- a/Eigen/src/Sparse/SparseUtil.h +++ b/Eigen/src/Sparse/SparseUtil.h @@ -114,7 +114,7 @@ template class SparseInnerVectorSet; template class SparseTriangularView; template class SparseSelfAdjointView; template class SparseDiagonalProduct; - +template class SparseView; template class SparseProduct; template class SparseTimeDenseProduct; diff --git a/Eigen/src/Sparse/SparseView.h b/Eigen/src/Sparse/SparseView.h new file mode 100644 index 000000000..32678b9ec --- /dev/null +++ b/Eigen/src/Sparse/SparseView.h @@ -0,0 +1,95 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. +// +// Copyright (C) 2010 Gael Guennebaud +// Copyright (C) 2010 Daniel Lowengrub +// +// Eigen is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 3 of the License, or (at your option) any later version. +// +// Alternatively, you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License and a copy of the GNU General Public License along with +// Eigen. If not, see . + +#ifndef EIGEN_SPARSEVIEW_H +#define EIGEN_SPARSEVIEW_H + +template +struct ei_traits > : ei_traits {}; + +template +class SparseView : public SparseMatrixBase > +{ +public: + EIGEN_SPARSE_PUBLIC_INTERFACE(SparseView); + + SparseView(const MatrixType& mat, const Scalar m_reference = Scalar(0), + typename NumTraits::Real m_epsilon = NumTraits::dummy_precision()) : + m_matrix(mat), m_reference(m_reference), m_epsilon(m_epsilon) {} + class InnerIterator; + + inline Index rows() const { return m_matrix.rows(); } + inline Index cols() const { return m_matrix.cols(); } + + inline Index innerSize() const { return m_matrix.innerSize(); } + inline Index outerSize() const { return m_matrix.outerSize(); } + +protected: + const typename MatrixType::Nested m_matrix; + Scalar m_reference; + typename NumTraits::Real m_epsilon; +}; + +template +class SparseView::InnerIterator : public MatrixType::InnerIterator +{ +public: + typedef typename MatrixType::InnerIterator IterBase; + InnerIterator(const SparseView& view, Index outer) : + DenseBase::InnerIterator(view.m_matrix, outer), m_view(view) + { + incrementToNonZero(); + } + + EIGEN_STRONG_INLINE InnerIterator& operator++() + { + IterBase::operator++(); + incrementToNonZero(); + return *this; + } + + using IterBase::value; + +protected: + const SparseView& m_view; + +private: + void incrementToNonZero() + { + while(ei_isMuchSmallerThan(value(), m_view.m_reference, m_view.m_epsilon) && (bool(*this))) + { + IterBase::operator++(); + } + } +}; + +template +const SparseView MatrixBase::sparseView(const Scalar m_reference, + typename NumTraits::Real m_epsilon) const +{ + return SparseView(derived(), m_reference, m_epsilon); +} + +#endif