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
10
11namespace stan {
12namespace math {
13
26template <typename EigMat, require_rev_matrix_t<EigMat>* = nullptr>
27inline auto svd(const EigMat& m) {
30
31 if (unlikely(m.size() == 0)) {
32 return std::make_tuple(mat_ret_type(Eigen::MatrixXd(0, 0)),
33 vec_ret_type(Eigen::VectorXd(0, 1)),
34 mat_ret_type(Eigen::MatrixXd(0, 0)));
35 }
36
37 const int M = std::min(m.rows(), m.cols());
38 auto arena_m = to_arena(m);
39
40 Eigen::JacobiSVD<Eigen::MatrixXd> svd(
41 arena_m.val(), Eigen::ComputeThinU | Eigen::ComputeThinV);
42
43 auto singular_values_d = svd.singularValues();
44
45 arena_t<Eigen::MatrixXd> arena_Fp(M, M);
46 arena_t<Eigen::MatrixXd> arena_Fm(M, M);
47
48 for (int i = 0; i < M; i++) {
49 for (int j = 0; j < M; j++) {
50 double a = 1.0 / (singular_values_d[j] - singular_values_d[i]);
51 double b = 1.0 / (singular_values_d[i] + singular_values_d[j]);
52 arena_Fp(i, j) = a + b;
53 arena_Fm(i, j) = a - b;
54 }
55 }
56 arena_Fp.diagonal().setZero();
57 arena_Fm.diagonal().setZero();
58
59 arena_t<vec_ret_type> singular_values = singular_values_d;
60 arena_t<mat_ret_type> arena_U = svd.matrixU();
61 arena_t<mat_ret_type> arena_V = svd.matrixV();
62
63 reverse_pass_callback([arena_m, arena_U, singular_values, arena_V, arena_Fp,
64 arena_Fm]() mutable {
65 // SVD-U reverse mode
66 Eigen::MatrixXd UUadjT = arena_U.val_op().transpose() * arena_U.adj_op();
67 auto u_adj
68 = .5 * arena_U.val_op()
69 * (arena_Fp.array() * (UUadjT - UUadjT.transpose()).array())
70 .matrix()
71 * arena_V.val_op().transpose()
72 + (Eigen::MatrixXd::Identity(arena_m.rows(), arena_m.rows())
73 - arena_U.val_op() * arena_U.val_op().transpose())
74 * arena_U.adj_op()
75 * singular_values.val_op().asDiagonal().inverse()
76 * arena_V.val_op().transpose();
77 // Singular values reverse mode
78 auto d_adj = arena_U.val_op() * singular_values.adj().asDiagonal()
79 * arena_V.val_op().transpose();
80 // SVD-V reverse mode
81 Eigen::MatrixXd VTVadj = arena_V.val_op().transpose() * arena_V.adj_op();
82 auto v_adj
83 = 0.5 * arena_U.val_op()
84 * (arena_Fm.array() * (VTVadj - VTVadj.transpose()).array())
85 .matrix()
86 * arena_V.val_op().transpose()
87 + arena_U.val_op() * singular_values.val_op().asDiagonal().inverse()
88 * arena_V.adj_op().transpose()
89 * (Eigen::MatrixXd::Identity(arena_m.cols(), arena_m.cols())
90 - arena_V.val_op() * arena_V.val_op().transpose());
91
92 arena_m.adj() += u_adj + d_adj + v_adj;
93 });
94
95 return std::make_tuple(mat_ret_type(arena_U), vec_ret_type(singular_values),
96 mat_ret_type(arena_V));
97}
98
99} // namespace math
100} // namespace stan
101
102#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 ...