Stan Math Library: stan/math/prim/mat/fun/LDLT_factor.hpp Source File

| Stan Math Library 2.10.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
4 #include <stan/math/prim/mat/fun/Eigen.hpp>
5 #include <boost/shared_ptr.hpp>
6 #include <stan/math/prim/mat/err/check_square.hpp>
7 #include <stan/math/prim/scal/fun/is_nan.hpp>
8
9 namespace stan {
10
11 namespace math {
12
13 // This class is conceptually similar to the corresponding Eigen class
14 // Any spd matrix A can be decomposed as LDL' where L is unit
15 // lower-triangular and D is diagonal with positive diagonal elements
16
17 template<typename T, int R, int C>
18 class LDLT_factor;
19
57 template<int R, int C, typename T>
58 class LDLT_factor<T, R, C> {
59 public:
60 LDLT_factor()
61 : N_(0), _ldltP(new Eigen::LDLT< Eigen::Matrix<T, R, C> >()) {}
62
63 explicit LDLT_factor(const Eigen::Matrix<T, R, C> &A)
64 : N_(0), _ldltP(new Eigen::LDLT< Eigen::Matrix<T, R, C> >()) {
65 compute(A);
66 }
67
68 inline void compute(const Eigen::Matrix<T, R, C> &A) {
69 stan::math::check_square("LDLT_factor", "A", A);
70 N_ = A.rows();
71 _ldltP->compute(A);
72 }
73
74 inline bool success() const {
75 using stan::math::is_nan;
76 // bool ret;
77 // ret = _ldltP->info() == Eigen::Success;
78 // ret = ret && _ldltP->isPositive();
79 // ret = ret && (_ldltP->vectorD().array() > 0).all();
80 // return ret;
81
82 if (_ldltP->info() != Eigen::Success)
83 return false;
84 if (!(_ldltP->isPositive()))
85 return false;
86 Eigen::Matrix<T, Eigen::Dynamic, 1> ldltP_diag(_ldltP->vectorD());
87 for (int i = 0; i < ldltP_diag.size(); ++i)
88 if (ldltP_diag(i) <= 0 || is_nan(ldltP_diag(i)))
89 return false;
90 return true;
91 }
92
93 inline T log_abs_det() const {
94 return _ldltP->vectorD().array().log().sum();
95 }
96
97 inline void inverse(Eigen::Matrix<T, R, C> &invA) const {
98 invA.setIdentity(N_);
99 _ldltP->solveInPlace(invA);
100 }
101
102 template<typename Rhs>
103 inline const
104 Eigen::internal::solve_retval<Eigen::LDLT< Eigen::Matrix<T, R, C> >, Rhs>
105 solve(const Eigen::MatrixBase<Rhs>& b) const {
106 return _ldltP->solve(b);
107 }
108
109 inline Eigen::Matrix<T, R, C>
110 solveRight(const Eigen::Matrix<T, R, C> &B) const {
111 return _ldltP->solve(B.transpose()).transpose();
112 }
113
114 inline Eigen::Matrix<T, Eigen::Dynamic, 1> vectorD() const {
115 return _ldltP->vectorD();
116 }
117
118 inline Eigen::LDLT<Eigen::Matrix<T, R, C> > matrixLDLT() const {
119 return _ldltP->matrixLDLT();
120 }
121
122 inline size_t rows() const { return N_; }
123 inline size_t cols() const { return N_; }
124
125 typedef size_t size_type;
126 typedef double value_type;
127
128 size_t N_;
129 boost::shared_ptr< Eigen::LDLT< Eigen::Matrix<T, R, C> > > _ldltP;
130 };
131 }
132 }
133 #endif
stan::math::LDLT_factor< T, R, C >::inverse
void inverse(Eigen::Matrix< T, R, C > &invA) const
Definition: LDLT_factor.hpp:97
stan::math::LDLT_factor< T, R, C >::solve
const Eigen::internal::solve_retval< Eigen::LDLT< Eigen::Matrix< T, R, C > >, Rhs > solve(const Eigen::MatrixBase< Rhs > &b) const
Definition: LDLT_factor.hpp:105
stan::math::LDLT_factor< T, R, C >::value_type
double value_type
Definition: LDLT_factor.hpp:126
is_nan.hpp
stan
Definition: log_sum_exp.hpp:8
stan::math::LDLT_factor< T, R, C >::_ldltP
boost::shared_ptr< Eigen::LDLT< Eigen::Matrix< T, R, C > > > _ldltP
Definition: LDLT_factor.hpp:129
stan::math::LDLT_factor< T, R, C >::LDLT_factor
LDLT_factor()
Definition: LDLT_factor.hpp:60
stan::math::LDLT_factor< T, R, C >::LDLT_factor
LDLT_factor(const Eigen::Matrix< T, R, C > &A)
Definition: LDLT_factor.hpp:63
stan::math::LDLT_factor< T, R, C >::solveRight
Eigen::Matrix< T, R, C > solveRight(const Eigen::Matrix< T, R, C > &B) const
Definition: LDLT_factor.hpp:110
Eigen
(Expert) Numerical traits for algorithmic differentiation variables.
Definition: Eigen_NumTraits.hpp:8
stan::math::LDLT_factor
Definition: LDLT_factor.hpp:18
stan::math::LDLT_factor< T, R, C >::success
bool success() const
Definition: LDLT_factor.hpp:74
stan::math::LDLT_factor< T, R, C >::matrixLDLT
Eigen::LDLT< Eigen::Matrix< T, R, C > > matrixLDLT() const
Definition: LDLT_factor.hpp:118
_ldltP
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.
Definition: mdivide_left_ldlt.hpp:23
stan::math::LDLT_factor< T, R, C >::vectorD
Eigen::Matrix< T, Eigen::Dynamic, 1 > vectorD() const
Definition: LDLT_factor.hpp:114
stan::math::LDLT_factor< T, R, C >::log_abs_det
T log_abs_det() const
Definition: LDLT_factor.hpp:93
stan::math::LDLT_factor< T, R, C >::N_
size_t N_
Definition: LDLT_factor.hpp:128
Eigen.hpp
stan::math::LDLT_factor< T, R, C >::size_type
size_t size_type
Definition: LDLT_factor.hpp:125
stan::math::LDLT_factor< T, R, C >::rows
size_t rows() const
Definition: LDLT_factor.hpp:122
stan::math::LDLT_factor< T, R, C >::compute
void compute(const Eigen::Matrix< T, R, C > &A)
Definition: LDLT_factor.hpp:68
stan::math::is_nan
int is_nan(const fvar< T > &x)
Returns 1 if the input's value is NaN and 0 otherwise.
Definition: is_nan.hpp:22
stan::math::transpose
Eigen::Matrix< T, C, R > transpose(const Eigen::Matrix< T, R, C > &m)
Definition: transpose.hpp:12
stan::math::check_square
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.
Definition: check_square.hpp:28
stan::math::LDLT_factor< T, R, C >::cols
size_t cols() const
Definition: LDLT_factor.hpp:123
N_
int N_
Definition: mdivide_left.hpp:20
check_square.hpp

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