Automatic Differentiation
 
Loading...
Searching...
No Matches
chol2inv.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_CHOL2INV_HPP
2#define STAN_MATH_PRIM_FUN_CHOL2INV_HPP
3
10
11namespace stan {
12namespace math {
13
23template <typename T, require_eigen_t<T>* = nullptr>
25 const Eigen::Ref<const plain_type_t<T>>& L_ref = L;
26 check_square("chol2inv", "L", L_ref);
27 check_lower_triangular("chol2inv", "L", L_ref);
28 int K = L.rows();
29 using T_result = plain_type_t<T>;
30 if (K == 0) {
31 return L_ref;
32 }
33 if (K == 1) {
34 T_result X(1, 1);
35 X.coeffRef(0) = inv_square(L_ref.coeff(0, 0));
36 return X;
37 }
38 T_result L_inv = mdivide_left_tri<Eigen::Lower>(L_ref);
39 T_result X(K, K);
40 for (int k = 0; k < K; ++k) {
41 X.coeffRef(k, k) = dot_self(L_inv.col(k).tail(K - k).eval());
42 for (int j = k + 1; j < K; ++j) {
43 int Kmj = K - j;
44 X.coeffRef(k, j) = X.coeffRef(j, k) = dot_product(
45 L_inv.col(k).tail(Kmj).eval(), L_inv.col(j).tail(Kmj).eval());
46 }
47 }
48 return X;
49}
50
51} // namespace math
52} // namespace stan
53
54#endif
void check_square(const char *function, const char *name, const T_y &y)
Check if the specified matrix is square.
fvar< T > inv_square(const fvar< T > &x)
plain_type_t< T > chol2inv(const T &L)
Returns the inverse of the matrix whose Cholesky factor is L.
Definition chol2inv.hpp:24
auto dot_self(const T &a)
Returns squared norm of a vector or matrix.
Definition dot_self.hpp:21
auto dot_product(const T_a &a, const T_b &b)
Returns the dot product of the specified vectors.
void check_lower_triangular(const char *function, const char *name, const T_y &y)
Check if the specified matrix is lower triangular.
typename plain_type< T >::type plain_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...