1#ifndef STAN_MATH_PRIM_CONSTRAINT_CHOLESKY_FACTOR_CONSTRAIN_HPP
2#define STAN_MATH_PRIM_CONSTRAINT_CHOLESKY_FACTOR_CONSTRAIN_HPP
28template <
typename T, require_eigen_col_vector_t<T>* =
nullptr>
29inline Eigen::Matrix<value_type_t<T>, Eigen::Dynamic, Eigen::Dynamic>
34 "num rows (must be greater or equal to num cols)", M,
37 "((N * (N + 1)) / 2 + (M - N) * N)",
38 ((N * (N + 1)) / 2 + (M - N) * N));
39 Eigen::Matrix<T_scalar, Eigen::Dynamic, Eigen::Dynamic> y(M, N);
42 const auto& x_ref =
to_ref(x);
43 for (
int m = 0; m < N; ++m) {
44 y.row(m).head(m) = x_ref.segment(pos, m);
46 y.coeffRef(m, m) =
exp(x_ref.coeff(pos++));
47 y.row(m).tail(N - m - 1).setZero();
50 for (
int m = N; m < M; ++m) {
51 y.row(m) = x_ref.segment(pos, N);
73template <
typename T, require_eigen_vector_t<T>* =
nullptr>
74inline Eigen::Matrix<value_type_t<T>, Eigen::Dynamic, Eigen::Dynamic>
77 "((N * (N + 1)) / 2 + (M - N) * N)",
78 ((N * (N + 1)) / 2 + (M - N) * N));
80 const auto& x_ref =
to_ref(x);
81 for (
int n = 0; n < N; ++n) {
83 lp += x_ref.coeff(pos++);
108template <
bool Jacobian,
typename T, require_not_std_vector_t<T>* =
nullptr>
138template <
bool Jacobian,
typename T, require_std_vector_t<T>* =
nullptr>
141 return apply_vector_unary<T>::apply(x, [&lp, M, N](
auto&& v) {
142 return cholesky_factor_constrain<Jacobian>(v, M, N, lp);
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.
void check_greater_or_equal(const char *function, const char *name, const T_y &y, const T_low &low, Idxs... idxs)
Throw an exception if y is not greater or equal than low.
Eigen::Matrix< value_type_t< T >, Eigen::Dynamic, Eigen::Dynamic > cholesky_factor_constrain(const T &x, int M, int N)
Return the Cholesky factor of the specified size read from the specified vector.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
void check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Check if the provided sizes match.
fvar< T > exp(const fvar< T > &x)
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...