Automatic Differentiation
 
Loading...
Searching...
No Matches
trace_quad_form.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_TRACE_QUAD_FORM_HPP
2#define STAN_MATH_PRIM_FUN_TRACE_QUAD_FORM_HPP
3
8
9namespace stan {
10namespace math {
11
24template <typename EigMat1, typename EigMat2,
25 require_all_eigen_vt<std::is_arithmetic, EigMat1, EigMat2>* = nullptr>
26inline auto trace_quad_form(EigMat1&& A, EigMat2&& B) {
27 check_square("trace_quad_form", "A", A);
28 check_multiplicable("trace_quad_form", "A", A, "B", B);
29 decltype(auto) B_ref = to_ref(std::forward<EigMat2>(B));
30 return make_holder(
31 [](auto&& A_, auto&& B_) { return B_.cwiseProduct(A_ * B_).sum(); },
32 std::forward<EigMat1>(A), std::forward<decltype(B_ref)>(B_ref));
33}
34
35} // namespace math
36} // namespace stan
37
38#endif
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.
auto make_holder(F &&func, Args &&... args)
Calls given function with given arguments.
Definition holder.hpp:481
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
return_type_t< EigMat1, EigMat2 > trace_quad_form(const EigMat1 &A, const EigMat2 &B)
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:18
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...