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
51template <typename Vec, require_col_vector_t<Vec>* = nullptr>
52inline auto positive_ordered_constrain(const Vec& x, return_type_t<Vec>& lp) {
53 const auto& x_ref = to_ref(x);
54 lp += sum(x_ref);
55 return positive_ordered_constrain(x_ref);
56}
57
75template <bool Jacobian, typename Vec, require_not_std_vector_t<Vec>* = nullptr>
76inline auto positive_ordered_constrain(const Vec& x, return_type_t<Vec>& lp) {
77 if (Jacobian) {
78 return positive_ordered_constrain(x, lp);
79 } else {
81 }
82}
83
102template <bool Jacobian, typename T, require_std_vector_t<T>* = nullptr>
103inline auto positive_ordered_constrain(const T& x, return_type_t<T>& lp) {
104 return apply_vector_unary<T>::apply(x, [&lp](auto&& v) {
105 return positive_ordered_constrain<Jacobian>(v, lp);
106 });
107}
108
109} // namespace math
110} // namespace stan
111
112#endif
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 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 ...