Automatic Differentiation
 
Loading...
Searching...
No Matches
eigenvalues_sym.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_EIGENVALUES_HPP
2#define STAN_MATH_REV_FUN_EIGENVALUES_HPP
3
13
14namespace stan {
15namespace math {
16
23template <typename T, require_rev_matrix_t<T>* = nullptr>
24inline auto eigenvalues_sym(const T& m) {
26 if (unlikely(m.size() == 0)) {
27 return return_t(Eigen::VectorXd(0));
28 }
29 check_symmetric("eigenvalues_sym", "m", m);
30
31 auto arena_m = to_arena(m);
32 Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> solver(arena_m.val());
33 arena_t<return_t> eigenvals = solver.eigenvalues();
34 auto eigenvecs = to_arena(solver.eigenvectors());
35
36 reverse_pass_callback([eigenvals, arena_m, eigenvecs]() mutable {
37 arena_m.adj()
38 += eigenvecs * eigenvals.adj().asDiagonal() * eigenvecs.transpose();
39 });
40
41 return return_t(eigenvals);
42}
43
44} // namespace math
45} // namespace stan
46#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.
matrix_cl< double > eigenvalues_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 ...