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
|
|
|
//
|
2010-06-24 23:21:58 +02:00
|
|
|
// Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
|
2009-08-24 13:46:14 -04:00
|
|
|
// Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
|
2010-06-17 18:30:47 +02:00
|
|
|
// Copyright (C) 2010 Vincent Lejeune
|
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
|
|
|
|
|
|
2012-04-15 11:06:28 +01:00
|
|
|
namespace Eigen {
|
|
|
|
|
|
2008-07-22 10:54:42 +00:00
|
|
|
/** \ingroup QR_Module
|
2010-06-29 10:10:47 -04:00
|
|
|
*
|
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
|
|
|
|
|
*
|
2010-08-05 08:56:19 +02:00
|
|
|
* This class performs a QR decomposition of a matrix \b A into matrices \b Q and \b R
|
|
|
|
|
* such that
|
|
|
|
|
* \f[
|
|
|
|
|
* \mathbf{A} = \mathbf{Q} \, \mathbf{R}
|
|
|
|
|
* \f]
|
|
|
|
|
* by using Householder transformations. Here, \b Q a unitary matrix and \b R an upper triangular matrix.
|
|
|
|
|
* The result is 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,
|
2010-03-08 19:31:27 +01:00
|
|
|
MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
|
2010-03-19 02:12:23 -04:00
|
|
|
MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
|
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;
|
2010-05-30 16:00:58 -04:00
|
|
|
typedef typename MatrixType::Index Index;
|
2010-06-24 17:48:38 +02:00
|
|
|
typedef Matrix<Scalar, RowsAtCompileTime, RowsAtCompileTime, (MatrixType::Flags&RowMajorBit) ? RowMajor : ColMajor, MaxRowsAtCompileTime, MaxRowsAtCompileTime> MatrixQType;
|
2010-10-25 10:15:22 -04:00
|
|
|
typedef typename internal::plain_diag_type<MatrixType>::type HCoeffsType;
|
|
|
|
|
typedef typename internal::plain_row_type<MatrixType>::type 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
|
|
|
*/
|
2010-04-21 17:15:57 +02:00
|
|
|
HouseholderQR() : m_qr(), m_hCoeffs(), m_temp(), 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 HouseholderQR()
|
|
|
|
|
*/
|
2010-05-30 16:00:58 -04:00
|
|
|
HouseholderQR(Index rows, Index cols)
|
2010-04-21 17:15:57 +02:00
|
|
|
: m_qr(rows, cols),
|
2011-07-21 11:19:36 +02:00
|
|
|
m_hCoeffs((std::min)(rows,cols)),
|
2010-04-21 17:15:57 +02:00
|
|
|
m_temp(cols),
|
|
|
|
|
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()),
|
2011-07-21 11:19:36 +02:00
|
|
|
m_hCoeffs((std::min)(matrix.rows(),matrix.cols())),
|
2010-04-21 17:15:57 +02:00
|
|
|
m_temp(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>
|
2010-10-25 10:15:22 -04:00
|
|
|
inline const internal::solve_retval<HouseholderQR, Rhs>
|
2009-11-08 10:21:26 -05:00
|
|
|
solve(const MatrixBase<Rhs>& b) const
|
|
|
|
|
{
|
2010-10-25 10:15:22 -04:00
|
|
|
eigen_assert(m_isInitialized && "HouseholderQR is not initialized.");
|
|
|
|
|
return internal::solve_retval<HouseholderQR, Rhs>(*this, b.derived());
|
2009-11-08 10:21:26 -05:00
|
|
|
}
|
2009-09-16 14:35:42 +02:00
|
|
|
|
2012-07-02 16:33:32 +02:00
|
|
|
/** This method returns an expression of the unitary matrix Q as a sequence of Householder transformations.
|
|
|
|
|
*
|
|
|
|
|
* The returned expression can directly be used to perform matrix products. It can also be assigned to a dense Matrix object.
|
|
|
|
|
* Here is an example showing how to recover the full or thin matrix Q, as well as how to perform matrix products using operator*:
|
|
|
|
|
*
|
|
|
|
|
* Example: \include HouseholderQR_householderQ.cpp
|
|
|
|
|
* Output: \verbinclude HouseholderQR_householderQ.out
|
|
|
|
|
*/
|
2009-12-02 11:11:09 -05:00
|
|
|
HouseholderSequenceType householderQ() const
|
2009-09-16 14:35:42 +02:00
|
|
|
{
|
2010-10-25 10:15:22 -04:00
|
|
|
eigen_assert(m_isInitialized && "HouseholderQR is not initialized.");
|
2009-09-16 14:35:42 +02:00
|
|
|
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
|
|
|
|
|
{
|
2010-10-25 10:15:22 -04:00
|
|
|
eigen_assert(m_isInitialized && "HouseholderQR is not initialized.");
|
2009-08-24 13:46:14 -04:00
|
|
|
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;
|
|
|
|
|
|
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; }
|
|
|
|
|
|
2008-04-26 18:26:05 +00:00
|
|
|
protected:
|
|
|
|
|
MatrixType m_qr;
|
2009-08-16 19:22:15 +02:00
|
|
|
HCoeffsType m_hCoeffs;
|
2010-04-21 17:15:57 +02:00
|
|
|
RowVectorType m_temp;
|
2009-05-22 14:27:58 +02:00
|
|
|
bool m_isInitialized;
|
2008-04-26 18:26:05 +00:00
|
|
|
};
|
|
|
|
|
|
2009-08-24 13:46:14 -04:00
|
|
|
template<typename MatrixType>
|
|
|
|
|
typename MatrixType::RealScalar HouseholderQR<MatrixType>::absDeterminant() const
|
|
|
|
|
{
|
2010-10-25 10:15:22 -04:00
|
|
|
eigen_assert(m_isInitialized && "HouseholderQR is not initialized.");
|
|
|
|
|
eigen_assert(m_qr.rows() == m_qr.cols() && "You can't take the determinant of a non-square matrix!");
|
|
|
|
|
return internal::abs(m_qr.diagonal().prod());
|
2009-08-24 13:46:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename MatrixType>
|
|
|
|
|
typename MatrixType::RealScalar HouseholderQR<MatrixType>::logAbsDeterminant() const
|
|
|
|
|
{
|
2010-10-25 10:15:22 -04:00
|
|
|
eigen_assert(m_isInitialized && "HouseholderQR is not initialized.");
|
|
|
|
|
eigen_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 13:46:14 -04:00
|
|
|
}
|
|
|
|
|
|
2010-10-25 10:15:22 -04:00
|
|
|
namespace internal {
|
|
|
|
|
|
2010-06-17 17:27:52 +02:00
|
|
|
/** \internal */
|
|
|
|
|
template<typename MatrixQR, typename HCoeffs>
|
2010-10-25 10:15:22 -04:00
|
|
|
void householder_qr_inplace_unblocked(MatrixQR& mat, HCoeffs& hCoeffs, typename MatrixQR::Scalar* tempData = 0)
|
2009-07-27 13:50:23 +02:00
|
|
|
{
|
2010-06-17 17:27:52 +02:00
|
|
|
typedef typename MatrixQR::Index Index;
|
|
|
|
|
typedef typename MatrixQR::Scalar Scalar;
|
|
|
|
|
typedef typename MatrixQR::RealScalar RealScalar;
|
|
|
|
|
Index rows = mat.rows();
|
|
|
|
|
Index cols = mat.cols();
|
2011-07-21 11:19:36 +02:00
|
|
|
Index size = (std::min)(rows,cols);
|
2009-09-16 14:35:42 +02:00
|
|
|
|
2010-10-25 10:15:22 -04:00
|
|
|
eigen_assert(hCoeffs.size() == size);
|
2009-08-16 19:22:15 +02:00
|
|
|
|
2010-06-17 17:27:52 +02:00
|
|
|
typedef Matrix<Scalar,MatrixQR::ColsAtCompileTime,1> TempType;
|
|
|
|
|
TempType tempVector;
|
|
|
|
|
if(tempData==0)
|
|
|
|
|
{
|
|
|
|
|
tempVector.resize(cols);
|
|
|
|
|
tempData = tempVector.data();
|
|
|
|
|
}
|
2008-04-26 18:26:05 +00:00
|
|
|
|
2010-05-30 16:00:58 -04:00
|
|
|
for(Index k = 0; k < size; ++k)
|
2008-04-26 18:26:05 +00:00
|
|
|
{
|
2010-05-30 16:00:58 -04:00
|
|
|
Index remainingRows = rows - k;
|
|
|
|
|
Index remainingCols = cols - k - 1;
|
2009-08-16 19:22:15 +02:00
|
|
|
|
2009-08-17 17:04:32 +02:00
|
|
|
RealScalar beta;
|
2010-06-17 17:27:52 +02:00
|
|
|
mat.col(k).tail(remainingRows).makeHouseholderInPlace(hCoeffs.coeffRef(k), beta);
|
|
|
|
|
mat.coeffRef(k,k) = beta;
|
2009-08-17 17:04:32 +02:00
|
|
|
|
|
|
|
|
// apply H to remaining part of m_qr from the left
|
2010-06-17 17:27:52 +02:00
|
|
|
mat.bottomRightCorner(remainingRows, remainingCols)
|
|
|
|
|
.applyHouseholderOnTheLeft(mat.col(k).tail(remainingRows-1), hCoeffs.coeffRef(k), tempData+k+1);
|
2008-04-26 18:26:05 +00:00
|
|
|
}
|
2010-06-17 17:27:52 +02:00
|
|
|
}
|
|
|
|
|
|
2010-06-17 18:30:47 +02:00
|
|
|
/** \internal */
|
|
|
|
|
template<typename MatrixQR, typename HCoeffs>
|
2010-10-25 10:15:22 -04:00
|
|
|
void householder_qr_inplace_blocked(MatrixQR& mat, HCoeffs& hCoeffs,
|
2010-06-17 18:30:47 +02:00
|
|
|
typename MatrixQR::Index maxBlockSize=32,
|
|
|
|
|
typename MatrixQR::Scalar* tempData = 0)
|
|
|
|
|
{
|
|
|
|
|
typedef typename MatrixQR::Index Index;
|
|
|
|
|
typedef typename MatrixQR::Scalar Scalar;
|
|
|
|
|
typedef typename MatrixQR::RealScalar RealScalar;
|
|
|
|
|
typedef Block<MatrixQR,Dynamic,Dynamic> BlockType;
|
|
|
|
|
|
|
|
|
|
Index rows = mat.rows();
|
|
|
|
|
Index cols = mat.cols();
|
2011-07-21 11:19:36 +02:00
|
|
|
Index size = (std::min)(rows, cols);
|
2010-06-17 18:30:47 +02:00
|
|
|
|
2010-06-24 17:48:38 +02:00
|
|
|
typedef Matrix<Scalar,Dynamic,1,ColMajor,MatrixQR::MaxColsAtCompileTime,1> TempType;
|
2010-06-17 18:30:47 +02:00
|
|
|
TempType tempVector;
|
|
|
|
|
if(tempData==0)
|
|
|
|
|
{
|
|
|
|
|
tempVector.resize(cols);
|
|
|
|
|
tempData = tempVector.data();
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-21 11:19:36 +02:00
|
|
|
Index blockSize = (std::min)(maxBlockSize,size);
|
2010-06-17 18:30:47 +02:00
|
|
|
|
2011-08-23 11:02:10 +02:00
|
|
|
Index k = 0;
|
2010-06-17 18:30:47 +02:00
|
|
|
for (k = 0; k < size; k += blockSize)
|
|
|
|
|
{
|
2011-07-21 11:19:36 +02:00
|
|
|
Index bs = (std::min)(size-k,blockSize); // actual size of the block
|
2010-06-17 18:30:47 +02:00
|
|
|
Index tcols = cols - k - bs; // trailing columns
|
|
|
|
|
Index brows = rows-k; // rows of the block
|
|
|
|
|
|
|
|
|
|
// partition the matrix:
|
|
|
|
|
// A00 | A01 | A02
|
|
|
|
|
// mat = A10 | A11 | A12
|
|
|
|
|
// A20 | A21 | A22
|
|
|
|
|
// and performs the qr dec of [A11^T A12^T]^T
|
|
|
|
|
// and update [A21^T A22^T]^T using level 3 operations.
|
|
|
|
|
// Finally, the algorithm continue on A22
|
|
|
|
|
|
|
|
|
|
BlockType A11_21 = mat.block(k,k,brows,bs);
|
|
|
|
|
Block<HCoeffs,Dynamic,1> hCoeffsSegment = hCoeffs.segment(k,bs);
|
|
|
|
|
|
2010-10-25 10:15:22 -04:00
|
|
|
householder_qr_inplace_unblocked(A11_21, hCoeffsSegment, tempData);
|
2010-06-17 18:30:47 +02:00
|
|
|
|
|
|
|
|
if(tcols)
|
|
|
|
|
{
|
|
|
|
|
BlockType A21_22 = mat.block(k,k+bs,brows,tcols);
|
2010-10-25 10:15:22 -04:00
|
|
|
apply_block_householder_on_the_left(A21_22,A11_21,hCoeffsSegment.adjoint());
|
2010-06-17 18:30:47 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-08 16:51:41 -05:00
|
|
|
template<typename _MatrixType, typename Rhs>
|
2010-10-25 10:15:22 -04:00
|
|
|
struct solve_retval<HouseholderQR<_MatrixType>, Rhs>
|
|
|
|
|
: solve_retval_base<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
|
|
|
{
|
2010-05-30 16:00:58 -04:00
|
|
|
const Index rows = dec().rows(), cols = dec().cols();
|
2011-07-21 11:19:36 +02:00
|
|
|
const Index rank = (std::min)(rows, cols);
|
2010-10-25 10:15:22 -04:00
|
|
|
eigen_assert(rhs().rows() == rows);
|
2009-11-08 10:21:26 -05:00
|
|
|
|
2010-02-20 15:53:57 +01:00
|
|
|
typename Rhs::PlainObject 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
|
2009-11-10 21:22:20 -05:00
|
|
|
c.applyOnTheLeft(householderSequence(
|
2010-04-22 14:11:18 -04:00
|
|
|
dec().matrixQR().leftCols(rank),
|
2010-01-04 21:24:43 -05:00
|
|
|
dec().hCoeffs().head(rank)).transpose()
|
2009-11-08 10:21:26 -05:00
|
|
|
);
|
|
|
|
|
|
2009-11-08 16:51:41 -05:00
|
|
|
dec().matrixQR()
|
2010-04-22 14:11:18 -04:00
|
|
|
.topLeftCorner(rank, rank)
|
2010-01-07 21:15:32 +01:00
|
|
|
.template triangularView<Upper>()
|
2010-04-22 14:11:18 -04:00
|
|
|
.solveInPlace(c.topRows(rank));
|
2009-11-08 10:21:26 -05:00
|
|
|
|
2010-04-22 14:11:18 -04:00
|
|
|
dst.topRows(rank) = c.topRows(rank);
|
|
|
|
|
dst.bottomRows(cols-rank).setZero();
|
2009-11-08 10:21:26 -05:00
|
|
|
}
|
|
|
|
|
};
|
2009-01-28 09:45:53 +00:00
|
|
|
|
2010-10-25 10:15:22 -04:00
|
|
|
} // end namespace internal
|
|
|
|
|
|
|
|
|
|
template<typename MatrixType>
|
|
|
|
|
HouseholderQR<MatrixType>& HouseholderQR<MatrixType>::compute(const MatrixType& matrix)
|
|
|
|
|
{
|
|
|
|
|
Index rows = matrix.rows();
|
|
|
|
|
Index cols = matrix.cols();
|
2011-07-21 11:19:36 +02:00
|
|
|
Index size = (std::min)(rows,cols);
|
2010-10-25 10:15:22 -04:00
|
|
|
|
|
|
|
|
m_qr = matrix;
|
|
|
|
|
m_hCoeffs.resize(size);
|
|
|
|
|
|
|
|
|
|
m_temp.resize(cols);
|
|
|
|
|
|
|
|
|
|
internal::householder_qr_inplace_blocked(m_qr, m_hCoeffs, 48, m_temp.data());
|
|
|
|
|
|
|
|
|
|
m_isInitialized = true;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
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>
|
2010-02-20 15:53:57 +01:00
|
|
|
const HouseholderQR<typename MatrixBase<Derived>::PlainObject>
|
2009-07-06 17:12:10 +02:00
|
|
|
MatrixBase<Derived>::householderQr() const
|
2008-04-26 18:26:05 +00:00
|
|
|
{
|
2010-02-20 15:53:57 +01:00
|
|
|
return HouseholderQR<PlainObject>(eval());
|
2008-04-26 18:26:05 +00:00
|
|
|
}
|
|
|
|
|
|
2012-04-15 11:06:28 +01:00
|
|
|
} // end namespace Eigen
|
2008-04-26 18:26:05 +00:00
|
|
|
|
|
|
|
|
#endif // EIGEN_QR_H
|