Automatic Differentiation
 
Loading...
Searching...
No Matches
trace_gen_quad_form.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_TRACE_GEN_QUAD_FORM_HPP
2#define STAN_MATH_PRIM_FUN_TRACE_GEN_QUAD_FORM_HPP
3
11#include <exception>
12
13namespace stan {
14namespace math {
15
32template <typename TD, typename TA, typename TB,
33 typename = require_all_eigen_t<TD, TA, TB>,
34 typename = require_all_not_vt_var<TD, TA, TB>,
35 typename = require_any_not_vt_arithmetic<TD, TA, TB>>
36inline auto trace_gen_quad_form(const TD& D, const TA& A, const TB& B) {
37 check_square("trace_gen_quad_form", "A", A);
38 check_square("trace_gen_quad_form", "D", D);
39 check_multiplicable("trace_gen_quad_form", "A", A, "B", B);
40 check_multiplicable("trace_gen_quad_form", "B", B, "D", D);
41 const auto& B_ref = to_ref(B);
42 return multiply(B_ref, D.transpose()).cwiseProduct(multiply(A, B_ref)).sum();
43}
44
63template <typename EigMatD, typename EigMatA, typename EigMatB,
64 require_all_eigen_vt<std::is_arithmetic, EigMatD, EigMatA,
65 EigMatB>* = nullptr>
66inline double trace_gen_quad_form(const EigMatD& D, const EigMatA& A,
67 const EigMatB& B) {
68 check_square("trace_gen_quad_form", "A", A);
69 check_square("trace_gen_quad_form", "D", D);
70 check_multiplicable("trace_gen_quad_form", "A", A, "B", B);
71 check_multiplicable("trace_gen_quad_form", "B", B, "D", D);
72 const auto& B_ref = to_ref(B);
73 return (B_ref * D.transpose()).cwiseProduct(A * B_ref).sum();
74}
75
76} // namespace math
77} // namespace stan
78
79#endif
require_all_t< container_type_check_base< is_eigen, value_type_t, TypeCheck, Check >... > require_all_eigen_vt
Require all of the types satisfy is_eigen.
Definition is_eigen.hpp:188
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 multiply(const Mat1 &m1, const Mat2 &m2)
Return the product of the specified matrices.
Definition multiply.hpp:19
auto trace_gen_quad_form(const TD &D, const TA &A, const TB &B)
Return the trace of D times the quadratic form of B and A.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...