1#ifndef STAN_MATH_REV_FUN_SVD_HPP
2#define STAN_MATH_REV_FUN_SVD_HPP
26template <
typename EigMat, require_rev_matrix_t<EigMat>* =
nullptr>
27inline auto svd(
const EigMat& m) {
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)));
37 const int M = std::min(m.rows(), m.cols());
40 Eigen::JacobiSVD<Eigen::MatrixXd>
svd(
41 arena_m.val(), Eigen::ComputeThinU | Eigen::ComputeThinV);
43 auto singular_values_d =
svd.singularValues();
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;
56 arena_Fp.diagonal().setZero();
57 arena_Fm.diagonal().setZero();
66 Eigen::MatrixXd UUadjT = arena_U.val_op().
transpose() * arena_U.adj_op();
68 = .5 * arena_U.val_op()
69 * (arena_Fp.array() * (UUadjT - UUadjT.transpose()).array())
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())
76 * arena_V.val_op().transpose();
79 * arena_V.val_op().transpose();
81 Eigen::MatrixXd VTVadj = arena_V.val_op().transpose() * arena_V.adj_op();
83 = 0.5 * arena_U.val_op()
84 * (arena_Fm.array() * (VTVadj - VTVadj.transpose()).array())
86 * arena_V.val_op().transpose()
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());
92 arena_m.adj() += u_adj + d_adj + v_adj;
95 return std::make_tuple(mat_ret_type(arena_U), vec_ret_type(
singular_values),
96 mat_ret_type(arena_V));
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...
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}
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 ...