Stan Math Library  2.20.0
reverse mode automatic differentiation
inverse.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_FWD_MAT_FUN_INVERSE_HPP
2 #define STAN_MATH_FWD_MAT_FUN_INVERSE_HPP
3 
6 #include <stan/math/fwd/core.hpp>
11 
12 namespace stan {
13 namespace math {
14 
15 template <typename T, int R, int C>
16 inline Eigen::Matrix<fvar<T>, R, C> inverse(
17  const Eigen::Matrix<fvar<T>, R, C>& m) {
18  check_square("inverse", "m", m);
19  Eigen::Matrix<T, R, C> m_deriv(m.rows(), m.cols());
20  Eigen::Matrix<T, R, C> m_inv(m.rows(), m.cols());
21 
22  for (size_type i = 0; i < m.rows(); i++) {
23  for (size_type j = 0; j < m.cols(); j++) {
24  m_inv(i, j) = m(i, j).val_;
25  m_deriv(i, j) = m(i, j).d_;
26  }
27  }
28 
29  m_inv = inverse(m_inv);
30 
31  m_deriv = multiply(multiply(m_inv, m_deriv), m_inv);
32  m_deriv = -m_deriv;
33 
34  return to_fvar(m_inv, m_deriv);
35 }
36 
37 } // namespace math
38 } // namespace stan
39 #endif
void check_square(const char *function, const char *name, const matrix_cl &y)
Check if the matrix_cl is square.
Eigen::Matrix< fvar< T >, R1, C1 > multiply(const Eigen::Matrix< fvar< T >, R1, C1 > &m, const fvar< T > &c)
Definition: multiply.hpp:14
std::vector< fvar< T > > to_fvar(const std::vector< T > &v)
Definition: to_fvar.hpp:12
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:11
Eigen::Matrix< fvar< T >, R, C > inverse(const Eigen::Matrix< fvar< T >, R, C > &m)
Definition: inverse.hpp:16
This template class represents scalars used in forward-mode automatic differentiation, which consist of values and directional derivatives of the specified template type.
Definition: fvar.hpp:41

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