2008-04-26 18:26:05 +00:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
2009-05-22 20:25:33 +02:00
|
|
|
// for linear algebra.
|
2008-04-26 18:26:05 +00:00
|
|
|
//
|
2009-08-22 01:13:21 -04:00
|
|
|
// Copyright (C) 2008-2009 Gael Guennebaud <g.gael@free.fr>
|
2009-08-24 13:46:14 -04:00
|
|
|
// Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
|
2008-04-26 18:26:05 +00:00
|
|
|
//
|
|
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
|
|
#ifndef EIGEN_QR_H
|
|
|
|
|
#define EIGEN_QR_H
|
|
|
|
|
|
2008-07-22 10:54:42 +00:00
|
|
|
/** \ingroup QR_Module
|
2008-11-22 19:51:05 +00:00
|
|
|
* \nonstableyet
|
2008-07-22 10:54:42 +00:00
|
|
|
*
|
2009-07-06 17:12:10 +02:00
|
|
|
* \class HouseholderQR
|
2008-04-26 18:26:05 +00:00
|
|
|
*
|
2009-07-06 17:12:10 +02:00
|
|
|
* \brief Householder QR decomposition of a matrix
|
2008-04-26 18:26:05 +00:00
|
|
|
*
|
|
|
|
|
* \param MatrixType the type of the matrix of which we are computing the QR decomposition
|
|
|
|
|
*
|
|
|
|
|
* This class performs a QR decomposition using Householder transformations. The result is
|
2008-06-07 22:47:11 +00:00
|
|
|
* stored in a compact way compatible with LAPACK.
|
2008-04-26 18:26:05 +00:00
|
|
|
*
|
2009-07-06 17:12:10 +02:00
|
|
|
* Note that no pivoting is performed. This is \b not a rank-revealing decomposition.
|
2009-10-28 18:19:29 -04:00
|
|
|
* If you want that feature, use FullPivHouseholderQR or ColPivHouseholderQR instead.
|
2009-08-24 13:46:14 -04:00
|
|
|
*
|
|
|
|
|
* This Householder QR decomposition is faster, but less numerically stable and less feature-full than
|
2009-10-28 18:19:29 -04:00
|
|
|
* FullPivHouseholderQR or ColPivHouseholderQR.
|
2009-07-06 17:12:10 +02:00
|
|
|
*
|
2009-08-22 01:13:21 -04:00
|
|
|
* \sa MatrixBase::householderQr()
|
2008-04-26 18:26:05 +00:00
|
|
|
*/
|
2009-11-08 10:21:26 -05:00
|
|
|
template<typename _MatrixType> class HouseholderQR
|
2008-04-26 18:26:05 +00:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
|
2009-11-08 10:21:26 -05:00
|
|
|
typedef _MatrixType MatrixType;
|
2009-08-16 19:22:15 +02:00
|
|
|
enum {
|
2009-08-24 13:46:14 -04:00
|
|
|
RowsAtCompileTime = MatrixType::RowsAtCompileTime,
|
|
|
|
|
ColsAtCompileTime = MatrixType::ColsAtCompileTime,
|
|
|
|
|
Options = MatrixType::Options,
|
|
|
|
|
DiagSizeAtCompileTime = EIGEN_ENUM_MIN(ColsAtCompileTime,RowsAtCompileTime)
|
2009-08-16 19:22:15 +02:00
|
|
|
};
|
2008-04-26 18:26:05 +00:00
|
|
|
typedef typename MatrixType::Scalar Scalar;
|
2008-06-07 22:47:11 +00:00
|
|
|
typedef typename MatrixType::RealScalar RealScalar;
|
2009-09-25 10:12:09 +02:00
|
|
|
typedef Matrix<Scalar, RowsAtCompileTime, RowsAtCompileTime, AutoAlign | (ei_traits<MatrixType>::Flags&RowMajorBit ? RowMajor : ColMajor)> MatrixQType;
|
2009-08-24 13:46:14 -04:00
|
|
|
typedef Matrix<Scalar, DiagSizeAtCompileTime, 1> HCoeffsType;
|
|
|
|
|
typedef Matrix<Scalar, 1, ColsAtCompileTime> RowVectorType;
|
2009-09-28 10:49:55 -04:00
|
|
|
typedef typename HouseholderSequence<MatrixType,HCoeffsType>::ConjugateReturnType HouseholderSequenceType;
|
2008-04-26 18:26:05 +00:00
|
|
|
|
2009-07-27 13:50:23 +02:00
|
|
|
/**
|
2009-05-22 14:27:58 +02:00
|
|
|
* \brief Default Constructor.
|
|
|
|
|
*
|
|
|
|
|
* The default constructor is useful in cases in which the user intends to
|
2009-07-06 17:12:10 +02:00
|
|
|
* perform decompositions via HouseholderQR::compute(const MatrixType&).
|
2009-05-22 14:27:58 +02:00
|
|
|
*/
|
2009-07-06 17:12:10 +02:00
|
|
|
HouseholderQR() : m_qr(), m_hCoeffs(), m_isInitialized(false) {}
|
2009-05-22 14:27:58 +02:00
|
|
|
|
2009-07-06 17:12:10 +02:00
|
|
|
HouseholderQR(const MatrixType& matrix)
|
2008-04-26 18:26:05 +00:00
|
|
|
: m_qr(matrix.rows(), matrix.cols()),
|
2009-08-16 19:22:15 +02:00
|
|
|
m_hCoeffs(std::min(matrix.rows(),matrix.cols())),
|
2009-05-22 14:27:58 +02:00
|
|
|
m_isInitialized(false)
|
2008-04-26 18:26:05 +00:00
|
|
|
{
|
2009-05-22 14:27:58 +02:00
|
|
|
compute(matrix);
|
2008-04-26 18:26:05 +00:00
|
|
|
}
|
2009-07-27 13:50:23 +02:00
|
|
|
|
2009-01-28 09:45:53 +00:00
|
|
|
/** 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-01-28 09:45:53 +00: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-07-06 17:12:10 +02:00
|
|
|
* Example: \include HouseholderQR_solve.cpp
|
|
|
|
|
* Output: \verbinclude HouseholderQR_solve.out
|
2009-01-28 09:45:53 +00:00
|
|
|
*/
|
2009-11-08 10:21:26 -05:00
|
|
|
template<typename Rhs>
|
|
|
|
|
inline const ei_solve_return_value<HouseholderQR, Rhs>
|
|
|
|
|
solve(const MatrixBase<Rhs>& b) const
|
|
|
|
|
{
|
|
|
|
|
ei_assert(m_isInitialized && "HouseholderQR is not initialized.");
|
|
|
|
|
return ei_solve_return_value<HouseholderQR, Rhs>(*this, b.derived());
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-16 14:35:42 +02:00
|
|
|
MatrixQType matrixQ() const;
|
|
|
|
|
|
|
|
|
|
HouseholderSequenceType matrixQAsHouseholderSequence() const
|
|
|
|
|
{
|
|
|
|
|
return HouseholderSequenceType(m_qr, m_hCoeffs.conjugate());
|
|
|
|
|
}
|
2009-07-27 13:50:23 +02:00
|
|
|
|
2009-07-06 17:24:11 +02:00
|
|
|
/** \returns a reference to the matrix where the Householder QR decomposition is stored
|
|
|
|
|
* in a LAPACK-compatible way.
|
|
|
|
|
*/
|
2009-08-24 13:46:14 -04:00
|
|
|
const MatrixType& matrixQR() const
|
|
|
|
|
{
|
|
|
|
|
ei_assert(m_isInitialized && "HouseholderQR is not initialized.");
|
|
|
|
|
return m_qr;
|
|
|
|
|
}
|
2008-04-26 18:26:05 +00:00
|
|
|
|
2009-08-15 23:12:39 -04:00
|
|
|
HouseholderQR& compute(const MatrixType& matrix);
|
2008-04-26 18:26:05 +00:00
|
|
|
|
2009-08-24 13:46:14 -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-11-08 10:21:26 -05:00
|
|
|
inline int rows() const { return m_qr.rows(); }
|
|
|
|
|
inline int cols() const { return m_qr.cols(); }
|
|
|
|
|
const HCoeffsType& hCoeffs() const { return m_hCoeffs; }
|
|
|
|
|
|
2008-04-26 18:26:05 +00:00
|
|
|
protected:
|
|
|
|
|
MatrixType m_qr;
|
2009-08-16 19:22:15 +02:00
|
|
|
HCoeffsType m_hCoeffs;
|
2009-05-22 14:27:58 +02:00
|
|
|
bool m_isInitialized;
|
2008-04-26 18:26:05 +00:00
|
|
|
};
|
|
|
|
|
|
2008-06-14 13:02:41 +00:00
|
|
|
#ifndef EIGEN_HIDE_HEAVY_CODE
|
|
|
|
|
|
2009-08-24 13:46:14 -04:00
|
|
|
template<typename MatrixType>
|
|
|
|
|
typename MatrixType::RealScalar HouseholderQR<MatrixType>::absDeterminant() const
|
|
|
|
|
{
|
|
|
|
|
ei_assert(m_isInitialized && "HouseholderQR 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>
|
|
|
|
|
typename MatrixType::RealScalar HouseholderQR<MatrixType>::logAbsDeterminant() const
|
|
|
|
|
{
|
|
|
|
|
ei_assert(m_isInitialized && "HouseholderQR is not initialized.");
|
|
|
|
|
ei_assert(m_qr.rows() == m_qr.cols() && "You can't take the determinant of a non-square matrix!");
|
|
|
|
|
return m_qr.diagonal().cwise().abs().cwise().log().sum();
|
|
|
|
|
}
|
|
|
|
|
|
2008-04-26 18:26:05 +00:00
|
|
|
template<typename MatrixType>
|
2009-08-15 23:12:39 -04:00
|
|
|
HouseholderQR<MatrixType>& HouseholderQR<MatrixType>::compute(const MatrixType& matrix)
|
2009-07-27 13:50:23 +02:00
|
|
|
{
|
2008-04-26 18:26:05 +00:00
|
|
|
int rows = matrix.rows();
|
|
|
|
|
int cols = matrix.cols();
|
2009-08-16 19:22:15 +02:00
|
|
|
int size = std::min(rows,cols);
|
2009-09-16 14:35:42 +02:00
|
|
|
|
2009-08-16 19:22:15 +02:00
|
|
|
m_qr = matrix;
|
|
|
|
|
m_hCoeffs.resize(size);
|
|
|
|
|
|
2009-08-22 01:13:21 -04:00
|
|
|
RowVectorType temp(cols);
|
2008-04-26 18:26:05 +00:00
|
|
|
|
2009-08-16 19:22:15 +02:00
|
|
|
for (int k = 0; k < size; ++k)
|
2008-04-26 18:26:05 +00:00
|
|
|
{
|
2009-08-16 19:22:15 +02:00
|
|
|
int remainingRows = rows - k;
|
2009-08-22 01:13:21 -04:00
|
|
|
int remainingCols = cols - k - 1;
|
2009-08-16 19:22:15 +02:00
|
|
|
|
2009-08-17 17:04:32 +02:00
|
|
|
RealScalar beta;
|
|
|
|
|
m_qr.col(k).end(remainingRows).makeHouseholderInPlace(&m_hCoeffs.coeffRef(k), &beta);
|
|
|
|
|
m_qr.coeffRef(k,k) = beta;
|
|
|
|
|
|
|
|
|
|
// apply H to remaining part of m_qr from the left
|
|
|
|
|
m_qr.corner(BottomRight, remainingRows, remainingCols)
|
|
|
|
|
.applyHouseholderOnTheLeft(m_qr.col(k).end(remainingRows-1), m_hCoeffs.coeffRef(k), &temp.coeffRef(k+1));
|
2008-04-26 18:26:05 +00:00
|
|
|
}
|
2009-05-22 14:27:58 +02:00
|
|
|
m_isInitialized = true;
|
2009-08-15 23:12:39 -04:00
|
|
|
return *this;
|
2008-04-26 18:26:05 +00:00
|
|
|
}
|
|
|
|
|
|
2009-11-08 16:51:41 -05:00
|
|
|
template<typename _MatrixType, typename Rhs>
|
|
|
|
|
struct ei_solve_impl<HouseholderQR<_MatrixType>, Rhs>
|
|
|
|
|
: ei_solve_return_value<HouseholderQR<_MatrixType>, Rhs>
|
2009-01-28 09:45:53 +00:00
|
|
|
{
|
2009-11-08 16:51:41 -05:00
|
|
|
EIGEN_MAKE_SOLVE_HELPERS(HouseholderQR<_MatrixType>,Rhs)
|
|
|
|
|
|
|
|
|
|
template<typename Dest> void evalTo(Dest& dst) const
|
2009-11-08 10:21:26 -05:00
|
|
|
{
|
2009-11-08 16:51:41 -05:00
|
|
|
const int rows = dec().rows(), cols = dec().cols();
|
|
|
|
|
dst.resize(cols, rhs().cols());
|
2009-11-08 10:21:26 -05:00
|
|
|
const int rank = std::min(rows, cols);
|
2009-11-08 16:51:41 -05:00
|
|
|
ei_assert(rhs().rows() == rows);
|
2009-11-08 10:21:26 -05:00
|
|
|
|
2009-11-08 16:51:41 -05:00
|
|
|
typename Rhs::PlainMatrixType c(rhs());
|
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
|
|
|
|
|
c.applyOnTheLeft(makeHouseholderSequence(
|
2009-11-08 16:51:41 -05:00
|
|
|
dec().matrixQR().corner(TopLeft,rows,rank),
|
|
|
|
|
dec().hCoeffs().start(rank)).transpose()
|
2009-11-08 10:21:26 -05:00
|
|
|
);
|
|
|
|
|
|
2009-11-08 16:51:41 -05:00
|
|
|
dec().matrixQR()
|
2009-11-08 10:21:26 -05:00
|
|
|
.corner(TopLeft, rank, rank)
|
|
|
|
|
.template triangularView<UpperTriangular>()
|
|
|
|
|
.solveInPlace(c.corner(TopLeft, rank, c.cols()));
|
|
|
|
|
|
|
|
|
|
dst.corner(TopLeft, rank, c.cols()) = c.corner(TopLeft, rank, c.cols());
|
|
|
|
|
dst.corner(BottomLeft, cols-rank, c.cols()).setZero();
|
|
|
|
|
}
|
|
|
|
|
};
|
2009-01-28 09:45:53 +00:00
|
|
|
|
2008-04-26 18:26:05 +00:00
|
|
|
/** \returns the matrix Q */
|
|
|
|
|
template<typename MatrixType>
|
2009-08-24 13:46:14 -04:00
|
|
|
typename HouseholderQR<MatrixType>::MatrixQType HouseholderQR<MatrixType>::matrixQ() const
|
2008-04-26 18:26:05 +00:00
|
|
|
{
|
2009-07-06 17:12:10 +02:00
|
|
|
ei_assert(m_isInitialized && "HouseholderQR is not initialized.");
|
2009-09-16 14:35:42 +02:00
|
|
|
return matrixQAsHouseholderSequence();
|
2008-04-26 18:26:05 +00:00
|
|
|
}
|
|
|
|
|
|
2008-06-14 13:02:41 +00:00
|
|
|
#endif // EIGEN_HIDE_HEAVY_CODE
|
|
|
|
|
|
2009-07-06 17:12:10 +02:00
|
|
|
/** \return the Householder QR decomposition of \c *this.
|
2008-04-26 18:26:05 +00:00
|
|
|
*
|
2009-07-06 17:12:10 +02:00
|
|
|
* \sa class HouseholderQR
|
2008-04-26 18:26:05 +00:00
|
|
|
*/
|
|
|
|
|
template<typename Derived>
|
2009-07-06 17:12:10 +02:00
|
|
|
const HouseholderQR<typename MatrixBase<Derived>::PlainMatrixType>
|
|
|
|
|
MatrixBase<Derived>::householderQr() const
|
2008-04-26 18:26:05 +00:00
|
|
|
{
|
2009-07-06 17:12:10 +02:00
|
|
|
return HouseholderQR<PlainMatrixType>(eval());
|
2008-04-26 18:26:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif // EIGEN_QR_H
|