Automatic Differentiation
 
Loading...
Searching...
No Matches
positive_ordered_constrain.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_CONSTRAINT_POSITIVE_ORDERED_CONSTRAIN_HPP
2#define STAN_MATH_PRIM_CONSTRAINT_POSITIVE_ORDERED_CONSTRAIN_HPP
3
8#include <cmath>
9
10namespace stan {
11namespace math {
12
22template <typename EigVec, require_eigen_col_vector_t<EigVec>* = nullptr,
23 require_not_st_var<EigVec>* = nullptr>
24inline auto positive_ordered_constrain(const EigVec& x) {
25 using std::exp;
26 Eigen::Index k = x.size();
28 if (k == 0) {
29 return y;
30 }
31 const auto& x_ref = to_ref(x);
32 y.coeffRef(0) = exp(x_ref.coeff(0));
33 for (Eigen::Index i = 1; i < k; ++i) {
34 y.coeffRef(i) = y.coeff(i - 1) + exp(x_ref.coeff(i));
35 }
36 return y;
37}
38
53template <typename Vec, typename Lp, require_col_vector_t<Vec>* = nullptr,
54 require_convertible_t<return_type_t<Vec>, Lp>* = nullptr>
55
56inline auto positive_ordered_constrain(Vec&& x, Lp& lp) {
57 auto&& x_ref = to_ref(std::forward<Vec>(x));
58 lp += sum(x_ref);
59 return positive_ordered_constrain(std::forward<decltype(x_ref)>(x_ref));
60}
61
73template <typename T, require_std_vector_t<T>* = nullptr>
74inline auto positive_ordered_constrain(T&& x) {
75 return apply_vector_unary<T>::apply(std::forward<T>(x), [](auto&& v) {
76 return positive_ordered_constrain(std::forward<decltype(v)>(v));
77 });
78}
79
94template <typename T, typename Lp, require_std_vector_t<T>* = nullptr,
95 require_convertible_t<return_type_t<T>, Lp>* = nullptr>
96inline auto positive_ordered_constrain(T&& x, Lp& lp) {
97 return apply_vector_unary<T>::apply(std::forward<T>(x), [&lp](auto&& v) {
98 return positive_ordered_constrain(std::forward<decltype(v)>(v), lp);
99 });
100}
101
121template <bool Jacobian, typename Vec, typename Lp,
123inline auto positive_ordered_constrain(Vec&& x, Lp& lp) {
124 if constexpr (Jacobian) {
125 return positive_ordered_constrain(std::forward<Vec>(x), lp);
126 } else {
127 return positive_ordered_constrain(std::forward<Vec>(x));
128 }
129}
130
131} // namespace math
132} // namespace stan
133
134#endif
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.
auto positive_ordered_constrain(const EigVec &x)
Return an increasing positive 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 ...