Stan Math Library  2.20.0
reverse mode automatic differentiation
log_determinant.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_MAT_FUN_LOG_DETERMINANT_HPP
2 #define STAN_MATH_REV_MAT_FUN_LOG_DETERMINANT_HPP
3 
4 #include <stan/math/rev/meta.hpp>
7 #include <stan/math/rev/core.hpp>
8 
9 namespace stan {
10 namespace math {
11 
12 template <int R, int C>
13 inline var log_determinant(const Eigen::Matrix<var, R, C>& m) {
14  using Eigen::Matrix;
15 
16  math::check_square("log_determinant", "m", m);
17 
18  Matrix<double, R, C> m_d(m.rows(), m.cols());
19  for (int i = 0; i < m.size(); ++i)
20  m_d(i) = m(i).val();
21 
22  Eigen::FullPivHouseholderQR<Matrix<double, R, C> > hh
23  = m_d.fullPivHouseholderQr();
24 
25  double val = hh.logAbsDeterminant();
26 
27  vari** varis
29  for (int i = 0; i < m.size(); ++i)
30  varis[i] = m(i).vi_;
31 
32  Matrix<double, R, C> m_inv_transpose = hh.inverse().transpose();
33  double* gradients
34  = ChainableStack::instance_->memalloc_.alloc_array<double>(m.size());
35  for (int i = 0; i < m.size(); ++i)
36  gradients[i] = m_inv_transpose(i);
37 
38  return var(new precomputed_gradients_vari(val, m.size(), varis, gradients));
39 }
40 
41 } // namespace math
42 } // namespace stan
43 #endif
void check_square(const char *function, const char *name, const matrix_cl &y)
Check if the matrix_cl is square.
The variable implementation base class.
Definition: vari.hpp:30
static STAN_THREADS_DEF AutodiffStackStorage * instance_
fvar< T > log_determinant(const Eigen::Matrix< fvar< T >, R, C > &m)
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:33
A variable implementation taking a sequence of operands and partial derivatives with respect to the o...
T * alloc_array(size_t n)
Allocate an array on the arena of the specified size to hold values of the specified template paramet...

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