Automatic Differentiation
 
Loading...
Searching...
No Matches
svd.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_SVD_HPP
2#define STAN_MATH_REV_FUN_SVD_HPP
3
11
12namespace stan {
13namespace math {
14
27template <typename EigMat, require_rev_matrix_t<EigMat>* = nullptr>
28inline auto svd(const EigMat& m) {
31
32 if (unlikely(m.size() == 0)) {
33 return std::make_tuple(mat_ret_type(Eigen::MatrixXd(0, 0)),
34 vec_ret_type(Eigen::VectorXd(0, 1)),
35 mat_ret_type(Eigen::MatrixXd(0, 0)));
36 }
37
38 const int M = std::min(m.rows(), m.cols());
39 auto arena_m = to_arena(m);
40
41 Eigen::JacobiSVD<Eigen::MatrixXd> svd(
42 arena_m.val(), Eigen::ComputeThinU | Eigen::ComputeThinV);
43
44 auto singular_values_d = svd.singularValues();
45
46 arena_t<Eigen::MatrixXd> arena_Fp(M, M);
47 arena_t<Eigen::MatrixXd> arena_Fm(M, M);
48
49 for (int i = 0; i < M; i++) {
50 for (int j = 0; j < M; j++) {
51 double a = 1.0 / (singular_values_d[j] - singular_values_d[i]);
52 double b = 1.0 / (singular_values_d[i] + singular_values_d[j]);
53 arena_Fp(i, j) = a + b;
54 arena_Fm(i, j) = a - b;
55 }
56 }
57 arena_Fp.diagonal().setZero();
58 arena_Fm.diagonal().setZero();
59
60 arena_t<vec_ret_type> singular_values = singular_values_d;
61 arena_t<mat_ret_type> arena_U = svd.matrixU();
62 arena_t<mat_ret_type> arena_V = svd.matrixV();
63
64 reverse_pass_callback([arena_m, arena_U, singular_values, arena_V, arena_Fp,
65 arena_Fm]() mutable {
66 // SVD-U reverse mode
67 Eigen::MatrixXd UUadjT = arena_U.val_op().transpose() * arena_U.adj_op();
68 auto u_adj
69 = .5 * arena_U.val_op()
70 * (arena_Fp.array() * (UUadjT - UUadjT.transpose()).array())
71 .matrix()
72 * arena_V.val_op().transpose()
73 + (Eigen::MatrixXd::Identity(arena_m.rows(), arena_m.rows())
74 - arena_U.val_op() * arena_U.val_op().transpose())
75 * arena_U.adj_op()
76 * singular_values.val_op().asDiagonal().inverse()
77 * arena_V.val_op().transpose();
78 // Singular values reverse mode
79 auto d_adj = arena_U.val_op() * singular_values.adj().asDiagonal()
80 * arena_V.val_op().transpose();
81 // SVD-V reverse mode
82 Eigen::MatrixXd VTVadj = arena_V.val_op().transpose() * arena_V.adj_op();
83 auto v_adj
84 = 0.5 * arena_U.val_op()
85 * (arena_Fm.array() * (VTVadj - VTVadj.transpose()).array())
86 .matrix()
87 * arena_V.val_op().transpose()
88 + arena_U.val_op() * singular_values.val_op().asDiagonal().inverse()
89 * arena_V.adj_op().transpose()
90 * (Eigen::MatrixXd::Identity(arena_m.cols(), arena_m.cols())
91 - arena_V.val_op() * arena_V.val_op().transpose());
92
93 arena_m.adj() += u_adj + d_adj + v_adj;
94 });
95
96 return std::make_tuple(mat_ret_type(arena_U), vec_ret_type(singular_values),
97 mat_ret_type(arena_V));
98}
99
100} // namespace math
101} // namespace stan
102
103#endif
#define unlikely(x)
auto transpose(Arg &&a)
Transposes a kernel generator expression.
void reverse_pass_callback(F &&functor)
Puts a callback on the autodiff stack to be called in reverse pass.
arena_t< T > to_arena(const T &a)
Converts given argument into a type that either has any dynamic allocation on AD stack or schedules i...
Definition to_arena.hpp:25
auto singular_values(const EigMat &m)
Return the vector of the singular values of the specified matrix in decreasing order of magnitude.
std::tuple< Eigen::Matrix< value_type_t< EigMat >, -1, -1 >, Eigen::Matrix< base_type_t< EigMat >, -1, 1 >, Eigen::Matrix< value_type_t< EigMat >, -1, -1 > > svd(const EigMat &m)
Given input matrix m, return the singular value decomposition (U,D,V) such that m = U*diag(D)*V^{T}
Definition svd.hpp:24
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 ...