Public Types | Public Member Functions | Public Attributes | List of all members
stan::math::LDLT_factor< T, R, C > Class Template Reference

#include "stan/math/prim/mat/fun/LDLT_factor.hpp"

Public Types

typedef Eigen::Matrix< T, Eigen::Dynamic, 1 > vector_t
 
typedef Eigen::Matrix< T, R, Cmatrix_t
 
typedef Eigen::LDLT< matrix_tldlt_t
 
typedef size_t size_type
 
typedef double value_type
 

Public Member Functions

 LDLT_factor ()
 
 LDLT_factor (const matrix_t &A)
 
void compute (const matrix_t &A)
 
bool success () const
 
T log_abs_det () const
 
void inverse (matrix_t &invA) const
 
template<typename Rhs >
const Eigen::internal::solve_retval< ldlt_t, Rhs > solve (const Eigen::MatrixBase< Rhs > &b) const
 
matrix_t solveRight (const matrix_t &B) const
 
vector_t vectorD () const
 
ldlt_t matrixLDLT () const
 
size_t rows () const
 
size_t cols () const
 

Public Attributes

size_t N_
 
boost::shared_ptr< ldlt_tldltP_
 

Detailed Description

template<typename T, int R, int C>
class stan::math::LDLT_factor< T, R, C >

LDLT_factor is a thin wrapper on Eigen::LDLT to allow for reusing factorizations and efficient autodiff of things like log determinants and solutions to linear systems.

Memory is allocated in the constructor and stored in a boost::shared_ptr, which ensures that is freed when the object is released.

After the constructor and/or compute() is called users of LDLT_factor are responsible for calling success() to check whether the factorization has succeeded. Use of an LDLT_factor object (e.g., in mdivide_left_ldlt) is undefined if success() is false.

It's usage pattern is:

Eigen::Matrix<T, R, C> A1, A2;
LDLT_factor<T, R, C> ldlt_A1(A1);
LDLT_factor<T, R, C> ldlt_A2;
ldlt_A2.compute(A2);

The caller should check that ldlt_A1.success() and ldlt_A2.success() are true or abort accordingly. Alternatively, call check_ldlt_factor().

Note that ldlt_A1 and ldlt_A2 are completely equivalent. They simply demonstrate two different ways to construct the factorization.

The caller can use the LDLT_factor objects as needed. For instance

x1 = mdivide_left_ldlt(ldlt_A1, b1);
x2 = mdivide_right_ldlt(b2, ldlt_A2);
d1 = log_determinant_ldlt(ldlt_A1);
d2 = log_determinant_ldlt(ldlt_A2);

This class is conceptually similar to the corresponding Eigen class. Any symmetric, positive-definite matrix A can be decomposed as LDL' where L is unit lower-triangular and D is diagonal with positive diagonal elements.

Template Parameters
Tscalare type held in the matrix
Rrows (as in Eigen)
Ccolumns (as in Eigen)

Definition at line 63 of file LDLT_factor.hpp.

Member Typedef Documentation

template<typename T, int R, int C>
typedef Eigen::LDLT<matrix_t> stan::math::LDLT_factor< T, R, C >::ldlt_t

Definition at line 67 of file LDLT_factor.hpp.

template<typename T, int R, int C>
typedef Eigen::Matrix<T, R, C> stan::math::LDLT_factor< T, R, C >::matrix_t

Definition at line 66 of file LDLT_factor.hpp.

template<typename T, int R, int C>
typedef size_t stan::math::LDLT_factor< T, R, C >::size_type

Definition at line 68 of file LDLT_factor.hpp.

template<typename T, int R, int C>
typedef double stan::math::LDLT_factor< T, R, C >::value_type

Definition at line 69 of file LDLT_factor.hpp.

template<typename T, int R, int C>
typedef Eigen::Matrix<T, Eigen::Dynamic, 1> stan::math::LDLT_factor< T, R, C >::vector_t

Definition at line 65 of file LDLT_factor.hpp.

Constructor & Destructor Documentation

template<typename T, int R, int C>
stan::math::LDLT_factor< T, R, C >::LDLT_factor ( )
inline

Definition at line 71 of file LDLT_factor.hpp.

71 : N_(0), ldltP_(new ldlt_t()) {}
Eigen::LDLT< matrix_t > ldlt_t
Definition: LDLT_factor.hpp:67
boost::shared_ptr< ldlt_t > ldltP_
template<typename T, int R, int C>
stan::math::LDLT_factor< T, R, C >::LDLT_factor ( const matrix_t A)
inlineexplicit

Definition at line 73 of file LDLT_factor.hpp.

References stan::math::LDLT_factor< T, R, C >::compute().

73  : N_(0), ldltP_(new ldlt_t()) {
74  compute(A);
75  }
Eigen::LDLT< matrix_t > ldlt_t
Definition: LDLT_factor.hpp:67
void compute(const matrix_t &A)
Definition: LDLT_factor.hpp:77
boost::shared_ptr< ldlt_t > ldltP_
static const double A
Definition: Units.h:82

Member Function Documentation

template<typename T, int R, int C>
size_t stan::math::LDLT_factor< T, R, C >::cols ( ) const
inline

