Loading [MathJax]/extensions/TeX/AMSsymbols.js
Automatic Differentiation
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
ub_constrain.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_CONSTRAINT_UB_CONSTRAIN_HPP
2#define STAN_MATH_PRIM_CONSTRAINT_UB_CONSTRAIN_HPP
3
13#include <cmath>
14
15namespace stan {
16namespace math {
17
34template <typename T, typename U, require_all_stan_scalar_t<T, U>* = nullptr,
35 require_all_not_st_var<T, U>* = nullptr>
36inline auto ub_constrain(const T& x, const U& ub) {
37 if (value_of_rec(ub) == INFTY) {
38 return identity_constrain(x, ub);
39 } else {
40 return subtract(ub, exp(x));
41 }
42}
43
65template <typename T, typename U, typename Lp,
69inline auto ub_constrain(const T& x, const U& ub, Lp& lp) {
70 if (value_of_rec(ub) == INFTY) {
71 return identity_constrain(x, ub);
72 } else {
73 lp += x;
74 return subtract(ub, exp(x));
75 }
76}
77
88template <typename T, typename U, require_eigen_t<T>* = nullptr,
89 require_stan_scalar_t<U>* = nullptr,
90 require_all_not_st_var<T, U>* = nullptr>
91inline auto ub_constrain(const T& x, const U& ub) {
92 return eval(x.unaryExpr([ub](auto&& xx) { return ub_constrain(xx, ub); }));
93}
94
107template <typename T, typename U, typename Lp, require_eigen_t<T>* = nullptr,
108 require_stan_scalar_t<U>* = nullptr,
109 require_all_not_st_var<T, U>* = nullptr,
110 require_convertible_t<return_type_t<T, U>, Lp>* = nullptr>
111inline auto ub_constrain(const T& x, const U& ub, Lp& lp) {
112 return eval(
113 x.unaryExpr([ub, &lp](auto&& xx) { return ub_constrain(xx, ub, lp); }));
114}
115
126template <typename T, typename U, require_all_eigen_t<T, U>* = nullptr,
127 require_all_not_st_var<T, U>* = nullptr>
128inline auto ub_constrain(const T& x, const U& ub) {
129 check_matching_dims("ub_constrain", "x", x, "ub", ub);
130 return eval(x.binaryExpr(
131 ub, [](auto&& xx, auto&& ubb) { return ub_constrain(xx, ubb); }));
132}
133
146template <typename T, typename U, typename Lp,
147 require_all_eigen_t<T, U>* = nullptr,
148 require_all_not_st_var<T, U>* = nullptr,
149 require_convertible_t<return_type_t<T, U>, Lp>* = nullptr>
150inline auto ub_constrain(const T& x, const U& ub, Lp& lp) {
151 check_matching_dims("ub_constrain", "x", x, "ub", ub);
152 return eval(x.binaryExpr(
153 ub, [&lp](auto&& xx, auto&& ubb) { return ub_constrain(xx, ubb, lp); }));
154}
155
166template <typename T, typename U, require_not_std_vector_t<U>* = nullptr>
167inline auto ub_constrain(const std::vector<T>& x, const U& ub) {
168 std::vector<plain_type_t<decltype(ub_constrain(x[0], ub))>> ret(x.size());
169 for (size_t i = 0; i < x.size(); ++i) {
170 ret[i] = ub_constrain(x[i], ub);
171 }
172 return ret;
173}
174
187template <typename T, typename U, typename Lp,
190inline auto ub_constrain(const std::vector<T>& x, const U& ub, Lp& lp) {
191 std::vector<plain_type_t<decltype(ub_constrain(x[0], ub))>> ret(x.size());
192 for (size_t i = 0; i < x.size(); ++i) {
193 ret[i] = ub_constrain(x[i], ub, lp);
194 }
195 return ret;
196}
197
208template <typename T, typename U>
209inline auto ub_constrain(const std::vector<T>& x, const std::vector<U>& ub) {
210 check_matching_dims("ub_constrain", "x", x, "ub", ub);
211 std::vector<plain_type_t<decltype(ub_constrain(x[0], ub[0]))>> ret(x.size());
212 for (size_t i = 0; i < x.size(); ++i) {
213 ret[i] = ub_constrain(x[i], ub[i]);
214 }
215 return ret;
216}
217
230template <typename T, typename U, typename Lp,
232inline auto ub_constrain(const std::vector<T>& x, const std::vector<U>& ub,
233 Lp& lp) {
234 check_matching_dims("ub_constrain", "x", x, "ub", ub);
235 std::vector<plain_type_t<decltype(ub_constrain(x[0], ub[0]))>> ret(x.size());
236 for (size_t i = 0; i < x.size(); ++i) {
237 ret[i] = ub_constrain(x[i], ub[i], lp);
238 }
239 return ret;
240}
241
262template <bool Jacobian, typename T, typename U, typename Lp,
264inline auto ub_constrain(const T& x, const U& ub, Lp& lp) {
265 if constexpr (Jacobian) {
266 return ub_constrain(x, ub, lp);
267 } else {
268 return ub_constrain(x, ub);
269 }
270}
271
272} // namespace math
273} // namespace stan
274
275#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.
subtraction_< as_operation_cl_t< T_a >, as_operation_cl_t< T_b > > subtract(T_a &&a, T_b &&b)
require_all_t< is_stan_scalar< std::decay_t< Types > >... > require_all_stan_scalar_t
Require all of the types satisfy is_stan_scalar.
require_not_t< is_std_vector< std::decay_t< T > > > require_not_std_vector_t
Require type does not satisfy is_std_vector.
require_all_not_t< is_var< scalar_type_t< std::decay_t< Types > > >... > require_all_not_st_var
Require none of the scalar types satisfy is_var.
Definition is_var.hpp:138
double value_of_rec(const fvar< T > &v)
Return the value of the specified variable.
T eval(T &&arg)
Inputs which have a plain_type equal to the own time are forwarded unmodified (for Eigen expressions ...
Definition eval.hpp:20
void check_matching_dims(const char *function, const char *name1, const T1 &y1, const char *name2, const T2 &y2)
Check if the two containers have the same dimensions.
auto ub_constrain(T &&x, U &&ub)
Return the upper-bounded value for the specified unconstrained matrix and upper bound.
auto identity_constrain(T &&x, Types &&...)
Returns the result of applying the identity constraint transform to the input.
static constexpr double INFTY
Positive infinity.
Definition constants.hpp:46
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 ...