2008-10-13 15:53:27 +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-10-13 15:53:27 +00:00
|
|
|
//
|
|
|
|
|
// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
|
2009-02-03 17:50:35 +00:00
|
|
|
// Copyright (C) 2009 Keir Mierle <mierle@gmail.com>
|
2009-03-31 13:55:40 +00:00
|
|
|
// Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
|
2008-10-13 15:53:27 +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_LDLT_H
|
|
|
|
|
#define EIGEN_LDLT_H
|
|
|
|
|
|
|
|
|
|
/** \ingroup cholesky_Module
|
|
|
|
|
*
|
|
|
|
|
* \class LDLT
|
|
|
|
|
*
|
2009-02-03 17:50:35 +00:00
|
|
|
* \brief Robust Cholesky decomposition of a matrix
|
2008-10-13 15:53:27 +00:00
|
|
|
*
|
2009-02-03 17:50:35 +00:00
|
|
|
* \param MatrixType the type of the matrix of which to compute the LDL^T Cholesky decomposition
|
2008-10-13 15:53:27 +00:00
|
|
|
*
|
2009-03-30 21:45:45 +00:00
|
|
|
* Perform a robust Cholesky decomposition of a positive semidefinite or negative semidefinite
|
2009-02-03 17:50:35 +00:00
|
|
|
* matrix \f$ A \f$ such that \f$ A = P^TLDL^*P \f$, where P is a permutation matrix, L
|
|
|
|
|
* is lower triangular with a unit diagonal and D is a diagonal matrix.
|
2008-10-13 15:53:27 +00:00
|
|
|
*
|
2009-03-30 21:45:45 +00:00
|
|
|
* The decomposition uses pivoting to ensure stability, so that L will have
|
2009-02-03 17:50:35 +00:00
|
|
|
* zeros in the bottom right rank(A) - n submatrix. Avoiding the square root
|
|
|
|
|
* on D also stabilizes the computation.
|
2008-10-13 15:53:27 +00:00
|
|
|
*
|
2009-10-29 21:11:05 -04:00
|
|
|
* Remember that Cholesky decompositions are not rank-revealing. Also, do not use a Cholesky
|
|
|
|
|
* decomposition to determine whether a system of equations has a solution.
|
2009-04-01 00:21:16 +00:00
|
|
|
*
|
2009-03-30 21:45:45 +00:00
|
|
|
* \sa MatrixBase::ldlt(), class LLT
|
|
|
|
|
*/
|
|
|
|
|
/* THIS PART OF THE DOX IS CURRENTLY DISABLED BECAUSE INACCURATE BECAUSE OF BUG IN THE DECOMPOSITION CODE
|
2008-10-13 15:53:27 +00:00
|
|
|
* Note that during the decomposition, only the upper triangular part of A is considered. Therefore,
|
|
|
|
|
* the strict lower part does not have to store correct values.
|
|
|
|
|
*/
|
2009-11-03 11:34:45 -05:00
|
|
|
template<typename _MatrixType> class LDLT
|
2008-10-13 15:53:27 +00:00
|
|
|
{
|
|
|
|
|
public:
|
2009-11-03 11:34:45 -05:00
|
|
|
typedef _MatrixType MatrixType;
|
2010-03-08 19:31:27 +01:00
|
|
|
enum {
|
|
|
|
|
RowsAtCompileTime = MatrixType::RowsAtCompileTime,
|
|
|
|
|
ColsAtCompileTime = MatrixType::ColsAtCompileTime,
|
|
|
|
|
Options = MatrixType::Options,
|
|
|
|
|
MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
|
|
|
|
|
MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
|
|
|
|
|
};
|
2008-10-13 15:53:27 +00:00
|
|
|
typedef typename MatrixType::Scalar Scalar;
|
|
|
|
|
typedef typename NumTraits<typename MatrixType::Scalar>::Real RealScalar;
|
2010-03-19 02:12:23 -04:00
|
|
|
typedef typename ei_plain_col_type<MatrixType, int>::type IntColVectorType;
|
2008-10-13 15:53:27 +00:00
|
|
|
|
2010-02-24 10:40:16 +01:00
|
|
|
/** \brief Default Constructor.
|
|
|
|
|
*
|
|
|
|
|
* The default constructor is useful in cases in which the user intends to
|
|
|
|
|
* perform decompositions via LDLT::compute(const MatrixType&).
|
|
|
|
|
*/
|
2009-05-22 15:58:20 +02:00
|
|
|
LDLT() : m_matrix(), m_p(), m_transpositions(), m_isInitialized(false) {}
|
|
|
|
|
|
2010-02-24 10:40:16 +01:00
|
|
|
/** \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 LDLT()
|
|
|
|
|
*/
|
|
|
|
|
LDLT(int size) : m_matrix(size,size), m_p(size), m_transpositions(size), m_isInitialized(false) {}
|
|
|
|
|
|
2008-10-13 15:53:27 +00:00
|
|
|
LDLT(const MatrixType& matrix)
|
2009-02-03 17:50:35 +00:00
|
|
|
: m_matrix(matrix.rows(), matrix.cols()),
|
|
|
|
|
m_p(matrix.rows()),
|
2009-05-22 15:58:20 +02:00
|
|
|
m_transpositions(matrix.rows()),
|
|
|
|
|
m_isInitialized(false)
|
2008-10-13 15:53:27 +00:00
|
|
|
{
|
|
|
|
|
compute(matrix);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \returns the lower triangular matrix L */
|
2010-01-07 21:15:32 +01:00
|
|
|
inline TriangularView<MatrixType, UnitLower> matrixL(void) const
|
2009-11-18 18:15:19 +01:00
|
|
|
{
|
2009-05-22 15:58:20 +02:00
|
|
|
ei_assert(m_isInitialized && "LDLT is not initialized.");
|
2009-07-20 13:27:41 +02:00
|
|
|
return m_matrix;
|
2009-05-22 15:58:20 +02:00
|
|
|
}
|
2008-10-13 15:53:27 +00:00
|
|
|
|
2009-02-03 17:50:35 +00:00
|
|
|
/** \returns a vector of integers, whose size is the number of rows of the matrix being decomposed,
|
|
|
|
|
* representing the P permutation i.e. the permutation of the rows. For its precise meaning,
|
2009-10-28 18:19:29 -04:00
|
|
|
* see the examples given in the documentation of class FullPivLU.
|
2009-02-03 17:50:35 +00:00
|
|
|
*/
|
|
|
|
|
inline const IntColVectorType& permutationP() const
|
|
|
|
|
{
|
2009-05-22 15:58:20 +02:00
|
|
|
ei_assert(m_isInitialized && "LDLT is not initialized.");
|
2009-02-03 17:50:35 +00:00
|
|
|
return m_p;
|
|
|
|
|
}
|
|
|
|
|
|
2008-10-13 15:53:27 +00:00
|
|
|
/** \returns the coefficients of the diagonal matrix D */
|
2009-07-20 13:27:41 +02:00
|
|
|
inline Diagonal<MatrixType,0> vectorD(void) const
|
|
|
|
|
{
|
2009-05-22 15:58:20 +02:00
|
|
|
ei_assert(m_isInitialized && "LDLT is not initialized.");
|
2009-07-20 13:27:41 +02:00
|
|
|
return m_matrix.diagonal();
|
2009-05-22 15:58:20 +02:00
|
|
|
}
|
2008-10-13 15:53:27 +00:00
|
|
|
|
2009-03-30 21:45:45 +00:00
|
|
|
/** \returns true if the matrix is positive (semidefinite) */
|
2009-07-20 13:27:41 +02:00
|
|
|
inline bool isPositive(void) const
|
|
|
|
|
{
|
2009-05-22 15:58:20 +02:00
|
|
|
ei_assert(m_isInitialized && "LDLT is not initialized.");
|
2009-07-20 13:27:41 +02:00
|
|
|
return m_sign == 1;
|
2009-05-22 15:58:20 +02:00
|
|
|
}
|
2009-03-30 21:45:45 +00:00
|
|
|
|
|
|
|
|
/** \returns true if the matrix is negative (semidefinite) */
|
2009-07-20 13:27:41 +02:00
|
|
|
inline bool isNegative(void) const
|
|
|
|
|
{
|
2009-05-22 15:58:20 +02:00
|
|
|
ei_assert(m_isInitialized && "LDLT is not initialized.");
|
2009-07-20 13:27:41 +02:00
|
|
|
return m_sign == -1;
|
2009-05-22 15:58:20 +02:00
|
|
|
}
|
2009-03-30 21:45:45 +00:00
|
|
|
|
2009-10-29 21:11:05 -04:00
|
|
|
/** \returns a solution x of \f$ A x = b \f$ using the current decomposition of A.
|
|
|
|
|
*
|
|
|
|
|
* \note_about_checking_solutions
|
|
|
|
|
*
|
|
|
|
|
* \sa solveInPlace(), MatrixBase::ldlt()
|
|
|
|
|
*/
|
|
|
|
|
template<typename Rhs>
|
2009-11-09 07:51:31 -05:00
|
|
|
inline const ei_solve_retval<LDLT, Rhs>
|
2009-10-29 21:11:05 -04:00
|
|
|
solve(const MatrixBase<Rhs>& b) const
|
|
|
|
|
{
|
|
|
|
|
ei_assert(m_isInitialized && "LDLT is not initialized.");
|
|
|
|
|
ei_assert(m_matrix.rows()==b.rows()
|
|
|
|
|
&& "LDLT::solve(): invalid number of rows of the right hand side matrix b");
|
2009-11-09 07:51:31 -05:00
|
|
|
return ei_solve_retval<LDLT, Rhs>(*this, b.derived());
|
2009-10-29 21:11:05 -04:00
|
|
|
}
|
2009-11-18 18:15:19 +01:00
|
|
|
|
2008-10-13 15:53:27 +00:00
|
|
|
template<typename Derived>
|
|
|
|
|
bool solveInPlace(MatrixBase<Derived> &bAndX) const;
|
|
|
|
|
|
2009-08-15 23:12:39 -04:00
|
|
|
LDLT& compute(const MatrixType& matrix);
|
2008-10-13 15:53:27 +00:00
|
|
|
|
2009-10-29 21:11:05 -04:00
|
|
|
/** \returns the LDLT decomposition matrix
|
|
|
|
|
*
|
|
|
|
|
* TODO: document the storage layout
|
|
|
|
|
*/
|
|
|
|
|
inline const MatrixType& matrixLDLT() const
|
|
|
|
|
{
|
|
|
|
|
ei_assert(m_isInitialized && "LDLT is not initialized.");
|
|
|
|
|
return m_matrix;
|
|
|
|
|
}
|
2009-11-03 11:34:45 -05:00
|
|
|
|
2010-02-24 19:16:10 +01:00
|
|
|
MatrixType reconstructedMatrix() const;
|
2010-02-24 10:40:16 +01:00
|
|
|
|
2009-11-03 11:34:45 -05:00
|
|
|
inline int rows() const { return m_matrix.rows(); }
|
|
|
|
|
inline int cols() const { return m_matrix.cols(); }
|
2009-11-18 18:15:19 +01:00
|
|
|
|
2008-10-13 15:53:27 +00:00
|
|
|
protected:
|
|
|
|
|
/** \internal
|
2009-02-03 17:50:35 +00:00
|
|
|
* Used to compute and store the Cholesky decomposition A = L D L^* = U^* D U.
|
2008-10-13 15:53:27 +00:00
|
|
|
* The strict upper part is used during the decomposition, the strict lower
|
|
|
|
|
* part correspond to the coefficients of L (its diagonal is equal to 1 and
|
|
|
|
|
* is not stored), and the diagonal entries correspond to D.
|
|
|
|
|
*/
|
|
|
|
|
MatrixType m_matrix;
|
2009-02-03 17:50:35 +00:00
|
|
|
IntColVectorType m_p;
|
2009-10-29 21:11:05 -04:00
|
|
|
IntColVectorType m_transpositions; // FIXME do we really need to store permanently the transpositions?
|
2009-04-01 00:21:16 +00:00
|
|
|
int m_sign;
|
2009-05-22 15:58:20 +02:00
|
|
|
bool m_isInitialized;
|
2008-10-13 15:53:27 +00:00
|
|
|
};
|
|
|
|
|
|
2009-03-30 21:45:45 +00:00
|
|
|
/** Compute / recompute the LDLT decomposition A = L D L^* = U^* D U of \a matrix
|
2008-10-13 15:53:27 +00:00
|
|
|
*/
|
|
|
|
|
template<typename MatrixType>
|
2009-08-15 23:12:39 -04:00
|
|
|
LDLT<MatrixType>& LDLT<MatrixType>::compute(const MatrixType& a)
|
2008-10-13 15:53:27 +00:00
|
|
|
{
|
2009-03-30 21:45:45 +00:00
|
|
|
ei_assert(a.rows()==a.cols());
|
2008-10-13 15:53:27 +00:00
|
|
|
const int size = a.rows();
|
|
|
|
|
|
2009-02-03 17:50:35 +00:00
|
|
|
m_matrix = a;
|
|
|
|
|
|
2010-02-24 10:40:16 +01:00
|
|
|
m_p.resize(size);
|
|
|
|
|
m_transpositions.resize(size);
|
|
|
|
|
m_isInitialized = false;
|
|
|
|
|
|
2009-02-03 17:50:35 +00:00
|
|
|
if (size <= 1) {
|
2009-02-04 20:20:34 +00:00
|
|
|
m_p.setZero();
|
|
|
|
|
m_transpositions.setZero();
|
2009-03-30 21:45:45 +00:00
|
|
|
m_sign = ei_real(a.coeff(0,0))>0 ? 1:-1;
|
2009-05-22 15:58:20 +02:00
|
|
|
m_isInitialized = true;
|
2009-08-15 23:12:39 -04:00
|
|
|
return *this;
|
2008-10-13 15:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
2009-02-06 14:01:01 +00:00
|
|
|
RealScalar cutoff = 0, biggest_in_corner;
|
2008-10-13 15:53:27 +00:00
|
|
|
|
2009-02-03 17:50:35 +00:00
|
|
|
// By using a temorary, packet-aligned products are guarenteed. In the LLT
|
|
|
|
|
// case this is unnecessary because the diagonal is included and will always
|
|
|
|
|
// have optimal alignment.
|
2010-03-08 19:31:27 +01:00
|
|
|
Matrix<Scalar, RowsAtCompileTime, 1, Options, MaxRowsAtCompileTime, 1> _temporary(size);
|
2008-10-13 15:53:27 +00:00
|
|
|
|
2009-02-03 17:50:35 +00:00
|
|
|
for (int j = 0; j < size; ++j)
|
2008-10-13 15:53:27 +00:00
|
|
|
{
|
2009-03-30 21:45:45 +00:00
|
|
|
// Find largest diagonal element
|
|
|
|
|
int index_of_biggest_in_corner;
|
2010-01-05 13:07:32 +01:00
|
|
|
biggest_in_corner = m_matrix.diagonal().tail(size-j).cwiseAbs()
|
2009-03-30 21:45:45 +00:00
|
|
|
.maxCoeff(&index_of_biggest_in_corner);
|
|
|
|
|
index_of_biggest_in_corner += j;
|
|
|
|
|
|
|
|
|
|
if(j == 0)
|
|
|
|
|
{
|
|
|
|
|
// The biggest overall is the point of reference to which further diagonals
|
|
|
|
|
// are compared; if any diagonal is negligible compared
|
2010-02-23 16:05:37 -05:00
|
|
|
// to the largest overall, the algorithm bails.
|
|
|
|
|
cutoff = ei_abs(NumTraits<Scalar>::epsilon() * biggest_in_corner);
|
2009-02-03 17:50:35 +00:00
|
|
|
|
2009-03-30 21:45:45 +00:00
|
|
|
m_sign = ei_real(m_matrix.diagonal().coeff(index_of_biggest_in_corner)) > 0 ? 1 : -1;
|
|
|
|
|
}
|
2008-10-13 15:53:27 +00:00
|
|
|
|
2009-02-03 17:50:35 +00:00
|
|
|
// Finish early if the matrix is not full rank.
|
|
|
|
|
if(biggest_in_corner < cutoff)
|
2008-10-13 15:53:27 +00:00
|
|
|
{
|
2009-02-03 17:50:35 +00:00
|
|
|
for(int i = j; i < size; i++) m_transpositions.coeffRef(i) = i;
|
|
|
|
|
break;
|
2008-10-13 15:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
2009-03-30 21:45:45 +00:00
|
|
|
m_transpositions.coeffRef(j) = index_of_biggest_in_corner;
|
|
|
|
|
if(j != index_of_biggest_in_corner)
|
2008-10-13 15:53:27 +00:00
|
|
|
{
|
2009-03-30 21:45:45 +00:00
|
|
|
m_matrix.row(j).swap(m_matrix.row(index_of_biggest_in_corner));
|
|
|
|
|
m_matrix.col(j).swap(m_matrix.col(index_of_biggest_in_corner));
|
2009-02-03 17:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (j == 0) {
|
|
|
|
|
m_matrix.row(0) = m_matrix.row(0).conjugate();
|
2010-01-04 21:24:43 -05:00
|
|
|
m_matrix.col(0).tail(size-1) = m_matrix.row(0).tail(size-1) / m_matrix.coeff(0,0);
|
2009-02-03 17:50:35 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-24 10:40:16 +01:00
|
|
|
RealScalar Djj = ei_real(m_matrix.coeff(j,j) - m_matrix.row(j).head(j).dot(m_matrix.col(j).head(j)));
|
2009-02-03 17:50:35 +00:00
|
|
|
m_matrix.coeffRef(j,j) = Djj;
|
|
|
|
|
|
|
|
|
|
int endSize = size - j - 1;
|
|
|
|
|
if (endSize > 0) {
|
2010-01-04 21:24:43 -05:00
|
|
|
_temporary.tail(endSize).noalias() = m_matrix.block(j+1,0, endSize, j)
|
|
|
|
|
* m_matrix.col(j).head(j).conjugate();
|
2008-10-13 15:53:27 +00:00
|
|
|
|
2010-01-04 21:24:43 -05:00
|
|
|
m_matrix.row(j).tail(endSize) = m_matrix.row(j).tail(endSize).conjugate()
|
2010-02-24 10:40:16 +01:00
|
|
|
- _temporary.tail(endSize).transpose();
|
2008-10-13 15:53:27 +00:00
|
|
|
|
2010-02-23 16:10:26 -05:00
|
|
|
if(ei_abs(Djj) > cutoff)
|
2010-02-23 16:05:37 -05:00
|
|
|
{
|
2010-02-23 16:10:26 -05:00
|
|
|
m_matrix.col(j).tail(endSize) = m_matrix.row(j).tail(endSize) / Djj;
|
2010-02-23 16:05:37 -05:00
|
|
|
}
|
2008-10-13 15:53:27 +00:00
|
|
|
}
|
|
|
|
|
}
|
2009-02-03 17:50:35 +00:00
|
|
|
|
|
|
|
|
// Reverse applied swaps to get P matrix.
|
|
|
|
|
for(int k = 0; k < size; ++k) m_p.coeffRef(k) = k;
|
|
|
|
|
for(int k = size-1; k >= 0; --k) {
|
|
|
|
|
std::swap(m_p.coeffRef(k), m_p.coeffRef(m_transpositions.coeff(k)));
|
|
|
|
|
}
|
2009-05-22 15:58:20 +02:00
|
|
|
|
|
|
|
|
m_isInitialized = true;
|
2009-08-15 23:12:39 -04:00
|
|
|
return *this;
|
2008-10-13 15:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
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<LDLT<_MatrixType>, Rhs>
|
|
|
|
|
: ei_solve_retval_base<LDLT<_MatrixType>, Rhs>
|
2008-10-13 15:53:27 +00:00
|
|
|
{
|
2009-11-08 16:51:41 -05:00
|
|
|
EIGEN_MAKE_SOLVE_HELPERS(LDLT<_MatrixType>,Rhs)
|
|
|
|
|
|
|
|
|
|
template<typename Dest> void evalTo(Dest& dst) const
|
2009-10-29 21:11:05 -04:00
|
|
|
{
|
2009-11-08 16:51:41 -05:00
|
|
|
dst = rhs();
|
|
|
|
|
dec().solveInPlace(dst);
|
2009-10-29 21:11:05 -04:00
|
|
|
}
|
|
|
|
|
};
|
2008-10-13 15:53:27 +00:00
|
|
|
|
|
|
|
|
/** This is the \em in-place version of solve().
|
|
|
|
|
*
|
|
|
|
|
* \param bAndX represents both the right-hand side matrix b and result x.
|
|
|
|
|
*
|
2009-04-01 00:21:16 +00:00
|
|
|
* \returns true always! If you need to check for existence of solutions, use another decomposition like LU, QR, or SVD.
|
|
|
|
|
*
|
2008-10-13 15:53:27 +00:00
|
|
|
* This version avoids a copy when the right hand side matrix b is not
|
|
|
|
|
* needed anymore.
|
|
|
|
|
*
|
|
|
|
|
* \sa LDLT::solve(), MatrixBase::ldlt()
|
|
|
|
|
*/
|
|
|
|
|
template<typename MatrixType>
|
|
|
|
|
template<typename Derived>
|
|
|
|
|
bool LDLT<MatrixType>::solveInPlace(MatrixBase<Derived> &bAndX) const
|
|
|
|
|
{
|
2009-05-22 15:58:20 +02:00
|
|
|
ei_assert(m_isInitialized && "LDLT is not initialized.");
|
2008-10-13 15:53:27 +00:00
|
|
|
const int size = m_matrix.rows();
|
2009-02-03 17:50:35 +00:00
|
|
|
ei_assert(size == bAndX.rows());
|
|
|
|
|
|
|
|
|
|
// z = P b
|
|
|
|
|
for(int i = 0; i < size; ++i) bAndX.row(m_transpositions.coeff(i)).swap(bAndX.row(i));
|
|
|
|
|
|
|
|
|
|
// y = L^-1 z
|
2009-07-06 23:43:20 +02:00
|
|
|
//matrixL().solveInPlace(bAndX);
|
2010-01-07 21:15:32 +01:00
|
|
|
m_matrix.template triangularView<UnitLower>().solveInPlace(bAndX);
|
2009-02-03 17:50:35 +00:00
|
|
|
|
|
|
|
|
// w = D^-1 y
|
2009-11-18 18:15:19 +01:00
|
|
|
bAndX = m_matrix.diagonal().asDiagonal().inverse() * bAndX;
|
2009-02-03 17:50:35 +00:00
|
|
|
|
|
|
|
|
// u = L^-T w
|
2010-01-07 21:15:32 +01:00
|
|
|
m_matrix.adjoint().template triangularView<UnitUpper>().solveInPlace(bAndX);
|
2009-02-03 17:50:35 +00:00
|
|
|
|
|
|
|
|
// x = P^T u
|
|
|
|
|
for (int i = size-1; i >= 0; --i) bAndX.row(m_transpositions.coeff(i)).swap(bAndX.row(i));
|
|
|
|
|
|
2008-10-13 15:53:27 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-24 10:40:16 +01:00
|
|
|
/** \returns the matrix represented by the decomposition,
|
|
|
|
|
* i.e., it returns the product: P^T L D L^* P.
|
|
|
|
|
* This function is provided for debug purpose. */
|
|
|
|
|
template<typename MatrixType>
|
2010-02-24 19:16:10 +01:00
|
|
|
MatrixType LDLT<MatrixType>::reconstructedMatrix() const
|
2010-02-24 10:40:16 +01:00
|
|
|
{
|
|
|
|
|
ei_assert(m_isInitialized && "LDLT is not initialized.");
|
|
|
|
|
const int size = m_matrix.rows();
|
|
|
|
|
MatrixType res(size,size);
|
|
|
|
|
res.setIdentity();
|
|
|
|
|
|
|
|
|
|
// PI
|
|
|
|
|
for(int i = 0; i < size; ++i) res.row(m_transpositions.coeff(i)).swap(res.row(i));
|
|
|
|
|
// L^* P
|
|
|
|
|
res = matrixL().adjoint() * res;
|
|
|
|
|
// D(L^*P)
|
|
|
|
|
res = vectorD().asDiagonal() * res;
|
|
|
|
|
// L(DL^*P)
|
|
|
|
|
res = matrixL() * res;
|
|
|
|
|
// P^T (LDL^*P)
|
|
|
|
|
for (int i = size-1; i >= 0; --i) res.row(m_transpositions.coeff(i)).swap(res.row(i));
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2008-10-13 15:53:27 +00:00
|
|
|
/** \cholesky_module
|
2009-02-03 17:50:35 +00:00
|
|
|
* \returns the Cholesky decomposition with full pivoting without square root of \c *this
|
2008-10-13 15:53:27 +00:00
|
|
|
*/
|
|
|
|
|
template<typename Derived>
|
2010-02-20 15:53:57 +01:00
|
|
|
inline const LDLT<typename MatrixBase<Derived>::PlainObject>
|
2008-10-13 15:53:27 +00:00
|
|
|
MatrixBase<Derived>::ldlt() const
|
|
|
|
|
{
|
2010-02-21 10:29:19 +01:00
|
|
|
return LDLT<PlainObject>(derived());
|
2008-10-13 15:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif // EIGEN_LDLT_H
|