SparseTriangularView.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) 2009 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // Eigen is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 3 of the License, or (at your option) any later version.
10 //
11 // Alternatively, you can redistribute it and/or
12 // modify it under the terms of the GNU General Public License as
13 // published by the Free Software Foundation; either version 2 of
14 // the License, or (at your option) any later version.
15 //
16 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License and a copy of the GNU General Public License along with
23 // Eigen. If not, see <http://www.gnu.org/licenses/>.
24 
25 #ifndef EIGEN_SPARSE_TRIANGULARVIEW_H
26 #define EIGEN_SPARSE_TRIANGULARVIEW_H
27 
28 namespace Eigen {
29 
30 namespace internal {
31 
32 template<typename MatrixType, int Mode>
33 struct traits<SparseTriangularView<MatrixType,Mode> >
34 : public traits<MatrixType>
35 {};
36 
37 } // namespace internal
38 
39 template<typename MatrixType, int Mode> class SparseTriangularView
40  : public SparseMatrixBase<SparseTriangularView<MatrixType,Mode> >
41 {
42  enum { SkipFirst = ((Mode&Lower) && !(MatrixType::Flags&RowMajorBit))
43  || ((Mode&Upper) && (MatrixType::Flags&RowMajorBit)),
44  SkipLast = !SkipFirst,
45  HasUnitDiag = (Mode&UnitDiag) ? 1 : 0
46  };
47 
48  public:
49 
51 
52  class InnerIterator;
53  class ReverseInnerIterator;
54 
55  inline Index rows() const { return m_matrix.rows(); }
56  inline Index cols() const { return m_matrix.cols(); }
57 
58  typedef typename MatrixType::Nested MatrixTypeNested;
59  typedef typename internal::remove_reference<MatrixTypeNested>::type MatrixTypeNestedNonRef;
60  typedef typename internal::remove_all<MatrixTypeNested>::type MatrixTypeNestedCleaned;
61 
62  inline SparseTriangularView(const MatrixType& matrix) : m_matrix(matrix) {}
63 
65  inline const MatrixTypeNestedCleaned& nestedExpression() const { return m_matrix; }
66 
67  template<typename OtherDerived>
69  solve(const MatrixBase<OtherDerived>& other) const;
70 
71  template<typename OtherDerived> void solveInPlace(MatrixBase<OtherDerived>& other) const;
72  template<typename OtherDerived> void solveInPlace(SparseMatrixBase<OtherDerived>& other) const;
73 
74  protected:
75  MatrixTypeNested m_matrix;
76 };
77 
78 template<typename MatrixType, int Mode>
79 class SparseTriangularView<MatrixType,Mode>::InnerIterator : public MatrixTypeNestedCleaned::InnerIterator
80 {
81  typedef typename MatrixTypeNestedCleaned::InnerIterator Base;
82  public:
83 
84  EIGEN_STRONG_INLINE InnerIterator(const SparseTriangularView& view, Index outer)
85  : Base(view.nestedExpression(), outer), m_returnOne(false)
86  {
87  if(SkipFirst)
88  {
89  while((*this) && (HasUnitDiag ? this->index()<=outer : this->index()<outer))
90  Base::operator++();
91  if(HasUnitDiag)
92  m_returnOne = true;
93  }
94  else if(HasUnitDiag && ((!Base::operator bool()) || Base::index()>=Base::outer()))
95  {
96  if((!SkipFirst) && Base::operator bool())
97  Base::operator++();
98  m_returnOne = true;
99  }
100  }
101 
102  EIGEN_STRONG_INLINE InnerIterator& operator++()
103  {
104  if(HasUnitDiag && m_returnOne)
105  m_returnOne = false;
106  else
107  {
108  Base::operator++();
109  if(HasUnitDiag && (!SkipFirst) && ((!Base::operator bool()) || Base::index()>=Base::outer()))
110  {
111  if((!SkipFirst) && Base::operator bool())
112  Base::operator++();
113  m_returnOne = true;
114  }
115  }
116  return *this;
117  }
118 
119  inline Index row() const { return Base::row(); }
120  inline Index col() const { return Base::col(); }
121  inline Index index() const
122  {
123  if(HasUnitDiag && m_returnOne) return Base::outer();
124  else return Base::index();
125  }
126  inline Scalar value() const
127  {
128  if(HasUnitDiag && m_returnOne) return Scalar(1);
129  else return Base::value();
130  }
131 
132  EIGEN_STRONG_INLINE operator bool() const
133  {
134  if(HasUnitDiag && m_returnOne)
135  return true;
136  return (SkipFirst ? Base::operator bool() : (Base::operator bool() && this->index() <= this->outer()));
137  }
138  protected:
139  bool m_returnOne;
140 };
141 
142 template<typename MatrixType, int Mode>
143 class SparseTriangularView<MatrixType,Mode>::ReverseInnerIterator : public MatrixTypeNestedCleaned::ReverseInnerIterator
144 {
145  typedef typename MatrixTypeNestedCleaned::ReverseInnerIterator Base;
146  public:
147 
148  EIGEN_STRONG_INLINE ReverseInnerIterator(const SparseTriangularView& view, Index outer)
149  : Base(view.nestedExpression(), outer)
150  {
151  eigen_assert((!HasUnitDiag) && "ReverseInnerIterator does not support yet triangular views with a unit diagonal");
152  if(SkipLast)
153  while((*this) && this->index()>outer)
154  --(*this);
155  }
156 
157  EIGEN_STRONG_INLINE InnerIterator& operator--()
158  { Base::operator--(); return *this; }
159 
160  inline Index row() const { return Base::row(); }
161  inline Index col() const { return Base::col(); }
162 
163  EIGEN_STRONG_INLINE operator bool() const
164  {
165  return SkipLast ? Base::operator bool() : (Base::operator bool() && this->index() >= this->outer());
166  }
167 };
168 
169 template<typename Derived>
170 template<int Mode>
171 inline const SparseTriangularView<Derived, Mode>
173 {
174  return derived();
175 }
176 
177 } // end namespace Eigen
178 
179 #endif // EIGEN_SPARSE_TRIANGULARVIEW_H