1#ifndef STAN_MATH_REV_FUN_GENERALIZED_INVERSE_HPP
2#define STAN_MATH_REV_FUN_GENERALIZED_INVERSE_HPP
23template <
typename T1,
typename T2>
25 return [G_arena, inv_G]()
mutable {
27 += -(inv_G.val_op().transpose() * inv_G.adj_op()
28 * inv_G.val_op().transpose())
29 + (-G_arena.val_op() * inv_G.val_op()
30 + Eigen::MatrixXd::Identity(G_arena.rows(), inv_G.cols()))
31 * inv_G.adj_op().transpose() * inv_G.val_op()
32 * inv_G.val_op().transpose()
33 + inv_G.val_op().transpose() * inv_G.val_op()
34 * inv_G.adj_op().transpose()
35 * (-inv_G.val_op() * G_arena.val_op()
36 + Eigen::MatrixXd::Identity(inv_G.rows(), G_arena.cols()));
64template <
typename VarMat, require_rev_matrix_t<VarMat>* =
nullptr>
71 if (G.rows() == G.cols()) {
73 Eigen::CompleteOrthogonalDecomposition<Eigen::MatrixXd>
74 complete_ortho_decomp_G
75 = G_arena.val().completeOrthogonalDecomposition();
76 if (!(complete_ortho_decomp_G.rank() < G.rows())) {
77 return ret_type(
inverse(G_arena));
82 return ret_type(inv_G);
84 }
else if (G.rows() < G.cols()) {
88 .solve(G_arena.val_op())
91 return ret_type(inv_G);
96 .solve(G_arena.val_op().transpose()));
98 return ret_type(inv_G);
auto generalized_inverse_lambda(T1 &G_arena, T2 &inv_G)
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::ColsAtCompileTime, EigMat::RowsAtCompileTime > generalized_inverse(const EigMat &G)
Returns the Moore-Penrose generalized inverse of the specified matrix.
Eigen::Matrix< value_type_t< EigMat >, EigMat::RowsAtCompileTime, EigMat::ColsAtCompileTime > inverse(const EigMat &m)
Forward mode specialization of calculating the inverse of the matrix.
std::conditional_t< is_any_var_matrix< ReturnType, Types... >::value, stan::math::var_value< stan::math::promote_scalar_t< double, ReturnType > >, stan::math::promote_scalar_t< stan::math::var_value< double >, ReturnType > > promote_var_matrix_t
Given an Eigen type and several inputs, determine if a matrix should be var<Matrix> or Matrix<var>.
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.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...