Stan Math Library  2.20.0
reverse mode automatic differentiation
LDLT_factor.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_LDLT_FACTOR_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_LDLT_FACTOR_HPP
3 
7 #include <boost/shared_ptr.hpp>
8 
9 namespace stan {
10 namespace math {
11 
62 template <typename T, int R, int C>
63 class LDLT_factor {
64  public:
65  typedef Eigen::Matrix<T, Eigen::Dynamic, 1> vector_t;
66  typedef Eigen::Matrix<T, R, C> matrix_t;
67  typedef Eigen::LDLT<matrix_t> ldlt_t;
68  typedef size_t size_type;
69  typedef double value_type;
70 
71  LDLT_factor() : N_(0), ldltP_(new ldlt_t()) {}
72 
73  explicit LDLT_factor(const matrix_t& A) : N_(0), ldltP_(new ldlt_t()) {
74  compute(A);
75  }
76 
77  inline void compute(const matrix_t& A) {
78  check_square("LDLT_factor", "A", A);
79  N_ = A.rows();
80  ldltP_->compute(A);
81  }
82 
83  inline bool success() const {
84  if (ldltP_->info() != Eigen::Success)
85  return false;
86  if (!(ldltP_->isPositive()))
87  return false;
88  vector_t ldltP_diag(ldltP_->vectorD());
89  for (int i = 0; i < ldltP_diag.size(); ++i)
90  if (ldltP_diag(i) <= 0 || is_nan(ldltP_diag(i)))
91  return false;
92  return true;
93  }
94 
95  inline T log_abs_det() const { return ldltP_->vectorD().array().log().sum(); }
96 
97  inline void inverse(matrix_t& invA) const {
98  invA.setIdentity(N_);
99  ldltP_->solveInPlace(invA);
100  }
101 
102 #if EIGEN_VERSION_AT_LEAST(3, 3, 0)
103  template <typename Rhs>
104  inline const Eigen::Solve<ldlt_t, Rhs> solve(
105  const Eigen::MatrixBase<Rhs>& b) const {
106  return ldltP_->solve(b);
107  }
108 #else
109  template <typename Rhs>
110  inline const Eigen::internal::solve_retval<ldlt_t, Rhs> solve(
111  const Eigen::MatrixBase<Rhs>& b) const {
112  return ldltP_->solve(b);
113  }
114 #endif
115 
116  inline matrix_t solveRight(const matrix_t& B) const {
117  return ldltP_->solve(B.transpose()).transpose();
118  }
119 
120  inline vector_t vectorD() const { return ldltP_->vectorD(); }
121 
122  inline ldlt_t matrixLDLT() const { return ldltP_->matrixLDLT(); }
123 
124  inline size_t rows() const { return N_; }
125  inline size_t cols() const { return N_; }
126 
127  size_t N_;
128  boost::shared_ptr<ldlt_t> ldltP_;
129 };
130 
131 } // namespace math
132 } // namespace stan
133 #endif
ldlt_t matrixLDLT() const
Eigen::Matrix< T, R, C > matrix_t
Definition: LDLT_factor.hpp:66
matrix_t solveRight(const matrix_t &B) const
void check_square(const char *function, const char *name, const matrix_cl &y)
Check if the matrix_cl is square.
void inverse(matrix_t &invA) const
Definition: LDLT_factor.hpp:97
const Eigen::internal::solve_retval< ldlt_t, Rhs > solve(const Eigen::MatrixBase< Rhs > &b) const
void compute(const matrix_t &A)
Definition: LDLT_factor.hpp:77
LDLT_factor is a thin wrapper on Eigen::LDLT to allow for reusing factorizations and efficient autodi...
Definition: LDLT_factor.hpp:63
LDLT_factor(const matrix_t &A)
Definition: LDLT_factor.hpp:73
boost::shared_ptr< ldlt_t > ldltP_
matrix_cl transpose(const matrix_cl &src)
Takes the transpose of the matrix on the OpenCL device.
Definition: transpose.hpp:20
Eigen::Matrix< T, Eigen::Dynamic, 1 > vector_t
Definition: LDLT_factor.hpp:65
vector_t vectorD() const
int is_nan(const fvar< T > &x)
Returns 1 if the input&#39;s value is NaN and 0 otherwise.
Definition: is_nan.hpp:20
Eigen::LDLT< matrix_t > ldlt_t
Definition: LDLT_factor.hpp:67

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