Stan Math Library  2.20.0
reverse mode automatic differentiation
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/rev/meta.hpp>
8 #include <stan/math/rev/core.hpp>
9 
10 namespace stan {
11 namespace math {
12 
13 namespace internal {
14 template <int R, int C>
15 class determinant_vari : public vari {
16  int rows_;
17  int cols_;
18  double* A_;
19  vari** adjARef_;
20 
21  public:
22  explicit determinant_vari(const Eigen::Matrix<var, R, C>& A)
24  rows_(A.rows()),
25  cols_(A.cols()),
26  A_(reinterpret_cast<double*>(ChainableStack::instance_->memalloc_.alloc(
27  sizeof(double) * A.rows() * A.cols()))),
28  adjARef_(
29  reinterpret_cast<vari**>(ChainableStack::instance_->memalloc_.alloc(
30  sizeof(vari*) * A.rows() * A.cols()))) {
31  Eigen::Map<Eigen::MatrixXd>(A_, rows_, cols_) = A.val();
32  Eigen::Map<matrix_vi>(adjARef_, rows_, cols_) = A.vi();
33  }
34  static double determinant_vari_calc(const Eigen::Matrix<var, R, C>& A) {
35  return A.val().determinant();
36  }
37  virtual void chain() {
38  Eigen::Map<matrix_vi>(adjARef_, rows_, cols_).adj()
39  += (adj_ * val_)
40  * Eigen::Map<Eigen::MatrixXd>(A_, rows_, cols_)
41  .inverse()
42  .transpose();
43  }
44 };
45 } // namespace internal
46 
47 template <int R, int C>
48 inline var determinant(const Eigen::Matrix<var, R, C>& m) {
49  check_square("determinant", "m", m);
51 }
52 
53 } // namespace math
54 } // namespace stan
55 #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
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
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:33
friend class var
Definition: vari.hpp:32
const double val_
The value of this variable.
Definition: vari.hpp:38
fvar< T > determinant(const Eigen::Matrix< fvar< T >, R, C > &m)
Definition: determinant.hpp:12
virtual void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
Definition: determinant.hpp:37
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
determinant_vari(const Eigen::Matrix< var, R, C > &A)
Definition: determinant.hpp:22
static double determinant_vari_calc(const Eigen::Matrix< var, R, C > &A)
Definition: determinant.hpp:34
double adj_
The adjoint of this variable, which is the partial derivative of this variable with respect to the ro...
Definition: vari.hpp:44
This struct always provides access to the autodiff stack using the singleton pattern.

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