Automatic Differentiation
 
Loading...
Searching...
No Matches
eigenvectors_sym.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_EIGENVECTORS_HPP
2#define STAN_MATH_REV_FUN_EIGENVECTORS_HPP
3
13
14namespace stan {
15namespace math {
16
24template <typename T, require_rev_matrix_t<T>* = nullptr>
25inline auto eigenvectors_sym(const T& m) {
26 using return_t = return_var_matrix_t<T>;
27 if (unlikely(m.size() == 0)) {
28 return return_t(Eigen::MatrixXd(0, 0));
29 }
30 check_symmetric("eigenvectors_sym", "m", m);
31
32 auto arena_m = to_arena(m);
33 Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> solver(arena_m.val());
34 arena_t<return_t> eigenvecs = solver.eigenvectors();
35 auto eigenvals = to_arena(solver.eigenvalues());
36
37 reverse_pass_callback([arena_m, eigenvals, eigenvecs]() mutable {
38 const auto p = arena_m.val().cols();
39 Eigen::MatrixXd f = (1
40 / (eigenvals.rowwise().replicate(p).transpose()
41 - eigenvals.rowwise().replicate(p))
42 .array());
43 f.diagonal().setZero();
44 arena_m.adj()
45 += eigenvecs.val_op()
46 * f.cwiseProduct(eigenvecs.val_op().transpose() * eigenvecs.adj_op())
47 * eigenvecs.val_op().transpose();
48 });
49
50 return return_t(eigenvecs);
51}
52
53} // namespace math
54} // namespace stan
55#endif
#define unlikely(x)
void check_symmetric(const char *function, const char *name, const matrix_cl< T > &y)
Check if the matrix_cl is symmetric.
auto transpose(Arg &&a)
Transposes a kernel generator expression.
int64_t cols(const T_x &x)
Returns the number of columns in the specified kernel generator expression.
Definition cols.hpp:21
matrix_cl< double > eigenvectors_sym(const matrix_cl< double > &m)
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
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 ...