Stan Math Library  2.20.0
reverse mode automatic differentiation
LDLT_alloc.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_MAT_FUN_LDLT_ALLOC_HPP
2 #define STAN_MATH_REV_MAT_FUN_LDLT_ALLOC_HPP
3 
4 #include <stan/math/rev/meta.hpp>
6 #include <stan/math/rev/core.hpp>
7 
8 namespace stan {
9 namespace math {
20 template <int R, int C>
21 class LDLT_alloc : public chainable_alloc {
22  public:
23  LDLT_alloc() : N_(0) {}
24  explicit LDLT_alloc(const Eigen::Matrix<var, R, C> &A) : N_(0) { compute(A); }
25 
31  inline void compute(const Eigen::Matrix<var, R, C> &A) {
32  Eigen::Matrix<double, R, C> Ad(A.rows(), A.cols());
33 
34  N_ = A.rows();
35  variA_.resize(A.rows(), A.cols());
36 
37  for (size_t j = 0; j < N_; j++) {
38  for (size_t i = 0; i < N_; i++) {
39  Ad(i, j) = A(i, j).val();
40  variA_(i, j) = A(i, j).vi_;
41  }
42  }
43 
44  ldlt_.compute(Ad);
45  }
46 
47  // Compute the log(abs(det(A))). This is just a convenience function.
48  inline double log_abs_det() const {
49  return ldlt_.vectorD().array().log().sum();
50  }
51 
52  size_t N_;
53  Eigen::LDLT<Eigen::Matrix<double, R, C> > ldlt_;
54  Eigen::Matrix<vari *, R, C> variA_;
55 };
56 } // namespace math
57 } // namespace stan
58 #endif
Eigen::LDLT< Eigen::Matrix< double, R, C > > ldlt_
Definition: LDLT_alloc.hpp:53
Eigen::Matrix< vari *, R, C > variA_
Definition: LDLT_alloc.hpp:54
This object stores the actual (double typed) LDLT factorization of an Eigen::Matrix<var> along with p...
Definition: LDLT_alloc.hpp:21
LDLT_alloc(const Eigen::Matrix< var, R, C > &A)
Definition: LDLT_alloc.hpp:24
double log_abs_det() const
Definition: LDLT_alloc.hpp:48
A chainable_alloc is an object which is constructed and destructed normally but the memory lifespan i...
void compute(const Eigen::Matrix< var, R, C > &A)
Compute the LDLT factorization and store pointers to the vari&#39;s of the matrix entries to be used when...
Definition: LDLT_alloc.hpp:31

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