16 template<
typename MatrixType,
int UpLo>
struct LLT_Traits;
52 template<
typename _MatrixType,
int _UpLo>
class LLT
55 typedef _MatrixType MatrixType;
57 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
58 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
59 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
61 typedef typename MatrixType::Scalar Scalar;
64 typedef typename MatrixType::StorageIndex StorageIndex;
67 PacketSize = internal::packet_traits<Scalar>::size,
68 AlignmentMask = int(PacketSize)-1,
72 typedef internal::LLT_Traits<MatrixType,UpLo> Traits;
80 LLT() : m_matrix(), m_isInitialized(false) {}
88 explicit LLT(
Index size) : m_matrix(size, size),
89 m_isInitialized(false) {}
91 template<
typename InputType>
93 : m_matrix(matrix.rows(), matrix.cols()),
94 m_isInitialized(false)
106 template<
typename InputType>
108 : m_matrix(matrix.derived()),
109 m_isInitialized(false)
115 inline typename Traits::MatrixU
matrixU()
const
117 eigen_assert(m_isInitialized &&
"LLT is not initialized.");
118 return Traits::getU(m_matrix);
122 inline typename Traits::MatrixL
matrixL()
const
124 eigen_assert(m_isInitialized &&
"LLT is not initialized.");
125 return Traits::getL(m_matrix);
138 template<
typename Rhs>
142 eigen_assert(m_isInitialized &&
"LLT is not initialized.");
143 eigen_assert(m_matrix.rows()==b.
rows()
144 &&
"LLT::solve(): invalid number of rows of the right hand side matrix b");
148 template<
typename Derived>
151 template<
typename InputType>
159 eigen_assert(m_isInitialized &&
"LLT is not initialized.");
160 eigen_assert(m_info ==
Success &&
"LLT failed because matrix appears to be negative");
161 return internal::rcond_estimate_helper(m_l1_norm, *
this);
170 eigen_assert(m_isInitialized &&
"LLT is not initialized.");
184 eigen_assert(m_isInitialized &&
"LLT is not initialized.");
195 inline Index rows()
const {
return m_matrix.rows(); }
196 inline Index cols()
const {
return m_matrix.cols(); }
198 template<
typename VectorType>
199 LLT rankUpdate(
const VectorType& vec,
const RealScalar& sigma = 1);
201 #ifndef EIGEN_PARSED_BY_DOXYGEN
202 template<
typename RhsType,
typename DstType>
204 void _solve_impl(
const RhsType &rhs, DstType &dst)
const;
209 static void check_template_parameters()
211 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar);
219 RealScalar m_l1_norm;
220 bool m_isInitialized;
226 template<
typename Scalar,
int UpLo>
struct llt_inplace;
228 template<
typename MatrixType,
typename VectorType>
229 static Index llt_rank_update_lower(MatrixType& mat,
const VectorType& vec,
const typename MatrixType::RealScalar& sigma)
232 typedef typename MatrixType::Scalar Scalar;
233 typedef typename MatrixType::RealScalar RealScalar;
234 typedef typename MatrixType::ColXpr ColXpr;
235 typedef typename internal::remove_all<ColXpr>::type ColXprCleaned;
236 typedef typename ColXprCleaned::SegmentReturnType ColXprSegment;
237 typedef Matrix<Scalar,Dynamic,1> TempVectorType;
238 typedef typename TempVectorType::SegmentReturnType TempVecSegment;
240 Index n = mat.cols();
241 eigen_assert(mat.rows()==n && vec.size()==n);
250 temp =
sqrt(sigma) * vec;
252 for(
Index i=0; i<n; ++i)
254 JacobiRotation<Scalar> g;
255 g.makeGivens(mat(i,i), -temp(i), &mat(i,i));
260 ColXprSegment x(mat.col(i).tail(rs));
261 TempVecSegment y(temp.tail(rs));
262 apply_rotation_in_the_plane(x, y, g);
270 for(
Index j=0; j<n; ++j)
272 RealScalar Ljj = numext::real(mat.coeff(j,j));
273 RealScalar dj = numext::abs2(Ljj);
274 Scalar wj = temp.coeff(j);
275 RealScalar swj2 = sigma*numext::abs2(wj);
276 RealScalar gamma = dj*beta + swj2;
278 RealScalar x = dj + swj2/beta;
279 if (x<=RealScalar(0))
281 RealScalar nLjj =
sqrt(x);
282 mat.coeffRef(j,j) = nLjj;
289 temp.tail(rs) -= (wj/Ljj) * mat.col(j).tail(rs);
291 mat.col(j).tail(rs) = (nLjj/Ljj) * mat.col(j).tail(rs) + (nLjj * sigma*numext::conj(wj)/gamma)*temp.tail(rs);
298 template<
typename Scalar>
struct llt_inplace<Scalar,
Lower>
300 typedef typename NumTraits<Scalar>::Real RealScalar;
301 template<
typename MatrixType>
302 static Index unblocked(MatrixType& mat)
306 eigen_assert(mat.rows()==mat.cols());
307 const Index size = mat.rows();
308 for(
Index k = 0; k < size; ++k)
312 Block<MatrixType,Dynamic,1> A21(mat,k+1,k,rs,1);
313 Block<MatrixType,1,Dynamic> A10(mat,k,0,1,k);
314 Block<MatrixType,Dynamic,Dynamic> A20(mat,k+1,0,rs,k);
316 RealScalar x = numext::real(mat.coeff(k,k));
317 if (k>0) x -= A10.squaredNorm();
318 if (x<=RealScalar(0))
320 mat.coeffRef(k,k) = x =
sqrt(x);
321 if (k>0 && rs>0) A21.noalias() -= A20 * A10.adjoint();
327 template<
typename MatrixType>
328 static Index blocked(MatrixType& m)
330 eigen_assert(m.rows()==m.cols());
331 Index size = m.rows();
335 Index blockSize = size/8;
336 blockSize = (blockSize/16)*16;
337 blockSize = (std::min)((std::max)(blockSize,
Index(8)),
Index(128));
339 for (
Index k=0; k<size; k+=blockSize)
345 Index bs = (std::min)(blockSize, size-k);
346 Index rs = size - k - bs;
347 Block<MatrixType,Dynamic,Dynamic> A11(m,k, k, bs,bs);
348 Block<MatrixType,Dynamic,Dynamic> A21(m,k+bs,k, rs,bs);
349 Block<MatrixType,Dynamic,Dynamic> A22(m,k+bs,k+bs,rs,rs);
352 if((ret=unblocked(A11))>=0)
return k+ret;
353 if(rs>0) A11.adjoint().template triangularView<Upper>().
template solveInPlace<OnTheRight>(A21);
354 if(rs>0) A22.template selfadjointView<Lower>().rankUpdate(A21,
typename NumTraits<RealScalar>::Literal(-1));
359 template<
typename MatrixType,
typename VectorType>
360 static Index rankUpdate(MatrixType& mat,
const VectorType& vec,
const RealScalar& sigma)
362 return Eigen::internal::llt_rank_update_lower(mat, vec, sigma);
366 template<
typename Scalar>
struct llt_inplace<Scalar,
Upper>
368 typedef typename NumTraits<Scalar>::Real RealScalar;
370 template<
typename MatrixType>
371 static EIGEN_STRONG_INLINE
Index unblocked(MatrixType& mat)
373 Transpose<MatrixType> matt(mat);
374 return llt_inplace<Scalar, Lower>::unblocked(matt);
376 template<
typename MatrixType>
377 static EIGEN_STRONG_INLINE
Index blocked(MatrixType& mat)
379 Transpose<MatrixType> matt(mat);
380 return llt_inplace<Scalar, Lower>::blocked(matt);
382 template<
typename MatrixType,
typename VectorType>
383 static Index rankUpdate(MatrixType& mat,
const VectorType& vec,
const RealScalar& sigma)
385 Transpose<MatrixType> matt(mat);
386 return llt_inplace<Scalar, Lower>::rankUpdate(matt, vec.conjugate(), sigma);
390 template<
typename MatrixType>
struct LLT_Traits<MatrixType,
Lower>
392 typedef const TriangularView<const MatrixType, Lower> MatrixL;
393 typedef const TriangularView<const typename MatrixType::AdjointReturnType, Upper> MatrixU;
394 static inline MatrixL getL(
const MatrixType& m) {
return MatrixL(m); }
395 static inline MatrixU getU(
const MatrixType& m) {
return MatrixU(m.adjoint()); }
396 static bool inplace_decomposition(MatrixType& m)
397 {
return llt_inplace<typename MatrixType::Scalar, Lower>::blocked(m)==-1; }
400 template<
typename MatrixType>
struct LLT_Traits<MatrixType,
Upper>
402 typedef const TriangularView<const typename MatrixType::AdjointReturnType, Lower> MatrixL;
403 typedef const TriangularView<const MatrixType, Upper> MatrixU;
404 static inline MatrixL getL(
const MatrixType& m) {
return MatrixL(m.adjoint()); }
405 static inline MatrixU getU(
const MatrixType& m) {
return MatrixU(m); }
406 static bool inplace_decomposition(MatrixType& m)
407 {
return llt_inplace<typename MatrixType::Scalar, Upper>::blocked(m)==-1; }
419 template<
typename MatrixType,
int _UpLo>
420 template<
typename InputType>
423 check_template_parameters();
427 m_matrix.resize(size, size);
431 m_l1_norm = RealScalar(0);
433 for (
Index col = 0; col < size; ++col) {
434 RealScalar abs_col_sum;
436 abs_col_sum = m_matrix.col(col).tail(size - col).template lpNorm<1>() + m_matrix.row(col).head(col).template lpNorm<1>();
438 abs_col_sum = m_matrix.col(col).head(col).template lpNorm<1>() + m_matrix.row(col).tail(size - col).template lpNorm<1>();
439 if (abs_col_sum > m_l1_norm)
440 m_l1_norm = abs_col_sum;
443 m_isInitialized =
true;
444 bool ok = Traits::inplace_decomposition(m_matrix);
455 template<
typename _MatrixType,
int _UpLo>
456 template<
typename VectorType>
459 EIGEN_STATIC_ASSERT_VECTOR_ONLY(VectorType);
460 eigen_assert(v.size()==m_matrix.cols());
461 eigen_assert(m_isInitialized);
462 if(internal::llt_inplace<typename MatrixType::Scalar, UpLo>::rankUpdate(m_matrix,v,sigma)>=0)
470 #ifndef EIGEN_PARSED_BY_DOXYGEN
471 template<
typename _MatrixType,
int _UpLo>
472 template<
typename RhsType,
typename DstType>
490 template<
typename MatrixType,
int _UpLo>
491 template<
typename Derived>
492 void LLT<MatrixType,_UpLo>::solveInPlace(MatrixBase<Derived> &bAndX)
const
494 eigen_assert(m_isInitialized &&
"LLT is not initialized.");
495 eigen_assert(m_matrix.rows()==bAndX.rows());
496 matrixL().solveInPlace(bAndX);
497 matrixU().solveInPlace(bAndX);
503 template<
typename MatrixType,
int _UpLo>
506 eigen_assert(m_isInitialized &&
"LLT is not initialized.");
507 return matrixL() * matrixL().adjoint().toDenseMatrix();
514 template<
typename Derived>
525 template<
typename MatrixType,
unsigned int UpLo>
534 #endif // EIGEN_LLT_H
const LLT< PlainObject, UpLo > llt() const
Definition: LLT.h:527
MatrixType reconstructedMatrix() const
Definition: LLT.h:504
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sqrt_op< typename Derived::Scalar >, const Derived > sqrt(const Eigen::ArrayBase< Derived > &x)
Traits::MatrixU matrixU() const
Definition: LLT.h:115
LLT(Index size)
Default Constructor with memory preallocation.
Definition: LLT.h:88
const MatrixType & matrixLLT() const
Definition: LLT.h:168
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:150
const Solve< LLT, Rhs > solve(const MatrixBase< Rhs > &b) const
Definition: LLT.h:140
Derived & derived()
Definition: EigenBase.h:45
Index rows() const
Definition: EigenBase.h:59
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: LLT.h:182
LLT(EigenBase< InputType > &matrix)
Constructs a LDLT factorization from a given matrix.
Definition: LLT.h:107
Definition: EigenBase.h:29
Definition: Constants.h:204
Eigen::Index Index
Definition: LLT.h:63
Standard Cholesky decomposition (LL^T) of a matrix and associated features.
Definition: LLT.h:52
Definition: Constants.h:434
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
Definition: Constants.h:206
RealScalar rcond() const
Definition: LLT.h:157
Definition: Constants.h:432
Index cols() const
Definition: EigenBase.h:62
const LLT< PlainObject > llt() const
Definition: LLT.h:516
Pseudo expression representing a solving operation.
Definition: Solve.h:62
LLT()
Default Constructor.
Definition: LLT.h:80
Traits::MatrixL matrixL() const
Definition: LLT.h:122
ComputationInfo
Definition: Constants.h:430
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
const LLT & adjoint() const
Definition: LLT.h:193