Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
log_determinant.hpp
Go to the documentation of this file.
1 #ifndef STAN__AGRAD__REV__MATRIX__LOG_DETERMINANT_HPP
2 #define STAN__AGRAD__REV__MATRIX__LOG_DETERMINANT_HPP
3 
4 #include <vector>
7 #include <stan/agrad/rev/var.hpp>
10 
11 // FIXME: use explicit files
12 #include <stan/agrad/rev.hpp>
13 
14 namespace stan {
15  namespace agrad {
16 
17  namespace {
18  template<int R,int C>
19  class log_determinant_vari : public vari {
20  int _rows;
21  int _cols;
22  double* A_;
23  vari** _adjARef;
24  public:
25  log_determinant_vari(const Eigen::Matrix<var,R,C> &A)
26  : vari(log_determinant_vari_calc(A)),
27  _rows(A.rows()),
28  _cols(A.cols()),
29  A_((double*)stan::agrad::memalloc_.alloc(sizeof(double)
30  * A.rows() * A.cols())),
31  _adjARef((vari**)stan::agrad::memalloc_.alloc(sizeof(vari*)
32  * A.rows() * A.cols()))
33  {
34  size_t pos = 0;
35  for (size_type j = 0; j < _cols; j++) {
36  for (size_type i = 0; i < _rows; i++) {
37  A_[pos] = A(i,j).val();
38  _adjARef[pos++] = A(i,j).vi_;
39  }
40  }
41  }
42  static
43  double log_determinant_vari_calc(const Eigen::Matrix<var,R,C> &A)
44  {
45  Eigen::Matrix<double,R,C> Ad(A.rows(),A.cols());
46  for (size_type j = 0; j < A.cols(); j++)
47  for (size_type i = 0; i < A.rows(); i++)
48  Ad(i,j) = A(i,j).val();
49  return Ad.fullPivHouseholderQr().logAbsDeterminant();
50  }
51  virtual void chain() {
52  using Eigen::Matrix;
53  using Eigen::Map;
54  Matrix<double,R,C> adjA(_rows,_cols);
55  adjA = adj_
56  * Map<Matrix<double,R,C> >(A_,_rows,_cols)
57  .inverse().transpose();
58  size_t pos = 0;
59  for (size_type j = 0; j < _cols; j++) {
60  for (size_type i = 0; i < _rows; i++) {
61  _adjARef[pos++]->adj_ += adjA(i,j);
62  }
63  }
64  }
65  };
66  }
67 
68  template <int R, int C>
69  inline var log_determinant(const Eigen::Matrix<var,R,C>& m) {
70  stan::math::check_square("log_determinant(%1%)",m,"m",(double*)0);
71  return var(new log_determinant_vari<R,C>(m));
72  }
73 
74  }
75 }
76 #endif
vari ** _adjARef
Eigen::Matrix< fvar< T >, R, C > inverse(const Eigen::Matrix< fvar< T >, R, C > &m)
Definition: inverse.hpp:21
int _rows
memory::stack_alloc memalloc_
Definition: var_stack.cpp:16
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic >::Index size_type
Definition: typedefs.hpp:14
size_t rows(const Eigen::Matrix< T, R, C > &m)
Definition: rows.hpp:12
size_t cols(const Eigen::Matrix< T, R, C > &m)
Definition: cols.hpp:12
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:27
int _cols
double * A_
fvar< T > log_determinant(const Eigen::Matrix< fvar< T >, R, C > &m)
bool check_square(const char *function, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y, const char *name, T_result *result)
Return true if the specified matrix is square.

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