Loading [MathJax]/extensions/TeX/AMSmath.js
Automatic Differentiation
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
simplex_free.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_CONSTRAINT_SIMPLEX_FREE_HPP
2#define STAN_MATH_PRIM_CONSTRAINT_SIMPLEX_FREE_HPP
3
10#include <cmath>
11
12namespace stan {
13namespace math {
14
29template <typename Vec, require_eigen_vector_t<Vec>* = nullptr>
30inline plain_type_t<Vec> simplex_free(const Vec& x) {
31 using T = value_type_t<Vec>;
32
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;
36 plain_type_t<Vec> y(Km1);
37
38 T cumsum = 0.0;
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));
43 }
44
45 return y;
46}
47
55template <typename T, require_std_vector_t<T>* = nullptr>
56inline auto simplex_free(const T& x) {
58 [](auto&& v) { return simplex_free(v); });
59}
60
61} // namespace math
62} // namespace stan
63
64#endif
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
fvar< T > log(const fvar< T > &x)
Definition log.hpp:18
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)
Definition sqrt.hpp:18
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.
Definition to_ref.hpp:17
typename plain_type< T >::type plain_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...