1c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// This file is part of Eigen, a lightweight C++ template library
2c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// for linear algebra.
3c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//
4c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// Copyright (C) 2009 Gael Guennebaud <gael.guennebaud@inria.fr>
57faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez// Copyright (C) 2012 Désiré Nuentsa-Wakam <desire.nuentsa_wakam@inria.fr>
6c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//
7c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// This Source Code Form is subject to the terms of the Mozilla
8c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// Public License v. 2.0. If a copy of the MPL was not distributed
9c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
11c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#ifndef EIGEN_SPARSE_TRIANGULARVIEW_H
12c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#define EIGEN_SPARSE_TRIANGULARVIEW_H
13c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
14c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathnamespace Eigen {
15c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
16c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathnamespace internal {
17c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
18c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename MatrixType, int Mode>
19c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct traits<SparseTriangularView<MatrixType,Mode> >
20c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath: public traits<MatrixType>
21c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{};
22c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
23c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath} // namespace internal
24c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
25c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename MatrixType, int Mode> class SparseTriangularView
26c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  : public SparseMatrixBase<SparseTriangularView<MatrixType,Mode> >
27c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
28c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    enum { SkipFirst = ((Mode&Lower) && !(MatrixType::Flags&RowMajorBit))
29c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath                    || ((Mode&Upper) &&  (MatrixType::Flags&RowMajorBit)),
30c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath           SkipLast = !SkipFirst,
317faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez           SkipDiag = (Mode&ZeroDiag) ? 1 : 0,
32c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath           HasUnitDiag = (Mode&UnitDiag) ? 1 : 0
33c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    };
34c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
35c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  public:
36c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
37c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_SPARSE_PUBLIC_INTERFACE(SparseTriangularView)
38c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
39c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    class InnerIterator;
40c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    class ReverseInnerIterator;
41c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
42c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    inline Index rows() const { return m_matrix.rows(); }
43c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    inline Index cols() const { return m_matrix.cols(); }
44c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
45c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    typedef typename MatrixType::Nested MatrixTypeNested;
46c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    typedef typename internal::remove_reference<MatrixTypeNested>::type MatrixTypeNestedNonRef;
47c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    typedef typename internal::remove_all<MatrixTypeNested>::type MatrixTypeNestedCleaned;
48c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
49c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    inline SparseTriangularView(const MatrixType& matrix) : m_matrix(matrix) {}
50c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
51c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /** \internal */
52c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    inline const MatrixTypeNestedCleaned& nestedExpression() const { return m_matrix; }
53c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
54c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<typename OtherDerived>
55c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    typename internal::plain_matrix_type_column_major<OtherDerived>::type
56c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    solve(const MatrixBase<OtherDerived>& other) const;
57c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
58c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<typename OtherDerived> void solveInPlace(MatrixBase<OtherDerived>& other) const;
59c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<typename OtherDerived> void solveInPlace(SparseMatrixBase<OtherDerived>& other) const;
60c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
61c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  protected:
62c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    MatrixTypeNested m_matrix;
63c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
64c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
65c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename MatrixType, int Mode>
66c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathclass SparseTriangularView<MatrixType,Mode>::InnerIterator : public MatrixTypeNestedCleaned::InnerIterator
67c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
68c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    typedef typename MatrixTypeNestedCleaned::InnerIterator Base;
697faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    typedef typename SparseTriangularView::Index Index;
70c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  public:
71c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
72c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STRONG_INLINE InnerIterator(const SparseTriangularView& view, Index outer)
73c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      : Base(view.nestedExpression(), outer), m_returnOne(false)
74c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
75c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      if(SkipFirst)
76c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      {
777faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        while((*this) && ((HasUnitDiag||SkipDiag)  ? this->index()<=outer : this->index()<outer))
78c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath          Base::operator++();
79c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        if(HasUnitDiag)
80c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath          m_returnOne = true;
81c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      }
82c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      else if(HasUnitDiag && ((!Base::operator bool()) || Base::index()>=Base::outer()))
83c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      {
84c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        if((!SkipFirst) && Base::operator bool())
85c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath          Base::operator++();
86c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        m_returnOne = true;
87c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      }
88c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
89c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
90c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STRONG_INLINE InnerIterator& operator++()
91c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
92c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      if(HasUnitDiag && m_returnOne)
93c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        m_returnOne = false;
94c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      else
95c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      {
96c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        Base::operator++();
97c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        if(HasUnitDiag && (!SkipFirst) && ((!Base::operator bool()) || Base::index()>=Base::outer()))
98c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        {
99c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath          if((!SkipFirst) && Base::operator bool())
100c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath            Base::operator++();
101c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath          m_returnOne = true;
102c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        }
103c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      }
104c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      return *this;
105c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
106c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
1077faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    inline Index row() const { return (MatrixType::Flags&RowMajorBit ? Base::outer() : this->index()); }
1087faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    inline Index col() const { return (MatrixType::Flags&RowMajorBit ? this->index() : Base::outer()); }
109c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    inline Index index() const
110c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
111c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      if(HasUnitDiag && m_returnOne)  return Base::outer();
112c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      else                            return Base::index();
113c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
114c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    inline Scalar value() const
115c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
116c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      if(HasUnitDiag && m_returnOne)  return Scalar(1);
117c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      else                            return Base::value();
118c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
119c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
120c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STRONG_INLINE operator bool() const
121c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
122c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      if(HasUnitDiag && m_returnOne)
123c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        return true;
1247faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      if(SkipFirst) return  Base::operator bool();
1257faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      else
1267faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      {
1277faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        if (SkipDiag) return (Base::operator bool() && this->index() < this->outer());
1287faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        else return (Base::operator bool() && this->index() <= this->outer());
1297faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      }
130c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
131c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  protected:
132c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    bool m_returnOne;
133c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
134c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
135c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename MatrixType, int Mode>
136c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathclass SparseTriangularView<MatrixType,Mode>::ReverseInnerIterator : public MatrixTypeNestedCleaned::ReverseInnerIterator
137c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
138c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    typedef typename MatrixTypeNestedCleaned::ReverseInnerIterator Base;
1397faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    typedef typename SparseTriangularView::Index Index;
140c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  public:
141c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
142c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STRONG_INLINE ReverseInnerIterator(const SparseTriangularView& view, Index outer)
143c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      : Base(view.nestedExpression(), outer)
144c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
145c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      eigen_assert((!HasUnitDiag) && "ReverseInnerIterator does not support yet triangular views with a unit diagonal");
1467faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      if(SkipLast) {
1477faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        while((*this) && (SkipDiag ? this->index()>=outer : this->index()>outer))
148c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath          --(*this);
1497faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      }
150c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
151c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
1527faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    EIGEN_STRONG_INLINE ReverseInnerIterator& operator--()
153c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    { Base::operator--(); return *this; }
154c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
155c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    inline Index row() const { return Base::row(); }
156c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    inline Index col() const { return Base::col(); }
157c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
158c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STRONG_INLINE operator bool() const
159c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
1607faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      if (SkipLast) return Base::operator bool() ;
1617faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      else
1627faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      {
1637faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        if(SkipDiag) return (Base::operator bool() && this->index() > this->outer());
1647faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        else return (Base::operator bool() && this->index() >= this->outer());
1657faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      }
166c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
167c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
168c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
169c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Derived>
170c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<int Mode>
171c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathinline const SparseTriangularView<Derived, Mode>
172c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathSparseMatrixBase<Derived>::triangularView() const
173c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
174c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  return derived();
175c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
176c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
177c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath} // end namespace Eigen
178c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
179c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#endif // EIGEN_SPARSE_TRIANGULARVIEW_H
180