Eigen  5.0.1
 
Loading...
Searching...
No Matches
RankRevealingBase.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// This Source Code Form is subject to the terms of the Mozilla
5// Public License v. 2.0. If a copy of the MPL was not distributed
6// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
7// SPDX-FileCopyrightText: The Eigen Authors
8// SPDX-License-Identifier: MPL-2.0
9
10#ifndef EIGEN_RANK_REVEALING_BASE_H
11#define EIGEN_RANK_REVEALING_BASE_H
12
13// IWYU pragma: private
14#include "./InternalHeaderCheck.h"
15
16namespace Eigen {
17
28template <typename Derived>
29class RankRevealingBase {
30 public:
31 using Scalar = typename internal::traits<Derived>::Scalar;
32 using RealScalar = typename NumTraits<Scalar>::Real;
33
34 RankRevealingBase()
35 : m_usePrescribedThreshold(false),
36 m_prescribedThreshold(RealScalar(0)),
37 m_maxpivot(RealScalar(0)),
38 m_nonzero_pivots(0) {}
39
57 Derived& setThreshold(const RealScalar& threshold) {
58 m_usePrescribedThreshold = true;
59 m_prescribedThreshold = threshold;
60 return self();
61 }
62
63 /** Allows to come back to the default behavior, letting Eigen use its default formula for
64 * determining the threshold.
65 *
66 * You should pass the special object Eigen::Default as parameter here.
67 * \code dec.setThreshold(Eigen::Default); \endcode
68 *
69 * See the documentation of setThreshold(const RealScalar&).
70 */
71 Derived& setThreshold(Default_t) {
72 m_usePrescribedThreshold = false;
73 return self();
74 }
76 /** Returns the threshold that will be used by certain methods such as rank().
77 *
78 * See the documentation of setThreshold(const RealScalar&).
79 */
80 RealScalar threshold() const {
81 eigen_assert(self().m_isInitialized || m_usePrescribedThreshold);
82 // Higham's backward error bound: ||ΔA||₂ ≤ c·min(m,n)·u·||A||₂.
83 // The factor of 4 covers the constant c.
84 return m_usePrescribedThreshold
85 ? m_prescribedThreshold
86 : NumTraits<Scalar>::epsilon() * RealScalar(4 * (std::min)(self().rows(), self().cols()));
87 }
88
95 inline Index rank() const {
96 using std::abs;
97 eigen_assert(self().m_isInitialized && "Decomposition is not initialized.");
98 RealScalar premultiplied_threshold = abs(m_maxpivot) * threshold();
99 Index result = 0;
100 for (Index i = 0; i < m_nonzero_pivots; ++i) result += (self().pivotCoeff(i) > premultiplied_threshold);
101 return result;
102 }
103
110 inline Index dimensionOfKernel() const {
111 eigen_assert(self().m_isInitialized && "Decomposition is not initialized.");
112 return self().cols() - rank();
113 }
114
122 inline bool isInjective() const {
123 eigen_assert(self().m_isInitialized && "Decomposition is not initialized.");
124 return rank() == self().cols();
125 }
126
134 inline bool isSurjective() const {
135 eigen_assert(self().m_isInitialized && "Decomposition is not initialized.");
136 return rank() == self().rows();
137 }
138
141 * \note This method has to determine which pivots should be considered nonzero.
142 * For that, it uses the threshold value that you can control by calling
143 * setThreshold(const RealScalar&).
144 */
145 inline bool isInvertible() const {
146 eigen_assert(self().m_isInitialized && "Decomposition is not initialized.");
157 inline Index nonzeroPivots() const {
158 eigen_assert(self().m_isInitialized && "Decomposition is not initialized.");
159 return m_nonzero_pivots;
160 }
161
165 RealScalar maxPivot() const { return m_maxpivot; }
166
167 protected:
168 bool m_usePrescribedThreshold;
169 RealScalar m_prescribedThreshold;
170 RealScalar m_maxpivot;
171 Index m_nonzero_pivots;
172
173 private:
174 Derived& self() { return static_cast<Derived&>(*this); }
175 const Derived& self() const { return static_cast<const Derived&>(*this); }
176};
177
178} // end namespace Eigen
179
180#endif // EIGEN_RANK_REVEALING_BASE_H
Index dimensionOfKernel() const
Definition RankRevealingBase.h:110
bool isInjective() const
Definition RankRevealingBase.h:122
bool isSurjective() const
Definition RankRevealingBase.h:134
Derived & setThreshold(Default_t)
Definition RankRevealingBase.h:71
Derived & setThreshold(const RealScalar &threshold)
Definition RankRevealingBase.h:57
Index nonzeroPivots() const
Definition RankRevealingBase.h:157
RealScalar threshold() const
Definition RankRevealingBase.h:80
RealScalar maxPivot() const
Definition RankRevealingBase.h:165
Index rank() const
Definition RankRevealingBase.h:95
bool isInvertible() const
Definition RankRevealingBase.h:145