Automatic Differentiation
 
Loading...
Searching...
No Matches
trace_gen_inv_quad_form_ldlt.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_TRACE_GEN_INV_QUAD_FORM_LDLT_HPP
2#define STAN_MATH_PRIM_FUN_TRACE_GEN_INV_QUAD_FORM_LDLT_HPP
3
12
13namespace stan {
14namespace math {
15
33template <typename EigMat1, typename T2, typename EigMat3,
34 require_not_col_vector_t<EigMat1>* = nullptr,
35 require_all_not_st_var<EigMat1, T2, EigMat3>* = nullptr>
37 const EigMat1& D, LDLT_factor<T2>& A, const EigMat3& B) {
38 check_square("trace_gen_inv_quad_form_ldlt", "D", D);
39 check_multiplicable("trace_gen_inv_quad_form_ldlt", "A", A.matrix(), "B", B);
40 check_multiplicable("trace_gen_inv_quad_form_ldlt", "B", B, "D", D);
41
42 if (D.size() == 0 || A.matrix().size() == 0) {
43 return 0;
44 }
45
46 return multiply(B, D.transpose()).cwiseProduct(mdivide_left_ldlt(A, B)).sum();
47}
48
66template <typename EigVec, typename T, typename EigMat,
70 const EigVec& D, LDLT_factor<T>& A, const EigMat& B) {
71 check_multiplicable("trace_gen_inv_quad_form_ldlt", "A", A.matrix(), "B", B);
72 check_multiplicable("trace_gen_inv_quad_form_ldlt", "B", B, "D", D);
73
74 if (D.size() == 0 || A.matrix().size() == 0) {
75 return 0;
76 }
77
78 return (B * D.asDiagonal()).cwiseProduct(mdivide_left_ldlt(A, B)).sum();
79}
80
81} // namespace math
82} // namespace stan
83
84#endif
LDLT_factor is a structure that holds a matrix of type T and the LDLT of its values.
require_t< is_col_vector< std::decay_t< T > > > require_col_vector_t
Require type satisfies is_col_vector.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
require_all_not_t< is_var< scalar_type_t< std::decay_t< Types > > >... > require_all_not_st_var
Require none of the scalar types satisfy is_var.
Definition is_var.hpp:138
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
Eigen::Matrix< value_type_t< EigMat >, Eigen::Dynamic, EigMat::ColsAtCompileTime > mdivide_left_ldlt(LDLT_factor< T > &A, const EigMat &b)
Returns the solution of the system Ax=b given an LDLT_factor of A.
return_type_t< EigMat1, T2, EigMat3 > trace_gen_inv_quad_form_ldlt(const EigMat1 &D, LDLT_factor< T2 > &A, const EigMat3 &B)
Compute the trace of an inverse quadratic form.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...