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-2015 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 2012 Désiré Nuentsa-Wakam <desire.nuentsa_wakam@inria.fr>
6 //
7 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 
11 #ifndef EIGEN_SPARSE_TRIANGULARVIEW_H
12 #define EIGEN_SPARSE_TRIANGULARVIEW_H
13 
14 // IWYU pragma: private
15 #include "./InternalHeaderCheck.h"
16 
17 namespace Eigen {
18 
29 template <typename MatrixType, unsigned int Mode>
30 class TriangularViewImpl<MatrixType, Mode, Sparse> : public SparseMatrixBase<TriangularView<MatrixType, Mode> > {
31  enum {
32  SkipFirst =
33  ((Mode & Lower) && !(MatrixType::Flags & RowMajorBit)) || ((Mode & Upper) && (MatrixType::Flags & RowMajorBit)),
34  SkipLast = !SkipFirst,
35  SkipDiag = (Mode & ZeroDiag) ? 1 : 0,
36  HasUnitDiag = (Mode & UnitDiag) ? 1 : 0
37  };
38 
40 
41  protected:
42  // dummy solve function to make TriangularView happy.
43  void solve() const;
44 
46 
47  public:
49 
50  typedef typename MatrixType::Nested MatrixTypeNested;
51  typedef std::remove_reference_t<MatrixTypeNested> MatrixTypeNestedNonRef;
53 
54  template <typename RhsType, typename DstType>
55  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void _solve_impl(const RhsType& rhs, DstType& dst) const {
57  dst = rhs;
58  this->solveInPlace(dst);
59  }
60 
62  template <typename OtherDerived>
64 
66  template <typename OtherDerived>
68 };
69 
70 namespace internal {
71 
72 template <typename ArgType, unsigned int Mode>
73 struct unary_evaluator<TriangularView<ArgType, Mode>, IteratorBased> : evaluator_base<TriangularView<ArgType, Mode> > {
75 
76  protected:
77  typedef typename XprType::Scalar Scalar;
78  typedef typename XprType::StorageIndex StorageIndex;
80 
81  enum {
82  SkipFirst =
83  ((Mode & Lower) && !(ArgType::Flags & RowMajorBit)) || ((Mode & Upper) && (ArgType::Flags & RowMajorBit)),
84  SkipLast = !SkipFirst,
85  SkipDiag = (Mode & ZeroDiag) ? 1 : 0,
86  HasUnitDiag = (Mode & UnitDiag) ? 1 : 0
87  };
88 
89  public:
90  enum { CoeffReadCost = evaluator<ArgType>::CoeffReadCost, Flags = XprType::Flags };
91 
92  explicit unary_evaluator(const XprType& xpr) : m_argImpl(xpr.nestedExpression()), m_arg(xpr.nestedExpression()) {}
93 
94  inline Index nonZerosEstimate() const { return m_argImpl.nonZerosEstimate(); }
95 
96  class InnerIterator : public EvalIterator {
97  typedef EvalIterator Base;
98 
99  public:
101  : Base(xprEval.m_argImpl, outer),
102  m_returnOne(false),
103  m_containsDiag(Base::outer() < xprEval.m_arg.innerSize()) {
104  if (SkipFirst) {
105  while ((*this) && ((HasUnitDiag || SkipDiag) ? this->index() <= outer : this->index() < outer))
107  if (HasUnitDiag) m_returnOne = m_containsDiag;
108  } else if (HasUnitDiag && ((!Base::operator bool()) || Base::index() >= Base::outer())) {
109  if ((!SkipFirst) && Base::operator bool()) Base::operator++();
110  m_returnOne = m_containsDiag;
111  }
112  }
113 
115  if (HasUnitDiag && m_returnOne)
116  m_returnOne = false;
117  else {
119  if (HasUnitDiag && (!SkipFirst) && ((!Base::operator bool()) || Base::index() >= Base::outer())) {
120  if ((!SkipFirst) && Base::operator bool()) Base::operator++();
121  m_returnOne = m_containsDiag;
122  }
123  }
124  return *this;
125  }
126 
127  EIGEN_STRONG_INLINE operator bool() const {
128  if (HasUnitDiag && m_returnOne) return true;
129  if (SkipFirst)
130  return Base::operator bool();
131  else {
132  if (SkipDiag)
133  return (Base::operator bool() && this->index() < this->outer());
134  else
135  return (Base::operator bool() && this->index() <= this->outer());
136  }
137  }
138 
139  inline Index row() const { return (ArgType::Flags & RowMajorBit ? Base::outer() : this->index()); }
140  inline Index col() const { return (ArgType::Flags & RowMajorBit ? this->index() : Base::outer()); }
141  inline StorageIndex index() const {
142  if (HasUnitDiag && m_returnOne)
143  return internal::convert_index<StorageIndex>(Base::outer());
144  else
145  return Base::index();
146  }
147  inline Scalar value() const {
148  if (HasUnitDiag && m_returnOne)
149  return Scalar(1);
150  else
151  return Base::value();
152  }
153 
154  protected:
157 
158  private:
160  };
161 
162  protected:
164  const ArgType& m_arg;
165 };
166 
167 } // end namespace internal
168 
169 template <typename Derived>
170 template <int Mode>
172  return TriangularView<const Derived, Mode>(derived());
173 }
174 
175 } // end namespace Eigen
176 
177 #endif // EIGEN_SPARSE_TRIANGULARVIEW_H
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:892
#define EIGEN_STRONG_INLINE
Definition: Macros.h:834
#define EIGEN_SPARSE_PUBLIC_INTERFACE(Derived)
Definition: SparseUtil.h:39
SCALAR Scalar
Definition: bench_gemm.cpp:45
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
An InnerIterator allows to loop over the element of any matrix expression.
Definition: CoreIterators.h:37
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
Base class of any sparse matrices or sparse expressions.
Definition: SparseMatrixBase.h:30
const TriangularView< const Derived, Mode > triangularView() const
Definition: SparseTriangularView.h:171
internal::remove_all_t< MatrixTypeNested > MatrixTypeNestedCleaned
Definition: SparseTriangularView.h:52
MatrixType::Nested MatrixTypeNested
Definition: SparseTriangularView.h:50
SparseMatrixBase< TriangularViewType > Base
Definition: SparseTriangularView.h:45
void solveInPlace(MatrixBase< OtherDerived > &other) const
void solveInPlace(SparseMatrixBase< OtherDerived > &other) const
std::remove_reference_t< MatrixTypeNested > MatrixTypeNestedNonRef
Definition: SparseTriangularView.h:51
TriangularView< MatrixType, Mode > TriangularViewType
Definition: SparseTriangularView.h:39
Definition: TriangularMatrix.h:163
Expression of a triangular part in a matrix.
Definition: TriangularMatrix.h:167
internal::traits< TriangularView >::Scalar Scalar
Definition: TriangularMatrix.h:170
EIGEN_STRONG_INLINE InnerIterator & operator++()
Definition: SparseTriangularView.h:114
EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator &xprEval, Index outer)
Definition: SparseTriangularView.h:100
@ UnitDiag
Definition: Constants.h:215
@ ZeroDiag
Definition: Constants.h:217
@ Lower
Definition: Constants.h:211
@ Upper
Definition: Constants.h:213
const unsigned int RowMajorBit
Definition: Constants.h:70
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 operator++(bfloat16 &a)
Definition: BFloat16.h:307
typename remove_all< T >::type remove_all_t
Definition: Meta.h:142
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE const T::Scalar * extract_data(const T &m)
Definition: BlasUtil.h:581
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
squared absolute value
Definition: GlobalFunctions.h:87
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
Extend namespace for flags.
Definition: fsi_chan_precond_driver.cc:56
Definition: Eigen_Colamd.h:49
Definition: Constants.h:522
Definition: Constants.h:580
Definition: CoreEvaluators.h:118
Definition: CoreEvaluators.h:104
Definition: Meta.h:205
XprType::StorageIndex StorageIndex
Definition: SparseTriangularView.h:78
Index nonZerosEstimate() const
Definition: SparseTriangularView.h:94
TriangularView< ArgType, Mode > XprType
Definition: SparseTriangularView.h:74
evaluator< ArgType > m_argImpl
Definition: SparseTriangularView.h:163
evaluator< ArgType >::InnerIterator EvalIterator
Definition: SparseTriangularView.h:79
unary_evaluator(const XprType &xpr)
Definition: SparseTriangularView.h:92
Definition: CoreEvaluators.h:82