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_FWD_MAT_FUN_DETERMINANT_HPP
2 #define STAN_MATH_FWD_MAT_FUN_DETERMINANT_HPP
3 
5 #include <stan/math/fwd/core.hpp>
12 #include <boost/math/tools/promotion.hpp>
13 #include <vector>
14 
15 namespace stan {
16  namespace math {
17 
18  template<typename T, int R, int C>
19  inline
20  fvar<T>
21  determinant(const Eigen::Matrix<fvar<T>, R, C>& m) {
23  using stan::math::inverse;
24 
25  stan::math::check_square("determinant", "m", m);
26  Eigen::Matrix<T, R, C> m_deriv(m.rows(), m.cols());
27  Eigen::Matrix<T, R, C> m_val(m.rows(), m.cols());
28 
29  for (size_type i = 0; i < m.rows(); i++) {
30  for (size_type j = 0; j < m.cols(); j++) {
31  m_deriv(i, j) = m(i, j).d_;
32  m_val(i, j) = m(i, j).val_;
33  }
34  }
35 
36  Eigen::Matrix<T, R, C> m_inv = inverse<T>(m_val);
37  m_deriv = multiply(m_inv, m_deriv);
38 
39  fvar<T> result;
40  result.val_ = m_val.determinant();
41  result.d_ = result.val_ * m_deriv.trace();
42 
43  // FIXME: I think this will overcopy compared to retur fvar<T>(...);
44  return result;
45  }
46  }
47 }
48 #endif
Eigen::Matrix< fvar< T >, R1, C1 > multiply(const Eigen::Matrix< fvar< T >, R1, C1 > &m, const fvar< T > &c)
Definition: multiply.hpp:20
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
Eigen::Matrix< fvar< T >, R, C > inverse(const Eigen::Matrix< fvar< T >, R, C > &m)
Definition: inverse.hpp:20
fvar< T > determinant(const Eigen::Matrix< fvar< T >, R, C > &m)
Definition: determinant.hpp:21
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.

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