Automatic Differentiation
 
Loading...
Searching...
No Matches
lub_constrain.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_CONSTRAINT_LUB_CONSTRAIN_HPP
2#define STAN_MATH_PRIM_CONSTRAINT_LUB_CONSTRAIN_HPP
3
20#include <cmath>
21
22namespace stan {
23namespace math {
24
44template <typename T, typename L, typename U,
45 require_all_stan_scalar_t<T, L, U>* = nullptr,
46 require_not_var_t<return_type_t<T, L, U>>* = nullptr>
47inline auto lub_constrain(T&& x, L&& lb, U&& ub) {
48 const bool is_lb_inf = value_of(lb) == NEGATIVE_INFTY;
49 const bool is_ub_inf = value_of(ub) == INFTY;
50 if (unlikely(is_ub_inf && is_lb_inf)) {
51 return identity_constrain(x, lb, ub);
52 } else if (unlikely(is_ub_inf)) {
53 return lb_constrain(identity_constrain(x, ub), lb);
54 } else if (unlikely(is_lb_inf)) {
55 return ub_constrain(identity_constrain(x, lb), ub);
56 } else {
57 check_less("lub_constrain", "lb", value_of(lb), value_of(ub));
58 return (ub - lb) * inv_logit(x) + lb;
59 }
60}
61
95template <typename T, typename L, typename U,
98inline auto lub_constrain(T&& x, L&& lb, U&& ub, return_type_t<T, L, U>& lp) {
99 const bool is_lb_inf = value_of(lb) == NEGATIVE_INFTY;
100 const bool is_ub_inf = value_of(ub) == INFTY;
101 if (unlikely(is_ub_inf && is_lb_inf)) {
102 return identity_constrain(x, ub, lb);
103 } else if (unlikely(is_ub_inf)) {
104 return lb_constrain(identity_constrain(x, ub), lb, lp);
105 } else if (unlikely(is_lb_inf)) {
106 return ub_constrain(identity_constrain(x, lb), ub, lp);
107 } else {
108 check_less("lub_constrain", "lb", value_of(lb), value_of(ub));
109 const auto diff = ub - lb;
110 lp += add(log(diff), subtract(-abs(x), multiply(2.0, log1p_exp(-abs(x)))));
111 return diff * inv_logit(x) + lb;
112 }
113}
114
118template <typename T, typename L, typename U, require_eigen_t<T>* = nullptr,
119 require_all_stan_scalar_t<L, U>* = nullptr,
120 require_not_var_t<return_type_t<T, L, U>>* = nullptr>
121inline auto lub_constrain(const T& x, const L& lb, const U& ub) {
122 return eval(
123 x.unaryExpr([ub, lb](auto&& xx) { return lub_constrain(xx, lb, ub); }));
124}
125
129template <typename T, typename L, typename U, require_eigen_t<T>* = nullptr,
130 require_all_stan_scalar_t<L, U>* = nullptr,
131 require_not_var_t<return_type_t<T, L, U>>* = nullptr>
132inline auto lub_constrain(const T& x, const L& lb, const U& ub,
134 return eval(x.unaryExpr(
135 [lb, ub, &lp](auto&& xx) { return lub_constrain(xx, lb, ub, lp); }));
136}
137
142template <typename T, typename L, typename U,
143 require_all_eigen_t<T, L>* = nullptr,
144 require_stan_scalar_t<U>* = nullptr,
146inline auto lub_constrain(const T& x, const L& lb, const U& ub) {
147 check_matching_dims("lub_constrain", "x", x, "lb", lb);
148 return eval(x.binaryExpr(
149 lb, [ub](auto&& x, auto&& lb) { return lub_constrain(x, lb, ub); }));
150}
151
156template <typename T, typename L, typename U,
157 require_all_eigen_t<T, L>* = nullptr,
158 require_stan_scalar_t<U>* = nullptr,
159 require_not_var_t<return_type_t<T, L, U>>* = nullptr>
160inline auto lub_constrain(const T& x, const L& lb, const U& ub,
161 return_type_t<T, L, U>& lp) {
162 check_matching_dims("lub_constrain", "x", x, "lb", lb);
163 return eval(x.binaryExpr(lb, [ub, &lp](auto&& x, auto&& lb) {
164 return lub_constrain(x, lb, ub, lp);
165 }));
166}
167
172template <typename T, typename L, typename U,
173 require_all_eigen_t<T, U>* = nullptr,
174 require_stan_scalar_t<L>* = nullptr,
175 require_not_var_t<return_type_t<T, L, U>>* = nullptr>
176inline auto lub_constrain(const T& x, const L& lb, const U& ub) {
177 check_matching_dims("lub_constrain", "x", x, "ub", ub);
178 return eval(x.binaryExpr(
179 ub, [lb](auto&& x, auto&& ub) { return lub_constrain(x, lb, ub); }));
180}
181
186template <typename T, typename L, typename U,
187 require_all_eigen_t<T, U>* = nullptr,
188 require_stan_scalar_t<L>* = nullptr,
189 require_not_var_t<return_type_t<T, L, U>>* = nullptr>
190inline auto lub_constrain(const T& x, const L& lb, const U& ub,
191 return_type_t<T, L, U>& lp) {
192 check_matching_dims("lub_constrain", "x", x, "ub", ub);
193 return eval(x.binaryExpr(ub, [lb, &lp](auto&& x, auto&& ub) {
194 return lub_constrain(x, lb, ub, lp);
195 }));
196}
197
201template <typename T, typename L, typename U,
202 require_all_eigen_t<T, L, U>* = nullptr,
203 require_not_var_t<return_type_t<T, L, U>>* = nullptr>
204inline auto lub_constrain(const T& x, const L& lb, const U& ub) {
205 check_matching_dims("lub_constrain", "x", x, "lb", lb);
206 check_matching_dims("lub_constrain", "x", x, "ub", ub);
207 auto x_ref = to_ref(x);
208 auto lb_ref = to_ref(lb);
209 auto ub_ref = to_ref(ub);
210 promote_scalar_t<return_type_t<T, L, U>, T> x_ret(x.rows(), x.cols());
211 for (Eigen::Index j = 0; j < x_ref.cols(); ++j) {
212 for (Eigen::Index i = 0; i < x_ref.rows(); ++i) {
213 x_ret.coeffRef(i, j) = lub_constrain(
214 x_ref.coeff(i, j), lb_ref.coeff(i, j), ub_ref.coeff(i, j));
215 }
216 }
217 return x_ret;
218}
219
223template <typename T, typename L, typename U,
226inline auto lub_constrain(const T& x, const L& lb, const U& ub,
228 check_matching_dims("lub_constrain", "x", x, "lb", lb);
229 check_matching_dims("lub_constrain", "x", x, "ub", ub);
230 auto x_ref = to_ref(x);
231 auto lb_ref = to_ref(lb);
232 auto ub_ref = to_ref(ub);
233 promote_scalar_t<return_type_t<T, L, U>, T> x_ret(x.rows(), x.cols());
234 for (Eigen::Index j = 0; j < x_ref.cols(); ++j) {
235 for (Eigen::Index i = 0; i < x_ref.rows(); ++i) {
236 x_ret.coeffRef(i, j) = lub_constrain(
237 x_ref.coeff(i, j), lb_ref.coeff(i, j), ub_ref.coeff(i, j), lp);
238 }
239 }
240 return x_ret;
241}
242
246template <typename T, typename L, typename U,
247 require_all_not_std_vector_t<L, U>* = nullptr>
248inline auto lub_constrain(const std::vector<T>& x, const L& lb, const U& ub) {
249 std::vector<plain_type_t<decltype(lub_constrain(x[0], lb, ub))>> ret(
250 x.size());
251 for (size_t i = 0; i < x.size(); ++i) {
252 ret[i] = lub_constrain(x[i], lb, ub);
253 }
254 return ret;
255}
256
260template <typename T, typename L, typename U,
262inline auto lub_constrain(const std::vector<T>& x, const L& lb, const U& ub,
264 std::vector<plain_type_t<decltype(lub_constrain(x[0], lb, ub))>> ret(
265 x.size());
266 for (size_t i = 0; i < x.size(); ++i) {
267 ret[i] = lub_constrain(x[i], lb, ub, lp);
268 }
269 return ret;
270}
271
275template <typename T, typename L, typename U,
277inline auto lub_constrain(const std::vector<T>& x, const L& lb,
278 const std::vector<U>& ub) {
279 check_matching_dims("lub_constrain", "x", x, "ub", ub);
280 std::vector<plain_type_t<decltype(lub_constrain(x[0], lb, ub[0]))>> ret(
281 x.size());
282 for (size_t i = 0; i < x.size(); ++i) {
283 ret[i] = lub_constrain(x[i], lb, ub[i]);
284 }
285 return ret;
286}
287
291template <typename T, typename L, typename U,
293inline auto lub_constrain(const std::vector<T>& x, const L& lb,
294 const std::vector<U>& ub,
296 check_matching_dims("lub_constrain", "x", x, "ub", ub);
297 std::vector<plain_type_t<decltype(lub_constrain(x[0], lb, ub[0]))>> ret(
298 x.size());
299 for (size_t i = 0; i < x.size(); ++i) {
300 ret[i] = lub_constrain(x[i], lb, ub[i], lp);
301 }
302 return ret;
303}
304
308template <typename T, typename L, typename U,
310inline auto lub_constrain(const std::vector<T>& x, const std::vector<L>& lb,
311 const U& ub) {
312 check_matching_dims("lub_constrain", "x", x, "lb", lb);
313 std::vector<plain_type_t<decltype(lub_constrain(x[0], lb[0], ub))>> ret(
314 x.size());
315 for (size_t i = 0; i < x.size(); ++i) {
316 ret[i] = lub_constrain(x[i], lb[i], ub);
317 }
318 return ret;
319}
320
324template <typename T, typename L, typename U,
326inline auto lub_constrain(const std::vector<T>& x, const std::vector<L>& lb,
327 const U& ub, return_type_t<T, L, U>& lp) {
328 check_matching_dims("lub_constrain", "x", x, "lb", lb);
329 std::vector<plain_type_t<decltype(lub_constrain(x[0], lb[0], ub))>> ret(
330 x.size());
331 for (size_t i = 0; i < x.size(); ++i) {
332 ret[i] = lub_constrain(x[i], lb[i], ub, lp);
333 }
334 return ret;
335}
336
340template <typename T, typename L, typename U>
341inline auto lub_constrain(const std::vector<T>& x, const std::vector<L>& lb,
342 const std::vector<U>& ub) {
343 check_matching_dims("lub_constrain", "x", x, "lb", lb);
344 check_matching_dims("lub_constrain", "x", x, "ub", ub);
345 std::vector<plain_type_t<decltype(lub_constrain(x[0], lb[0], ub[0]))>> ret(
346 x.size());
347 for (size_t i = 0; i < x.size(); ++i) {
348 ret[i] = lub_constrain(x[i], lb[i], ub[i]);
349 }
350 return ret;
351}
352
356template <typename T, typename L, typename U>
357inline auto lub_constrain(const std::vector<T>& x, const std::vector<L>& lb,
358 const std::vector<U>& ub,
360 check_matching_dims("lub_constrain", "x", x, "lb", lb);
361 check_matching_dims("lub_constrain", "x", x, "ub", ub);
362 std::vector<plain_type_t<decltype(lub_constrain(x[0], lb[0], ub[0]))>> ret(
363 x.size());
364 for (size_t i = 0; i < x.size(); ++i) {
365 ret[i] = lub_constrain(x[i], lb[i], ub[i], lp);
366 }
367 return ret;
368}
369
394template <bool Jacobian, typename T, typename L, typename U>
395inline auto lub_constrain(const T& x, const L& lb, const U& ub,
397 if (Jacobian) {
398 return lub_constrain(x, lb, ub, lp);
399 } else {
400 return lub_constrain(x, lb, ub);
401 }
402}
403
407template <typename T, typename L, typename U>
408inline auto lub_constrain(const T& x, const std::tuple<L, U>& bounds) {
409 return lub_constrain(x, std::get<0>(bounds), std::get<1>(bounds));
410}
411
415template <typename T, typename L, typename U>
416inline auto lub_constrain(const T& x, const std::tuple<L, U>& bounds,
418 return lub_constrain(x, std::get<0>(bounds), std::get<1>(bounds), lp);
419}
420
424template <bool Jacobian, typename T, typename L, typename U>
425inline auto lub_constrain(const T& x, const std::tuple<L, U>& bounds,
427 return lub_constrain<Jacobian>(x, std::get<0>(bounds), std::get<1>(bounds),
428 lp);
429}
430
431} // namespace math
432} // namespace stan
433
434#endif
#define unlikely(x)
require_all_t< is_eigen< std::decay_t< Types > >... > require_all_eigen_t
Require all of the types satisfy is_eigen.
Definition is_eigen.hpp:120
subtraction_< as_operation_cl_t< T_a >, as_operation_cl_t< T_b > > subtract(T_a &&a, T_b &&b)
addition_< as_operation_cl_t< T_a >, as_operation_cl_t< T_b > > add(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_t< is_stan_scalar< std::decay_t< T > > > require_stan_scalar_t
Require type satisfies 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_std_vector< std::decay_t< Types > >... > require_all_not_std_vector_t
Require none of the types satisfy is_std_vector.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
require_not_t< is_var< std::decay_t< T > > > require_not_var_t
Require type does not satisfy is_var.
Definition is_var.hpp:29
fvar< T > abs(const fvar< T > &x)
Definition abs.hpp:15
typename promote_scalar_type< std::decay_t< T >, std::decay_t< S > >::type promote_scalar_t
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
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
fvar< T > log(const fvar< T > &x)
Definition log.hpp:18
static constexpr double NEGATIVE_INFTY
Negative infinity.
Definition constants.hpp:51
auto multiply(const Mat1 &m1, const Mat2 &m2)
Return the product of the specified matrices.
Definition multiply.hpp:19
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.
fvar< T > log1p_exp(const fvar< T > &x)
Definition log1p_exp.hpp:14
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
auto lb_constrain(T &&x, L &&lb)
Return the lower-bounded value for the specified unconstrained input and specified lower bound.
auto ub_constrain(T &&x, U &&ub)
Return the upper-bounded value for the specified unconstrained matrix and upper bound.
matrix_cl< double > lub_constrain(const T &x, const L &lb, const U &ub)
Return the lower and upper-bounded matrix derived by transforming the specified free matrix given the...
void check_less(const char *function, const char *name, const T_y &y, const T_high &high, Idxs... idxs)
Throw an exception if y is not strictly less than high.
fvar< T > inv_logit(const fvar< T > &x)
Returns the inverse logit function applied to the argument.
Definition inv_logit.hpp:20
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
typename plain_type< T >::type plain_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...