1#ifndef STAN_MATH_PRIM_CONSTRAINT_SUM_TO_ZERO_FREE_HPP
2#define STAN_MATH_PRIM_CONSTRAINT_SUM_TO_ZERO_FREE_HPP
37template <
typename Vec, require_eigen_vector_t<Vec>* =
nullptr>
39 const auto& z_ref =
to_ref(z);
43 const auto N = z.size() - 1;
50 y.coeffRef(N - 1) = -z_ref(N) *
sqrt(N * (N + 1)) / N;
54 for (
int i = N - 2; i >= 0; --i) {
55 double n =
static_cast<double>(i + 1);
56 auto w = y(i + 1) /
sqrt((n + 1) * (n + 2));
58 y.coeffRef(i) = (sum_w - z_ref(i + 1)) *
sqrt(n * (n + 1)) / n;
71template <
typename T, require_std_vector_t<T>* =
nullptr>
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
plain_type_t< Vec > sum_to_zero_free(const Vec &z)
Return an unconstrained vector.
void check_sum_to_zero(const char *function, const char *name, const T &theta)
Throw an exception if the specified vector does not sum to 0.
fvar< T > sqrt(const fvar< T > &x)
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 ...