Automatic Differentiation
 
Loading...
Searching...
No Matches
ordered_constrain.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_CONSTRAINT_ORDERED_CONSTRAIN_HPP
2#define STAN_MATH_PRIM_CONSTRAINT_ORDERED_CONSTRAIN_HPP
3
9#include <cmath>
10
11namespace stan {
12namespace math {
13
24template <typename EigVec, require_eigen_col_vector_t<EigVec>* = nullptr,
25 require_not_st_var<EigVec>* = nullptr>
26inline plain_type_t<EigVec> ordered_constrain(const EigVec& x) {
27 using std::exp;
28 Eigen::Index k = x.size();
30 const auto& x_ref = to_ref(x);
31 if (unlikely(k == 0)) {
32 return y;
33 }
34 y[0] = x_ref[0];
35 for (Eigen::Index i = 1; i < k; ++i) {
36 y.coeffRef(i) = y.coeff(i - 1) + exp(x_ref.coeff(i));
37 }
38 return y;
39}
40
53template <typename EigVec, require_eigen_col_vector_t<EigVec>* = nullptr>
54inline auto ordered_constrain(const EigVec& x, value_type_t<EigVec>& lp) {
55 const auto& x_ref = to_ref(x);
56 if (likely(x.size() > 1)) {
57 lp += sum(x_ref.tail(x.size() - 1));
58 }
59 return ordered_constrain(x_ref);
60}
61
80template <bool Jacobian, typename T, require_not_std_vector_t<T>* = nullptr>
81inline auto ordered_constrain(const T& x, return_type_t<T>& lp) {
82 if (Jacobian) {
83 return ordered_constrain(x, lp);
84 } else {
85 return ordered_constrain(x);
86 }
87}
88
107template <bool Jacobian, typename T, require_std_vector_t<T>* = nullptr>
108inline auto ordered_constrain(const T& x, return_type_t<T>& lp) {
109 return apply_vector_unary<T>::apply(
110 x, [&lp](auto&& v) { return ordered_constrain<Jacobian>(v, lp); });
111}
112
113} // namespace math
114} // namespace stan
115
116#endif
#define likely(x)
#define unlikely(x)
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
plain_type_t< EigVec > ordered_constrain(const EigVec &x)
Return an increasing ordered vector derived from the specified free vector.
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:13
typename plain_type< T >::type plain_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...