Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
SparseTriangularView.h
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// SPDX-License-Identifier: MPL-2.0
11
12#ifndef EIGEN_SPARSE_TRIANGULARVIEW_H
13#define EIGEN_SPARSE_TRIANGULARVIEW_H
14
15// IWYU pragma: private
16#include "./InternalHeaderCheck.h"
17
18namespace Eigen {
19
30template <typename MatrixType, unsigned int Mode>
31class TriangularViewImpl<MatrixType, Mode, Sparse> : public SparseMatrixBase<TriangularView<MatrixType, Mode> > {
32 using TriangularViewType = TriangularView<MatrixType, Mode>;
33
34 protected:
35 // dummy solve function to make TriangularView happy.
36 void solve() const;
37
38 using Base = SparseMatrixBase<TriangularViewType>;
39
40 public:
41 EIGEN_SPARSE_PUBLIC_INTERFACE(TriangularViewType)
42
43 using MatrixTypeNested = typename MatrixType::Nested;
44 using MatrixTypeNestedNonRef = std::remove_reference_t<MatrixTypeNested>;
45 using MatrixTypeNestedCleaned = internal::remove_all_t<MatrixTypeNested>;
46
47 template <typename RhsType, typename DstType>
48 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void _solve_impl(const RhsType& rhs, DstType& dst) const {
49 EIGEN_IF_CONSTEXPR ((std::is_same<RhsType, DstType>::value)) {
50 if (internal::extract_data(dst) != internal::extract_data(rhs)) dst = rhs;
51 } else {
52 dst = rhs;
53 }
54 this->solveInPlace(dst);
55 }
56
57 EIGEN_DEVICE_FUNC constexpr Index rows() const noexcept { return derived().nestedExpression().rows(); }
58 EIGEN_DEVICE_FUNC constexpr Index cols() const noexcept { return derived().nestedExpression().cols(); }
59
61 template <typename OtherDerived>
63
68 template <typename OtherDerived>
69 void solveInPlace(SparseMatrixBase<OtherDerived>& other) const;
70};
71
72namespace internal {
73
74template <typename ArgType, unsigned int Mode>
75struct unary_evaluator<TriangularView<ArgType, Mode>, IteratorBased> : evaluator_base<TriangularView<ArgType, Mode> > {
76 using XprType = TriangularView<ArgType, Mode>;
77
78 protected:
79 using Scalar = typename XprType::Scalar;
80 using StorageIndex = typename XprType::StorageIndex;
81 using EvalIterator = typename evaluator<ArgType>::InnerIterator;
82
83 enum {
84 SkipFirst =
85 ((Mode & Lower) && !(ArgType::Flags & RowMajorBit)) || ((Mode & Upper) && (ArgType::Flags & RowMajorBit)),
86 SkipLast = !SkipFirst,
87 SkipDiag = (Mode & ZeroDiag) ? 1 : 0,
88 HasUnitDiag = (Mode & UnitDiag) ? 1 : 0
89 };
90
91 public:
92 enum { CoeffReadCost = evaluator<ArgType>::CoeffReadCost, Flags = XprType::Flags };
93
94 explicit unary_evaluator(const XprType& xpr) : m_argImpl(xpr.nestedExpression()), m_arg(xpr.nestedExpression()) {}
95
96 inline Index nonZerosEstimate() const { return m_argImpl.nonZerosEstimate(); }
97
98 class InnerIterator : public EvalIterator {
99 using Base = EvalIterator;
100
101 public:
102 EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator& xprEval, Index outer)
103 : Base(xprEval.m_argImpl, outer),
104 m_returnOne(false),
105 m_containsDiag(Base::outer() < xprEval.m_arg.innerSize()) {
106 EIGEN_IF_CONSTEXPR (SkipFirst) {
107 while ((*this) && ((HasUnitDiag || SkipDiag) ? this->index() <= outer : this->index() < outer))
108 Base::operator++();
109 EIGEN_IF_CONSTEXPR (HasUnitDiag) m_returnOne = m_containsDiag;
110 } else EIGEN_IF_CONSTEXPR (HasUnitDiag) {
111 if ((!Base::operator bool()) || Base::index() >= Base::outer()) {
112 if (Base::operator bool()) Base::operator++();
113 m_returnOne = m_containsDiag;
114 }
115 }
116 }
117
118 EIGEN_STRONG_INLINE InnerIterator& operator++() {
119 EIGEN_IF_CONSTEXPR (HasUnitDiag) {
120 if (m_returnOne) {
121 m_returnOne = false;
122 return *this;
123 }
124 }
125 // Once the diagonal has been returned and the entries are exhausted, the iterator stays at its end. Otherwise
126 // the code below would arm the unit diagonal again, as Base is then past the diagonal.
127 EIGEN_IF_CONSTEXPR (HasUnitDiag && !SkipFirst) {
128 if (!(*this)) return *this;
129 }
130 Base::operator++();
131 EIGEN_IF_CONSTEXPR (HasUnitDiag && !SkipFirst) {
132 if ((!Base::operator bool()) || Base::index() >= Base::outer()) {
133 if (Base::operator bool()) Base::operator++();
134 m_returnOne = m_containsDiag;
135 }
136 }
137 return *this;
138 }
139
140 EIGEN_STRONG_INLINE operator bool() const {
141 EIGEN_IF_CONSTEXPR (HasUnitDiag) {
142 if (m_returnOne) return true;
143 }
144 EIGEN_IF_CONSTEXPR (SkipFirst) {
145 return Base::operator bool();
146 } else {
147 EIGEN_IF_CONSTEXPR (SkipDiag) {
148 return (Base::operator bool() && this->index() < this->outer());
149 } else {
150 return (Base::operator bool() && this->index() <= this->outer());
151 }
152 }
153 }
154
155 inline Index row() const { return (ArgType::Flags & RowMajorBit ? Base::outer() : Index(this->index())); }
156 inline Index col() const { return (ArgType::Flags & RowMajorBit ? Index(this->index()) : Base::outer()); }
157 inline StorageIndex index() const {
158 EIGEN_IF_CONSTEXPR (HasUnitDiag) {
159 if (m_returnOne) return internal::convert_index<StorageIndex>(Base::outer());
160 }
161 return Base::index();
162 }
163 inline Scalar value() const {
164 EIGEN_IF_CONSTEXPR (HasUnitDiag) {
165 if (m_returnOne) return Scalar(1);
166 }
167 return Base::value();
168 }
169
170 protected:
171 bool m_returnOne;
172 bool m_containsDiag;
173
174 private:
175 Scalar& valueRef();
176 };
177
178 protected:
179 evaluator<ArgType> m_argImpl;
180 const ArgType& m_arg;
181};
182
183} // end namespace internal
184
185template <typename Derived>
186template <int Mode>
187inline const TriangularView<const Derived, Mode> SparseMatrixBase<Derived>::triangularView() const {
188 return TriangularView<const Derived, Mode>(derived());
189}
190
191} // end namespace Eigen
192
193#endif // EIGEN_SPARSE_TRIANGULARVIEW_H
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:53
void solveInPlace(MatrixBase< OtherDerived > &other) const
void solveInPlace(SparseMatrixBase< OtherDerived > &other) const
Expression of a triangular part in a matrix.
Definition TriangularMatrix.h:426
@ UnitDiag
Definition Constants.h:216
@ ZeroDiag
Definition Constants.h:218
@ Lower
Definition Constants.h:212
@ Upper
Definition Constants.h:214
constexpr unsigned int RowMajorBit
Definition Constants.h:71
constexpr TriangularView< MatrixType, Mode > & derived()
Definition EigenBase.h:50
Definition Constants.h:545