Automatic Differentiation
 
Loading...
Searching...
No Matches
generalized_inverse.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_GENERALIZED_INVERSE_HPP
2#define STAN_MATH_REV_FUN_GENERALIZED_INVERSE_HPP
3
10
11namespace stan {
12namespace math {
13
14namespace internal {
15/*
16 * Reverse mode specialization of calculating the generalized inverse of a
17 * matrix.
18 * <ul><li> Golub, G.H. and Pereyra, V. The Differentiation of Pseudo-Inverses
19 * and Nonlinear Least Squares Problems Whose Variables Separate. <i>SIAM
20 * Journal on Numerical Analysis</i>, Vol. 10, No. 2 (Apr., 1973), pp.
21 * 413-432</li></ul>
22 */
23template <typename T1, typename T2>
24inline auto generalized_inverse_lambda(T1& G_arena, T2& inv_G) {
25 return [G_arena, inv_G]() mutable {
26 G_arena.adj()
27 += -(inv_G.val().transpose() * inv_G.adj_op() * inv_G.val().transpose())
28 + (-G_arena.val() * inv_G.val()
29 + Eigen::MatrixXd::Identity(G_arena.rows(), inv_G.cols()))
30 * inv_G.adj_op().transpose() * inv_G.val()
31 * inv_G.val().transpose()
32 + inv_G.val().transpose() * inv_G.val() * inv_G.adj_op().transpose()
33 * (-inv_G.val() * G_arena.val()
34 + Eigen::MatrixXd::Identity(inv_G.rows(), G_arena.cols()));
35 };
36}
37} // namespace internal
38
39/*
40 * Reverse mode specialization of calculating the generalized inverse of a
41 * matrix.
42 *
43 * @param G specified matrix
44 * @return Generalized inverse of the matrix (an empty matrix if the specified
45 * matrix has size zero).
46 *
47 * @note For the derivatives of this function to exist the matrix must be
48 * of constant rank.
49 * Reverse mode differentiation algorithm reference:
50 *
51 * <ul><li> Golub, G.H. and Pereyra, V. The Differentiation of Pseudo-Inverses
52 * and Nonlinear Least Squares Problems Whose Variables Separate. <i>SIAM
53 * Journal on Numerical Analysis</i>, Vol. 10, No. 2 (Apr., 1973), pp.
54 * 413-432</li></ul>
55 *
56 * Equation 4.12 in the paper
57 *
58 * See also
59 * http://mathoverflow.net/questions/25778/analytical-formula-for-numerical-derivative-of-the-matrix-pseudo-inverse
60 *
61 */
62template <typename VarMat, require_rev_matrix_t<VarMat>* = nullptr>
63inline auto generalized_inverse(const VarMat& G) {
65
66 if (G.size() == 0)
67 return ret_type(G);
68
69 if (G.rows() == G.cols()) {
70 arena_t<VarMat> G_arena(G);
71 Eigen::CompleteOrthogonalDecomposition<Eigen::MatrixXd>
72 complete_ortho_decomp_G
73 = G_arena.val().completeOrthogonalDecomposition();
74 if (!(complete_ortho_decomp_G.rank() < G.rows())) {
75 return ret_type(inverse(G_arena));
76 } else {
77 arena_t<ret_type> inv_G(complete_ortho_decomp_G.pseudoInverse());
80 return ret_type(inv_G);
81 }
82 } else if (G.rows() < G.cols()) {
83 arena_t<VarMat> G_arena(G);
84 arena_t<ret_type> inv_G((G_arena.val() * G_arena.val().transpose())
85 .ldlt()
86 .solve(G_arena.val())
87 .transpose());
89 return ret_type(inv_G);
90 } else {
91 arena_t<VarMat> G_arena(G);
92 arena_t<ret_type> inv_G((G_arena.val().transpose() * G_arena.val())
93 .ldlt()
94 .solve(G_arena.val().transpose()));
96 return ret_type(inv_G);
97 }
98}
99
100} // namespace math
101} // namespace stan
102#endif
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.
Definition inverse.hpp:28
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 ...