| Stan Math Library 2.11.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
4 #include <stan/math/prim/mat/fun/Eigen.hpp>
5 #include <stan/math/prim/mat/fun/multiply.hpp>
6 #include <stan/math/fwd/core.hpp>
7 #include <stan/math/fwd/mat/fun/to_fvar.hpp>
8 #include <stan/math/fwd/mat/fun/multiply.hpp>
9 #include <stan/math/prim/mat/fun/inverse.hpp>
10 #include <stan/math/prim/mat/err/check_square.hpp>
11 #include <boost/math/tools/promotion.hpp>
12 #include <vector>
13
14 namespace stan {
15 namespace math {
16
17 template<typename T, int R, int C>
18 inline
19 Eigen::Matrix<fvar<T>, R, C>
20 inverse(const Eigen::Matrix<fvar<T>, R, C>& m) {
21 using stan::math::multiply;
22 using stan::math::multiply;
23 using stan::math::inverse;
24 stan::math::check_square("inverse", "m", m);
25 Eigen::Matrix<T, R, C> m_deriv(m.rows(), m.cols());
26 Eigen::Matrix<T, R, C> m_inv(m.rows(), m.cols());
27
28 for (size_type i = 0; i < m.rows(); i++) {
29 for (size_type j = 0; j < m.cols(); j++) {
30 m_inv(i, j) = m(i, j).val_;
31 m_deriv(i, j) = m(i, j).d_;
32 }
33 }
34
35 m_inv = stan::math::inverse(m_inv);
36
37 m_deriv = multiply(multiply(m_inv, m_deriv), m_inv);
38 m_deriv = -m_deriv;
39
40 return to_fvar(m_inv, m_deriv);
41 }
42 }
43 }
44 #endif
multiply.hpp
core.hpp
stan
Definition: log_sum_exp.hpp:8
stan::math::multiply
Eigen::Matrix< fvar< T >, R1, C1 > multiply(const Eigen::Matrix< fvar< T >, R1, C1 > &m, const fvar< T > &c)
Definition: multiply.hpp:21
multiply.hpp
stan::math::to_fvar
std::vector< fvar< T > > to_fvar(const std::vector< T > &v)
Definition: to_fvar.hpp:14
stan::math::size_type
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
stan::math::inverse
Eigen::Matrix< fvar< T >, R, C > inverse(const Eigen::Matrix< fvar< T >, R, C > &m)
Definition: inverse.hpp:20
inverse.hpp
Eigen.hpp
stan::math::check_square
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.
Definition: check_square.hpp:28
to_fvar.hpp
stan::math::fvar
Definition: fvar.hpp:13
check_square.hpp

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