1#ifndef STAN_MATH_PRIM_CONSTRAINT_CHOLESKY_CORR_FREE_HPP
2#define STAN_MATH_PRIM_CONSTRAINT_CHOLESKY_CORR_FREE_HPP
13template <
typename T, require_eigen_t<T>* =
nullptr>
21 const auto& x_ref =
to_ref(x);
22 int K = (x.rows() * (x.rows() - 1)) / 2;
23 Matrix<value_type_t<T>, Dynamic, 1> z(K);
25 for (
int i = 1; i < x.rows(); ++i) {
26 z.coeffRef(k++) =
corr_free(x_ref.coeff(i, 0));
27 double sum_sqs =
square(x_ref.coeff(i, 0));
28 for (
int j = 1; j < i; ++j) {
29 z.coeffRef(k++) =
corr_free(x_ref.coeff(i, j) / std::sqrt(1.0 - sum_sqs));
30 sum_sqs +=
square(x_ref.coeff(i, j));
43template <
typename T, require_std_vector_t<T>* =
nullptr>
45 return apply_vector_unary<T>::apply(
void check_square(const char *function, const char *name, const T_y &y)
Check if the specified matrix is square.
auto cholesky_corr_free(const T &x)
Overload of cholesky_corr_free() to untransform each matrix in a standard vector.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
T corr_free(const T &y)
Return the unconstrained scalar that when transformed to a valid correlation produces the specified v...
fvar< T > square(const fvar< T > &x)
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...