Stan Math Library  2.15.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 {
96  return ldltP_->vectorD().array().log().sum();
97  }
98 
99  inline void inverse(matrix_t& invA) const {
100  invA.setIdentity(N_);
101  ldltP_->solveInPlace(invA);
102  }
103 
104 #if EIGEN_VERSION_AT_LEAST(3, 3, 0)
105  template <typename Rhs>
106  inline const Eigen::Solve<ldlt_t, Rhs>
107  solve(const Eigen::MatrixBase<Rhs>& b) const {
108  return ldltP_->solve(b);
109  }
110 #else
111  template <typename Rhs>
112  inline const Eigen::internal::solve_retval<ldlt_t, Rhs>
113  solve(const Eigen::MatrixBase<Rhs>& b) const {
114  return ldltP_->solve(b);
115  }
116 #endif
117 
118  inline matrix_t solveRight(const matrix_t& B) const {
119  return ldltP_->solve(B.transpose()).transpose();
120  }
121 
122  inline vector_t vectorD() const {
123  return ldltP_->vectorD();
124  }
125 
126  inline ldlt_t matrixLDLT() const {
127  return ldltP_->matrixLDLT();
128  }
129 
130  inline size_t rows() const { return N_; }
131  inline size_t cols() const { return N_; }
132 
133 
134  size_t N_;
135  boost::shared_ptr<ldlt_t> ldltP_;
136  };
137 
138  }
139 }
140 #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 inverse(matrix_t &invA) const
Definition: LDLT_factor.hpp:99
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_
void check_square(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Check if the specified matrix is square.
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:21
Eigen::LDLT< matrix_t > ldlt_t
Definition: LDLT_factor.hpp:67
Eigen::Matrix< T, C, R > transpose(const Eigen::Matrix< T, R, C > &m)
Definition: transpose.hpp:12

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