| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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/prim/mat/fun/Eigen.hpp>
5 #include <stan/math/prim/mat/err/check_square.hpp>
6 #include <stan/math/rev/core.hpp>
7
8 namespace stan {
9 namespace math {
10
11 template <int R, int C>
12 inline var log_determinant(const Eigen::Matrix<var, R, C>& m) {
13 using Eigen::Matrix;
14
15 math::check_square("log_determinant", "m", m);
16
17 Matrix<double, R, C> m_d(m.rows(), m.cols());
18 for (int i = 0; i < m.size(); ++i)
19 m_d(i) = m(i).val();
20
21 Eigen::FullPivHouseholderQR<Matrix<double, R, C> > hh
22 = m_d.fullPivHouseholderQr();
23
24 double val = hh.logAbsDeterminant();
25
26 vari** varis
27 = ChainableStack::memalloc_.alloc_array<vari*>(m.size());
28 for (int i = 0; i < m.size(); ++i)
29 varis[i] = m(i).vi_;
30
31 Matrix<double, R, C> m_inv_transpose = hh.inverse().transpose();
32 double* gradients
33 = ChainableStack::memalloc_.alloc_array<double>(m.size());
34 for (int i = 0; i < m.size(); ++i)
35 gradients[i] = m_inv_transpose(i);
36
37 return var(new precomputed_gradients_vari(val, m.size(),
38 varis, gradients));
39 }
40
41 }
42 }
43 #endif
core.hpp
stan
Definition: log_sum_exp.hpp:8
stan::math::AutodiffStackStorage::memalloc_
static stack_alloc memalloc_
Definition: autodiffstackstorage.hpp:16
stan::math::vari
The variable implementation base class.
Definition: vari.hpp:30
stan::math::log_determinant
fvar< T > log_determinant(const Eigen::Matrix< fvar< T >, R, C > &m)
Definition: log_determinant.hpp:20
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:30
stan::math::precomputed_gradients_vari
A variable implementation taking a sequence of operands and partial derivatives with respect to the o...
Definition: precomputed_gradients.hpp:20
Eigen.hpp
stan::math::stack_alloc::alloc_array
T * alloc_array(size_t n)
Allocate an array on the arena of the specified size to hold values of the specified template paramet...
Definition: stack_alloc.hpp:182
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
check_square.hpp

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