2009-08-22 01:13:21 -04:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
|
|
|
|
// for linear algebra.
|
|
|
|
|
//
|
2010-06-24 23:21:58 +02:00
|
|
|
// Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
|
2009-08-22 01:13:21 -04:00
|
|
|
// Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
|
|
|
|
|
//
|
|
|
|
|
// Eigen is free software; you can redistribute it and/or
|
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
|
// version 3 of the License, or (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// Alternatively, you can redistribute it and/or
|
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
|
// published by the Free Software Foundation; either version 2 of
|
|
|
|
|
// the License, or (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
|
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
// License and a copy of the GNU General Public License along with
|
|
|
|
|
// Eigen. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
2009-08-23 18:04:33 -04:00
|
|
|
#ifndef EIGEN_COLPIVOTINGHOUSEHOLDERQR_H
|
|
|
|
|
#define EIGEN_COLPIVOTINGHOUSEHOLDERQR_H
|
2009-08-22 01:13:21 -04:00
|
|
|
|
|
|
|
|
/** \ingroup QR_Module
|
|
|
|
|
*
|
2009-10-28 18:19:29 -04:00
|
|
|
* \class ColPivHouseholderQR
|
2009-08-22 01:13:21 -04:00
|
|
|
*
|
2009-08-24 11:11:41 -04:00
|
|
|
* \brief Householder rank-revealing QR decomposition of a matrix with column-pivoting
|
2009-08-22 01:13:21 -04:00
|
|
|
*
|
|
|
|
|
* \param MatrixType the type of the matrix of which we are computing the QR decomposition
|
|
|
|
|
*
|
|
|
|
|
* This class performs a rank-revealing QR decomposition using Householder transformations.
|
|
|
|
|
*
|
2009-08-24 11:11:41 -04:00
|
|
|
* This decomposition performs column pivoting in order to be rank-revealing and improve
|
2009-10-28 18:19:29 -04:00
|
|
|
* numerical stability. It is slower than HouseholderQR, and faster than FullPivHouseholderQR.
|
2009-08-22 01:13:21 -04:00
|
|
|
*
|
2009-10-28 18:19:29 -04:00
|
|
|
* \sa MatrixBase::colPivHouseholderQr()
|
2009-08-22 01:13:21 -04:00
|
|
|
*/
|
2009-11-08 10:21:26 -05:00
|
|
|
template<typename _MatrixType> class ColPivHouseholderQR
|
2009-08-22 01:13:21 -04:00
|
|
|
{
|
|
|
|
|
public:
|
2009-09-16 15:56:20 +02:00
|
|
|
|
2009-11-08 10:21:26 -05:00
|
|
|
typedef _MatrixType MatrixType;
|
2009-08-22 01:13:21 -04:00
|
|
|
enum {
|
|
|
|
|
RowsAtCompileTime = MatrixType::RowsAtCompileTime,
|
|
|
|
|
ColsAtCompileTime = MatrixType::ColsAtCompileTime,
|
|
|
|
|
Options = MatrixType::Options,
|
2010-03-08 19:31:27 +01:00
|
|
|
MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
|
2010-03-19 02:12:23 -04:00
|
|
|
MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
|
2009-08-22 01:13:21 -04:00
|
|
|
};
|
|
|
|
|
typedef typename MatrixType::Scalar Scalar;
|
|
|
|
|
typedef typename MatrixType::RealScalar RealScalar;
|
2010-05-30 16:00:58 -04:00
|
|
|
typedef typename MatrixType::Index Index;
|
2010-03-08 19:31:27 +01:00
|
|
|
typedef Matrix<Scalar, RowsAtCompileTime, RowsAtCompileTime, Options, MaxRowsAtCompileTime, MaxRowsAtCompileTime> MatrixQType;
|
2010-03-19 02:12:23 -04:00
|
|
|
typedef typename ei_plain_diag_type<MatrixType>::type HCoeffsType;
|
2010-03-08 19:31:27 +01:00
|
|
|
typedef PermutationMatrix<ColsAtCompileTime, MaxColsAtCompileTime> PermutationType;
|
2010-05-30 16:00:58 -04:00
|
|
|
typedef typename ei_plain_row_type<MatrixType, Index>::type IntRowVectorType;
|
2010-03-19 02:12:23 -04:00
|
|
|
typedef typename ei_plain_row_type<MatrixType>::type RowVectorType;
|
|
|
|
|
typedef typename ei_plain_row_type<MatrixType, RealScalar>::type RealRowVectorType;
|
2009-09-28 10:49:55 -04:00
|
|
|
typedef typename HouseholderSequence<MatrixType,HCoeffsType>::ConjugateReturnType HouseholderSequenceType;
|
2009-08-22 01:13:21 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* \brief Default Constructor.
|
|
|
|
|
*
|
|
|
|
|
* The default constructor is useful in cases in which the user intends to
|
2009-10-28 18:19:29 -04:00
|
|
|
* perform decompositions via ColPivHouseholderQR::compute(const MatrixType&).
|
2009-08-22 01:13:21 -04:00
|
|
|
*/
|
2010-04-21 17:15:57 +02:00
|
|
|
ColPivHouseholderQR()
|
|
|
|
|
: m_qr(),
|
|
|
|
|
m_hCoeffs(),
|
|
|
|
|
m_colsPermutation(),
|
|
|
|
|
m_colsTranspositions(),
|
|
|
|
|
m_temp(),
|
|
|
|
|
m_colSqNorms(),
|
|
|
|
|
m_isInitialized(false) {}
|
|
|
|
|
|
|
|
|
|
/** \brief Default Constructor with memory preallocation
|
|
|
|
|
*
|
|
|
|
|
* Like the default constructor but with preallocation of the internal data
|
|
|
|
|
* according to the specified problem \a size.
|
|
|
|
|
* \sa ColPivHouseholderQR()
|
|
|
|
|
*/
|
2010-05-30 16:00:58 -04:00
|
|
|
ColPivHouseholderQR(Index rows, Index cols)
|
2010-04-21 17:15:57 +02:00
|
|
|
: m_qr(rows, cols),
|
|
|
|
|
m_hCoeffs(std::min(rows,cols)),
|
|
|
|
|
m_colsPermutation(cols),
|
|
|
|
|
m_colsTranspositions(cols),
|
|
|
|
|
m_temp(cols),
|
|
|
|
|
m_colSqNorms(cols),
|
|
|
|
|
m_isInitialized(false),
|
|
|
|
|
m_usePrescribedThreshold(false) {}
|
2009-08-22 01:13:21 -04:00
|
|
|
|
2009-10-28 18:19:29 -04:00
|
|
|
ColPivHouseholderQR(const MatrixType& matrix)
|
2009-08-22 01:13:21 -04:00
|
|
|
: m_qr(matrix.rows(), matrix.cols()),
|
|
|
|
|
m_hCoeffs(std::min(matrix.rows(),matrix.cols())),
|
2010-04-21 17:15:57 +02:00
|
|
|
m_colsPermutation(matrix.cols()),
|
|
|
|
|
m_colsTranspositions(matrix.cols()),
|
|
|
|
|
m_temp(matrix.cols()),
|
|
|
|
|
m_colSqNorms(matrix.cols()),
|
2009-12-01 13:26:29 -05:00
|
|
|
m_isInitialized(false),
|
|
|
|
|
m_usePrescribedThreshold(false)
|
2009-08-22 01:13:21 -04:00
|
|
|
{
|
|
|
|
|
compute(matrix);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** This method finds a solution x to the equation Ax=b, where A is the matrix of which
|
|
|
|
|
* *this is the QR decomposition, if any exists.
|
|
|
|
|
*
|
|
|
|
|
* \param b the right-hand-side of the equation to solve.
|
|
|
|
|
*
|
2009-11-08 10:21:26 -05:00
|
|
|
* \returns a solution.
|
2009-08-22 01:13:21 -04:00
|
|
|
*
|
|
|
|
|
* \note The case where b is a matrix is not yet implemented. Also, this
|
|
|
|
|
* code is space inefficient.
|
|
|
|
|
*
|
2009-11-08 10:21:26 -05:00
|
|
|
* \note_about_checking_solutions
|
|
|
|
|
*
|
|
|
|
|
* \note_about_arbitrary_choice_of_solution
|
|
|
|
|
*
|
2009-10-28 18:19:29 -04:00
|
|
|
* Example: \include ColPivHouseholderQR_solve.cpp
|
|
|
|
|
* Output: \verbinclude ColPivHouseholderQR_solve.out
|
2009-08-22 01:13:21 -04:00
|
|
|
*/
|
2009-11-08 10:21:26 -05:00
|
|
|
template<typename Rhs>
|
2009-11-09 07:51:31 -05:00
|
|
|
inline const ei_solve_retval<ColPivHouseholderQR, Rhs>
|
2009-11-08 10:21:26 -05:00
|
|
|
solve(const MatrixBase<Rhs>& b) const
|
|
|
|
|
{
|
|
|
|
|
ei_assert(m_isInitialized && "ColPivHouseholderQR is not initialized.");
|
2009-11-09 07:51:31 -05:00
|
|
|
return ei_solve_retval<ColPivHouseholderQR, Rhs>(*this, b.derived());
|
2009-11-08 10:21:26 -05:00
|
|
|
}
|
2009-08-22 01:13:21 -04:00
|
|
|
|
2009-12-02 11:11:09 -05:00
|
|
|
HouseholderSequenceType householderQ(void) const;
|
2009-08-22 01:13:21 -04:00
|
|
|
|
|
|
|
|
/** \returns a reference to the matrix where the Householder QR decomposition is stored
|
|
|
|
|
*/
|
2009-08-24 13:46:14 -04:00
|
|
|
const MatrixType& matrixQR() const
|
|
|
|
|
{
|
2009-10-28 18:19:29 -04:00
|
|
|
ei_assert(m_isInitialized && "ColPivHouseholderQR is not initialized.");
|
2009-08-24 13:46:14 -04:00
|
|
|
return m_qr;
|
|
|
|
|
}
|
2009-08-22 01:13:21 -04:00
|
|
|
|
2009-10-28 18:19:29 -04:00
|
|
|
ColPivHouseholderQR& compute(const MatrixType& matrix);
|
2009-09-16 15:56:20 +02:00
|
|
|
|
2009-11-17 08:14:54 -05:00
|
|
|
const PermutationType& colsPermutation() const
|
2009-08-22 01:13:21 -04:00
|
|
|
{
|
2009-10-28 18:19:29 -04:00
|
|
|
ei_assert(m_isInitialized && "ColPivHouseholderQR is not initialized.");
|
2010-04-21 17:15:57 +02:00
|
|
|
return m_colsPermutation;
|
2009-08-22 01:13:21 -04:00
|
|
|
}
|
2009-09-16 15:56:20 +02:00
|
|
|
|
2009-08-24 11:11:41 -04:00
|
|
|
/** \returns the absolute value of the determinant of the matrix of which
|
|
|
|
|
* *this is the QR decomposition. It has only linear complexity
|
|
|
|
|
* (that is, O(n) where n is the dimension of the square matrix)
|
|
|
|
|
* as the QR decomposition has already been computed.
|
|
|
|
|
*
|
|
|
|
|
* \note This is only for square matrices.
|
|
|
|
|
*
|
|
|
|
|
* \warning a determinant can be very big or small, so for matrices
|
|
|
|
|
* of large enough dimension, there is a risk of overflow/underflow.
|
|
|
|
|
* One way to work around that is to use logAbsDeterminant() instead.
|
|
|
|
|
*
|
|
|
|
|
* \sa logAbsDeterminant(), MatrixBase::determinant()
|
|
|
|
|
*/
|
|
|
|
|
typename MatrixType::RealScalar absDeterminant() const;
|
|
|
|
|
|
|
|
|
|
/** \returns the natural log of the absolute value of the determinant of the matrix of which
|
|
|
|
|
* *this is the QR decomposition. It has only linear complexity
|
|
|
|
|
* (that is, O(n) where n is the dimension of the square matrix)
|
|
|
|
|
* as the QR decomposition has already been computed.
|
|
|
|
|
*
|
|
|
|
|
* \note This is only for square matrices.
|
|
|
|
|
*
|
|
|
|
|
* \note This method is useful to work around the risk of overflow/underflow that's inherent
|
|
|
|
|
* to determinant computation.
|
|
|
|
|
*
|
|
|
|
|
* \sa absDeterminant(), MatrixBase::determinant()
|
|
|
|
|
*/
|
|
|
|
|
typename MatrixType::RealScalar logAbsDeterminant() const;
|
2009-09-16 15:56:20 +02:00
|
|
|
|
2009-08-24 11:11:41 -04:00
|
|
|
/** \returns the rank of the matrix of which *this is the QR decomposition.
|
|
|
|
|
*
|
2009-12-01 13:26:29 -05:00
|
|
|
* \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&).
|
2009-08-24 11:11:41 -04:00
|
|
|
*/
|
2010-05-30 16:00:58 -04:00
|
|
|
inline Index rank() const
|
2009-08-22 01:13:21 -04:00
|
|
|
{
|
2009-10-28 18:19:29 -04:00
|
|
|
ei_assert(m_isInitialized && "ColPivHouseholderQR is not initialized.");
|
2009-12-01 13:26:29 -05:00
|
|
|
RealScalar premultiplied_threshold = ei_abs(m_maxpivot) * threshold();
|
2010-05-30 16:00:58 -04:00
|
|
|
Index result = 0;
|
|
|
|
|
for(Index i = 0; i < m_nonzero_pivots; ++i)
|
2009-12-01 13:26:29 -05:00
|
|
|
result += (ei_abs(m_qr.coeff(i,i)) > premultiplied_threshold);
|
|
|
|
|
return result;
|
2009-08-22 01:13:21 -04:00
|
|
|
}
|
|
|
|
|
|
2009-08-24 11:11:41 -04:00
|
|
|
/** \returns the dimension of the kernel of the matrix of which *this is the QR decomposition.
|
|
|
|
|
*
|
2009-12-01 13:26:29 -05:00
|
|
|
* \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&).
|
2009-08-24 11:11:41 -04:00
|
|
|
*/
|
2010-05-30 16:00:58 -04:00
|
|
|
inline Index dimensionOfKernel() const
|
2009-08-24 11:11:41 -04:00
|
|
|
{
|
2009-10-28 18:19:29 -04:00
|
|
|
ei_assert(m_isInitialized && "ColPivHouseholderQR is not initialized.");
|
2009-12-01 13:26:29 -05:00
|
|
|
return cols() - rank();
|
2009-08-24 11:11:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \returns true if the matrix of which *this is the QR decomposition represents an injective
|
|
|
|
|
* linear map, i.e. has trivial kernel; false otherwise.
|
|
|
|
|
*
|
2009-12-01 13:26:29 -05:00
|
|
|
* \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&).
|
2009-08-24 11:11:41 -04:00
|
|
|
*/
|
|
|
|
|
inline bool isInjective() const
|
|
|
|
|
{
|
2009-10-28 18:19:29 -04:00
|
|
|
ei_assert(m_isInitialized && "ColPivHouseholderQR is not initialized.");
|
2009-12-01 13:26:29 -05:00
|
|
|
return rank() == cols();
|
2009-08-24 11:11:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \returns true if the matrix of which *this is the QR decomposition represents a surjective
|
|
|
|
|
* linear map; false otherwise.
|
|
|
|
|
*
|
2009-12-01 13:26:29 -05:00
|
|
|
* \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&).
|
2009-08-24 11:11:41 -04:00
|
|
|
*/
|
|
|
|
|
inline bool isSurjective() const
|
|
|
|
|
{
|
2009-10-28 18:19:29 -04:00
|
|
|
ei_assert(m_isInitialized && "ColPivHouseholderQR is not initialized.");
|
2009-12-01 13:26:29 -05:00
|
|
|
return rank() == rows();
|
2009-08-24 11:11:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \returns true if the matrix of which *this is the QR decomposition is invertible.
|
|
|
|
|
*
|
2009-12-01 13:26:29 -05:00
|
|
|
* \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&).
|
2009-08-24 11:11:41 -04:00
|
|
|
*/
|
|
|
|
|
inline bool isInvertible() const
|
|
|
|
|
{
|
2009-10-28 18:19:29 -04:00
|
|
|
ei_assert(m_isInitialized && "ColPivHouseholderQR is not initialized.");
|
2009-08-24 11:11:41 -04:00
|
|
|
return isInjective() && isSurjective();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \returns the inverse of the matrix of which *this is the QR decomposition.
|
|
|
|
|
*
|
|
|
|
|
* \note If this matrix is not invertible, the returned matrix has undefined coefficients.
|
|
|
|
|
* Use isInvertible() to first determine whether this matrix is invertible.
|
|
|
|
|
*/
|
2009-11-08 10:21:26 -05:00
|
|
|
inline const
|
2009-12-01 10:22:54 +01:00
|
|
|
ei_solve_retval<ColPivHouseholderQR, typename MatrixType::IdentityReturnType>
|
2009-11-08 10:21:26 -05:00
|
|
|
inverse() const
|
2009-08-24 11:11:41 -04:00
|
|
|
{
|
2009-11-08 10:21:26 -05:00
|
|
|
ei_assert(m_isInitialized && "ColPivHouseholderQR is not initialized.");
|
2009-12-01 10:22:54 +01:00
|
|
|
return ei_solve_retval<ColPivHouseholderQR,typename MatrixType::IdentityReturnType>
|
2009-12-01 13:16:51 +01:00
|
|
|
(*this, MatrixType::Identity(m_qr.rows(), m_qr.cols()));
|
2009-08-24 11:11:41 -04:00
|
|
|
}
|
|
|
|
|
|
2010-05-30 16:00:58 -04:00
|
|
|
inline Index rows() const { return m_qr.rows(); }
|
|
|
|
|
inline Index cols() const { return m_qr.cols(); }
|
2009-11-08 10:21:26 -05:00
|
|
|
const HCoeffsType& hCoeffs() const { return m_hCoeffs; }
|
|
|
|
|
|
2009-12-01 13:26:29 -05:00
|
|
|
/** 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;
|
2010-05-28 10:18:37 +02:00
|
|
|
return *this;
|
2009-12-01 13:26:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 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;
|
2010-05-28 10:18:37 +02:00
|
|
|
return *this;
|
2009-12-01 13:26:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Returns the threshold that will be used by certain methods such as rank().
|
|
|
|
|
*
|
|
|
|
|
* See the documentation of setThreshold(const RealScalar&).
|
|
|
|
|
*/
|
|
|
|
|
RealScalar threshold() const
|
|
|
|
|
{
|
|
|
|
|
ei_assert(m_isInitialized || m_usePrescribedThreshold);
|
|
|
|
|
return m_usePrescribedThreshold ? m_prescribedThreshold
|
|
|
|
|
// this formula comes from experimenting (see "LU precision tuning" thread on the list)
|
|
|
|
|
// and turns out to be identical to Higham's formula used already in LDLt.
|
2010-02-10 10:52:28 +01:00
|
|
|
: NumTraits<Scalar>::epsilon() * m_qr.diagonalSize();
|
2009-12-01 13:26:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \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()
|
|
|
|
|
*/
|
2010-05-30 16:00:58 -04:00
|
|
|
inline Index nonzeroPivots() const
|
2009-12-01 13:26:29 -05:00
|
|
|
{
|
|
|
|
|
ei_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; }
|
|
|
|
|
|
2009-08-22 01:13:21 -04:00
|
|
|
protected:
|
|
|
|
|
MatrixType m_qr;
|
|
|
|
|
HCoeffsType m_hCoeffs;
|
2010-04-21 17:15:57 +02:00
|
|
|
PermutationType m_colsPermutation;
|
|
|
|
|
IntRowVectorType m_colsTranspositions;
|
|
|
|
|
RowVectorType m_temp;
|
|
|
|
|
RealRowVectorType m_colSqNorms;
|
2009-12-01 13:26:29 -05:00
|
|
|
bool m_isInitialized, m_usePrescribedThreshold;
|
|
|
|
|
RealScalar m_prescribedThreshold, m_maxpivot;
|
2010-05-30 16:00:58 -04:00
|
|
|
Index m_nonzero_pivots;
|
|
|
|
|
Index m_det_pq;
|
2009-08-22 01:13:21 -04:00
|
|
|
};
|
|
|
|
|
|
2009-08-24 11:11:41 -04:00
|
|
|
template<typename MatrixType>
|
2009-10-28 18:19:29 -04:00
|
|
|
typename MatrixType::RealScalar ColPivHouseholderQR<MatrixType>::absDeterminant() const
|
2009-08-24 11:11:41 -04:00
|
|
|
{
|
2009-10-28 18:19:29 -04:00
|
|
|
ei_assert(m_isInitialized && "ColPivHouseholderQR is not initialized.");
|
2009-08-24 11:11:41 -04:00
|
|
|
ei_assert(m_qr.rows() == m_qr.cols() && "You can't take the determinant of a non-square matrix!");
|
|
|
|
|
return ei_abs(m_qr.diagonal().prod());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename MatrixType>
|
2009-10-28 18:19:29 -04:00
|
|
|
typename MatrixType::RealScalar ColPivHouseholderQR<MatrixType>::logAbsDeterminant() const
|
2009-08-24 11:11:41 -04:00
|
|
|
{
|
2009-10-28 18:19:29 -04:00
|
|
|
ei_assert(m_isInitialized && "ColPivHouseholderQR is not initialized.");
|
2009-08-24 11:11:41 -04:00
|
|
|
ei_assert(m_qr.rows() == m_qr.cols() && "You can't take the determinant of a non-square matrix!");
|
2009-12-04 23:17:14 +01:00
|
|
|
return m_qr.diagonal().cwiseAbs().array().log().sum();
|
2009-08-24 11:11:41 -04:00
|
|
|
}
|
|
|
|
|
|
2009-08-22 01:13:21 -04:00
|
|
|
template<typename MatrixType>
|
2009-10-28 18:19:29 -04:00
|
|
|
ColPivHouseholderQR<MatrixType>& ColPivHouseholderQR<MatrixType>::compute(const MatrixType& matrix)
|
2009-08-22 01:13:21 -04:00
|
|
|
{
|
2010-05-30 16:00:58 -04:00
|
|
|
Index rows = matrix.rows();
|
|
|
|
|
Index cols = matrix.cols();
|
|
|
|
|
Index size = matrix.diagonalSize();
|
2009-09-16 15:56:20 +02:00
|
|
|
|
2009-08-22 01:13:21 -04:00
|
|
|
m_qr = matrix;
|
|
|
|
|
m_hCoeffs.resize(size);
|
|
|
|
|
|
2010-04-21 17:15:57 +02:00
|
|
|
m_temp.resize(cols);
|
2009-08-22 01:13:21 -04:00
|
|
|
|
2010-04-21 17:15:57 +02:00
|
|
|
m_colsTranspositions.resize(matrix.cols());
|
2010-05-30 16:00:58 -04:00
|
|
|
Index number_of_transpositions = 0;
|
2009-09-16 15:56:20 +02:00
|
|
|
|
2010-04-21 17:15:57 +02:00
|
|
|
m_colSqNorms.resize(cols);
|
2010-05-30 16:00:58 -04:00
|
|
|
for(Index k = 0; k < cols; ++k)
|
2010-04-21 17:15:57 +02:00
|
|
|
m_colSqNorms.coeffRef(k) = m_qr.col(k).squaredNorm();
|
2009-09-16 15:56:20 +02:00
|
|
|
|
2010-04-21 17:15:57 +02:00
|
|
|
RealScalar threshold_helper = m_colSqNorms.maxCoeff() * ei_abs2(NumTraits<Scalar>::epsilon()) / rows;
|
2009-09-16 15:56:20 +02:00
|
|
|
|
2009-12-01 13:26:29 -05:00
|
|
|
m_nonzero_pivots = size; // the generic case is that in which all pivots are nonzero (invertible case)
|
|
|
|
|
m_maxpivot = RealScalar(0);
|
2009-09-16 15:56:20 +02:00
|
|
|
|
2010-05-30 16:00:58 -04:00
|
|
|
for(Index k = 0; k < size; ++k)
|
2009-12-01 13:26:29 -05:00
|
|
|
{
|
2010-04-21 17:15:57 +02:00
|
|
|
// first, we look up in our table m_colSqNorms which column has the biggest squared norm
|
2010-05-30 16:00:58 -04:00
|
|
|
Index biggest_col_index;
|
2010-04-21 17:15:57 +02:00
|
|
|
RealScalar biggest_col_sq_norm = m_colSqNorms.tail(cols-k).maxCoeff(&biggest_col_index);
|
2009-12-01 13:26:29 -05:00
|
|
|
biggest_col_index += k;
|
|
|
|
|
|
2010-04-21 17:15:57 +02:00
|
|
|
// since our table m_colSqNorms accumulates imprecision at every step, we must now recompute
|
2009-12-01 13:26:29 -05:00
|
|
|
// the actual squared norm of the selected column.
|
|
|
|
|
// Note that not doing so does result in solve() sometimes returning inf/nan values
|
|
|
|
|
// when running the unit test with 1000 repetitions.
|
2010-01-04 21:24:43 -05:00
|
|
|
biggest_col_sq_norm = m_qr.col(biggest_col_index).tail(rows-k).squaredNorm();
|
2009-12-01 13:26:29 -05:00
|
|
|
|
|
|
|
|
// we store that back into our table: it can't hurt to correct our table.
|
2010-04-21 17:15:57 +02:00
|
|
|
m_colSqNorms.coeffRef(biggest_col_index) = biggest_col_sq_norm;
|
2009-12-01 13:26:29 -05:00
|
|
|
|
2009-12-01 13:51:35 -05:00
|
|
|
// if the current biggest column is smaller than epsilon times the initial biggest column,
|
|
|
|
|
// terminate to avoid generating nan/inf values.
|
2009-12-01 13:26:29 -05:00
|
|
|
// Note that here, if we test instead for "biggest == 0", we get a failure every 1000 (or so)
|
|
|
|
|
// repetitions of the unit test, with the result of solve() filled with large values of the order
|
2009-12-01 13:51:35 -05:00
|
|
|
// of 1/(size*epsilon).
|
|
|
|
|
if(biggest_col_sq_norm < threshold_helper * (rows-k))
|
2009-08-22 01:13:21 -04:00
|
|
|
{
|
2009-12-01 13:26:29 -05:00
|
|
|
m_nonzero_pivots = k;
|
2010-01-04 21:24:43 -05:00
|
|
|
m_hCoeffs.tail(size-k).setZero();
|
2010-04-22 14:11:18 -04:00
|
|
|
m_qr.bottomRightCorner(rows-k,cols-k)
|
2010-01-07 21:15:32 +01:00
|
|
|
.template triangularView<StrictlyLower>()
|
2009-12-01 13:26:29 -05:00
|
|
|
.setZero();
|
2009-08-22 01:13:21 -04:00
|
|
|
break;
|
|
|
|
|
}
|
2009-09-16 15:56:20 +02:00
|
|
|
|
2009-12-01 13:26:29 -05:00
|
|
|
// apply the transposition to the columns
|
2010-04-21 17:15:57 +02:00
|
|
|
m_colsTranspositions.coeffRef(k) = biggest_col_index;
|
2009-12-01 13:26:29 -05:00
|
|
|
if(k != biggest_col_index) {
|
|
|
|
|
m_qr.col(k).swap(m_qr.col(biggest_col_index));
|
2010-04-21 17:15:57 +02:00
|
|
|
std::swap(m_colSqNorms.coeffRef(k), m_colSqNorms.coeffRef(biggest_col_index));
|
2009-08-22 01:13:21 -04:00
|
|
|
++number_of_transpositions;
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-01 13:26:29 -05:00
|
|
|
// generate the householder vector, store it below the diagonal
|
2009-08-22 01:13:21 -04:00
|
|
|
RealScalar beta;
|
2010-01-04 21:24:43 -05:00
|
|
|
m_qr.col(k).tail(rows-k).makeHouseholderInPlace(m_hCoeffs.coeffRef(k), beta);
|
2009-12-01 13:26:29 -05:00
|
|
|
|
|
|
|
|
// apply the householder transformation to the diagonal coefficient
|
2009-08-22 01:13:21 -04:00
|
|
|
m_qr.coeffRef(k,k) = beta;
|
|
|
|
|
|
2009-12-01 13:26:29 -05:00
|
|
|
// remember the maximum absolute value of diagonal coefficients
|
|
|
|
|
if(ei_abs(beta) > m_maxpivot) m_maxpivot = ei_abs(beta);
|
|
|
|
|
|
|
|
|
|
// apply the householder transformation
|
2010-04-22 14:11:18 -04:00
|
|
|
m_qr.bottomRightCorner(rows-k, cols-k-1)
|
2010-04-21 17:15:57 +02:00
|
|
|
.applyHouseholderOnTheLeft(m_qr.col(k).tail(rows-k-1), m_hCoeffs.coeffRef(k), &m_temp.coeffRef(k+1));
|
2009-09-16 15:56:20 +02:00
|
|
|
|
2009-12-01 13:26:29 -05:00
|
|
|
// update our table of squared norms of the columns
|
2010-04-21 17:15:57 +02:00
|
|
|
m_colSqNorms.tail(cols-k-1) -= m_qr.row(k).tail(cols-k-1).cwiseAbs2();
|
2009-08-22 01:13:21 -04:00
|
|
|
}
|
|
|
|
|
|
2010-04-21 17:15:57 +02:00
|
|
|
m_colsPermutation.setIdentity(cols);
|
2010-05-30 16:00:58 -04:00
|
|
|
for(Index k = 0; k < m_nonzero_pivots; ++k)
|
2010-04-21 17:15:57 +02:00
|
|
|
m_colsPermutation.applyTranspositionOnTheRight(k, m_colsTranspositions.coeff(k));
|
2009-08-22 01:13:21 -04:00
|
|
|
|
|
|
|
|
m_det_pq = (number_of_transpositions%2) ? -1 : 1;
|
|
|
|
|
m_isInitialized = true;
|
2009-09-16 15:56:20 +02:00
|
|
|
|
2009-08-22 01:13:21 -04:00
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-08 16:51:41 -05:00
|
|
|
template<typename _MatrixType, typename Rhs>
|
2009-11-09 07:51:31 -05:00
|
|
|
struct ei_solve_retval<ColPivHouseholderQR<_MatrixType>, Rhs>
|
|
|
|
|
: ei_solve_retval_base<ColPivHouseholderQR<_MatrixType>, Rhs>
|
2009-08-22 01:13:21 -04:00
|
|
|
{
|
2009-11-08 16:51:41 -05:00
|
|
|
EIGEN_MAKE_SOLVE_HELPERS(ColPivHouseholderQR<_MatrixType>,Rhs)
|
2010-01-05 13:07:32 +01:00
|
|
|
|
2009-11-08 16:51:41 -05:00
|
|
|
template<typename Dest> void evalTo(Dest& dst) const
|
2009-08-24 11:11:41 -04:00
|
|
|
{
|
2010-05-30 16:00:58 -04:00
|
|
|
ei_assert(rhs().rows() == dec().rows());
|
|
|
|
|
|
2010-05-21 02:05:25 +02:00
|
|
|
const int cols = dec().cols(),
|
2010-05-30 16:00:58 -04:00
|
|
|
nonzero_pivots = dec().nonzeroPivots();
|
2009-11-08 10:21:26 -05:00
|
|
|
|
2009-12-01 13:26:29 -05:00
|
|
|
if(nonzero_pivots == 0)
|
2009-08-24 11:11:41 -04:00
|
|
|
{
|
2009-11-08 10:21:26 -05:00
|
|
|
dst.setZero();
|
|
|
|
|
return;
|
2009-08-24 11:11:41 -04:00
|
|
|
}
|
|
|
|
|
|
2010-02-20 15:53:57 +01:00
|
|
|
typename Rhs::PlainObject c(rhs());
|
2009-09-16 15:56:20 +02:00
|
|
|
|
2009-11-08 10:21:26 -05:00
|
|
|
// Note that the matrix Q = H_0^* H_1^*... so its inverse is Q^* = (H_0 H_1 ...)^T
|
2009-11-10 21:22:20 -05:00
|
|
|
c.applyOnTheLeft(householderSequence(
|
2009-12-01 13:26:29 -05:00
|
|
|
dec().matrixQR(),
|
|
|
|
|
dec().hCoeffs(),
|
2009-12-02 11:11:09 -05:00
|
|
|
true,
|
2010-01-11 08:48:39 -05:00
|
|
|
dec().nonzeroPivots(),
|
|
|
|
|
0
|
2009-12-01 13:26:29 -05:00
|
|
|
));
|
2009-08-24 13:46:14 -04:00
|
|
|
|
2009-11-08 16:51:41 -05:00
|
|
|
dec().matrixQR()
|
2010-04-22 14:11:18 -04:00
|
|
|
.topLeftCorner(nonzero_pivots, nonzero_pivots)
|
2010-01-07 21:15:32 +01:00
|
|
|
.template triangularView<Upper>()
|
2010-04-22 14:11:18 -04:00
|
|
|
.solveInPlace(c.topRows(nonzero_pivots));
|
2009-12-01 13:26:29 -05:00
|
|
|
|
|
|
|
|
|
2010-02-20 15:53:57 +01:00
|
|
|
typename Rhs::PlainObject d(c);
|
2010-04-22 14:11:18 -04:00
|
|
|
d.topRows(nonzero_pivots)
|
2009-12-01 13:26:29 -05:00
|
|
|
= dec().matrixQR()
|
2010-04-22 14:11:18 -04:00
|
|
|
.topLeftCorner(nonzero_pivots, nonzero_pivots)
|
2010-01-07 21:15:32 +01:00
|
|
|
.template triangularView<Upper>()
|
2010-04-22 14:11:18 -04:00
|
|
|
* c.topRows(nonzero_pivots);
|
2009-08-22 01:13:21 -04:00
|
|
|
|
2010-05-30 16:00:58 -04:00
|
|
|
for(Index i = 0; i < nonzero_pivots; ++i) dst.row(dec().colsPermutation().indices().coeff(i)) = c.row(i);
|
|
|
|
|
for(Index i = nonzero_pivots; i < cols; ++i) dst.row(dec().colsPermutation().indices().coeff(i)).setZero();
|
2009-11-08 10:21:26 -05:00
|
|
|
}
|
|
|
|
|
};
|
2009-08-22 01:13:21 -04:00
|
|
|
|
2009-09-16 15:56:20 +02:00
|
|
|
/** \returns the matrix Q as a sequence of householder transformations */
|
2009-08-22 01:13:21 -04:00
|
|
|
template<typename MatrixType>
|
2009-12-02 11:11:09 -05:00
|
|
|
typename ColPivHouseholderQR<MatrixType>::HouseholderSequenceType ColPivHouseholderQR<MatrixType>
|
|
|
|
|
::householderQ() const
|
2009-08-22 01:13:21 -04:00
|
|
|
{
|
2009-10-28 18:19:29 -04:00
|
|
|
ei_assert(m_isInitialized && "ColPivHouseholderQR is not initialized.");
|
2010-01-11 08:48:39 -05:00
|
|
|
return HouseholderSequenceType(m_qr, m_hCoeffs.conjugate(), false, m_nonzero_pivots, 0);
|
2009-08-22 01:13:21 -04:00
|
|
|
}
|
|
|
|
|
|
2009-08-23 18:04:33 -04:00
|
|
|
/** \return the column-pivoting Householder QR decomposition of \c *this.
|
2009-08-22 01:13:21 -04:00
|
|
|
*
|
2009-10-28 18:19:29 -04:00
|
|
|
* \sa class ColPivHouseholderQR
|
2009-08-22 01:13:21 -04:00
|
|
|
*/
|
|
|
|
|
template<typename Derived>
|
2010-02-20 15:53:57 +01:00
|
|
|
const ColPivHouseholderQR<typename MatrixBase<Derived>::PlainObject>
|
2009-10-28 18:19:29 -04:00
|
|
|
MatrixBase<Derived>::colPivHouseholderQr() const
|
2009-08-22 01:13:21 -04:00
|
|
|
{
|
2010-02-20 15:53:57 +01:00
|
|
|
return ColPivHouseholderQR<PlainObject>(eval());
|
2009-08-22 01:13:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-08-23 18:04:33 -04:00
|
|
|
#endif // EIGEN_COLPIVOTINGHOUSEHOLDERQR_H
|