mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
Extract RankRevealingBase CRTP mixin to eliminate decomposition code duplication
libeigen/eigen!2272 Co-authored-by: Rasmus Munk Larsen <rmlarsen@gmail.com>
This commit is contained in:
1
Eigen/LU
1
Eigen/LU
@@ -25,6 +25,7 @@
|
||||
|
||||
#include "src/misc/Kernel.h"
|
||||
#include "src/misc/Image.h"
|
||||
#include "src/misc/RankRevealingBase.h"
|
||||
|
||||
// IWYU pragma: begin_exports
|
||||
#include "src/LU/FullPivLU.h"
|
||||
|
||||
2
Eigen/QR
2
Eigen/QR
@@ -31,6 +31,8 @@
|
||||
* \endcode
|
||||
*/
|
||||
|
||||
#include "src/misc/RankRevealingBase.h"
|
||||
|
||||
// IWYU pragma: begin_exports
|
||||
#include "src/QR/HouseholderQR.h"
|
||||
#include "src/QR/FullPivHouseholderQR.h"
|
||||
|
||||
@@ -60,11 +60,23 @@ struct traits<FullPivLU<MatrixType_, PermutationIndex_> > : traits<MatrixType_>
|
||||
* \sa MatrixBase::fullPivLu(), MatrixBase::determinant(), MatrixBase::inverse()
|
||||
*/
|
||||
template <typename MatrixType_, typename PermutationIndex_>
|
||||
class FullPivLU : public SolverBase<FullPivLU<MatrixType_, PermutationIndex_> > {
|
||||
class FullPivLU : public SolverBase<FullPivLU<MatrixType_, PermutationIndex_> >,
|
||||
public RankRevealingBase<FullPivLU<MatrixType_, PermutationIndex_> > {
|
||||
public:
|
||||
typedef MatrixType_ MatrixType;
|
||||
typedef SolverBase<FullPivLU> Base;
|
||||
typedef RankRevealingBase<FullPivLU> RankRevealingBase_;
|
||||
friend class SolverBase<FullPivLU>;
|
||||
friend class RankRevealingBase<FullPivLU>;
|
||||
using RankRevealingBase_::dimensionOfKernel;
|
||||
using RankRevealingBase_::isInjective;
|
||||
using RankRevealingBase_::isInvertible;
|
||||
using RankRevealingBase_::isSurjective;
|
||||
using RankRevealingBase_::maxPivot;
|
||||
using RankRevealingBase_::nonzeroPivots;
|
||||
using RankRevealingBase_::rank;
|
||||
using RankRevealingBase_::setThreshold;
|
||||
using RankRevealingBase_::threshold;
|
||||
|
||||
EIGEN_GENERIC_PUBLIC_INTERFACE(FullPivLU)
|
||||
enum {
|
||||
@@ -148,23 +160,6 @@ class FullPivLU : public SolverBase<FullPivLU<MatrixType_, PermutationIndex_> >
|
||||
return m_lu;
|
||||
}
|
||||
|
||||
/** \returns the number of nonzero pivots in the LU decomposition.
|
||||
* Here nonzero is meant in the exact sense, not in a fuzzy sense.
|
||||
* So that notion isn't really intrinsically interesting, but it is
|
||||
* still useful when implementing algorithms.
|
||||
*
|
||||
* \sa rank()
|
||||
*/
|
||||
inline Index nonzeroPivots() const {
|
||||
eigen_assert(m_isInitialized && "LU is not initialized.");
|
||||
return m_nonzero_pivots;
|
||||
}
|
||||
|
||||
/** \returns the absolute value of the biggest pivot, i.e. the biggest
|
||||
* diagonal coefficient of U.
|
||||
*/
|
||||
RealScalar maxPivot() const { return m_maxpivot; }
|
||||
|
||||
/** \returns the permutation matrix P
|
||||
*
|
||||
* \sa permutationQ()
|
||||
@@ -278,114 +273,10 @@ class FullPivLU : public SolverBase<FullPivLU<MatrixType_, PermutationIndex_> >
|
||||
*/
|
||||
typename internal::traits<MatrixType>::Scalar determinant() const;
|
||||
|
||||
/** Allows to prescribe a threshold to be used by certain methods, such as rank(),
|
||||
* who need to determine when pivots are to be considered nonzero. This is not used for the
|
||||
* LU decomposition itself.
|
||||
*
|
||||
* When it needs to get the threshold value, Eigen calls threshold(). By default, this
|
||||
* uses a formula to automatically determine a reasonable threshold.
|
||||
* Once you have called the present method setThreshold(const RealScalar&),
|
||||
* your value is used instead.
|
||||
*
|
||||
* \param threshold The new value to use as the threshold.
|
||||
*
|
||||
* A pivot will be considered nonzero if its absolute value is strictly greater than
|
||||
* \f$ \vert pivot \vert \leqslant threshold \times \vert maxpivot \vert \f$
|
||||
* where maxpivot is the biggest pivot.
|
||||
*
|
||||
* If you want to come back to the default behavior, call setThreshold(Default_t)
|
||||
*/
|
||||
FullPivLU& setThreshold(const RealScalar& threshold) {
|
||||
m_usePrescribedThreshold = true;
|
||||
m_prescribedThreshold = threshold;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** Allows to come back to the default behavior, letting Eigen use its default formula for
|
||||
* determining the threshold.
|
||||
*
|
||||
* You should pass the special object Eigen::Default as parameter here.
|
||||
* \code lu.setThreshold(Eigen::Default); \endcode
|
||||
*
|
||||
* See the documentation of setThreshold(const RealScalar&).
|
||||
*/
|
||||
FullPivLU& setThreshold(Default_t) {
|
||||
m_usePrescribedThreshold = false;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** Returns the threshold that will be used by certain methods such as rank().
|
||||
*
|
||||
* See the documentation of setThreshold(const RealScalar&).
|
||||
*/
|
||||
RealScalar threshold() const {
|
||||
eigen_assert(m_isInitialized || m_usePrescribedThreshold);
|
||||
return m_usePrescribedThreshold ? m_prescribedThreshold
|
||||
// Higham's backward error bound for Gaussian elimination with
|
||||
// complete pivoting (Theorem 9.4) is ||ΔA||₂ ≤ c·min(m,n)·u·||A||₂.
|
||||
// The factor of 4 covers the constant c.
|
||||
: NumTraits<Scalar>::epsilon() * RealScalar(4 * m_lu.diagonalSize());
|
||||
}
|
||||
|
||||
/** \returns the rank of the matrix of which *this is the LU decomposition.
|
||||
*
|
||||
* \note This method has to determine which pivots should be considered nonzero.
|
||||
* For that, it uses the threshold value that you can control by calling
|
||||
* setThreshold(const RealScalar&).
|
||||
*/
|
||||
inline Index rank() const {
|
||||
/** \returns the absolute value of the i-th pivot coefficient (for RankRevealingBase). */
|
||||
RealScalar pivotCoeff(Index i) const {
|
||||
using std::abs;
|
||||
eigen_assert(m_isInitialized && "LU is not initialized.");
|
||||
RealScalar premultiplied_threshold = abs(m_maxpivot) * threshold();
|
||||
Index result = 0;
|
||||
for (Index i = 0; i < m_nonzero_pivots; ++i) result += (abs(m_lu.coeff(i, i)) > premultiplied_threshold);
|
||||
return result;
|
||||
}
|
||||
|
||||
/** \returns the dimension of the kernel of the matrix of which *this is the LU decomposition.
|
||||
*
|
||||
* \note This method has to determine which pivots should be considered nonzero.
|
||||
* For that, it uses the threshold value that you can control by calling
|
||||
* setThreshold(const RealScalar&).
|
||||
*/
|
||||
inline Index dimensionOfKernel() const {
|
||||
eigen_assert(m_isInitialized && "LU is not initialized.");
|
||||
return cols() - rank();
|
||||
}
|
||||
|
||||
/** \returns true if the matrix of which *this is the LU decomposition represents an injective
|
||||
* linear map, i.e. has trivial kernel; false otherwise.
|
||||
*
|
||||
* \note This method has to determine which pivots should be considered nonzero.
|
||||
* For that, it uses the threshold value that you can control by calling
|
||||
* setThreshold(const RealScalar&).
|
||||
*/
|
||||
inline bool isInjective() const {
|
||||
eigen_assert(m_isInitialized && "LU is not initialized.");
|
||||
return rank() == cols();
|
||||
}
|
||||
|
||||
/** \returns true if the matrix of which *this is the LU decomposition represents a surjective
|
||||
* linear map; false otherwise.
|
||||
*
|
||||
* \note This method has to determine which pivots should be considered nonzero.
|
||||
* For that, it uses the threshold value that you can control by calling
|
||||
* setThreshold(const RealScalar&).
|
||||
*/
|
||||
inline bool isSurjective() const {
|
||||
eigen_assert(m_isInitialized && "LU is not initialized.");
|
||||
return rank() == rows();
|
||||
}
|
||||
|
||||
/** \returns true if the matrix of which *this is the LU decomposition is invertible.
|
||||
*
|
||||
* \note This method has to determine which pivots should be considered nonzero.
|
||||
* For that, it uses the threshold value that you can control by calling
|
||||
* setThreshold(const RealScalar&).
|
||||
*/
|
||||
inline bool isInvertible() const {
|
||||
eigen_assert(m_isInitialized && "LU is not initialized.");
|
||||
return isInjective() && (m_lu.rows() == m_lu.cols());
|
||||
return abs(m_lu.coeff(i, i));
|
||||
}
|
||||
|
||||
/** \returns the inverse of the matrix of which *this is the LU decomposition.
|
||||
@@ -424,15 +315,13 @@ class FullPivLU : public SolverBase<FullPivLU<MatrixType_, PermutationIndex_> >
|
||||
PermutationQType m_q;
|
||||
IntColVectorType m_rowsTranspositions;
|
||||
IntRowVectorType m_colsTranspositions;
|
||||
Index m_nonzero_pivots;
|
||||
RealScalar m_l1_norm;
|
||||
RealScalar m_maxpivot, m_prescribedThreshold;
|
||||
signed char m_det_pq;
|
||||
bool m_isInitialized, m_usePrescribedThreshold;
|
||||
bool m_isInitialized;
|
||||
};
|
||||
|
||||
template <typename MatrixType, typename PermutationIndex>
|
||||
FullPivLU<MatrixType, PermutationIndex>::FullPivLU() : m_isInitialized(false), m_usePrescribedThreshold(false) {}
|
||||
FullPivLU<MatrixType, PermutationIndex>::FullPivLU() : m_isInitialized(false) {}
|
||||
|
||||
template <typename MatrixType, typename PermutationIndex>
|
||||
FullPivLU<MatrixType, PermutationIndex>::FullPivLU(Index rows, Index cols)
|
||||
@@ -441,8 +330,7 @@ FullPivLU<MatrixType, PermutationIndex>::FullPivLU(Index rows, Index cols)
|
||||
m_q(cols),
|
||||
m_rowsTranspositions(rows),
|
||||
m_colsTranspositions(cols),
|
||||
m_isInitialized(false),
|
||||
m_usePrescribedThreshold(false) {}
|
||||
m_isInitialized(false) {}
|
||||
|
||||
template <typename MatrixType, typename PermutationIndex>
|
||||
template <typename InputType>
|
||||
@@ -452,8 +340,7 @@ FullPivLU<MatrixType, PermutationIndex>::FullPivLU(const EigenBase<InputType>& m
|
||||
m_q(matrix.cols()),
|
||||
m_rowsTranspositions(matrix.rows()),
|
||||
m_colsTranspositions(matrix.cols()),
|
||||
m_isInitialized(false),
|
||||
m_usePrescribedThreshold(false) {
|
||||
m_isInitialized(false) {
|
||||
compute(matrix.derived());
|
||||
}
|
||||
|
||||
@@ -465,8 +352,7 @@ FullPivLU<MatrixType, PermutationIndex>::FullPivLU(EigenBase<InputType>& matrix)
|
||||
m_q(matrix.cols()),
|
||||
m_rowsTranspositions(matrix.rows()),
|
||||
m_colsTranspositions(matrix.cols()),
|
||||
m_isInitialized(false),
|
||||
m_usePrescribedThreshold(false) {
|
||||
m_isInitialized(false) {
|
||||
computeInPlace();
|
||||
}
|
||||
|
||||
@@ -487,8 +373,8 @@ void FullPivLU<MatrixType, PermutationIndex>::computeInPlace() {
|
||||
m_colsTranspositions.resize(m_lu.cols());
|
||||
Index number_of_transpositions = 0; // number of NONTRIVIAL transpositions, i.e. m_rowsTranspositions[i]!=i
|
||||
|
||||
m_nonzero_pivots = size; // the generic case is that in which all pivots are nonzero (invertible case)
|
||||
m_maxpivot = RealScalar(0);
|
||||
this->m_nonzero_pivots = size; // the generic case is that in which all pivots are nonzero (invertible case)
|
||||
this->m_maxpivot = RealScalar(0);
|
||||
|
||||
for (Index k = 0; k < size; ++k) {
|
||||
// First, we need to find the pivot.
|
||||
@@ -507,7 +393,7 @@ void FullPivLU<MatrixType, PermutationIndex>::computeInPlace() {
|
||||
if (numext::is_exactly_zero(biggest_in_corner)) {
|
||||
// before exiting, make sure to initialize the still uninitialized transpositions
|
||||
// in a sane state without destroying what we already have.
|
||||
m_nonzero_pivots = k;
|
||||
this->m_nonzero_pivots = k;
|
||||
for (Index i = k; i < size; ++i) {
|
||||
m_rowsTranspositions.coeffRef(i) = internal::convert_index<StorageIndex>(i);
|
||||
m_colsTranspositions.coeffRef(i) = internal::convert_index<StorageIndex>(i);
|
||||
@@ -517,7 +403,7 @@ void FullPivLU<MatrixType, PermutationIndex>::computeInPlace() {
|
||||
|
||||
RealScalar abs_pivot = internal::abs_knowing_score<Scalar>()(
|
||||
m_lu(row_of_biggest_in_corner, col_of_biggest_in_corner), biggest_in_corner);
|
||||
if (abs_pivot > m_maxpivot) m_maxpivot = abs_pivot;
|
||||
if (abs_pivot > this->m_maxpivot) this->m_maxpivot = abs_pivot;
|
||||
|
||||
// Now that we've found the pivot, we need to apply the row/col swaps to
|
||||
// bring it to the location (k,k).
|
||||
|
||||
@@ -51,11 +51,23 @@ struct traits<ColPivHouseholderQR<MatrixType_, PermutationIndex_>> : traits<Matr
|
||||
* \sa MatrixBase::colPivHouseholderQr()
|
||||
*/
|
||||
template <typename MatrixType_, typename PermutationIndex_>
|
||||
class ColPivHouseholderQR : public SolverBase<ColPivHouseholderQR<MatrixType_, PermutationIndex_>> {
|
||||
class ColPivHouseholderQR : public SolverBase<ColPivHouseholderQR<MatrixType_, PermutationIndex_>>,
|
||||
public RankRevealingBase<ColPivHouseholderQR<MatrixType_, PermutationIndex_>> {
|
||||
public:
|
||||
typedef MatrixType_ MatrixType;
|
||||
typedef SolverBase<ColPivHouseholderQR> Base;
|
||||
typedef RankRevealingBase<ColPivHouseholderQR> RankRevealingBase_;
|
||||
friend class SolverBase<ColPivHouseholderQR>;
|
||||
friend class RankRevealingBase<ColPivHouseholderQR>;
|
||||
using RankRevealingBase_::dimensionOfKernel;
|
||||
using RankRevealingBase_::isInjective;
|
||||
using RankRevealingBase_::isInvertible;
|
||||
using RankRevealingBase_::isSurjective;
|
||||
using RankRevealingBase_::maxPivot;
|
||||
using RankRevealingBase_::nonzeroPivots;
|
||||
using RankRevealingBase_::rank;
|
||||
using RankRevealingBase_::setThreshold;
|
||||
using RankRevealingBase_::threshold;
|
||||
typedef PermutationIndex_ PermutationIndex;
|
||||
EIGEN_GENERIC_PUBLIC_INTERFACE(ColPivHouseholderQR)
|
||||
|
||||
@@ -82,7 +94,6 @@ class ColPivHouseholderQR : public SolverBase<ColPivHouseholderQR<MatrixType_, P
|
||||
m_colNormsUpdated.resize(cols);
|
||||
m_colNormsDirect.resize(cols);
|
||||
m_isInitialized = false;
|
||||
m_usePrescribedThreshold = false;
|
||||
}
|
||||
|
||||
public:
|
||||
@@ -100,8 +111,7 @@ class ColPivHouseholderQR : public SolverBase<ColPivHouseholderQR<MatrixType_, P
|
||||
m_temp(),
|
||||
m_colNormsUpdated(),
|
||||
m_colNormsDirect(),
|
||||
m_isInitialized(false),
|
||||
m_usePrescribedThreshold(false) {}
|
||||
m_isInitialized(false) {}
|
||||
|
||||
/** \brief Default Constructor with memory preallocation
|
||||
*
|
||||
@@ -252,65 +262,10 @@ class ColPivHouseholderQR : public SolverBase<ColPivHouseholderQR<MatrixType_, P
|
||||
*/
|
||||
typename MatrixType::Scalar signDeterminant() const;
|
||||
|
||||
/** \returns the rank of the matrix of which *this is the QR decomposition.
|
||||
*
|
||||
* \note This method has to determine which pivots should be considered nonzero.
|
||||
* For that, it uses the threshold value that you can control by calling
|
||||
* setThreshold(const RealScalar&).
|
||||
*/
|
||||
inline Index rank() const {
|
||||
/** \returns the absolute value of the i-th pivot coefficient (for RankRevealingBase). */
|
||||
RealScalar pivotCoeff(Index i) const {
|
||||
using std::abs;
|
||||
eigen_assert(m_isInitialized && "ColPivHouseholderQR is not initialized.");
|
||||
RealScalar premultiplied_threshold = abs(m_maxpivot) * threshold();
|
||||
Index result = 0;
|
||||
for (Index i = 0; i < m_nonzero_pivots; ++i) result += (abs(m_qr.coeff(i, i)) > premultiplied_threshold);
|
||||
return result;
|
||||
}
|
||||
|
||||
/** \returns the dimension of the kernel of the matrix of which *this is the QR decomposition.
|
||||
*
|
||||
* \note This method has to determine which pivots should be considered nonzero.
|
||||
* For that, it uses the threshold value that you can control by calling
|
||||
* setThreshold(const RealScalar&).
|
||||
*/
|
||||
inline Index dimensionOfKernel() const {
|
||||
eigen_assert(m_isInitialized && "ColPivHouseholderQR is not initialized.");
|
||||
return cols() - rank();
|
||||
}
|
||||
|
||||
/** \returns true if the matrix of which *this is the QR decomposition represents an injective
|
||||
* linear map, i.e. has trivial kernel; false otherwise.
|
||||
*
|
||||
* \note This method has to determine which pivots should be considered nonzero.
|
||||
* For that, it uses the threshold value that you can control by calling
|
||||
* setThreshold(const RealScalar&).
|
||||
*/
|
||||
inline bool isInjective() const {
|
||||
eigen_assert(m_isInitialized && "ColPivHouseholderQR is not initialized.");
|
||||
return rank() == cols();
|
||||
}
|
||||
|
||||
/** \returns true if the matrix of which *this is the QR decomposition represents a surjective
|
||||
* linear map; false otherwise.
|
||||
*
|
||||
* \note This method has to determine which pivots should be considered nonzero.
|
||||
* For that, it uses the threshold value that you can control by calling
|
||||
* setThreshold(const RealScalar&).
|
||||
*/
|
||||
inline bool isSurjective() const {
|
||||
eigen_assert(m_isInitialized && "ColPivHouseholderQR is not initialized.");
|
||||
return rank() == rows();
|
||||
}
|
||||
|
||||
/** \returns true if the matrix of which *this is the QR decomposition is invertible.
|
||||
*
|
||||
* \note This method has to determine which pivots should be considered nonzero.
|
||||
* For that, it uses the threshold value that you can control by calling
|
||||
* setThreshold(const RealScalar&).
|
||||
*/
|
||||
inline bool isInvertible() const {
|
||||
eigen_assert(m_isInitialized && "ColPivHouseholderQR is not initialized.");
|
||||
return isInjective() && isSurjective();
|
||||
return abs(m_qr.coeff(i, i));
|
||||
}
|
||||
|
||||
/** \returns the inverse of the matrix of which *this is the QR decomposition.
|
||||
@@ -332,72 +287,6 @@ class ColPivHouseholderQR : public SolverBase<ColPivHouseholderQR<MatrixType_, P
|
||||
*/
|
||||
const HCoeffsType& hCoeffs() const { return m_hCoeffs; }
|
||||
|
||||
/** Allows to prescribe a threshold to be used by certain methods, such as rank(),
|
||||
* who need to determine when pivots are to be considered nonzero. This is not used for the
|
||||
* QR decomposition itself.
|
||||
*
|
||||
* When it needs to get the threshold value, Eigen calls threshold(). By default, this
|
||||
* uses a formula to automatically determine a reasonable threshold.
|
||||
* Once you have called the present method setThreshold(const RealScalar&),
|
||||
* your value is used instead.
|
||||
*
|
||||
* \param threshold The new value to use as the threshold.
|
||||
*
|
||||
* A pivot will be considered nonzero if its absolute value is strictly greater than
|
||||
* \f$ \vert pivot \vert \leqslant threshold \times \vert maxpivot \vert \f$
|
||||
* where maxpivot is the biggest pivot.
|
||||
*
|
||||
* If you want to come back to the default behavior, call setThreshold(Default_t)
|
||||
*/
|
||||
ColPivHouseholderQR& setThreshold(const RealScalar& threshold) {
|
||||
m_usePrescribedThreshold = true;
|
||||
m_prescribedThreshold = threshold;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** Allows to come back to the default behavior, letting Eigen use its default formula for
|
||||
* determining the threshold.
|
||||
*
|
||||
* You should pass the special object Eigen::Default as parameter here.
|
||||
* \code qr.setThreshold(Eigen::Default); \endcode
|
||||
*
|
||||
* See the documentation of setThreshold(const RealScalar&).
|
||||
*/
|
||||
ColPivHouseholderQR& setThreshold(Default_t) {
|
||||
m_usePrescribedThreshold = false;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** Returns the threshold that will be used by certain methods such as rank().
|
||||
*
|
||||
* See the documentation of setThreshold(const RealScalar&).
|
||||
*/
|
||||
RealScalar threshold() const {
|
||||
eigen_assert(m_isInitialized || m_usePrescribedThreshold);
|
||||
return m_usePrescribedThreshold ? m_prescribedThreshold
|
||||
// Higham's backward error bound for Householder QR (Theorem 19.4) is
|
||||
// ||ΔA||₂ ≤ c·min(m,n)·u·||A||₂. The factor of 4 covers the
|
||||
// constant c (typically 3–6 worst-case, ~1 probabilistically).
|
||||
: NumTraits<Scalar>::epsilon() * RealScalar(4 * m_qr.diagonalSize());
|
||||
}
|
||||
|
||||
/** \returns the number of nonzero pivots in the QR decomposition.
|
||||
* Here nonzero is meant in the exact sense, not in a fuzzy sense.
|
||||
* So that notion isn't really intrinsically interesting, but it is
|
||||
* still useful when implementing algorithms.
|
||||
*
|
||||
* \sa rank()
|
||||
*/
|
||||
inline Index nonzeroPivots() const {
|
||||
eigen_assert(m_isInitialized && "ColPivHouseholderQR is not initialized.");
|
||||
return m_nonzero_pivots;
|
||||
}
|
||||
|
||||
/** \returns the absolute value of the biggest pivot, i.e. the biggest
|
||||
* diagonal coefficient of R.
|
||||
*/
|
||||
RealScalar maxPivot() const { return m_maxpivot; }
|
||||
|
||||
/** \brief Reports whether the QR factorization was successful.
|
||||
*
|
||||
* \note This function always returns \c Success. It is provided for compatibility
|
||||
@@ -431,9 +320,7 @@ class ColPivHouseholderQR : public SolverBase<ColPivHouseholderQR<MatrixType_, P
|
||||
RowVectorType m_temp;
|
||||
RealRowVectorType m_colNormsUpdated;
|
||||
RealRowVectorType m_colNormsDirect;
|
||||
bool m_isInitialized, m_usePrescribedThreshold;
|
||||
RealScalar m_prescribedThreshold, m_maxpivot;
|
||||
Index m_nonzero_pivots;
|
||||
bool m_isInitialized;
|
||||
Index m_det_p;
|
||||
};
|
||||
|
||||
@@ -515,8 +402,8 @@ void ColPivHouseholderQR<MatrixType, PermutationIndex>::computeInPlace() {
|
||||
numext::abs2<RealScalar>(m_colNormsUpdated.maxCoeff() * NumTraits<RealScalar>::epsilon()) / RealScalar(rows);
|
||||
RealScalar norm_downdate_threshold = numext::sqrt(NumTraits<RealScalar>::epsilon());
|
||||
|
||||
m_nonzero_pivots = size; // the generic case is that in which all pivots are nonzero (invertible case)
|
||||
m_maxpivot = RealScalar(0);
|
||||
this->m_nonzero_pivots = size; // the generic case is that in which all pivots are nonzero (invertible case)
|
||||
this->m_maxpivot = RealScalar(0);
|
||||
|
||||
for (Index k = 0; k < size; ++k) {
|
||||
// first, we look up in our table m_colNormsUpdated which column has the biggest norm
|
||||
@@ -526,7 +413,8 @@ void ColPivHouseholderQR<MatrixType, PermutationIndex>::computeInPlace() {
|
||||
|
||||
// Track the number of meaningful pivots but do not stop the decomposition to make
|
||||
// sure that the initial matrix is properly reproduced. See bug 941.
|
||||
if (m_nonzero_pivots == size && biggest_col_sq_norm < threshold_helper * RealScalar(rows - k)) m_nonzero_pivots = k;
|
||||
if (this->m_nonzero_pivots == size && biggest_col_sq_norm < threshold_helper * RealScalar(rows - k))
|
||||
this->m_nonzero_pivots = k;
|
||||
|
||||
// apply the transposition to the columns
|
||||
m_colsTranspositions.coeffRef(k) = static_cast<PermutationIndex>(biggest_col_index);
|
||||
@@ -545,7 +433,7 @@ void ColPivHouseholderQR<MatrixType, PermutationIndex>::computeInPlace() {
|
||||
m_qr.coeffRef(k, k) = beta;
|
||||
|
||||
// remember the maximum absolute value of diagonal coefficients
|
||||
if (abs(beta) > m_maxpivot) m_maxpivot = abs(beta);
|
||||
if (abs(beta) > this->m_maxpivot) this->m_maxpivot = abs(beta);
|
||||
|
||||
// apply the householder transformation
|
||||
m_qr.bottomRightCorner(rows - k, cols - k - 1)
|
||||
|
||||
@@ -60,11 +60,23 @@ struct traits<FullPivHouseholderQRMatrixQReturnType<MatrixType, PermutationIndex
|
||||
* \sa MatrixBase::fullPivHouseholderQr()
|
||||
*/
|
||||
template <typename MatrixType_, typename PermutationIndex_>
|
||||
class FullPivHouseholderQR : public SolverBase<FullPivHouseholderQR<MatrixType_, PermutationIndex_> > {
|
||||
class FullPivHouseholderQR : public SolverBase<FullPivHouseholderQR<MatrixType_, PermutationIndex_> >,
|
||||
public RankRevealingBase<FullPivHouseholderQR<MatrixType_, PermutationIndex_> > {
|
||||
public:
|
||||
typedef MatrixType_ MatrixType;
|
||||
typedef SolverBase<FullPivHouseholderQR> Base;
|
||||
typedef RankRevealingBase<FullPivHouseholderQR> RankRevealingBase_;
|
||||
friend class SolverBase<FullPivHouseholderQR>;
|
||||
friend class RankRevealingBase<FullPivHouseholderQR>;
|
||||
using RankRevealingBase_::dimensionOfKernel;
|
||||
using RankRevealingBase_::isInjective;
|
||||
using RankRevealingBase_::isInvertible;
|
||||
using RankRevealingBase_::isSurjective;
|
||||
using RankRevealingBase_::maxPivot;
|
||||
using RankRevealingBase_::nonzeroPivots;
|
||||
using RankRevealingBase_::rank;
|
||||
using RankRevealingBase_::setThreshold;
|
||||
using RankRevealingBase_::threshold;
|
||||
typedef PermutationIndex_ PermutationIndex;
|
||||
EIGEN_GENERIC_PUBLIC_INTERFACE(FullPivHouseholderQR)
|
||||
|
||||
@@ -105,8 +117,7 @@ class FullPivHouseholderQR : public SolverBase<FullPivHouseholderQR<MatrixType_,
|
||||
m_cols_transpositions(),
|
||||
m_cols_permutation(),
|
||||
m_temp(),
|
||||
m_isInitialized(false),
|
||||
m_usePrescribedThreshold(false) {}
|
||||
m_isInitialized(false) {}
|
||||
|
||||
/** \brief Default Constructor with memory preallocation
|
||||
*
|
||||
@@ -121,8 +132,7 @@ class FullPivHouseholderQR : public SolverBase<FullPivHouseholderQR<MatrixType_,
|
||||
m_cols_transpositions((std::min)(rows, cols)),
|
||||
m_cols_permutation(cols),
|
||||
m_temp(cols),
|
||||
m_isInitialized(false),
|
||||
m_usePrescribedThreshold(false) {}
|
||||
m_isInitialized(false) {}
|
||||
|
||||
/** \brief Constructs a QR factorization from a given matrix
|
||||
*
|
||||
@@ -144,8 +154,7 @@ class FullPivHouseholderQR : public SolverBase<FullPivHouseholderQR<MatrixType_,
|
||||
m_cols_transpositions((std::min)(matrix.rows(), matrix.cols())),
|
||||
m_cols_permutation(matrix.cols()),
|
||||
m_temp(matrix.cols()),
|
||||
m_isInitialized(false),
|
||||
m_usePrescribedThreshold(false) {
|
||||
m_isInitialized(false) {
|
||||
compute(matrix.derived());
|
||||
}
|
||||
|
||||
@@ -164,8 +173,7 @@ class FullPivHouseholderQR : public SolverBase<FullPivHouseholderQR<MatrixType_,
|
||||
m_cols_transpositions((std::min)(matrix.rows(), matrix.cols())),
|
||||
m_cols_permutation(matrix.cols()),
|
||||
m_temp(matrix.cols()),
|
||||
m_isInitialized(false),
|
||||
m_usePrescribedThreshold(false) {
|
||||
m_isInitialized(false) {
|
||||
computeInPlace();
|
||||
}
|
||||
|
||||
@@ -273,65 +281,10 @@ class FullPivHouseholderQR : public SolverBase<FullPivHouseholderQR<MatrixType_,
|
||||
*/
|
||||
typename MatrixType::Scalar signDeterminant() const;
|
||||
|
||||
/** \returns the rank of the matrix of which *this is the QR decomposition.
|
||||
*
|
||||
* \note This method has to determine which pivots should be considered nonzero.
|
||||
* For that, it uses the threshold value that you can control by calling
|
||||
* setThreshold(const RealScalar&).
|
||||
*/
|
||||
inline Index rank() const {
|
||||
/** \returns the absolute value of the i-th pivot coefficient (for RankRevealingBase). */
|
||||
RealScalar pivotCoeff(Index i) const {
|
||||
using std::abs;
|
||||
eigen_assert(m_isInitialized && "FullPivHouseholderQR is not initialized.");
|
||||
RealScalar premultiplied_threshold = abs(m_maxpivot) * threshold();
|
||||
Index result = 0;
|
||||
for (Index i = 0; i < m_nonzero_pivots; ++i) result += (abs(m_qr.coeff(i, i)) > premultiplied_threshold);
|
||||
return result;
|
||||
}
|
||||
|
||||
/** \returns the dimension of the kernel of the matrix of which *this is the QR decomposition.
|
||||
*
|
||||
* \note This method has to determine which pivots should be considered nonzero.
|
||||
* For that, it uses the threshold value that you can control by calling
|
||||
* setThreshold(const RealScalar&).
|
||||
*/
|
||||
inline Index dimensionOfKernel() const {
|
||||
eigen_assert(m_isInitialized && "FullPivHouseholderQR is not initialized.");
|
||||
return cols() - rank();
|
||||
}
|
||||
|
||||
/** \returns true if the matrix of which *this is the QR decomposition represents an injective
|
||||
* linear map, i.e. has trivial kernel; false otherwise.
|
||||
*
|
||||
* \note This method has to determine which pivots should be considered nonzero.
|
||||
* For that, it uses the threshold value that you can control by calling
|
||||
* setThreshold(const RealScalar&).
|
||||
*/
|
||||
inline bool isInjective() const {
|
||||
eigen_assert(m_isInitialized && "FullPivHouseholderQR is not initialized.");
|
||||
return rank() == cols();
|
||||
}
|
||||
|
||||
/** \returns true if the matrix of which *this is the QR decomposition represents a surjective
|
||||
* linear map; false otherwise.
|
||||
*
|
||||
* \note This method has to determine which pivots should be considered nonzero.
|
||||
* For that, it uses the threshold value that you can control by calling
|
||||
* setThreshold(const RealScalar&).
|
||||
*/
|
||||
inline bool isSurjective() const {
|
||||
eigen_assert(m_isInitialized && "FullPivHouseholderQR is not initialized.");
|
||||
return rank() == rows();
|
||||
}
|
||||
|
||||
/** \returns true if the matrix of which *this is the QR decomposition is invertible.
|
||||
*
|
||||
* \note This method has to determine which pivots should be considered nonzero.
|
||||
* For that, it uses the threshold value that you can control by calling
|
||||
* setThreshold(const RealScalar&).
|
||||
*/
|
||||
inline bool isInvertible() const {
|
||||
eigen_assert(m_isInitialized && "FullPivHouseholderQR is not initialized.");
|
||||
return isInjective() && isSurjective();
|
||||
return abs(m_qr.coeff(i, i));
|
||||
}
|
||||
|
||||
/** \returns the inverse of the matrix of which *this is the QR decomposition.
|
||||
@@ -353,72 +306,6 @@ class FullPivHouseholderQR : public SolverBase<FullPivHouseholderQR<MatrixType_,
|
||||
*/
|
||||
const HCoeffsType& hCoeffs() const { return m_hCoeffs; }
|
||||
|
||||
/** Allows to prescribe a threshold to be used by certain methods, such as rank(),
|
||||
* who need to determine when pivots are to be considered nonzero. This is not used for the
|
||||
* QR decomposition itself.
|
||||
*
|
||||
* When it needs to get the threshold value, Eigen calls threshold(). By default, this
|
||||
* uses a formula to automatically determine a reasonable threshold.
|
||||
* Once you have called the present method setThreshold(const RealScalar&),
|
||||
* your value is used instead.
|
||||
*
|
||||
* \param threshold The new value to use as the threshold.
|
||||
*
|
||||
* A pivot will be considered nonzero if its absolute value is strictly greater than
|
||||
* \f$ \vert pivot \vert \leqslant threshold \times \vert maxpivot \vert \f$
|
||||
* where maxpivot is the biggest pivot.
|
||||
*
|
||||
* If you want to come back to the default behavior, call setThreshold(Default_t)
|
||||
*/
|
||||
FullPivHouseholderQR& setThreshold(const RealScalar& threshold) {
|
||||
m_usePrescribedThreshold = true;
|
||||
m_prescribedThreshold = threshold;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** Allows to come back to the default behavior, letting Eigen use its default formula for
|
||||
* determining the threshold.
|
||||
*
|
||||
* You should pass the special object Eigen::Default as parameter here.
|
||||
* \code qr.setThreshold(Eigen::Default); \endcode
|
||||
*
|
||||
* See the documentation of setThreshold(const RealScalar&).
|
||||
*/
|
||||
FullPivHouseholderQR& setThreshold(Default_t) {
|
||||
m_usePrescribedThreshold = false;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** Returns the threshold that will be used by certain methods such as rank().
|
||||
*
|
||||
* See the documentation of setThreshold(const RealScalar&).
|
||||
*/
|
||||
RealScalar threshold() const {
|
||||
eigen_assert(m_isInitialized || m_usePrescribedThreshold);
|
||||
return m_usePrescribedThreshold ? m_prescribedThreshold
|
||||
// Higham's backward error bound for Householder QR (Theorem 19.4) is
|
||||
// ||ΔA||₂ ≤ c·min(m,n)·u·||A||₂. The factor of 4 covers the
|
||||
// constant c (typically 3–6 worst-case, ~1 probabilistically).
|
||||
: NumTraits<Scalar>::epsilon() * RealScalar(4 * m_qr.diagonalSize());
|
||||
}
|
||||
|
||||
/** \returns the number of nonzero pivots in the QR decomposition.
|
||||
* Here nonzero is meant in the exact sense, not in a fuzzy sense.
|
||||
* So that notion isn't really intrinsically interesting, but it is
|
||||
* still useful when implementing algorithms.
|
||||
*
|
||||
* \sa rank()
|
||||
*/
|
||||
inline Index nonzeroPivots() const {
|
||||
eigen_assert(m_isInitialized && "LU is not initialized.");
|
||||
return m_nonzero_pivots;
|
||||
}
|
||||
|
||||
/** \returns the absolute value of the biggest pivot, i.e. the biggest
|
||||
* diagonal coefficient of U.
|
||||
*/
|
||||
RealScalar maxPivot() const { return m_maxpivot; }
|
||||
|
||||
#ifndef EIGEN_PARSED_BY_DOXYGEN
|
||||
template <typename RhsType, typename DstType>
|
||||
void _solve_impl(const RhsType& rhs, DstType& dst) const;
|
||||
@@ -438,9 +325,7 @@ class FullPivHouseholderQR : public SolverBase<FullPivHouseholderQR<MatrixType_,
|
||||
IntDiagSizeVectorType m_cols_transpositions;
|
||||
PermutationType m_cols_permutation;
|
||||
RowVectorType m_temp;
|
||||
bool m_isInitialized, m_usePrescribedThreshold;
|
||||
RealScalar m_prescribedThreshold, m_maxpivot;
|
||||
Index m_nonzero_pivots;
|
||||
bool m_isInitialized;
|
||||
RealScalar m_precision;
|
||||
Index m_det_p;
|
||||
};
|
||||
@@ -513,8 +398,8 @@ void FullPivHouseholderQR<MatrixType, PermutationIndex>::computeInPlace() {
|
||||
|
||||
RealScalar biggest(0);
|
||||
|
||||
m_nonzero_pivots = size; // the generic case is that in which all pivots are nonzero (invertible case)
|
||||
m_maxpivot = RealScalar(0);
|
||||
this->m_nonzero_pivots = size; // the generic case is that in which all pivots are nonzero (invertible case)
|
||||
this->m_maxpivot = RealScalar(0);
|
||||
|
||||
for (Index k = 0; k < size; ++k) {
|
||||
Index row_of_biggest_in_corner, col_of_biggest_in_corner;
|
||||
@@ -532,7 +417,7 @@ void FullPivHouseholderQR<MatrixType, PermutationIndex>::computeInPlace() {
|
||||
|
||||
// if the corner is negligible, then we have less than full rank, and we can finish early
|
||||
if (internal::isMuchSmallerThan(biggest_in_corner, biggest, m_precision)) {
|
||||
m_nonzero_pivots = k;
|
||||
this->m_nonzero_pivots = k;
|
||||
for (Index i = k; i < size; i++) {
|
||||
m_rows_transpositions.coeffRef(i) = internal::convert_index<PermutationIndex>(i);
|
||||
m_cols_transpositions.coeffRef(i) = internal::convert_index<PermutationIndex>(i);
|
||||
@@ -557,7 +442,7 @@ void FullPivHouseholderQR<MatrixType, PermutationIndex>::computeInPlace() {
|
||||
m_qr.coeffRef(k, k) = beta;
|
||||
|
||||
// remember the maximum absolute value of diagonal coefficients
|
||||
if (abs(beta) > m_maxpivot) m_maxpivot = abs(beta);
|
||||
if (abs(beta) > this->m_maxpivot) this->m_maxpivot = abs(beta);
|
||||
|
||||
m_qr.bottomRightCorner(rows - k, cols - k - 1)
|
||||
.applyHouseholderOnTheLeft(m_qr.col(k).tail(rows - k - 1), m_hCoeffs.coeffRef(k), &m_temp.coeffRef(k + 1));
|
||||
|
||||
178
Eigen/src/misc/RankRevealingBase.h
Normal file
178
Eigen/src/misc/RankRevealingBase.h
Normal file
@@ -0,0 +1,178 @@
|
||||
// This file is part of Eigen, a lightweight C++ template library
|
||||
// for linear algebra.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the Mozilla
|
||||
// Public License v. 2.0. If a copy of the MPL was not distributed
|
||||
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
|
||||
#ifndef EIGEN_RANK_REVEALING_BASE_H
|
||||
#define EIGEN_RANK_REVEALING_BASE_H
|
||||
|
||||
// IWYU pragma: private
|
||||
#include "./InternalHeaderCheck.h"
|
||||
|
||||
namespace Eigen {
|
||||
|
||||
/** \brief CRTP mixin providing threshold management, rank computation, and rank-derived queries
|
||||
* for rank-revealing decompositions (FullPivLU, ColPivHouseholderQR, FullPivHouseholderQR).
|
||||
*
|
||||
* \tparam Derived the concrete decomposition class (CRTP parameter)
|
||||
*
|
||||
* The derived class must provide:
|
||||
* - rows(), cols() (inherited from SolverBase)
|
||||
* - m_isInitialized (bool member, also used by SolverBase)
|
||||
* - pivotCoeff(Index i) returning the absolute value of the i-th pivot
|
||||
*/
|
||||
template <typename Derived>
|
||||
class RankRevealingBase {
|
||||
public:
|
||||
typedef typename internal::traits<Derived>::Scalar Scalar;
|
||||
typedef typename NumTraits<Scalar>::Real RealScalar;
|
||||
|
||||
RankRevealingBase()
|
||||
: m_usePrescribedThreshold(false),
|
||||
m_prescribedThreshold(RealScalar(0)),
|
||||
m_maxpivot(RealScalar(0)),
|
||||
m_nonzero_pivots(0) {}
|
||||
|
||||
/** Allows to prescribe a threshold to be used by certain methods, such as rank(),
|
||||
* who need to determine when pivots are to be considered nonzero. This is not used for the
|
||||
* decomposition itself.
|
||||
*
|
||||
* When it needs to get the threshold value, Eigen calls threshold(). By default, this
|
||||
* uses a formula to automatically determine a reasonable threshold.
|
||||
* Once you have called the present method setThreshold(const RealScalar&),
|
||||
* your value is used instead.
|
||||
*
|
||||
* \param threshold The new value to use as the threshold.
|
||||
*
|
||||
* A pivot will be considered nonzero if its absolute value is strictly greater than
|
||||
* \f$ \vert pivot \vert \leqslant threshold \times \vert maxpivot \vert \f$
|
||||
* where maxpivot is the biggest pivot.
|
||||
*
|
||||
* If you want to come back to the default behavior, call setThreshold(Default_t)
|
||||
*/
|
||||
Derived& setThreshold(const RealScalar& threshold) {
|
||||
m_usePrescribedThreshold = true;
|
||||
m_prescribedThreshold = threshold;
|
||||
return self();
|
||||
}
|
||||
|
||||
/** Allows to come back to the default behavior, letting Eigen use its default formula for
|
||||
* determining the threshold.
|
||||
*
|
||||
* You should pass the special object Eigen::Default as parameter here.
|
||||
* \code dec.setThreshold(Eigen::Default); \endcode
|
||||
*
|
||||
* See the documentation of setThreshold(const RealScalar&).
|
||||
*/
|
||||
Derived& setThreshold(Default_t) {
|
||||
m_usePrescribedThreshold = false;
|
||||
return self();
|
||||
}
|
||||
|
||||
/** Returns the threshold that will be used by certain methods such as rank().
|
||||
*
|
||||
* See the documentation of setThreshold(const RealScalar&).
|
||||
*/
|
||||
RealScalar threshold() const {
|
||||
eigen_assert(self().m_isInitialized || m_usePrescribedThreshold);
|
||||
// Higham's backward error bound: ||ΔA||₂ ≤ c·min(m,n)·u·||A||₂.
|
||||
// The factor of 4 covers the constant c.
|
||||
return m_usePrescribedThreshold
|
||||
? m_prescribedThreshold
|
||||
: NumTraits<Scalar>::epsilon() * RealScalar(4 * (std::min)(self().rows(), self().cols()));
|
||||
}
|
||||
|
||||
/** \returns the rank of the matrix of which *this is the decomposition.
|
||||
*
|
||||
* \note This method has to determine which pivots should be considered nonzero.
|
||||
* For that, it uses the threshold value that you can control by calling
|
||||
* setThreshold(const RealScalar&).
|
||||
*/
|
||||
inline Index rank() const {
|
||||
using std::abs;
|
||||
eigen_assert(self().m_isInitialized && "Decomposition is not initialized.");
|
||||
RealScalar premultiplied_threshold = abs(m_maxpivot) * threshold();
|
||||
Index result = 0;
|
||||
for (Index i = 0; i < m_nonzero_pivots; ++i) result += (self().pivotCoeff(i) > premultiplied_threshold);
|
||||
return result;
|
||||
}
|
||||
|
||||
/** \returns the dimension of the kernel of the matrix of which *this is the decomposition.
|
||||
*
|
||||
* \note This method has to determine which pivots should be considered nonzero.
|
||||
* For that, it uses the threshold value that you can control by calling
|
||||
* setThreshold(const RealScalar&).
|
||||
*/
|
||||
inline Index dimensionOfKernel() const {
|
||||
eigen_assert(self().m_isInitialized && "Decomposition is not initialized.");
|
||||
return self().cols() - rank();
|
||||
}
|
||||
|
||||
/** \returns true if the matrix of which *this is the decomposition represents an injective
|
||||
* linear map, i.e. has trivial kernel; false otherwise.
|
||||
*
|
||||
* \note This method has to determine which pivots should be considered nonzero.
|
||||
* For that, it uses the threshold value that you can control by calling
|
||||
* setThreshold(const RealScalar&).
|
||||
*/
|
||||
inline bool isInjective() const {
|
||||
eigen_assert(self().m_isInitialized && "Decomposition is not initialized.");
|
||||
return rank() == self().cols();
|
||||
}
|
||||
|
||||
/** \returns true if the matrix of which *this is the decomposition represents a surjective
|
||||
* linear map; false otherwise.
|
||||
*
|
||||
* \note This method has to determine which pivots should be considered nonzero.
|
||||
* For that, it uses the threshold value that you can control by calling
|
||||
* setThreshold(const RealScalar&).
|
||||
*/
|
||||
inline bool isSurjective() const {
|
||||
eigen_assert(self().m_isInitialized && "Decomposition is not initialized.");
|
||||
return rank() == self().rows();
|
||||
}
|
||||
|
||||
/** \returns true if the matrix of which *this is the decomposition is invertible.
|
||||
*
|
||||
* \note This method has to determine which pivots should be considered nonzero.
|
||||
* For that, it uses the threshold value that you can control by calling
|
||||
* setThreshold(const RealScalar&).
|
||||
*/
|
||||
inline bool isInvertible() const {
|
||||
eigen_assert(self().m_isInitialized && "Decomposition is not initialized.");
|
||||
return isInjective() && isSurjective();
|
||||
}
|
||||
|
||||
/** \returns the number of nonzero pivots in the decomposition.
|
||||
* Here nonzero is meant in the exact sense, not in a fuzzy sense.
|
||||
* So that notion isn't really intrinsically interesting, but it is
|
||||
* still useful when implementing algorithms.
|
||||
*
|
||||
* \sa rank()
|
||||
*/
|
||||
inline Index nonzeroPivots() const {
|
||||
eigen_assert(self().m_isInitialized && "Decomposition is not initialized.");
|
||||
return m_nonzero_pivots;
|
||||
}
|
||||
|
||||
/** \returns the absolute value of the biggest pivot, i.e. the biggest
|
||||
* diagonal coefficient of U (or R).
|
||||
*/
|
||||
RealScalar maxPivot() const { return m_maxpivot; }
|
||||
|
||||
protected:
|
||||
bool m_usePrescribedThreshold;
|
||||
RealScalar m_prescribedThreshold;
|
||||
RealScalar m_maxpivot;
|
||||
Index m_nonzero_pivots;
|
||||
|
||||
private:
|
||||
Derived& self() { return static_cast<Derived&>(*this); }
|
||||
const Derived& self() const { return static_cast<const Derived&>(*this); }
|
||||
};
|
||||
|
||||
} // end namespace Eigen
|
||||
|
||||
#endif // EIGEN_RANK_REVEALING_BASE_H
|
||||
Reference in New Issue
Block a user