Automatic Differentiation
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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(const Vec& x, Lp& lp) {
57 const auto& x_ref = to_ref(x);
58 lp += sum(x_ref);
59 return positive_ordered_constrain(x_ref);
60}
61
73template <typename T, require_std_vector_t<T>* = nullptr>
74inline auto positive_ordered_constrain(const T& x) {
76 x, [](auto&& v) { return positive_ordered_constrain(v); });
77}
78
93template <typename T, typename Lp, require_std_vector_t<T>* = nullptr,
94 require_convertible_t<return_type_t<T>, Lp>* = nullptr>
95inline auto positive_ordered_constrain(const T& x, Lp& lp) {
97 x, [&lp](auto&& v) { return positive_ordered_constrain(v, lp); });
98}
99
119template <bool Jacobian, typename Vec, typename Lp,
121inline auto positive_ordered_constrain(const Vec& x, Lp& lp) {
122 if constexpr (Jacobian) {
123 return positive_ordered_constrain(x, lp);
124 } else {
126 }
127}
128
129} // namespace math
130} // namespace stan
131
132#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.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
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
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:15
typename plain_type< T >::type plain_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...