Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LDLT_factor.hpp
Go to the documentation of this file.
1 #ifndef STAN__MATH__MATRIX__LDLT_FACTOR_HPP
2 #define STAN__MATH__MATRIX__LDLT_FACTOR_HPP
3 
5 #include <boost/shared_ptr.hpp>
7 
8 namespace stan {
9  namespace math {
10 
11  // This class is conceptually similar to the corresponding Eigen class
12  // Any spd matrix A can be decomposed as LDL' where L is unit lower-triangular
13  // and D is diagonal with positive diagonal elements
14 
15  template<typename T, int R, int C>
16  class LDLT_factor;
17 
55  template<int R, int C, typename T>
56  class LDLT_factor<T,R,C> {
57  public:
59  : N_(0), _ldltP(new Eigen::LDLT< Eigen::Matrix<T,R,C> >()) {}
60 
61  LDLT_factor(const Eigen::Matrix<T,R,C> &A)
62  : N_(0), _ldltP(new Eigen::LDLT< Eigen::Matrix<T,R,C> >())
63  {
64  compute(A);
65  }
66 
67  inline void compute(const Eigen::Matrix<T,R,C> &A) {
68  stan::math::check_square("LDLT_factor(%1%)",A,"A",(T*)0);
69  N_ = A.rows();
70  _ldltP->compute(A);
71  }
72 
73  inline bool success() const {
74  bool ret;
75  ret = _ldltP->info() == Eigen::Success;
76  ret = ret && _ldltP->isPositive();
77  ret = ret && (_ldltP->vectorD().array() > 0).all();
78  return ret;
79  }
80 
81  inline T log_abs_det() const {
82  return _ldltP->vectorD().array().log().sum();
83  }
84 
85  inline void inverse(Eigen::Matrix<T,R,C> &invA) const {
86  invA.setIdentity(N_);
87  _ldltP->solveInPlace(invA);
88  }
89 
90  template<typename Rhs>
91  inline const Eigen::internal::solve_retval<Eigen::LDLT< Eigen::Matrix<T,R,C> >, Rhs>
92  solve(const Eigen::MatrixBase<Rhs>& b) const {
93  return _ldltP->solve(b);
94  }
95 
96  inline Eigen::Matrix<T,R,C> solveRight(const Eigen::Matrix<T,R,C> &B) const {
97  return _ldltP->solve(B.transpose()).transpose();
98  }
99 
100  inline Eigen::Matrix<T,Eigen::Dynamic,1> vectorD() const {
101  return _ldltP->vectorD();
102  }
103 
104  inline Eigen::LDLT<Eigen::Matrix<T,R,C> > matrixLDLT() const {
105  return _ldltP->matrixLDLT();
106  }
107 
108  inline size_t rows() const { return N_; }
109  inline size_t cols() const { return N_; }
110 
111  typedef size_t size_type;
112  typedef double value_type;
113 
114  size_t N_;
115  boost::shared_ptr< Eigen::LDLT< Eigen::Matrix<T,R,C> > > _ldltP;
116  };
117  }
118 }
119 #endif
void inverse(Eigen::Matrix< T, R, C > &invA) const
Definition: LDLT_factor.hpp:85
Eigen::Matrix< T, Eigen::Dynamic, 1 > vectorD() const
int N_
boost::shared_ptr< Eigen::LDLT< Eigen::Matrix< T, R, C > > > _ldltP
LDLT_factor(const Eigen::Matrix< T, R, C > &A)
Definition: LDLT_factor.hpp:61
Eigen::Matrix< T, C, R > transpose(const Eigen::Matrix< T, R, C > &m)
Definition: transpose.hpp:12
const Eigen::internal::solve_retval< Eigen::LDLT< Eigen::Matrix< T, R, C > >, Rhs > solve(const Eigen::MatrixBase< Rhs > &b) const
Definition: LDLT_factor.hpp:92
Eigen::LDLT< Eigen::Matrix< T, R, C > > matrixLDLT() const
boost::shared_ptr< Eigen::LDLT< Eigen::Matrix< double, R1, C1 > > > _ldltP
This share_ptr is used to prevent copying the LDLT factorizations for mdivide_left_ldlt(ldltA,b) when ldltA is a LDLT_factor<double>.
void compute(const Eigen::Matrix< T, R, C > &A)
Definition: LDLT_factor.hpp:67
Eigen::Matrix< T, R, C > solveRight(const Eigen::Matrix< T, R, C > &B) const
Definition: LDLT_factor.hpp:96
bool check_square(const char *function, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y, const char *name, T_result *result)
Return true if the specified matrix is square.

     [ Stan Home Page ] © 2011–2014, Stan Development Team.