Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
determinant.hpp
Go to the documentation of this file.
1 #ifndef STAN__AGRAD__FWD__MATRIX__DETERMINANT_HPP
2 #define STAN__AGRAD__FWD__MATRIX__DETERMINANT_HPP
3 
4 #include <vector>
5 #include <boost/math/tools/promotion.hpp>
14 
15 namespace stan {
16  namespace agrad {
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 
24  stan::math::check_square("determinant(%1%)",m,"m",(double*)0);
25  Eigen::Matrix<T,R,C> m_deriv(m.rows(), m.cols());
26  Eigen::Matrix<T,R,C> m_val(m.rows(), m.cols());
27  Eigen::Matrix<T,R,C> m_inv(m.rows(), m.cols());
28  fvar<T> result;
29 
30  for(size_type i = 0; i < m.rows(); i++) {
31  for(size_type j = 0; j < m.cols(); j++) {
32  m_deriv(i,j) = m(i,j).d_;
33  m_val(i,j) = m(i,j).val_;
34  }
35  }
36 
37  m_inv = stan::math::inverse(m_val);
38  m_deriv = multiply(m_inv, m_deriv);
39 
40  result.val_ = m_val.determinant();
41  result.d_ = result.val_ * m_deriv.trace();
42 
43  return result;
44  }
45  }
46 }
47 #endif
Eigen::Matrix< fvar< T >, R1, C1 > multiply(const Eigen::Matrix< fvar< T >, R1, C1 > &m, const fvar< T > &c)
Definition: multiply.hpp:21
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic >::Index size_type
Definition: typedefs.hpp:14
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > inverse(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &m)
Returns the inverse of the specified matrix.
Definition: inverse.hpp:18
boost::enable_if_c< boost::is_arithmetic< T >::value, Eigen::Matrix< double, R, C > >::type multiply(const Eigen::Matrix< double, R, C > &m, T c)
Return specified matrix multiplied by specified scalar.
Definition: multiply.hpp:25
fvar< T > determinant(const Eigen::Matrix< fvar< T >, R, C > &m)
Definition: determinant.hpp:21
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.