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>
27 using std::exp;
28 auto&& x_ref = to_ref(std::forward<EigVec>(x));
29 Eigen::Index k = x_ref.size();
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
55template <typename EigVec, typename Lp,
58inline auto ordered_constrain(EigVec&& x, Lp& lp) {
59 auto&& x_ref = to_ref(std::forward<EigVec>(x));
60 if (likely(x_ref.size() > 1)) {
61 lp += sum(x_ref.tail(x_ref.size() - 1));
62 }
63 return ordered_constrain(std::forward<decltype(x_ref)>(x_ref));
64}
65
78template <typename T, require_std_vector_t<T>* = nullptr>
79inline auto ordered_constrain(T&& x) {
80 return apply_vector_unary<T>::apply(std::forward<T>(x), [](auto&& v) {
81 return ordered_constrain(std::forward<decltype(v)>(v));
82 });
83}
84
100template <typename T, typename Lp, require_std_vector_t<T>* = nullptr,
101 require_convertible_t<return_type_t<T>, Lp>* = nullptr>
102inline auto ordered_constrain(T&& x, Lp& lp) {
103 return apply_vector_unary<T>::apply(std::forward<T>(x), [&lp](auto&& v) {
104 return ordered_constrain(std::forward<decltype(v)>(v), lp);
105 });
106}
107
128template <bool Jacobian, typename T, typename Lp,
130inline auto ordered_constrain(T&& x, Lp& lp) {
131 if constexpr (Jacobian) {
132 return ordered_constrain(std::forward<T>(x), lp);
133 } else {
134 return ordered_constrain(std::forward<T>(x));
135 }
136}
137
138} // namespace math
139} // namespace stan
140
141#endif
#define likely(x)
#define unlikely(x)
require_t< std::is_convertible< std::decay_t< T >, std::decay_t< S > > > require_convertible_t
Require types T and S satisfies std::is_convertible.
require_t< is_eigen_col_vector< std::decay_t< T > > > require_eigen_col_vector_t
Require type satisfies is_eigen_col_vector.
Definition is_vector.hpp:98
plain_type_t< EigVec > ordered_constrain(EigVec &&x)
Return an increasing ordered vector derived from the specified free vector.
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:18
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:15
typename plain_type< std::decay_t< T > >::type plain_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...