Eigen  5.0.1
 
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
65 template <typename OtherDerived>
66 void solveInPlace(SparseMatrixBase<OtherDerived>& other) const;
67};
68
69namespace internal {
70
71template <typename ArgType, unsigned int Mode>
72struct unary_evaluator<TriangularView<ArgType, Mode>, IteratorBased> : evaluator_base<TriangularView<ArgType, Mode> > {
73 using XprType = TriangularView<ArgType, Mode>;
74
75 protected:
76 using Scalar = typename XprType::Scalar;
77 using StorageIndex = typename XprType::StorageIndex;
78 using EvalIterator = typename evaluator<ArgType>::InnerIterator;
79
80 enum {
81 SkipFirst =
82 ((Mode & Lower) && !(ArgType::Flags & RowMajorBit)) || ((Mode & Upper) && (ArgType::Flags & RowMajorBit)),
83 SkipLast = !SkipFirst,
84 SkipDiag = (Mode & ZeroDiag) ? 1 : 0,
85 HasUnitDiag = (Mode & UnitDiag) ? 1 : 0
86 };
87
88 public:
89 enum { CoeffReadCost = evaluator<ArgType>::CoeffReadCost, Flags = XprType::Flags };
90
91 explicit unary_evaluator(const XprType& xpr) : m_argImpl(xpr.nestedExpression()), m_arg(xpr.nestedExpression()) {}
92
93 inline Index nonZerosEstimate() const { return m_argImpl.nonZerosEstimate(); }
94
95 class InnerIterator : public EvalIterator {
96 using Base = EvalIterator;
97
98 public:
99 EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator& xprEval, Index outer)
100 : Base(xprEval.m_argImpl, outer),
101 m_returnOne(false),
102 m_containsDiag(Base::outer() < xprEval.m_arg.innerSize()) {
103 EIGEN_IF_CONSTEXPR (SkipFirst) {
104 while ((*this) && ((HasUnitDiag || SkipDiag) ? this->index() <= outer : this->index() < outer))
105 Base::operator++();
106 EIGEN_IF_CONSTEXPR (HasUnitDiag) m_returnOne = m_containsDiag;
107 } else EIGEN_IF_CONSTEXPR (HasUnitDiag) {
108 if ((!Base::operator bool()) || Base::index() >= Base::outer()) {
109 if (Base::operator bool()) Base::operator++();
110 m_returnOne = m_containsDiag;
111 }
112 }
113 }
114
115 EIGEN_STRONG_INLINE InnerIterator& operator++() {
116 EIGEN_IF_CONSTEXPR (HasUnitDiag) {
117 if (m_returnOne) {
118 m_returnOne = false;
119 return *this;
120 }
121 }
122 Base::operator++();
123 EIGEN_IF_CONSTEXPR (HasUnitDiag && !SkipFirst) {
124 if ((!Base::operator bool()) || Base::index() >= Base::outer()) {
125 if (Base::operator bool()) Base::operator++();
126 m_returnOne = m_containsDiag;
127 }
128 }
129 return *this;
130 }
131
132 EIGEN_STRONG_INLINE operator bool() const {
133 EIGEN_IF_CONSTEXPR (HasUnitDiag) {
134 if (m_returnOne) return true;
135 }
136 EIGEN_IF_CONSTEXPR (SkipFirst) {
137 return Base::operator bool();
138 } else {
139 EIGEN_IF_CONSTEXPR (SkipDiag) {
140 return (Base::operator bool() && this->index() < this->outer());
141 } else {
142 return (Base::operator bool() && this->index() <= this->outer());
143 }
144 }
145 }
146
147 inline Index row() const { return (ArgType::Flags & RowMajorBit ? Base::outer() : this->index()); }
148 inline Index col() const { return (ArgType::Flags & RowMajorBit ? this->index() : Base::outer()); }
149 inline StorageIndex index() const {
150 EIGEN_IF_CONSTEXPR (HasUnitDiag) {
151 if (m_returnOne) return internal::convert_index<StorageIndex>(Base::outer());
152 }
153 return Base::index();
154 }
155 inline Scalar value() const {
156 EIGEN_IF_CONSTEXPR (HasUnitDiag) {
157 if (m_returnOne) return Scalar(1);
158 }
159 return Base::value();
160 }
161
162 protected:
163 bool m_returnOne;
164 bool m_containsDiag;
165
166 private:
167 Scalar& valueRef();
168 };
169
170 protected:
171 evaluator<ArgType> m_argImpl;
172 const ArgType& m_arg;
173};
174
175} // end namespace internal
176
177template <typename Derived>
178template <int Mode>
179inline const TriangularView<const Derived, Mode> SparseMatrixBase<Derived>::triangularView() const {
180 return TriangularView<const Derived, Mode>(derived());
181}
182
183} // end namespace Eigen
184
185#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