Stan Math Library  2.8.0
reverse mode automatic differentiation
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
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 
7 #include <stan/math/rev/core.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  (stan::math::ChainableStack::memalloc_
29  .alloc(sizeof(double) * A.rows() * A.cols()))),
30  _adjARef(reinterpret_cast<vari**>
31  (stan::math::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  stan::math::check_square("determinant", "m", m);
68  return var(new determinant_vari<R, C>(m));
69  }
70 
71  }
72 }
73 #endif
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
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:32
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
double * A_
Definition: determinant.hpp:19
AutodiffStackStorage< chainable, chainable_alloc > ChainableStack
fvar< T > determinant(const Eigen::Matrix< fvar< T >, R, C > &m)
Definition: determinant.hpp:21
vari ** _adjARef
Definition: determinant.hpp:20
int _rows
Definition: determinant.hpp:17
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
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.
int _cols
Definition: determinant.hpp:18

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