Automatic Differentiation
 
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, require_all_stan_scalar_t<T, U>* = nullptr,
66 require_all_not_st_var<T, U>* = nullptr>
67inline auto ub_constrain(const T& x, const U& ub,
68 std::decay_t<return_type_t<T, U>>& lp) {
69 if (value_of_rec(ub) == INFTY) {
70 return identity_constrain(x, ub);
71 } else {
72 lp += x;
73 return subtract(ub, exp(x));
74 }
75}
76
87template <typename T, typename U, require_eigen_t<T>* = nullptr,
88 require_stan_scalar_t<U>* = nullptr,
89 require_all_not_st_var<T, U>* = nullptr>
90inline auto ub_constrain(const T& x, const U& ub) {
91 return eval(x.unaryExpr([ub](auto&& xx) { return ub_constrain(xx, ub); }));
92}
93
105template <typename T, typename U, require_eigen_t<T>* = nullptr,
106 require_stan_scalar_t<U>* = nullptr,
107 require_all_not_st_var<T, U>* = nullptr>
108inline auto ub_constrain(const T& x, const U& ub,
109 std::decay_t<return_type_t<T, U>>& lp) {
110 return eval(
111 x.unaryExpr([ub, &lp](auto&& xx) { return ub_constrain(xx, ub, lp); }));
112}
113
124template <typename T, typename U, require_all_eigen_t<T, U>* = nullptr,
125 require_all_not_st_var<T, U>* = nullptr>
126inline auto ub_constrain(const T& x, const U& ub) {
127 check_matching_dims("ub_constrain", "x", x, "ub", ub);
128 return eval(x.binaryExpr(
129 ub, [](auto&& xx, auto&& ubb) { return ub_constrain(xx, ubb); }));
130}
131
143template <typename T, typename U, require_all_eigen_t<T, U>* = nullptr,
144 require_all_not_st_var<T, U>* = nullptr>
145inline auto ub_constrain(const T& x, const U& ub,
146 std::decay_t<return_type_t<T, U>>& lp) {
147 check_matching_dims("ub_constrain", "x", x, "ub", ub);
148 return eval(x.binaryExpr(
149 ub, [&lp](auto&& xx, auto&& ubb) { return ub_constrain(xx, ubb, lp); }));
150}
151
162template <typename T, typename U, require_not_std_vector_t<U>* = nullptr>
163inline auto ub_constrain(const std::vector<T>& x, const U& ub) {
164 std::vector<plain_type_t<decltype(ub_constrain(x[0], ub))>> ret(x.size());
165 for (size_t i = 0; i < x.size(); ++i) {
166 ret[i] = ub_constrain(x[i], ub);
167 }
168 return ret;
169}
170
182template <typename T, typename U, require_not_std_vector_t<U>* = nullptr>
183inline auto ub_constrain(const std::vector<T>& x, const U& ub,
185 std::vector<plain_type_t<decltype(ub_constrain(x[0], ub))>> ret(x.size());
186 for (size_t i = 0; i < x.size(); ++i) {
187 ret[i] = ub_constrain(x[i], ub, lp);
188 }
189 return ret;
190}
191
202template <typename T, typename U>
203inline auto ub_constrain(const std::vector<T>& x, const std::vector<U>& ub) {
204 check_matching_dims("ub_constrain", "x", x, "ub", ub);
205 std::vector<plain_type_t<decltype(ub_constrain(x[0], ub[0]))>> ret(x.size());
206 for (size_t i = 0; i < x.size(); ++i) {
207 ret[i] = ub_constrain(x[i], ub[i]);
208 }
209 return ret;
210}
211
223template <typename T, typename U>
224inline auto ub_constrain(const std::vector<T>& x, const std::vector<U>& ub,
226 check_matching_dims("ub_constrain", "x", x, "ub", ub);
227 std::vector<plain_type_t<decltype(ub_constrain(x[0], ub[0]))>> ret(x.size());
228 for (size_t i = 0; i < x.size(); ++i) {
229 ret[i] = ub_constrain(x[i], ub[i], lp);
230 }
231 return ret;
232}
233
252template <bool Jacobian, typename T, typename U>
253inline auto ub_constrain(const T& x, const U& ub, return_type_t<T, U>& lp) {
254 if (Jacobian) {
255 return ub_constrain(x, ub, lp);
256 } else {
257 return ub_constrain(x, ub);
258 }
259}
260
261} // namespace math
262} // namespace stan
263
264#endif
subtraction_< as_operation_cl_t< T_a >, as_operation_cl_t< T_b > > subtract(T_a &&a, T_b &&b)
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
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:13
typename plain_type< T >::type plain_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...