Stan Math Library  2.11.0
reverse mode automatic differentiation
log_determinant_ldlt.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_MAT_FUN_LOG_DETERMINANT_LDLT_HPP
2 #define STAN_MATH_REV_MAT_FUN_LOG_DETERMINANT_LDLT_HPP
3 
5 #include <stan/math/rev/core.hpp>
8 
9 namespace stan {
10  namespace math {
11  namespace {
12 
22  template<int R, int C>
23  class log_det_ldlt_vari : public vari {
24  public:
25  explicit log_det_ldlt_vari(const stan::math::LDLT_factor<var, R, C> &A)
26  : vari(A._alloc->log_abs_det()), _alloc_ldlt(A._alloc)
27  { }
28 
29  virtual void chain() {
30  Eigen::Matrix<double, R, C> invA;
31 
32  // If we start computing Jacobians, this may be a bit inefficient
33  invA.setIdentity(_alloc_ldlt->N_, _alloc_ldlt->N_);
34  _alloc_ldlt->_ldlt.solveInPlace(invA);
35 
36  for (size_t j = 0; j < _alloc_ldlt->N_; j++) {
37  for (size_t i = 0; i < _alloc_ldlt->N_; i++) {
38  _alloc_ldlt->_variA(i, j)->adj_ += adj_ * invA(i, j);
39  }
40  }
41  }
42 
43  const LDLT_alloc<R, C> *_alloc_ldlt;
44  };
45  }
46 
47  template<int R, int C>
49  return var(new log_det_ldlt_vari<R, C>(A));
50  }
51  }
52 }
53 #endif
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:31
mdivide_left_ldlt_alloc< R1, C1, R2, C2 > * _alloc
const LDLT_alloc< R, C > * _alloc_ldlt
T log_determinant_ldlt(stan::math::LDLT_factor< T, R, C > &A)

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