Definition at line 125 of file LDLT_factor.hpp.

References stan::math::LDLT_factor< T, R, C >::N_.

125 { return N_; }
template<typename T, int R, int C>
void stan::math::LDLT_factor< T, R, C >::compute ( const matrix_t A)
inline

Definition at line 77 of file LDLT_factor.hpp.

References stan::math::check_square(), stan::math::LDLT_factor< T, R, C >::ldltP_, and stan::math::LDLT_factor< T, R, C >::N_.

Referenced by stan::math::LDLT_factor< var, R, C >::LDLT_factor(), and stan::math::LDLT_factor< T, R, C >::LDLT_factor().

77  {
78  check_square("LDLT_factor", "A", A);
79  N_ = A.rows();
80  ldltP_->compute(A);
81  }
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)
static const double A
Definition: Units.h:82
template<typename T, int R, int C>
void stan::math::LDLT_factor< T, R, C >::inverse ( matrix_t invA) const
inline

Definition at line 97 of file LDLT_factor.hpp.

References b, stan::math::LDLT_factor< T, R, C >::ldltP_, stan::math::LDLT_factor< T, R, C >::N_, and stan::math::LDLT_factor< T, R, C >::solve().

97  {
98  invA.setIdentity(N_);
99  ldltP_->solveInPlace(invA);
100  }
boost::shared_ptr< ldlt_t > ldltP_
template<typename T, int R, int C>
T stan::math::LDLT_factor< T, R, C >::log_abs_det ( ) const
inline

Definition at line 95 of file LDLT_factor.hpp.

References stan::math::LDLT_factor< T, R, C >::ldltP_.

Referenced by stan::math::log_determinant_ldlt().

95 { return ldltP_->vectorD().array().log().sum(); }
boost::shared_ptr< ldlt_t > ldltP_
template<typename T, int R, int C>
ldlt_t stan::math::LDLT_factor< T, R, C >::matrixLDLT ( ) const
inline

Definition at line 122 of file LDLT_factor.hpp.

References stan::math::LDLT_factor< T, R, C >::ldltP_.

122 { return ldltP_->matrixLDLT(); }
boost::shared_ptr< ldlt_t > ldltP_
template<typename T, int R, int C>
size_t stan::math::LDLT_factor< T, R, C >::rows ( ) const
inline

Definition at line 124 of file LDLT_factor.hpp.

References stan::math::LDLT_factor< T, R, C >::N_.

124 { return N_; }
template<typename T, int R, int C>
template<typename Rhs >
const Eigen::internal::solve_retval<ldlt_t, Rhs> stan::math::LDLT_factor< T, R, C >::solve ( const Eigen::MatrixBase< Rhs > &  b) const
inline

Definition at line 110 of file LDLT_factor.hpp.

References stan::math::LDLT_factor< T, R, C >::ldltP_.

Referenced by stan::math::LDLT_factor< var, R, C >::compute(), stan::math::LDLT_factor< T, R, C >::inverse(), and stan::math::mdivide_left_ldlt().

111  {
112  return ldltP_->solve(b);
113  }
boost::shared_ptr< ldlt_t > ldltP_
const hit & b
Definition: hits.cxx:21
template<typename T, int R, int C>
matrix_t stan::math::LDLT_factor< T, R, C >::solveRight ( const matrix_t B) const
inline

Definition at line 116 of file LDLT_factor.hpp.

References stan::math::LDLT_factor< T, R, C >::ldltP_, and stan::math::transpose().

Referenced by stan::math::mdivide_right_ldlt().

116  {
117  return ldltP_->solve(B.transpose()).transpose();
118  }
boost::shared_ptr< ldlt_t > ldltP_
Eigen::Matrix< T, C, R > transpose(const Eigen::Matrix< T, R, C > &m)
Definition: transpose.hpp:10
template<typename T, int R, int C>
bool stan::math::LDLT_factor< T, R, C >::success ( ) const
inline

Definition at line 83 of file LDLT_factor.hpp.

References MECModelEnuComparisons::i, stan::math::is_nan(), and stan::math::LDLT_factor< T, R, C >::ldltP_.

Referenced by stan::math::check_ldlt_factor().

83  {
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  }
Eigen::Matrix< T, Eigen::Dynamic, 1 > vector_t
Definition: LDLT_factor.hpp:65
boost::shared_ptr< ldlt_t > ldltP_
int is_nan(const fvar< T > &x)
Definition: is_nan.hpp:19
template<typename T, int R, int C>
vector_t stan::math::LDLT_factor< T, R, C >::vectorD ( ) const
inline

Definition at line 120 of file LDLT_factor.hpp.

References stan::math::LDLT_factor< T, R, C >::ldltP_.

Referenced by stan::math::check_ldlt_factor().

120 { return ldltP_->vectorD(); }
boost::shared_ptr< ldlt_t > ldltP_

Member Data Documentation

template<typename T, int R, int C>
boost::shared_ptr<ldlt_t> stan::math::LDLT_factor< T, R, C >::ldltP_
template<typename T, int R, int C>
size_t stan::math::LDLT_factor< T, R, C >::N_

The documentation for this class was generated from the following file: