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>
33 const auto& x_ref =
to_ref(x);
34 check_simplex(
"stan::math::simplex_free",
"Simplex variable", x_ref);
35 Eigen::Index Km1 = x_ref.size() - 1;
39 for (
int i = 0; i < Km1; ++i) {
40 cumsum +=
log(x_ref.coeff(i));
41 double n =
static_cast<double>(i + 1);
42 y.coeffRef(i) = (cumsum - n *
log(x_ref.coeff(i + 1))) /
sqrt(n * (n + 1));
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 > 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.
fvar< T > sqrt(const fvar< T > &x)
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 ...