LDLT.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2011 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 2009 Keir Mierle <mierle@gmail.com>
6 // Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
7 // Copyright (C) 2011 Timothy E. Holy <tim.holy@gmail.com >
8 //
9 // This Source Code Form is subject to the terms of the Mozilla
10 // Public License v. 2.0. If a copy of the MPL was not distributed
11 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
12 
13 #ifndef EIGEN_LDLT_H
14 #define EIGEN_LDLT_H
15 
16 namespace Eigen {
17 
18 namespace internal {
19 template<typename MatrixType, int UpLo> struct LDLT_Traits;
20 }
21 
45 template<typename _MatrixType, int _UpLo> class LDLT
46 {
47  public:
48  typedef _MatrixType MatrixType;
49  enum {
50  RowsAtCompileTime = MatrixType::RowsAtCompileTime,
51  ColsAtCompileTime = MatrixType::ColsAtCompileTime,
52  Options = MatrixType::Options & ~RowMajorBit, // these are the options for the TmpMatrixType, we need a ColMajor matrix here!
53  MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
54  MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime,
55  UpLo = _UpLo
56  };
57  typedef typename MatrixType::Scalar Scalar;
59  typedef typename MatrixType::Index Index;
61 
64 
66 
72  LDLT() : m_matrix(), m_transpositions(), m_isInitialized(false) {}
73 
80  LDLT(Index size)
81  : m_matrix(size, size),
82  m_transpositions(size),
83  m_temporary(size),
84  m_isInitialized(false)
85  {}
86 
92  LDLT(const MatrixType& matrix)
93  : m_matrix(matrix.rows(), matrix.cols()),
94  m_transpositions(matrix.rows()),
95  m_temporary(matrix.rows()),
96  m_isInitialized(false)
97  {
98  compute(matrix);
99  }
100 
104  void setZero()
105  {
106  m_isInitialized = false;
107  }
108 
110  inline typename Traits::MatrixU matrixU() const
111  {
112  eigen_assert(m_isInitialized && "LDLT is not initialized.");
113  return Traits::getU(m_matrix);
114  }
115 
117  inline typename Traits::MatrixL matrixL() const
118  {
119  eigen_assert(m_isInitialized && "LDLT is not initialized.");
120  return Traits::getL(m_matrix);
121  }
122 
125  inline const TranspositionType& transpositionsP() const
126  {
127  eigen_assert(m_isInitialized && "LDLT is not initialized.");
128  return m_transpositions;
129  }
130 
133  {
134  eigen_assert(m_isInitialized && "LDLT is not initialized.");
135  return m_matrix.diagonal();
136  }
137 
139  inline bool isPositive() const
140  {
141  eigen_assert(m_isInitialized && "LDLT is not initialized.");
142  return m_sign == 1;
143  }
144 
145  #ifdef EIGEN2_SUPPORT
146  inline bool isPositiveDefinite() const
147  {
148  return isPositive();
149  }
150  #endif
151 
153  inline bool isNegative(void) const
154  {
155  eigen_assert(m_isInitialized && "LDLT is not initialized.");
156  return m_sign == -1;
157  }
158 
174  template<typename Rhs>
176  solve(const MatrixBase<Rhs>& b) const
177  {
178  eigen_assert(m_isInitialized && "LDLT is not initialized.");
179  eigen_assert(m_matrix.rows()==b.rows()
180  && "LDLT::solve(): invalid number of rows of the right hand side matrix b");
181  return internal::solve_retval<LDLT, Rhs>(*this, b.derived());
182  }
183 
184  #ifdef EIGEN2_SUPPORT
185  template<typename OtherDerived, typename ResultType>
186  bool solve(const MatrixBase<OtherDerived>& b, ResultType *result) const
187  {
188  *result = this->solve(b);
189  return true;
190  }
191  #endif
192 
193  template<typename Derived>
194  bool solveInPlace(MatrixBase<Derived> &bAndX) const;
195 
196  LDLT& compute(const MatrixType& matrix);
197 
198  template <typename Derived>
199  LDLT& rankUpdate(const MatrixBase<Derived>& w, const RealScalar& alpha=1);
200 
205  inline const MatrixType& matrixLDLT() const
206  {
207  eigen_assert(m_isInitialized && "LDLT is not initialized.");
208  return m_matrix;
209  }
210 
211  MatrixType reconstructedMatrix() const;
212 
213  inline Index rows() const { return m_matrix.rows(); }
214  inline Index cols() const { return m_matrix.cols(); }
215 
222  {
223  eigen_assert(m_isInitialized && "LDLT is not initialized.");
224  return Success;
225  }
226 
227  protected:
228 
235  MatrixType m_matrix;
236  TranspositionType m_transpositions;
237  TmpMatrixType m_temporary;
238  int m_sign;
240 };
241 
242 namespace internal {
243 
244 template<int UpLo> struct ldlt_inplace;
245 
246 template<> struct ldlt_inplace<Lower>
247 {
248  template<typename MatrixType, typename TranspositionType, typename Workspace>
249  static bool unblocked(MatrixType& mat, TranspositionType& transpositions, Workspace& temp, int* sign=0)
250  {
251  using std::abs;
252  typedef typename MatrixType::Scalar Scalar;
253  typedef typename MatrixType::RealScalar RealScalar;
254  typedef typename MatrixType::Index Index;
255  eigen_assert(mat.rows()==mat.cols());
256  const Index size = mat.rows();
257 
258  if (size <= 1)
259  {
260  transpositions.setIdentity();
261  if(sign)
262  *sign = numext::real(mat.coeff(0,0))>0 ? 1:-1;
263  return true;
264  }
265 
266  RealScalar cutoff(0), biggest_in_corner;
267 
268  for (Index k = 0; k < size; ++k)
269  {
270  // Find largest diagonal element
271  Index index_of_biggest_in_corner;
272  biggest_in_corner = mat.diagonal().tail(size-k).cwiseAbs().maxCoeff(&index_of_biggest_in_corner);
273  index_of_biggest_in_corner += k;
274 
275  if(k == 0)
276  {
277  // The biggest overall is the point of reference to which further diagonals
278  // are compared; if any diagonal is negligible compared
279  // to the largest overall, the algorithm bails.
280  cutoff = abs(NumTraits<Scalar>::epsilon() * biggest_in_corner);
281  }
282 
283  // Finish early if the matrix is not full rank.
284  if(biggest_in_corner < cutoff)
285  {
286  for(Index i = k; i < size; i++) transpositions.coeffRef(i) = i;
287  if(sign) *sign = 0;
288  break;
289  }
290 
291  transpositions.coeffRef(k) = index_of_biggest_in_corner;
292  if(k != index_of_biggest_in_corner)
293  {
294  // apply the transposition while taking care to consider only
295  // the lower triangular part
296  Index s = size-index_of_biggest_in_corner-1; // trailing size after the biggest element
297  mat.row(k).head(k).swap(mat.row(index_of_biggest_in_corner).head(k));
298  mat.col(k).tail(s).swap(mat.col(index_of_biggest_in_corner).tail(s));
299  std::swap(mat.coeffRef(k,k),mat.coeffRef(index_of_biggest_in_corner,index_of_biggest_in_corner));
300  for(int i=k+1;i<index_of_biggest_in_corner;++i)
301  {
302  Scalar tmp = mat.coeffRef(i,k);
303  mat.coeffRef(i,k) = numext::conj(mat.coeffRef(index_of_biggest_in_corner,i));
304  mat.coeffRef(index_of_biggest_in_corner,i) = numext::conj(tmp);
305  }
307  mat.coeffRef(index_of_biggest_in_corner,k) = numext::conj(mat.coeff(index_of_biggest_in_corner,k));
308  }
309 
310  // partition the matrix:
311  // A00 | - | -
312  // lu = A10 | A11 | -
313  // A20 | A21 | A22
314  Index rs = size - k - 1;
315  Block<MatrixType,Dynamic,1> A21(mat,k+1,k,rs,1);
316  Block<MatrixType,1,Dynamic> A10(mat,k,0,1,k);
317  Block<MatrixType,Dynamic,Dynamic> A20(mat,k+1,0,rs,k);
318 
319  if(k>0)
320  {
321  temp.head(k) = mat.diagonal().head(k).asDiagonal() * A10.adjoint();
322  mat.coeffRef(k,k) -= (A10 * temp.head(k)).value();
323  if(rs>0)
324  A21.noalias() -= A20 * temp.head(k);
325  }
326  if((rs>0) && (abs(mat.coeffRef(k,k)) > cutoff))
327  A21 /= mat.coeffRef(k,k);
328 
329  if(sign)
330  {
331  // LDLT is not guaranteed to work for indefinite matrices, but let's try to get the sign right
332  int newSign = numext::real(mat.diagonal().coeff(index_of_biggest_in_corner)) > 0;
333  if(k == 0)
334  *sign = newSign;
335  else if(*sign != newSign)
336  *sign = 0;
337  }
338  }
339 
340  return true;
341  }
342 
343  // Reference for the algorithm: Davis and Hager, "Multiple Rank
344  // Modifications of a Sparse Cholesky Factorization" (Algorithm 1)
345  // Trivial rearrangements of their computations (Timothy E. Holy)
346  // allow their algorithm to work for rank-1 updates even if the
347  // original matrix is not of full rank.
348  // Here only rank-1 updates are implemented, to reduce the
349  // requirement for intermediate storage and improve accuracy
350  template<typename MatrixType, typename WDerived>
351  static bool updateInPlace(MatrixType& mat, MatrixBase<WDerived>& w, const typename MatrixType::RealScalar& sigma=1)
352  {
353  using numext::isfinite;
354  typedef typename MatrixType::Scalar Scalar;
355  typedef typename MatrixType::RealScalar RealScalar;
356  typedef typename MatrixType::Index Index;
357 
358  const Index size = mat.rows();
359  eigen_assert(mat.cols() == size && w.size()==size);
360 
361  RealScalar alpha = 1;
362 
363  // Apply the update
364  for (Index j = 0; j < size; j++)
365  {
366  // Check for termination due to an original decomposition of low-rank
367  if (!(isfinite)(alpha))
368  break;
369 
370  // Update the diagonal terms
371  RealScalar dj = numext::real(mat.coeff(j,j));
372  Scalar wj = w.coeff(j);
373  RealScalar swj2 = sigma*numext::abs2(wj);
374  RealScalar gamma = dj*alpha + swj2;
375 
376  mat.coeffRef(j,j) += swj2/alpha;
377  alpha += swj2/dj;
378 
379 
380  // Update the terms of L
381  Index rs = size-j-1;
382  w.tail(rs) -= wj * mat.col(j).tail(rs);
383  if(gamma != 0)
384  mat.col(j).tail(rs) += (sigma*numext::conj(wj)/gamma)*w.tail(rs);
385  }
386  return true;
387  }
388 
389  template<typename MatrixType, typename TranspositionType, typename Workspace, typename WType>
390  static bool update(MatrixType& mat, const TranspositionType& transpositions, Workspace& tmp, const WType& w, const typename MatrixType::RealScalar& sigma=1)
391  {
392  // Apply the permutation to the input w
393  tmp = transpositions * w;
394 
395  return ldlt_inplace<Lower>::updateInPlace(mat,tmp,sigma);
396  }
397 };
398 
399 template<> struct ldlt_inplace<Upper>
400 {
401  template<typename MatrixType, typename TranspositionType, typename Workspace>
402  static EIGEN_STRONG_INLINE bool unblocked(MatrixType& mat, TranspositionType& transpositions, Workspace& temp, int* sign=0)
403  {
404  Transpose<MatrixType> matt(mat);
405  return ldlt_inplace<Lower>::unblocked(matt, transpositions, temp, sign);
406  }
407 
408  template<typename MatrixType, typename TranspositionType, typename Workspace, typename WType>
409  static EIGEN_STRONG_INLINE bool update(MatrixType& mat, TranspositionType& transpositions, Workspace& tmp, WType& w, const typename MatrixType::RealScalar& sigma=1)
410  {
411  Transpose<MatrixType> matt(mat);
412  return ldlt_inplace<Lower>::update(matt, transpositions, tmp, w.conjugate(), sigma);
413  }
414 };
415 
416 template<typename MatrixType> struct LDLT_Traits<MatrixType,Lower>
417 {
420  static inline MatrixL getL(const MatrixType& m) { return m; }
421  static inline MatrixU getU(const MatrixType& m) { return m.adjoint(); }
422 };
423 
424 template<typename MatrixType> struct LDLT_Traits<MatrixType,Upper>
425 {
428  static inline MatrixL getL(const MatrixType& m) { return m.adjoint(); }
429  static inline MatrixU getU(const MatrixType& m) { return m; }
430 };
431 
432 } // end namespace internal
433 
436 template<typename MatrixType, int _UpLo>
438 {
439  eigen_assert(a.rows()==a.cols());
440  const Index size = a.rows();
441 
442  m_matrix = a;
443 
444  m_transpositions.resize(size);
445  m_isInitialized = false;
446  m_temporary.resize(size);
447 
448  internal::ldlt_inplace<UpLo>::unblocked(m_matrix, m_transpositions, m_temporary, &m_sign);
449 
450  m_isInitialized = true;
451  return *this;
452 }
453 
459 template<typename MatrixType, int _UpLo>
460 template<typename Derived>
462 {
463  const Index size = w.rows();
464  if (m_isInitialized)
465  {
466  eigen_assert(m_matrix.rows()==size);
467  }
468  else
469  {
470  m_matrix.resize(size,size);
471  m_matrix.setZero();
472  m_transpositions.resize(size);
473  for (Index i = 0; i < size; i++)
474  m_transpositions.coeffRef(i) = i;
475  m_temporary.resize(size);
476  m_sign = sigma>=0 ? 1 : -1;
477  m_isInitialized = true;
478  }
479 
480  internal::ldlt_inplace<UpLo>::update(m_matrix, m_transpositions, m_temporary, w, sigma);
481 
482  return *this;
483 }
484 
485 namespace internal {
486 template<typename _MatrixType, int _UpLo, typename Rhs>
487 struct solve_retval<LDLT<_MatrixType,_UpLo>, Rhs>
488  : solve_retval_base<LDLT<_MatrixType,_UpLo>, Rhs>
489 {
491  EIGEN_MAKE_SOLVE_HELPERS(LDLTType,Rhs)
492 
493  template<typename Dest> void evalTo(Dest& dst) const
494  {
495  eigen_assert(rhs().rows() == dec().matrixLDLT().rows());
496  // dst = P b
497  dst = dec().transpositionsP() * rhs();
498 
499  // dst = L^-1 (P b)
500  dec().matrixL().solveInPlace(dst);
501 
502  // dst = D^-1 (L^-1 P b)
503  // more precisely, use pseudo-inverse of D (see bug 241)
504  using std::abs;
505  using std::max;
506  typedef typename LDLTType::MatrixType MatrixType;
507  typedef typename LDLTType::Scalar Scalar;
508  typedef typename LDLTType::RealScalar RealScalar;
509  const Diagonal<const MatrixType> vectorD = dec().vectorD();
510  RealScalar tolerance = (max)(vectorD.array().abs().maxCoeff() * NumTraits<Scalar>::epsilon(),
511  RealScalar(1) / NumTraits<RealScalar>::highest()); // motivated by LAPACK's xGELSS
512  for (Index i = 0; i < vectorD.size(); ++i) {
513  if(abs(vectorD(i)) > tolerance)
514  dst.row(i) /= vectorD(i);
515  else
516  dst.row(i).setZero();
517  }
518 
519  // dst = L^-T (D^-1 L^-1 P b)
520  dec().matrixU().solveInPlace(dst);
521 
522  // dst = P^-1 (L^-T D^-1 L^-1 P b) = A^-1 b
523  dst = dec().transpositionsP().transpose() * dst;
524  }
525 };
526 }
527 
541 template<typename MatrixType,int _UpLo>
542 template<typename Derived>
544 {
545  eigen_assert(m_isInitialized && "LDLT is not initialized.");
546  eigen_assert(m_matrix.rows() == bAndX.rows());
547 
548  bAndX = this->solve(bAndX);
549 
550  return true;
551 }
552 
556 template<typename MatrixType, int _UpLo>
558 {
559  eigen_assert(m_isInitialized && "LDLT is not initialized.");
560  const Index size = m_matrix.rows();
561  MatrixType res(size,size);
562 
563  // P
564  res.setIdentity();
565  res = transpositionsP() * res;
566  // L^* P
567  res = matrixU() * res;
568  // D(L^*P)
569  res = vectorD().asDiagonal() * res;
570  // L(DL^*P)
571  res = matrixL() * res;
572  // P^T (LDL^*P)
573  res = transpositionsP().transpose() * res;
574 
575  return res;
576 }
577 
581 template<typename MatrixType, unsigned int UpLo>
584 {
585  return LDLT<PlainObject,UpLo>(m_matrix);
586 }
587 
591 template<typename Derived>
594 {
595  return LDLT<PlainObject>(derived());
596 }
597 
598 } // end namespace Eigen
599 
600 #endif // EIGEN_LDLT_H
Robust Cholesky decomposition of a matrix with pivoting.
Definition: LDLT.h:45
const AutoDiffScalar< DerType > & conj(const AutoDiffScalar< DerType > &x)
TranspositionType m_transpositions
Definition: LDLT.h:236
const LDLT< PlainObject > ldlt() const
Definition: LDLT.h:593
NumTraits< typename MatrixType::Scalar >::Real RealScalar
Definition: LDLT.h:58
static bool update(MatrixType &mat, const TranspositionType &transpositions, Workspace &tmp, const WType &w, const typename MatrixType::RealScalar &sigma=1)
Definition: LDLT.h:390
#define EIGEN_STRONG_INLINE
const TriangularView< const typename MatrixType::AdjointReturnType, TransposeMode > adjoint() const
_MatrixType MatrixType
Definition: LDLT.h:48
SegmentReturnType tail(Index vecSize)
Definition: DenseBase.h:811
Expression of the transpose of a matrix.
Definition: Transpose.h:57
LDLT(Index size)
Default Constructor with memory preallocation.
Definition: LDLT.h:80
Index rows() const
Definition: LDLT.h:213
iterative scaling algorithm to equilibrate rows and column norms in matrices
Definition: matrix.hpp:471
LDLT & rankUpdate(const MatrixBase< Derived > &w, const RealScalar &alpha=1)
static bool unblocked(MatrixType &mat, TranspositionType &transpositions, Workspace &temp, int *sign=0)
Definition: LDLT.h:249
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:88
static bool updateInPlace(MatrixType &mat, MatrixBase< WDerived > &w, const typename MatrixType::RealScalar &sigma=1)
Definition: LDLT.h:351
void setZero()
Definition: LDLT.h:104
const unsigned int RowMajorBit
Matrix< Scalar, RowsAtCompileTime, 1, Options, MaxRowsAtCompileTime, 1 > TmpMatrixType
Definition: LDLT.h:60
EIGEN_STRONG_INLINE const CwiseUnaryOp< internal::scalar_abs2_op< Scalar >, const Derived > abs2() const
MatrixType m_matrix
Definition: LDLT.h:235
const MatrixType & matrixLDLT() const
Definition: LDLT.h:205
EIGEN_STRONG_INLINE const CwiseUnaryOp< internal::scalar_abs_op< Scalar >, const Derived > abs() const
RealReturnType real() const
static EIGEN_STRONG_INLINE bool update(MatrixType &mat, TranspositionType &transpositions, Workspace &tmp, WType &w, const typename MatrixType::RealScalar &sigma=1)
Definition: LDLT.h:409
LDLT()
Default Constructor.
Definition: LDLT.h:72
MatrixType::Index Index
Definition: LDLT.h:59
bool solveInPlace(MatrixBase< Derived > &bAndX) const
Definition: LDLT.h:543
bool isNegative(void) const
Definition: LDLT.h:153
const TriangularView< const typename MatrixType::AdjointReturnType, UnitLower > MatrixL
Definition: LDLT.h:426
Provides a generic way to set and pass user-specified options.
Definition: options.hpp:65
const TriangularView< const MatrixType, UnitLower > MatrixL
Definition: LDLT.h:418
Transpositions< RowsAtCompileTime, MaxRowsAtCompileTime > TranspositionType
Definition: LDLT.h:62
MatrixType reconstructedMatrix() const
Definition: LDLT.h:557
static MatrixL getL(const MatrixType &m)
Definition: LDLT.h:420
internal::LDLT_Traits< MatrixType, UpLo > Traits
Definition: LDLT.h:65
const TranspositionType & transpositionsP() const
Definition: LDLT.h:125
static MatrixU getU(const MatrixType &m)
Definition: LDLT.h:421
Traits::MatrixU matrixU() const
Definition: LDLT.h:110
void rhs(const real_t *x, real_t *f)
Index cols() const
Definition: LDLT.h:214
TmpMatrixType m_temporary
Definition: LDLT.h:237
bool isPositive() const
Definition: LDLT.h:139
static MatrixU getU(const MatrixType &m)
Definition: LDLT.h:429
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: LDLT.h:221
Expression of a fixed-size or dynamic-size block.
Definition: Core/Block.h:102
MatrixType::Scalar Scalar
Definition: LDLT.h:57
Diagonal< const MatrixType > vectorD() const
Definition: LDLT.h:132
Traits::MatrixL matrixL() const
Definition: LDLT.h:117
Base class for triangular part in a matrix.
int m_sign
Definition: LDLT.h:238
static EIGEN_STRONG_INLINE bool unblocked(MatrixType &mat, TranspositionType &transpositions, Workspace &temp, int *sign=0)
Definition: LDLT.h:402
#define EIGEN_MAKE_SOLVE_HELPERS(DecompositionType, Rhs)
Definition: Solve.h:61
const TriangularView< const MatrixType, UnitUpper > MatrixU
Definition: LDLT.h:427
const TriangularView< const typename MatrixType::AdjointReturnType, UnitUpper > MatrixU
Definition: LDLT.h:419
Expression of a diagonal/subdiagonal/superdiagonal in a matrix.
Definition: Diagonal.h:64
#define eigen_assert(x)
const internal::solve_retval< LDLT, Rhs > solve(const MatrixBase< Rhs > &b) const
Definition: LDLT.h:176
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
PermutationMatrix< RowsAtCompileTime, MaxRowsAtCompileTime > PermutationType
Definition: LDLT.h:63
bool() isfinite(const T &x)
bool m_isInitialized
Definition: LDLT.h:239
LDLT & compute(const MatrixType &matrix)
Definition: LDLT.h:437
LDLT(const MatrixType &matrix)
Constructor with decomposition.
Definition: LDLT.h:92
const LDLT< PlainObject, UpLo > ldlt() const
Definition: LDLT.h:583
static MatrixL getL(const MatrixType &m)
Definition: LDLT.h:428


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:34:48