| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

determinant.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_MAT_FUN_DETERMINANT_HPP
2 #define STAN_MATH_REV_MAT_FUN_DETERMINANT_HPP
3
4 #include <stan/math/prim/mat/err/check_square.hpp>
5 #include <stan/math/prim/mat/fun/Eigen.hpp>
6 #include <stan/math/prim/mat/fun/typedefs.hpp>
7 #include <stan/math/rev/core.hpp>
8 #include <stan/math/rev/mat/fun/typedefs.hpp>
9 #include <vector>
10
11 namespace stan {
12 namespace math {
13
14 namespace {
15 template<int R, int C>
16 class determinant_vari : public vari {
17 int rows_;
18 int cols_;
19 double* A_;
20 vari** adjARef_;
21
22 public:
23 explicit determinant_vari(const Eigen::Matrix<var, R, C> &A)
24 : vari(determinant_vari_calc(A)),
25 rows_(A.rows()),
26 cols_(A.cols()),
27 A_(reinterpret_cast<double*>
28 (ChainableStack::memalloc_
29 .alloc(sizeof(double) * A.rows() * A.cols()))),
30 adjARef_(reinterpret_cast<vari**>
31 (ChainableStack::memalloc_
32 .alloc(sizeof(vari*) * A.rows() * A.cols()))) {
33 size_t pos = 0;
34 for (size_type j = 0; j < cols_; j++) {
35 for (size_type i = 0; i < rows_; i++) {
36 A_[pos] = A(i, j).val();
37 adjARef_[pos++] = A(i, j).vi_;
38 }
39 }
40 }
41 static
42 double determinant_vari_calc(const Eigen::Matrix<var, R, C> &A) {
43 Eigen::Matrix<double, R, C> Ad(A.rows(), A.cols());
44 for (size_type j = 0; j < A.rows(); j++)
45 for (size_type i = 0; i < A.cols(); i++)
46 Ad(i, j) = A(i, j).val();
47 return Ad.determinant();
48 }
49 virtual void chain() {
50 using Eigen::Matrix;
51 using Eigen::Map;
52 Matrix<double, R, C> adjA(rows_, cols_);
53 adjA = (adj_ * val_) *
54 Map<Matrix<double, R, C> >(A_, rows_, cols_).inverse().transpose();
55 size_t pos = 0;
56 for (size_type j = 0; j < cols_; j++) {
57 for (size_type i = 0; i < rows_; i++) {
58 adjARef_[pos++]->adj_ += adjA(i, j);
59 }
60 }
61 }
62 };
63 }
64
65 template <int R, int C>
66 inline var determinant(const Eigen::Matrix<var, R, C>& m) {
67 check_square("determinant", "m", m);
68 return var(new determinant_vari<R, C>(m));
69 }
70
71 }
72 }
73 #endif
stan::math::rows
int rows(const Eigen::Matrix< T, R, C > &m)
Return the number of rows in the specified matrix, vector, or row vector.
Definition: rows.hpp:20
core.hpp
stan
Definition: log_sum_exp.hpp:8
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:30
stan::math::size_type
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic >::Index size_type
Type for sizes and indexes in an Eigen matrix with double e.
Definition: typedefs.hpp:13
A_
double * A_
Definition: determinant.hpp:19
rows_
int rows_
Definition: determinant.hpp:17
typedefs.hpp
stan::math::determinant
fvar< T > determinant(const Eigen::Matrix< fvar< T >, R, C > &m)
Definition: determinant.hpp:21
adjARef_
vari ** adjARef_
Definition: determinant.hpp:20
stan::math::cols
int cols(const Eigen::Matrix< T, R, C > &m)
Return the number of columns in the specified matrix, vector, or row vector.
Definition: cols.hpp:20
Eigen.hpp
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::ChainableStack
AutodiffStackStorage< vari, chainable_alloc > ChainableStack
Definition: chainablestack.hpp:10
cols_
int cols_
Definition: determinant.hpp:18
typedefs.hpp
check_square.hpp

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