Stan Math Library  2.12.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 
5 #include <boost/shared_ptr.hpp>
8 
9 namespace stan {
10  namespace math {
11 
12  template<typename T, int R, int C>
13  class LDLT_factor;
14 
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  explicit LDLT_factor(const Eigen::Matrix<T, R, C> &A)
62  : N_(0), ldltP_(new Eigen::LDLT< Eigen::Matrix<T, R, C> >()) {
63  compute(A);
64  }
65 
66  inline void compute(const Eigen::Matrix<T, R, C> &A) {
67  check_square("LDLT_factor", "A", A);
68  N_ = A.rows();
69  ldltP_->compute(A);
70  }
71 
72  inline bool success() const {
73  if (ldltP_->info() != Eigen::Success)
74  return false;
75  if (!(ldltP_->isPositive()))
76  return false;
77  Eigen::Matrix<T, Eigen::Dynamic, 1> ldltP_diag(ldltP_->vectorD());
78  for (int i = 0; i < ldltP_diag.size(); ++i)
79  if (ldltP_diag(i) <= 0 || is_nan(ldltP_diag(i)))
80  return false;
81  return true;
82  }
83 
84  inline T log_abs_det() const {
85  return ldltP_->vectorD().array().log().sum();
86  }
87 
88  inline void inverse(Eigen::Matrix<T, R, C> &invA) const {
89  invA.setIdentity(N_);
90  ldltP_->solveInPlace(invA);
91  }
92 
93  template<typename Rhs>
94  inline const
95  Eigen::internal::solve_retval<Eigen::LDLT< Eigen::Matrix<T, R, C> >, Rhs>
96  solve(const Eigen::MatrixBase<Rhs>& b) const {
97  return ldltP_->solve(b);
98  }
99 
100  inline Eigen::Matrix<T, R, C>
101  solveRight(const Eigen::Matrix<T, R, C> &B) const {
102  return ldltP_->solve(B.transpose()).transpose();
103  }
104 
105  inline Eigen::Matrix<T, Eigen::Dynamic, 1> vectorD() const {
106  return ldltP_->vectorD();
107  }
108 
109  inline Eigen::LDLT<Eigen::Matrix<T, R, C> > matrixLDLT() const {
110  return ldltP_->matrixLDLT();
111  }
112 
113  inline size_t rows() const { return N_; }
114  inline size_t cols() const { return N_; }
115 
116  typedef size_t size_type;
117  typedef double value_type;
118 
119  size_t N_;
120  boost::shared_ptr< Eigen::LDLT< Eigen::Matrix<T, R, C> > > ldltP_;
121  };
122 
123  }
124 }
125 #endif
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.
void inverse(Eigen::Matrix< T, R, C > &invA) const
Definition: LDLT_factor.hpp:88
const Eigen::internal::solve_retval< Eigen::LDLT< Eigen::Matrix< T, R, C > >, Rhs > solve(const Eigen::MatrixBase< Rhs > &b) const
Definition: LDLT_factor.hpp:96
LDLT_factor(const Eigen::Matrix< T, R, C > &A)
Definition: LDLT_factor.hpp:61
Eigen::Matrix< T, R, C > solveRight(const Eigen::Matrix< T, R, C > &B) const
(Expert) Numerical traits for algorithmic differentiation variables.
Eigen::LDLT< Eigen::Matrix< T, R, C > > matrixLDLT() const
Eigen::Matrix< T, Eigen::Dynamic, 1 > vectorD() const
boost::shared_ptr< Eigen::LDLT< Eigen::Matrix< T, R, C > > > ldltP_
void compute(const Eigen::Matrix< T, R, C > &A)
Definition: LDLT_factor.hpp:66
int is_nan(const fvar< T > &x)
Returns 1 if the input's value is NaN and 0 otherwise.
Definition: is_nan.hpp:21
Eigen::Matrix< T, C, R > transpose(const Eigen::Matrix< T, R, C > &m)
Definition: transpose.hpp:12
bool check_square(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Return true if the specified matrix is square.
int N_

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