mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-07-20 20:04:26 +08:00
give FullPivotingHouseholderQR all the modern comfort
This commit is contained in:
parent
154bdac9f4
commit
c9a307f330
@ -94,7 +94,7 @@ template<typename MatrixType> class FullPivotingHouseholderQR
|
|||||||
* Output: \verbinclude FullPivotingHouseholderQR_solve.out
|
* Output: \verbinclude FullPivotingHouseholderQR_solve.out
|
||||||
*/
|
*/
|
||||||
template<typename OtherDerived, typename ResultType>
|
template<typename OtherDerived, typename ResultType>
|
||||||
void solve(const MatrixBase<OtherDerived>& b, ResultType *result) const;
|
bool solve(const MatrixBase<OtherDerived>& b, ResultType *result) const;
|
||||||
|
|
||||||
MatrixType matrixQ(void) const;
|
MatrixType matrixQ(void) const;
|
||||||
|
|
||||||
@ -106,22 +106,117 @@ template<typename MatrixType> class FullPivotingHouseholderQR
|
|||||||
|
|
||||||
const IntRowVectorType& colsPermutation() const
|
const IntRowVectorType& colsPermutation() const
|
||||||
{
|
{
|
||||||
ei_assert(m_isInitialized && "FULLPIVOTINGHOUSEHOLDERQR is not initialized.");
|
ei_assert(m_isInitialized && "FullPivotingHouseholderQR is not initialized.");
|
||||||
return m_cols_permutation;
|
return m_cols_permutation;
|
||||||
}
|
}
|
||||||
|
|
||||||
const IntColVectorType& rowsTranspositions() const
|
const IntColVectorType& rowsTranspositions() const
|
||||||
{
|
{
|
||||||
ei_assert(m_isInitialized && "FULLPIVOTINGHOUSEHOLDERQR is not initialized.");
|
ei_assert(m_isInitialized && "FullPivotingHouseholderQR is not initialized.");
|
||||||
return m_rows_transpositions;
|
return m_rows_transpositions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** \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.
|
||||||
|
*
|
||||||
|
* \sa MatrixBase::determinant()
|
||||||
|
*/
|
||||||
|
typename MatrixType::RealScalar absDeterminant() const;
|
||||||
|
|
||||||
|
/** \returns the rank of the matrix of which *this is the QR decomposition.
|
||||||
|
*
|
||||||
|
* \note This is computed at the time of the construction of the QR decomposition. This
|
||||||
|
* method does not perform any further computation.
|
||||||
|
*/
|
||||||
inline int rank() const
|
inline int rank() const
|
||||||
{
|
{
|
||||||
ei_assert(m_isInitialized && "FULLPIVOTINGHOUSEHOLDERQR is not initialized.");
|
ei_assert(m_isInitialized && "FullPivotingHouseholderQR is not initialized.");
|
||||||
return m_rank;
|
return m_rank;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** \returns the dimension of the kernel of the matrix of which *this is the QR decomposition.
|
||||||
|
*
|
||||||
|
* \note Since the rank is computed at the time of the construction of the QR decomposition, this
|
||||||
|
* method almost does not perform any further computation.
|
||||||
|
*/
|
||||||
|
inline int dimensionOfKernel() const
|
||||||
|
{
|
||||||
|
ei_assert(m_isInitialized && "FullPivotingHouseholderQR is not initialized.");
|
||||||
|
return m_qr.cols() - m_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 Since the rank is computed at the time of the construction of the QR decomposition, this
|
||||||
|
* method almost does not perform any further computation.
|
||||||
|
*/
|
||||||
|
inline bool isInjective() const
|
||||||
|
{
|
||||||
|
ei_assert(m_isInitialized && "FullPivotingHouseholderQR is not initialized.");
|
||||||
|
return m_rank == m_qr.cols();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** \returns true if the matrix of which *this is the QR decomposition represents a surjective
|
||||||
|
* linear map; false otherwise.
|
||||||
|
*
|
||||||
|
* \note Since the rank is computed at the time of the construction of the QR decomposition, this
|
||||||
|
* method almost does not perform any further computation.
|
||||||
|
*/
|
||||||
|
inline bool isSurjective() const
|
||||||
|
{
|
||||||
|
ei_assert(m_isInitialized && "FullPivotingHouseholderQR is not initialized.");
|
||||||
|
return m_rank == m_qr.rows();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** \returns true if the matrix of which *this is the QR decomposition is invertible.
|
||||||
|
*
|
||||||
|
* \note Since the rank is computed at the time of the construction of the QR decomposition, this
|
||||||
|
* method almost does not perform any further computation.
|
||||||
|
*/
|
||||||
|
inline bool isInvertible() const
|
||||||
|
{
|
||||||
|
ei_assert(m_isInitialized && "FullPivotingHouseholderQR is not initialized.");
|
||||||
|
return isInjective() && isSurjective();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Computes the inverse of the matrix of which *this is the QR decomposition.
|
||||||
|
*
|
||||||
|
* \param result a pointer to the matrix into which to store the inverse. Resized if needed.
|
||||||
|
*
|
||||||
|
* \note If this matrix is not invertible, *result is left with undefined coefficients.
|
||||||
|
* Use isInvertible() to first determine whether this matrix is invertible.
|
||||||
|
*
|
||||||
|
* \sa inverse()
|
||||||
|
*/
|
||||||
|
inline void computeInverse(MatrixType *result) const
|
||||||
|
{
|
||||||
|
ei_assert(m_isInitialized && "FullPivotingHouseholderQR is not initialized.");
|
||||||
|
ei_assert(m_qr.rows() == m_qr.cols() && "You can't take the inverse of a non-square matrix!");
|
||||||
|
solve(MatrixType::Identity(m_qr.rows(), m_qr.cols()), result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** \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.
|
||||||
|
*
|
||||||
|
* \sa computeInverse()
|
||||||
|
*/
|
||||||
|
inline MatrixType inverse() const
|
||||||
|
{
|
||||||
|
MatrixType result;
|
||||||
|
computeInverse(&result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
MatrixType m_qr;
|
MatrixType m_qr;
|
||||||
HCoeffsType m_hCoeffs;
|
HCoeffsType m_hCoeffs;
|
||||||
@ -135,6 +230,14 @@ template<typename MatrixType> class FullPivotingHouseholderQR
|
|||||||
|
|
||||||
#ifndef EIGEN_HIDE_HEAVY_CODE
|
#ifndef EIGEN_HIDE_HEAVY_CODE
|
||||||
|
|
||||||
|
template<typename MatrixType>
|
||||||
|
typename MatrixType::RealScalar FullPivotingHouseholderQR<MatrixType>::absDeterminant() const
|
||||||
|
{
|
||||||
|
ei_assert(m_isInitialized && "FullPivotingHouseholderQR is not initialized.");
|
||||||
|
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>
|
template<typename MatrixType>
|
||||||
FullPivotingHouseholderQR<MatrixType>& FullPivotingHouseholderQR<MatrixType>::compute(const MatrixType& matrix)
|
FullPivotingHouseholderQR<MatrixType>& FullPivotingHouseholderQR<MatrixType>::compute(const MatrixType& matrix)
|
||||||
{
|
{
|
||||||
@ -213,12 +316,14 @@ FullPivotingHouseholderQR<MatrixType>& FullPivotingHouseholderQR<MatrixType>::co
|
|||||||
|
|
||||||
template<typename MatrixType>
|
template<typename MatrixType>
|
||||||
template<typename OtherDerived, typename ResultType>
|
template<typename OtherDerived, typename ResultType>
|
||||||
void FullPivotingHouseholderQR<MatrixType>::solve(
|
bool FullPivotingHouseholderQR<MatrixType>::solve(
|
||||||
const MatrixBase<OtherDerived>& b,
|
const MatrixBase<OtherDerived>& b,
|
||||||
ResultType *result
|
ResultType *result
|
||||||
) const
|
) const
|
||||||
{
|
{
|
||||||
ei_assert(m_isInitialized && "FullPivotingHouseholderQR is not initialized.");
|
ei_assert(m_isInitialized && "FullPivotingHouseholderQR is not initialized.");
|
||||||
|
if(m_rank==0) return false;
|
||||||
|
|
||||||
const int rows = m_qr.rows();
|
const int rows = m_qr.rows();
|
||||||
const int cols = b.cols();
|
const int cols = b.cols();
|
||||||
ei_assert(b.rows() == rows);
|
ei_assert(b.rows() == rows);
|
||||||
@ -234,6 +339,14 @@ void FullPivotingHouseholderQR<MatrixType>::solve(
|
|||||||
.applyHouseholderOnTheLeft(m_qr.col(k).end(remainingSize-1), m_hCoeffs.coeff(k), &temp.coeffRef(0));
|
.applyHouseholderOnTheLeft(m_qr.col(k).end(remainingSize-1), m_hCoeffs.coeff(k), &temp.coeffRef(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!isSurjective())
|
||||||
|
{
|
||||||
|
// is c is in the image of R ?
|
||||||
|
RealScalar biggest_in_upper_part_of_c = c.corner(TopLeft, m_rank, c.cols()).cwise().abs().maxCoeff();
|
||||||
|
RealScalar biggest_in_lower_part_of_c = c.corner(BottomLeft, rows-m_rank, c.cols()).cwise().abs().maxCoeff();
|
||||||
|
if(!ei_isMuchSmallerThan(biggest_in_lower_part_of_c, biggest_in_upper_part_of_c, m_precision))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
m_qr.corner(TopLeft, m_rank, m_rank)
|
m_qr.corner(TopLeft, m_rank, m_rank)
|
||||||
.template triangularView<UpperTriangular>()
|
.template triangularView<UpperTriangular>()
|
||||||
.solveInPlace(c.corner(TopLeft, m_rank, c.cols()));
|
.solveInPlace(c.corner(TopLeft, m_rank, c.cols()));
|
||||||
@ -241,6 +354,7 @@ void FullPivotingHouseholderQR<MatrixType>::solve(
|
|||||||
result->resize(m_qr.cols(), b.cols());
|
result->resize(m_qr.cols(), b.cols());
|
||||||
for(int i = 0; i < m_rank; ++i) result->row(m_cols_permutation.coeff(i)) = c.row(i);
|
for(int i = 0; i < m_rank; ++i) result->row(m_cols_permutation.coeff(i)) = c.row(i);
|
||||||
for(int i = m_rank; i < m_qr.cols(); ++i) result->row(m_cols_permutation.coeff(i)).setZero();
|
for(int i = m_rank; i < m_qr.cols(); ++i) result->row(m_cols_permutation.coeff(i)).setZero();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** \returns the matrix Q */
|
/** \returns the matrix Q */
|
||||||
|
@ -39,6 +39,11 @@ template<typename MatrixType> void qr()
|
|||||||
createRandomMatrixOfRank(rank,rows,cols,m1);
|
createRandomMatrixOfRank(rank,rows,cols,m1);
|
||||||
FullPivotingHouseholderQR<MatrixType> qr(m1);
|
FullPivotingHouseholderQR<MatrixType> qr(m1);
|
||||||
VERIFY_IS_APPROX(rank, qr.rank());
|
VERIFY_IS_APPROX(rank, qr.rank());
|
||||||
|
VERIFY(cols - qr.rank() == qr.dimensionOfKernel());
|
||||||
|
VERIFY(!qr.isInjective());
|
||||||
|
VERIFY(!qr.isInvertible());
|
||||||
|
VERIFY(!qr.isSurjective());
|
||||||
|
|
||||||
|
|
||||||
MatrixType r = qr.matrixQR();
|
MatrixType r = qr.matrixQR();
|
||||||
// FIXME need better way to construct trapezoid
|
// FIXME need better way to construct trapezoid
|
||||||
@ -54,8 +59,10 @@ template<typename MatrixType> void qr()
|
|||||||
MatrixType m2 = MatrixType::Random(cols,cols2);
|
MatrixType m2 = MatrixType::Random(cols,cols2);
|
||||||
MatrixType m3 = m1*m2;
|
MatrixType m3 = m1*m2;
|
||||||
m2 = MatrixType::Random(cols,cols2);
|
m2 = MatrixType::Random(cols,cols2);
|
||||||
qr.solve(m3, &m2);
|
VERIFY(qr.solve(m3, &m2));
|
||||||
VERIFY_IS_APPROX(m3, m1*m2);
|
VERIFY_IS_APPROX(m3, m1*m2);
|
||||||
|
m3 = MatrixType::Random(rows,cols2);
|
||||||
|
VERIFY(!qr.solve(m3, &m2));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename MatrixType> void qr_invertible()
|
template<typename MatrixType> void qr_invertible()
|
||||||
@ -74,8 +81,12 @@ template<typename MatrixType> void qr_invertible()
|
|||||||
}
|
}
|
||||||
|
|
||||||
FullPivotingHouseholderQR<MatrixType> qr(m1);
|
FullPivotingHouseholderQR<MatrixType> qr(m1);
|
||||||
|
VERIFY(qr.isInjective());
|
||||||
|
VERIFY(qr.isInvertible());
|
||||||
|
VERIFY(qr.isSurjective());
|
||||||
|
|
||||||
m3 = MatrixType::Random(size,size);
|
m3 = MatrixType::Random(size,size);
|
||||||
qr.solve(m3, &m2);
|
VERIFY(qr.solve(m3, &m2));
|
||||||
VERIFY_IS_APPROX(m3, m1*m2);
|
VERIFY_IS_APPROX(m3, m1*m2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user