1#ifndef STAN_MATH_PRIM_CONSTRAINT_SIMPLEX_FREE_HPP
2#define STAN_MATH_PRIM_CONSTRAINT_SIMPLEX_FREE_HPP
29template <
typename Vec, require_eigen_vector_t<Vec>* =
nullptr>
34 const auto& x_ref =
to_ref(x);
35 check_simplex(
"stan::math::simplex_free",
"Simplex variable", x_ref);
36 Eigen::Index Km1 = x_ref.size() - 1;
38 T stick_len = x_ref.coeff(Km1);
39 for (Eigen::Index k = Km1; --k >= 0;) {
40 stick_len += x_ref.coeff(k);
41 T z_k = x_ref.coeff(k) / stick_len;
42 y.coeffRef(k) =
logit(z_k) +
log(Km1 - k);
55template <
typename T, require_std_vector_t<T>* =
nullptr>
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
fvar< T > logit(const fvar< T > &x)
fvar< T > log(const fvar< T > &x)
plain_type_t< Vec > simplex_free(const Vec &x)
Return an unconstrained vector that when transformed produces the specified simplex.
void check_simplex(const char *function, const char *name, const T &theta)
Throw an exception if the specified vector is not a simplex.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
typename plain_type< T >::type plain_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...