1#ifndef STAN_MATH_REV_FUN_GENERALIZED_INVERSE_HPP
2#define STAN_MATH_REV_FUN_GENERALIZED_INVERSE_HPP
22template <
typename T1,
typename T2>
24 return [G_arena, inv_G]()
mutable {
26 += -(inv_G.val_op().transpose() * inv_G.adj_op()
27 * inv_G.val_op().transpose())
28 + (-G_arena.val_op() * inv_G.val_op()
29 + Eigen::MatrixXd::Identity(G_arena.rows(), inv_G.cols()))
30 * inv_G.adj_op().transpose() * inv_G.val_op()
31 * inv_G.val_op().transpose()
32 + inv_G.val_op().transpose() * inv_G.val_op()
33 * inv_G.adj_op().transpose()
34 * (-inv_G.val_op() * G_arena.val_op()
35 + Eigen::MatrixXd::Identity(inv_G.rows(), G_arena.cols()));
63template <
typename VarMat, require_rev_matrix_t<VarMat>* =
nullptr>
70 if (G.rows() == G.cols()) {
72 Eigen::CompleteOrthogonalDecomposition<Eigen::MatrixXd>
73 complete_ortho_decomp_G
74 = G_arena.val().completeOrthogonalDecomposition();
75 if (!(complete_ortho_decomp_G.rank() < G.rows())) {
76 return ret_type(
inverse(G_arena));
81 return ret_type(inv_G);
83 }
else if (G.rows() < G.cols()) {
87 .solve(G_arena.val_op())
90 return ret_type(inv_G);
95 .solve(G_arena.val_op().transpose()));
97 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 ...