16 template<
typename MatrixType,
int UpLo>
struct LLT_Traits;
50 template<
typename _MatrixType,
int _UpLo>
class LLT
53 typedef _MatrixType MatrixType;
55 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
56 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
57 Options = MatrixType::Options,
58 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
60 typedef typename MatrixType::Scalar Scalar;
62 typedef typename MatrixType::Index Index;
65 PacketSize = internal::packet_traits<Scalar>::size,
66 AlignmentMask = int(PacketSize)-1,
70 typedef internal::LLT_Traits<MatrixType,UpLo> Traits;
78 LLT() : m_matrix(), m_isInitialized(false) {}
86 LLT(Index size) : m_matrix(size, size),
87 m_isInitialized(false) {}
89 LLT(
const MatrixType& matrix)
90 : m_matrix(matrix.rows(), matrix.cols()),
91 m_isInitialized(false)
97 inline typename Traits::MatrixU
matrixU()
const
99 eigen_assert(m_isInitialized &&
"LLT is not initialized.");
100 return Traits::getU(m_matrix);
104 inline typename Traits::MatrixL
matrixL()
const
106 eigen_assert(m_isInitialized &&
"LLT is not initialized.");
107 return Traits::getL(m_matrix);
120 template<
typename Rhs>
121 inline const internal::solve_retval<LLT, Rhs>
124 eigen_assert(m_isInitialized &&
"LLT is not initialized.");
125 eigen_assert(m_matrix.rows()==b.rows()
126 &&
"LLT::solve(): invalid number of rows of the right hand side matrix b");
127 return internal::solve_retval<LLT, Rhs>(*
this, b.derived());
130 #ifdef EIGEN2_SUPPORT
131 template<
typename OtherDerived,
typename ResultType>
134 *result = this->
solve(b);
138 bool isPositiveDefinite()
const {
return true; }
141 template<
typename Derived>
142 void solveInPlace(MatrixBase<Derived> &bAndX)
const;
152 eigen_assert(m_isInitialized &&
"LLT is not initialized.");
166 eigen_assert(m_isInitialized &&
"LLT is not initialized.");
170 inline Index rows()
const {
return m_matrix.rows(); }
171 inline Index cols()
const {
return m_matrix.cols(); }
173 template<
typename VectorType>
174 LLT rankUpdate(
const VectorType& vec,
const RealScalar& sigma = 1);
182 bool m_isInitialized;
188 template<
typename Scalar,
int UpLo>
struct llt_inplace;
190 template<
typename MatrixType,
typename VectorType>
191 static typename MatrixType::Index llt_rank_update_lower(MatrixType& mat,
const VectorType& vec,
const typename MatrixType::RealScalar& sigma)
193 typedef typename MatrixType::Scalar Scalar;
194 typedef typename MatrixType::RealScalar RealScalar;
195 typedef typename MatrixType::Index Index;
196 typedef typename MatrixType::ColXpr ColXpr;
197 typedef typename internal::remove_all<ColXpr>::type ColXprCleaned;
198 typedef typename ColXprCleaned::SegmentReturnType ColXprSegment;
199 typedef Matrix<Scalar,Dynamic,1> TempVectorType;
200 typedef typename TempVectorType::SegmentReturnType TempVecSegment;
203 eigen_assert(mat.rows()==n && vec.size()==n);
212 temp = sqrt(sigma) * vec;
214 for(
int i=0; i<n; ++i)
216 JacobiRotation<Scalar> g;
217 g.makeGivens(mat(i,i), -temp(i), &mat(i,i));
222 ColXprSegment x(mat.col(i).tail(rs));
223 TempVecSegment y(temp.tail(rs));
224 apply_rotation_in_the_plane(x, y, g);
232 for(
int j=0; j<n; ++j)
234 RealScalar Ljj = real(mat.coeff(j,j));
235 RealScalar dj = abs2(Ljj);
236 Scalar wj = temp.coeff(j);
237 RealScalar swj2 = sigma*abs2(wj);
238 RealScalar gamma = dj*beta + swj2;
240 RealScalar x = dj + swj2/beta;
241 if (x<=RealScalar(0))
243 RealScalar nLjj = sqrt(x);
244 mat.coeffRef(j,j) = nLjj;
251 temp.tail(rs) -= (wj/Ljj) * mat.col(j).tail(rs);
253 mat.col(j).tail(rs) = (nLjj/Ljj) * mat.col(j).tail(rs) + (nLjj * sigma*conj(wj)/gamma)*temp.tail(rs);
260 template<
typename Scalar>
struct llt_inplace<Scalar,
Lower>
262 typedef typename NumTraits<Scalar>::Real RealScalar;
263 template<
typename MatrixType>
264 static typename MatrixType::Index unblocked(MatrixType& mat)
266 typedef typename MatrixType::Index Index;
268 eigen_assert(mat.rows()==mat.cols());
269 const Index size = mat.rows();
270 for(Index k = 0; k < size; ++k)
274 Block<MatrixType,Dynamic,1> A21(mat,k+1,k,rs,1);
275 Block<MatrixType,1,Dynamic> A10(mat,k,0,1,k);
276 Block<MatrixType,Dynamic,Dynamic> A20(mat,k+1,0,rs,k);
278 RealScalar x = real(mat.coeff(k,k));
279 if (k>0) x -= A10.squaredNorm();
280 if (x<=RealScalar(0))
282 mat.coeffRef(k,k) = x = sqrt(x);
283 if (k>0 && rs>0) A21.noalias() -= A20 * A10.adjoint();
284 if (rs>0) A21 *= RealScalar(1)/x;
289 template<
typename MatrixType>
290 static typename MatrixType::Index blocked(MatrixType& m)
292 typedef typename MatrixType::Index Index;
293 eigen_assert(m.rows()==m.cols());
294 Index size = m.rows();
298 Index blockSize = size/8;
299 blockSize = (blockSize/16)*16;
300 blockSize = (std::min)((std::max)(blockSize,Index(8)), Index(128));
302 for (Index k=0; k<size; k+=blockSize)
308 Index bs = (std::min)(blockSize, size-k);
309 Index rs = size - k - bs;
310 Block<MatrixType,Dynamic,Dynamic> A11(m,k, k, bs,bs);
311 Block<MatrixType,Dynamic,Dynamic> A21(m,k+bs,k, rs,bs);
312 Block<MatrixType,Dynamic,Dynamic> A22(m,k+bs,k+bs,rs,rs);
315 if((ret=unblocked(A11))>=0)
return k+ret;
316 if(rs>0) A11.adjoint().template triangularView<Upper>().
template solveInPlace<OnTheRight>(A21);
317 if(rs>0) A22.template selfadjointView<Lower>().rankUpdate(A21,-1);
322 template<
typename MatrixType,
typename VectorType>
323 static typename MatrixType::Index rankUpdate(MatrixType& mat,
const VectorType& vec,
const RealScalar& sigma)
325 return Eigen::internal::llt_rank_update_lower(mat, vec, sigma);
329 template<
typename Scalar>
struct llt_inplace<Scalar,
Upper>
331 typedef typename NumTraits<Scalar>::Real RealScalar;
333 template<
typename MatrixType>
334 static EIGEN_STRONG_INLINE
typename MatrixType::Index unblocked(MatrixType& mat)
336 Transpose<MatrixType> matt(mat);
337 return llt_inplace<Scalar, Lower>::unblocked(matt);
339 template<
typename MatrixType>
340 static EIGEN_STRONG_INLINE
typename MatrixType::Index blocked(MatrixType& mat)
342 Transpose<MatrixType> matt(mat);
343 return llt_inplace<Scalar, Lower>::blocked(matt);
345 template<
typename MatrixType,
typename VectorType>
346 static typename MatrixType::Index rankUpdate(MatrixType& mat,
const VectorType& vec,
const RealScalar& sigma)
348 Transpose<MatrixType> matt(mat);
349 return llt_inplace<Scalar, Lower>::rankUpdate(matt, vec.conjugate(), sigma);
353 template<
typename MatrixType>
struct LLT_Traits<MatrixType,
Lower>
355 typedef const TriangularView<const MatrixType, Lower> MatrixL;
356 typedef const TriangularView<const typename MatrixType::AdjointReturnType, Upper> MatrixU;
357 static inline MatrixL getL(
const MatrixType& m) {
return m; }
358 static inline MatrixU getU(
const MatrixType& m) {
return m.adjoint(); }
359 static bool inplace_decomposition(MatrixType& m)
360 {
return llt_inplace<typename MatrixType::Scalar, Lower>::blocked(m)==-1; }
363 template<
typename MatrixType>
struct LLT_Traits<MatrixType,
Upper>
365 typedef const TriangularView<const typename MatrixType::AdjointReturnType, Lower> MatrixL;
366 typedef const TriangularView<const MatrixType, Upper> MatrixU;
367 static inline MatrixL getL(
const MatrixType& m) {
return m.adjoint(); }
368 static inline MatrixU getU(
const MatrixType& m) {
return m; }
369 static bool inplace_decomposition(MatrixType& m)
370 {
return llt_inplace<typename MatrixType::Scalar, Upper>::blocked(m)==-1; }
382 template<
typename MatrixType,
int _UpLo>
385 eigen_assert(a.rows()==a.cols());
386 const Index size = a.rows();
387 m_matrix.resize(size, size);
390 m_isInitialized =
true;
391 bool ok = Traits::inplace_decomposition(m_matrix);
402 template<
typename _MatrixType,
int _UpLo>
403 template<
typename VectorType>
406 EIGEN_STATIC_ASSERT_VECTOR_ONLY(VectorType);
407 eigen_assert(v.size()==m_matrix.cols());
408 eigen_assert(m_isInitialized);
409 if(internal::llt_inplace<typename MatrixType::Scalar, UpLo>::rankUpdate(m_matrix,v,sigma)>=0)
418 template<
typename _MatrixType,
int UpLo,
typename Rhs>
419 struct solve_retval<
LLT<_MatrixType, UpLo>, Rhs>
420 : solve_retval_base<LLT<_MatrixType, UpLo>, Rhs>
423 EIGEN_MAKE_SOLVE_HELPERS(LLTType,Rhs)
425 template<typename Dest>
void evalTo(Dest& dst)
const
428 dec().solveInPlace(dst);
446 template<
typename MatrixType,
int _UpLo>
447 template<
typename Derived>
448 void LLT<MatrixType,_UpLo>::solveInPlace(MatrixBase<Derived> &bAndX)
const
450 eigen_assert(m_isInitialized &&
"LLT is not initialized.");
451 eigen_assert(m_matrix.rows()==bAndX.rows());
452 matrixL().solveInPlace(bAndX);
453 matrixU().solveInPlace(bAndX);
459 template<
typename MatrixType,
int _UpLo>
462 eigen_assert(m_isInitialized &&
"LLT is not initialized.");
463 return matrixL() * matrixL().adjoint().toDenseMatrix();
469 template<
typename Derived>
479 template<
typename MatrixType,
unsigned int UpLo>
488 #endif // EIGEN_LLT_H