1#ifndef STAN_MATH_REV_FUN_TRACE_QUAD_FORM_HPP
2#define STAN_MATH_REV_FUN_TRACE_QUAD_FORM_HPP
18template <
typename Ta,
int Ra,
int Ca,
typename Tb,
int Rb,
int Cb>
22 const Eigen::Matrix<Tb, Rb, Cb>& B)
27 Eigen::Matrix<Ta, Ra, Ca>
A_;
28 Eigen::Matrix<Tb, Rb, Cb>
B_;
31template <
typename Ta,
int Ra,
int Ca,
typename Tb,
int Rb,
int Cb>
34 static inline void chainA(Eigen::Matrix<double, Ra, Ca>& A,
35 const Eigen::Matrix<double, Rb, Cb>& Bd,
37 static inline void chainB(Eigen::Matrix<double, Rb, Cb>& B,
38 const Eigen::Matrix<double, Ra, Ca>& Ad,
39 const Eigen::Matrix<double, Rb, Cb>& Bd,
42 static inline void chainA(Eigen::Matrix<var, Ra, Ca>& A,
43 const Eigen::Matrix<double, Rb, Cb>& Bd,
45 A.adj() += adjC * Bd * Bd.transpose();
47 static inline void chainB(Eigen::Matrix<var, Rb, Cb>& B,
48 const Eigen::Matrix<double, Ra, Ca>& Ad,
49 const Eigen::Matrix<double, Rb, Cb>& Bd,
51 B.adj() += adjC * (Ad + Ad.transpose()) * Bd;
54 inline void chainAB(Eigen::Matrix<Ta, Ra, Ca>& A,
55 Eigen::Matrix<Tb, Rb, Cb>& B,
56 const Eigen::Matrix<double, Ra, Ca>& Ad,
57 const Eigen::Matrix<double, Rb, Cb>& Bd,
double adjC) {
76template <
typename EigMat1,
typename EigMat2,
83 constexpr int Ra = EigMat1::RowsAtCompileTime;
84 constexpr int Ca = EigMat1::ColsAtCompileTime;
85 constexpr int Rb = EigMat2::RowsAtCompileTime;
86 constexpr int Cb = EigMat2::ColsAtCompileTime;
114template <
typename Mat1,
typename Mat2,
133 arena_A.adj().noalias()
141 arena_B.adj().noalias()
145 arena_B.adj() += res.adj()
160 arena_B.adj().noalias()
161 += res.adj() * (arena_A + arena_A.transpose()) *
value_of(arena_B);
164 += res.adj() * (arena_A + arena_A.transpose()) *
value_of(arena_B);
171 res = (arena_B.transpose() *
value_of(arena_A) * arena_B).
trace();
175 arena_A.adj().noalias() += res.adj() * arena_B * arena_B.transpose();
177 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