SparseView.h
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2011 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 // Copyright (C) 2010 Daniel Lowengrub <lowdanie@gmail.com>
00006 //
00007 // Eigen is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public
00009 // License as published by the Free Software Foundation; either
00010 // version 3 of the License, or (at your option) any later version.
00011 //
00012 // Alternatively, you can redistribute it and/or
00013 // modify it under the terms of the GNU General Public License as
00014 // published by the Free Software Foundation; either version 2 of
00015 // the License, or (at your option) any later version.
00016 //
00017 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00018 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00019 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00020 // GNU General Public License for more details.
00021 //
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License and a copy of the GNU General Public License along with
00024 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00025 
00026 #ifndef EIGEN_SPARSEVIEW_H
00027 #define EIGEN_SPARSEVIEW_H
00028 
00029 namespace Eigen { 
00030 
00031 namespace internal {
00032 
00033 template<typename MatrixType>
00034 struct traits<SparseView<MatrixType> > : traits<MatrixType>
00035 {
00036   typedef int Index;
00037   typedef Sparse StorageKind;
00038   enum {
00039     Flags = int(traits<MatrixType>::Flags) & (RowMajorBit)
00040   };
00041 };
00042 
00043 } // end namespace internal
00044 
00045 template<typename MatrixType>
00046 class SparseView : public SparseMatrixBase<SparseView<MatrixType> >
00047 {
00048   typedef typename MatrixType::Nested MatrixTypeNested;
00049   typedef typename internal::remove_all<MatrixTypeNested>::type _MatrixTypeNested;
00050 public:
00051   EIGEN_SPARSE_PUBLIC_INTERFACE(SparseView)
00052 
00053   SparseView(const MatrixType& mat, const Scalar& m_reference = Scalar(0),
00054              typename NumTraits<Scalar>::Real m_epsilon = NumTraits<Scalar>::dummy_precision()) : 
00055     m_matrix(mat), m_reference(m_reference), m_epsilon(m_epsilon) {}
00056 
00057   class InnerIterator;
00058 
00059   inline Index rows() const { return m_matrix.rows(); }
00060   inline Index cols() const { return m_matrix.cols(); }
00061 
00062   inline Index innerSize() const { return m_matrix.innerSize(); }
00063   inline Index outerSize() const { return m_matrix.outerSize(); }
00064 
00065 protected:
00066   MatrixTypeNested m_matrix;
00067   Scalar m_reference;
00068   typename NumTraits<Scalar>::Real m_epsilon;
00069 };
00070 
00071 template<typename MatrixType>
00072 class SparseView<MatrixType>::InnerIterator : public _MatrixTypeNested::InnerIterator
00073 {
00074 public:
00075   typedef typename _MatrixTypeNested::InnerIterator IterBase;
00076   InnerIterator(const SparseView& view, Index outer) :
00077   IterBase(view.m_matrix, outer), m_view(view)
00078   {
00079     incrementToNonZero();
00080   }
00081 
00082   EIGEN_STRONG_INLINE InnerIterator& operator++()
00083   {
00084     IterBase::operator++();
00085     incrementToNonZero();
00086     return *this;
00087   }
00088 
00089   using IterBase::value;
00090 
00091 protected:
00092   const SparseView& m_view;
00093 
00094 private:
00095   void incrementToNonZero()
00096   {
00097     while((bool(*this)) && internal::isMuchSmallerThan(value(), m_view.m_reference, m_view.m_epsilon))
00098     {
00099       IterBase::operator++();
00100     }
00101   }
00102 };
00103 
00104 template<typename Derived>
00105 const SparseView<Derived> MatrixBase<Derived>::sparseView(const Scalar& m_reference,
00106                                                           typename NumTraits<Scalar>::Real m_epsilon) const
00107 {
00108   return SparseView<Derived>(derived(), m_reference, m_epsilon);
00109 }
00110 
00111 } // end namespace Eigen
00112 
00113 #endif