14#include "./InternalHeaderCheck.h"
20template <
typename MatrixType_,
int UpLo_>
21struct traits<LLT<MatrixType_, UpLo_> > : traits<MatrixType_> {
22 typedef MatrixXpr XprKind;
23 typedef SolverStorage StorageKind;
24 typedef int StorageIndex;
28template <
typename MatrixType,
int UpLo>
69template <
typename MatrixType_,
int UpLo_>
72 typedef MatrixType_ MatrixType;
76 EIGEN_GENERIC_PUBLIC_INTERFACE(
LLT)
77 enum { MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime };
79 enum { PacketSize = internal::packet_traits<Scalar>::size, AlignmentMask = int(PacketSize) - 1, UpLo = UpLo_ };
81 typedef internal::LLT_Traits<MatrixType, UpLo> Traits;
89 LLT() : m_matrix(), m_isInitialized(false) {}
99 template <
typename InputType>
100 explicit LLT(
const EigenBase<InputType>& matrix) : m_matrix(matrix.rows(), matrix.cols()), m_isInitialized(false) {
111 template <
typename InputType>
117 inline typename Traits::MatrixU
matrixU()
const {
118 eigen_assert(m_isInitialized &&
"LLT is not initialized.");
119 return Traits::getU(m_matrix);
123 inline typename Traits::MatrixL
matrixL()
const {
124 eigen_assert(m_isInitialized &&
"LLT is not initialized.");
125 return Traits::getL(m_matrix);
128#ifdef EIGEN_PARSED_BY_DOXYGEN
139 template <
typename Rhs>
143 template <
typename Derived>
146 template <
typename InputType>
153 eigen_assert(m_isInitialized &&
"LLT is not initialized.");
154 eigen_assert(m_info ==
Success &&
"LLT failed because matrix appears to be negative");
155 return internal::rcond_estimate_helper(m_l1_norm, *
this);
163 eigen_assert(m_isInitialized &&
"LLT is not initialized.");
175 eigen_assert(m_isInitialized &&
"LLT is not initialized.");
187 inline EIGEN_CONSTEXPR
Index rows() const EIGEN_NOEXCEPT {
return m_matrix.rows(); }
188 inline EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT {
return m_matrix.cols(); }
190 template <
typename VectorType>
191 LLT& rankUpdate(
const VectorType& vec,
const RealScalar& sigma = 1);
193#ifndef EIGEN_PARSED_BY_DOXYGEN
194 template <
typename RhsType,
typename DstType>
195 void _solve_impl(
const RhsType& rhs, DstType& dst)
const;
197 template <
bool Conjugate,
typename RhsType,
typename DstType>
198 void _solve_impl_transposed(
const RhsType& rhs, DstType& dst)
const;
202 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
209 RealScalar m_l1_norm;
210 bool m_isInitialized;
211 ComputationInfo m_info;
216template <
typename Scalar,
int UpLo>
219template <
typename MatrixType,
typename VectorType>
220static Index llt_rank_update_lower(MatrixType& mat,
const VectorType& vec,
221 const typename MatrixType::RealScalar& sigma) {
223 typedef typename MatrixType::Scalar Scalar;
224 typedef typename MatrixType::RealScalar RealScalar;
225 typedef typename MatrixType::ColXpr ColXpr;
226 typedef internal::remove_all_t<ColXpr> ColXprCleaned;
227 typedef typename ColXprCleaned::SegmentReturnType ColXprSegment;
228 typedef Matrix<Scalar, Dynamic, 1> TempVectorType;
229 typedef typename TempVectorType::SegmentReturnType TempVecSegment;
231 Index n = mat.cols();
232 eigen_assert(mat.rows() == n && vec.size() == n);
240 temp =
sqrt(sigma) * vec;
242 for (Index i = 0; i < n; ++i) {
243 JacobiRotation<Scalar> g;
244 g.makeGivens(mat(i, i), -temp(i), &mat(i, i));
246 Index rs = n - i - 1;
248 ColXprSegment x(mat.col(i).tail(rs));
249 TempVecSegment y(temp.tail(rs));
250 apply_rotation_in_the_plane(x, y, g);
256 for (Index j = 0; j < n; ++j) {
257 RealScalar Ljj = numext::real(mat.coeff(j, j));
258 RealScalar dj = numext::abs2(Ljj);
259 Scalar wj = temp.coeff(j);
260 RealScalar swj2 = sigma * numext::abs2(wj);
261 RealScalar gamma = dj * beta + swj2;
263 RealScalar x = dj + swj2 / beta;
264 if (x <= RealScalar(0))
return j;
265 RealScalar nLjj =
sqrt(x);
266 mat.coeffRef(j, j) = nLjj;
270 Index rs = n - j - 1;
272 temp.tail(rs) -= (wj / Ljj) * mat.col(j).tail(rs);
273 if (!numext::is_exactly_zero(gamma))
274 mat.col(j).tail(rs) =
275 (nLjj / Ljj) * mat.col(j).tail(rs) + (nLjj * sigma * numext::conj(wj) / gamma) * temp.tail(rs);
282template <
typename Scalar>
283struct llt_inplace<Scalar,
Lower> {
284 typedef typename NumTraits<Scalar>::Real RealScalar;
285 template <
typename MatrixType>
286 static Index unblocked(MatrixType& mat) {
289 eigen_assert(mat.rows() == mat.cols());
290 const Index size = mat.rows();
291 for (Index k = 0; k < size; ++k) {
292 Index rs = size - k - 1;
294 Block<MatrixType, Dynamic, 1> A21(mat, k + 1, k, rs, 1);
295 Block<MatrixType, 1, Dynamic> A10(mat, k, 0, 1, k);
296 Block<MatrixType, Dynamic, Dynamic> A20(mat, k + 1, 0, rs, k);
298 RealScalar x = numext::real(mat.coeff(k, k));
299 if (k > 0) x -= A10.squaredNorm();
300 if (x <= RealScalar(0))
return k;
301 mat.coeffRef(k, k) = x =
sqrt(x);
302 if (k > 0 && rs > 0) A21.noalias() -= A20 * A10.adjoint();
303 if (rs > 0) A21 /= x;
308 template <
typename MatrixType>
309 static Index blocked(MatrixType& m) {
310 eigen_assert(m.rows() == m.cols());
311 Index size = m.rows();
312 if (size < 32)
return unblocked(m);
314 Index blockSize = size / 8;
315 blockSize = (blockSize / 16) * 16;
316 blockSize = (std::min)((std::max)(blockSize,
Index(8)),
Index(128));
318 for (Index k = 0; k < size; k += blockSize) {
323 Index bs = (std::min)(blockSize, size - k);
324 Index rs = size - k - bs;
325 Block<MatrixType, Dynamic, Dynamic> A11(m, k, k, bs, bs);
326 Block<MatrixType, Dynamic, Dynamic> A21(m, k + bs, k, rs, bs);
327 Block<MatrixType, Dynamic, Dynamic> A22(m, k + bs, k + bs, rs, rs);
330 if ((ret = unblocked(A11)) >= 0)
return k + ret;
331 if (rs > 0) A11.adjoint().template triangularView<Upper>().template solveInPlace<OnTheRight>(A21);
333 A22.template selfadjointView<Lower>().rankUpdate(A21,
334 typename NumTraits<RealScalar>::Literal(-1));
339 template <
typename MatrixType,
typename VectorType>
340 static Index rankUpdate(MatrixType& mat,
const VectorType& vec,
const RealScalar& sigma) {
341 return Eigen::internal::llt_rank_update_lower(mat, vec, sigma);
345template <
typename Scalar>
346struct llt_inplace<Scalar,
Upper> {
347 typedef typename NumTraits<Scalar>::Real RealScalar;
349 template <
typename MatrixType>
350 static EIGEN_STRONG_INLINE Index unblocked(MatrixType& mat) {
351 Transpose<MatrixType> matt(mat);
352 return llt_inplace<Scalar, Lower>::unblocked(matt);
354 template <
typename MatrixType>
355 static EIGEN_STRONG_INLINE
Index blocked(MatrixType& mat) {
357 return llt_inplace<Scalar, Lower>::blocked(matt);
359 template <
typename MatrixType,
typename VectorType>
360 static Index rankUpdate(MatrixType& mat,
const VectorType& vec,
const RealScalar& sigma) {
361 Transpose<MatrixType> matt(mat);
362 return llt_inplace<Scalar, Lower>::rankUpdate(matt, vec.conjugate(), sigma);
366template <
typename MatrixType>
367struct LLT_Traits<MatrixType,
Lower> {
368 typedef const TriangularView<const MatrixType, Lower> MatrixL;
369 typedef const TriangularView<const typename MatrixType::AdjointReturnType, Upper> MatrixU;
370 static inline MatrixL getL(
const MatrixType& m) {
return MatrixL(m); }
371 static inline MatrixU getU(
const MatrixType& m) {
return MatrixU(m.adjoint()); }
372 static bool inplace_decomposition(MatrixType& m) {
373 return llt_inplace<typename MatrixType::Scalar, Lower>::blocked(m) == -1;
377template <
typename MatrixType>
378struct LLT_Traits<MatrixType,
Upper> {
379 typedef const TriangularView<const typename MatrixType::AdjointReturnType, Lower> MatrixL;
380 typedef const TriangularView<const MatrixType, Upper> MatrixU;
381 static inline MatrixL getL(
const MatrixType& m) {
return MatrixL(m.adjoint()); }
382 static inline MatrixU getU(
const MatrixType& m) {
return MatrixU(m); }
383 static bool inplace_decomposition(MatrixType& m) {
384 return llt_inplace<typename MatrixType::Scalar, Upper>::blocked(m) == -1;
397template <
typename MatrixType,
int UpLo_>
398template <
typename InputType>
402 m_matrix.resize(size, size);
403 if (!internal::is_same_dense(m_matrix, a.
derived())) m_matrix = a.
derived();
406 m_l1_norm = RealScalar(0);
408 for (
Index col = 0; col < size; ++col) {
409 RealScalar abs_col_sum;
412 m_matrix.col(col).tail(size - col).template lpNorm<1>() + m_matrix.row(col).head(col).template lpNorm<1>();
415 m_matrix.col(col).head(col).template lpNorm<1>() + m_matrix.row(col).tail(size - col).template lpNorm<1>();
416 if (abs_col_sum > m_l1_norm) m_l1_norm = abs_col_sum;
419 m_isInitialized =
true;
420 bool ok = Traits::inplace_decomposition(m_matrix);
431template <
typename MatrixType_,
int UpLo_>
432template <
typename VectorType>
434 EIGEN_STATIC_ASSERT_VECTOR_ONLY(VectorType);
435 eigen_assert(v.size() == m_matrix.cols());
436 eigen_assert(m_isInitialized);
437 if (internal::llt_inplace<typename MatrixType::Scalar, UpLo>::rankUpdate(m_matrix, v, sigma) >= 0)
445#ifndef EIGEN_PARSED_BY_DOXYGEN
446template <
typename MatrixType_,
int UpLo_>
447template <
typename RhsType,
typename DstType>
449 _solve_impl_transposed<true>(rhs, dst);
452template <
typename MatrixType_,
int UpLo_>
453template <
bool Conjugate,
typename RhsType,
typename DstType>
454void LLT<MatrixType_, UpLo_>::_solve_impl_transposed(
const RhsType& rhs, DstType& dst)
const {
457 matrixL().template conjugateIf<!Conjugate>().solveInPlace(dst);
458 matrixU().template conjugateIf<!Conjugate>().solveInPlace(dst);
475template <
typename MatrixType,
int UpLo_>
476template <
typename Derived>
477void LLT<MatrixType, UpLo_>::solveInPlace(
const MatrixBase<Derived>& bAndX)
const {
478 eigen_assert(m_isInitialized &&
"LLT is not initialized.");
479 eigen_assert(m_matrix.rows() == bAndX.rows());
480 matrixL().solveInPlace(bAndX);
481 matrixU().solveInPlace(bAndX);
487template <
typename MatrixType,
int UpLo_>
489 eigen_assert(m_isInitialized &&
"LLT is not initialized.");
490 return matrixL() * matrixL().adjoint().toDenseMatrix();
497template <
typename Derived>
506template <
typename MatrixType,
unsigned int UpLo>
Standard Cholesky decomposition (LL^T) of a matrix and associated features.
Definition LLT.h:70
ComputationInfo info() const
Reports whether previous computation was successful.
Definition LLT.h:174
LLT(Index size)
Default Constructor with memory preallocation.
Definition LLT.h:97
RealScalar rcond() const
Definition LLT.h:152
const MatrixType & matrixLLT() const
Definition LLT.h:162
const Solve< LLT, Rhs > solve(const MatrixBase< Rhs > &b) const
LLT(EigenBase< InputType > &matrix)
Constructs a LLT factorization from a given matrix.
Definition LLT.h:112
Traits::MatrixU matrixU() const
Definition LLT.h:117
const LLT & adjoint() const EIGEN_NOEXCEPT
Definition LLT.h:185
LLT()
Default Constructor.
Definition LLT.h:89
MatrixType reconstructedMatrix() const
Definition LLT.h:488
Traits::MatrixL matrixL() const
Definition LLT.h:123
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:52
Expression of a selfadjoint matrix from a triangular part of a dense matrix.
Definition SelfAdjointView.h:51
Pseudo expression representing a solving operation.
Definition Solve.h:62
A base class for matrix decomposition and solvers.
Definition SolverBase.h:72
LLT< MatrixType_, UpLo_ > & derived()
Definition EigenBase.h:49
Expression of the transpose of a matrix.
Definition Transpose.h:56
ComputationInfo
Definition Constants.h:438
@ Lower
Definition Constants.h:211
@ Upper
Definition Constants.h:213
@ NumericalIssue
Definition Constants.h:442
@ Success
Definition Constants.h:440
Namespace containing all symbols from the Eigen library.
Definition Core:137
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sqrt_op< typename Derived::Scalar >, const Derived > sqrt(const Eigen::ArrayBase< Derived > &x)
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:83
Definition EigenBase.h:33
EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition EigenBase.h:61
Derived & derived()
Definition EigenBase.h:49
EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition EigenBase.h:59
EIGEN_CONSTEXPR Index size() const EIGEN_NOEXCEPT
Definition EigenBase.h:64