1#ifndef STAN_MATH_REV_FUN_SVD_HPP
2#define STAN_MATH_REV_FUN_SVD_HPP
27template <
typename EigMat, require_rev_matrix_t<EigMat>* =
nullptr>
28inline auto svd(
const EigMat& m) {
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)));
38 const int M = std::min(m.rows(), m.cols());
41 Eigen::JacobiSVD<Eigen::MatrixXd>
svd(
42 arena_m.val(), Eigen::ComputeThinU | Eigen::ComputeThinV);
44 auto singular_values_d =
svd.singularValues();
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;
57 arena_Fp.diagonal().setZero();
58 arena_Fm.diagonal().setZero();
67 Eigen::MatrixXd UUadjT = arena_U.val_op().
transpose() * arena_U.adj_op();
69 = .5 * arena_U.val_op()
70 * (arena_Fp.array() * (UUadjT - UUadjT.transpose()).array())
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())
77 * arena_V.val_op().transpose();
80 * arena_V.val_op().transpose();
82 Eigen::MatrixXd VTVadj = arena_V.val_op().transpose() * arena_V.adj_op();
84 = 0.5 * arena_U.val_op()
85 * (arena_Fm.array() * (VTVadj - VTVadj.transpose()).array())
87 * arena_V.val_op().transpose()
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());
93 arena_m.adj() += u_adj + d_adj + v_adj;
96 return std::make_tuple(mat_ret_type(arena_U), vec_ret_type(
singular_values),
97 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 ...