SparseView.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2011 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 2010 Daniel Lowengrub <lowdanie@gmail.com>
6 //
7 // Eigen is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Lesser General Public
9 // License as published by the Free Software Foundation; either
10 // version 3 of the License, or (at your option) any later version.
11 //
12 // Alternatively, you can redistribute it and/or
13 // modify it under the terms of the GNU General Public License as
14 // published by the Free Software Foundation; either version 2 of
15 // the License, or (at your option) any later version.
16 //
17 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public
23 // License and a copy of the GNU General Public License along with
24 // Eigen. If not, see <http://www.gnu.org/licenses/>.
25 
26 #ifndef EIGEN_SPARSEVIEW_H
27 #define EIGEN_SPARSEVIEW_H
28 
29 namespace Eigen {
30 
31 namespace internal {
32 
33 template<typename MatrixType>
34 struct traits<SparseView<MatrixType> > : traits<MatrixType>
35 {
36  typedef int Index;
37  typedef Sparse StorageKind;
38  enum {
40  };
41 };
42 
43 } // end namespace internal
44 
45 template<typename MatrixType>
46 class SparseView : public SparseMatrixBase<SparseView<MatrixType> >
47 {
48  typedef typename MatrixType::Nested MatrixTypeNested;
49  typedef typename internal::remove_all<MatrixTypeNested>::type _MatrixTypeNested;
50 public:
52 
53  SparseView(const MatrixType& mat, const Scalar& m_reference = Scalar(0),
54  typename NumTraits<Scalar>::Real m_epsilon = NumTraits<Scalar>::dummy_precision()) :
55  m_matrix(mat), m_reference(m_reference), m_epsilon(m_epsilon) {}
56 
57  class InnerIterator;
58 
59  inline Index rows() const { return m_matrix.rows(); }
60  inline Index cols() const { return m_matrix.cols(); }
61 
62  inline Index innerSize() const { return m_matrix.innerSize(); }
63  inline Index outerSize() const { return m_matrix.outerSize(); }
64 
65 protected:
66  MatrixTypeNested m_matrix;
67  Scalar m_reference;
69 };
70 
71 template<typename MatrixType>
72 class SparseView<MatrixType>::InnerIterator : public _MatrixTypeNested::InnerIterator
73 {
74 public:
75  typedef typename _MatrixTypeNested::InnerIterator IterBase;
76  InnerIterator(const SparseView& view, Index outer) :
77  IterBase(view.m_matrix, outer), m_view(view)
78  {
79  incrementToNonZero();
80  }
81 
82  EIGEN_STRONG_INLINE InnerIterator& operator++()
83  {
84  IterBase::operator++();
85  incrementToNonZero();
86  return *this;
87  }
88 
89  using IterBase::value;
90 
91 protected:
92  const SparseView& m_view;
93 
94 private:
95  void incrementToNonZero()
96  {
97  while((bool(*this)) && internal::isMuchSmallerThan(value(), m_view.m_reference, m_view.m_epsilon))
98  {
99  IterBase::operator++();
100  }
101  }
102 };
103 
104 template<typename Derived>
106  typename NumTraits<Scalar>::Real m_epsilon) const
107 {
108  return SparseView<Derived>(derived(), m_reference, m_epsilon);
109 }
110 
111 } // end namespace Eigen
112 
113 #endif