1#ifndef STAN_MATH_REV_FUN_TRACE_QUAD_FORM_HPP
2#define STAN_MATH_REV_FUN_TRACE_QUAD_FORM_HPP
19template <
typename Ta,
int Ra,
int Ca,
typename Tb,
int Rb,
int Cb>
23 const Eigen::Matrix<Tb, Rb, Cb>& B)
28 Eigen::Matrix<Ta, Ra, Ca>
A_;
29 Eigen::Matrix<Tb, Rb, Cb>
B_;
32template <
typename Ta,
int Ra,
int Ca,
typename Tb,
int Rb,
int Cb>
35 static inline void chainA(Eigen::Matrix<double, Ra, Ca>& A,
36 const Eigen::Matrix<double, Rb, Cb>& Bd,
38 static inline void chainB(Eigen::Matrix<double, Rb, Cb>& B,
39 const Eigen::Matrix<double, Ra, Ca>& Ad,
40 const Eigen::Matrix<double, Rb, Cb>& Bd,
43 static inline void chainA(Eigen::Matrix<var, Ra, Ca>& A,
44 const Eigen::Matrix<double, Rb, Cb>& Bd,
46 A.adj() += adjC * Bd * Bd.transpose();
48 static inline void chainB(Eigen::Matrix<var, Rb, Cb>& B,
49 const Eigen::Matrix<double, Ra, Ca>& Ad,
50 const Eigen::Matrix<double, Rb, Cb>& Bd,
52 B.adj() += adjC * (Ad + Ad.transpose()) * Bd;
55 inline void chainAB(Eigen::Matrix<Ta, Ra, Ca>& A,
56 Eigen::Matrix<Tb, Rb, Cb>& B,
57 const Eigen::Matrix<double, Ra, Ca>& Ad,
58 const Eigen::Matrix<double, Rb, Cb>& Bd,
double adjC) {
77template <
typename EigMat1,
typename EigMat2,
84 constexpr int Ra = EigMat1::RowsAtCompileTime;
85 constexpr int Ca = EigMat1::ColsAtCompileTime;
86 constexpr int Rb = EigMat2::RowsAtCompileTime;
87 constexpr int Cb = EigMat2::ColsAtCompileTime;
115template <
typename Mat1,
typename Mat2,
134 arena_A.adj().noalias()
142 arena_B.adj().noalias()
146 arena_B.adj() += res.adj()
161 arena_B.adj().noalias()
162 += res.adj() * (arena_A + arena_A.transpose()) *
value_of(arena_B);
165 += res.adj() * (arena_A + arena_A.transpose()) *
value_of(arena_B);
172 res = (arena_B.transpose() *
value_of(arena_A) * arena_B).
trace();
176 arena_A.adj().noalias() += res.adj() * arena_B * arena_B.transpose();
178 arena_A.adj() += res.adj() * arena_B * arena_B.transpose();
A chainable_alloc is an object which is constructed and destructed normally but the memory lifespan i...
require_all_t< is_eigen< std::decay_t< Types > >... > require_all_eigen_t
Require all of the types satisfy is_eigen.
require_all_t< is_matrix< std::decay_t< Types > >... > require_all_matrix_t
Require all of the types satisfy is_matrix.
auto transpose(Arg &&a)
Transposes a kernel generator expression.
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
require_any_t< is_var_matrix< std::decay_t< Types > >... > require_any_var_matrix_t
Require any of the types satisfy is_var_matrix.
require_any_t< is_var< scalar_type_t< std::decay_t< Types > > >... > require_any_st_var
Require any of the scalar types satisfy is_var.
void check_square(const char *function, const char *name, const T_y &y)
Check if the specified matrix is square.
void check_multiplicable(const char *function, const char *name1, const T1 &y1, const char *name2, const T2 &y2)
Check if the matrices can be multiplied.
void reverse_pass_callback(F &&functor)
Puts a callback on the autodiff stack to be called in reverse pass.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
value_type_t< T > trace(const T &m)
Calculates trace (sum of diagonal) of given kernel generator expression.
return_type_t< EigMat1, EigMat2 > trace_quad_form(const EigMat1 &A, const EigMat2 &B)
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.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Metaprogramming struct to detect whether a given type is constant in the mathematical sense (not the ...
Check if a type is a var_value whose value_type is derived from Eigen::EigenBase