Automatic Differentiation
 
Loading...
Searching...
No Matches
inverse.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_INVERSE_HPP
2#define STAN_MATH_REV_FUN_INVERSE_HPP
3
10
11namespace stan {
12namespace math {
13
22template <typename T, require_rev_matrix_t<T>* = nullptr>
23inline auto inverse(const T& m) {
24 check_square("inverse", "m", m);
25
26 using ret_type = return_var_matrix_t<T>;
27 if (unlikely(m.size() == 0)) {
28 return ret_type(m);
29 }
30
31 arena_t<T> arena_m = m;
32 arena_t<promote_scalar_t<double, T>> res_val = arena_m.val().inverse();
33 arena_t<ret_type> res = res_val;
34
35 reverse_pass_callback([res, res_val, arena_m]() mutable {
36 arena_m.adj() -= res_val.transpose() * res.adj_op() * res_val.transpose();
37 });
38
39 return ret_type(res);
40}
41
42} // namespace math
43} // namespace stan
44#endif
#define unlikely(x)
void check_square(const char *function, const char *name, const T_y &y)
Check if the specified matrix is square.
void reverse_pass_callback(F &&functor)
Puts a callback on the autodiff stack to be called in reverse pass.
Eigen::Matrix< value_type_t< EigMat >, EigMat::RowsAtCompileTime, EigMat::ColsAtCompileTime > inverse(const EigMat &m)
Forward mode specialization of calculating the inverse of the matrix.
Definition inverse.hpp:29
typename internal::arena_type_impl< std::decay_t< T > >::type arena_t
Determines a type that can be used in place of T that does any dynamic allocations on the AD stack.
std::conditional_t< is_any_var_matrix< ReturnType, Types... >::value, stan::math::var_value< stan::math::promote_scalar_t< double, plain_type_t< ReturnType > > >, stan::math::promote_scalar_t< stan::math::var_value< double >, plain_type_t< ReturnType > > > return_var_matrix_t
Given an Eigen type and several inputs, determine if a matrix should be var<Matrix> or Matrix<var>.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...