Automatic Differentiation
 
Loading...
Searching...
No Matches
lb_constrain.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_CONSTRAINT_LB_CONSTRAIN_HPP
2#define STAN_MATH_PRIM_CONSTRAINT_LB_CONSTRAIN_HPP
3
14#include <cmath>
15
16namespace stan {
17namespace math {
18
35template <typename T, typename L, require_all_stan_scalar_t<T, L>* = nullptr,
36 require_all_not_st_var<T, L>* = nullptr>
37inline auto lb_constrain(const T& x, const L& lb) {
39 return identity_constrain(x, lb);
40 } else {
41 return add(exp(x), lb);
42 }
43}
44
58template <typename T, typename L, require_all_stan_scalar_t<T, L>* = nullptr,
59 require_all_not_st_var<T, L>* = nullptr>
60inline auto lb_constrain(const T& x, const L& lb, return_type_t<T, L>& lp) {
61 if (value_of_rec(lb) == NEGATIVE_INFTY) {
62 return identity_constrain(x, lb);
63 } else {
64 lp += x;
65 return add(exp(x), lb);
66 }
67}
68
79template <typename T, typename L, require_eigen_t<T>* = nullptr,
80 require_stan_scalar_t<L>* = nullptr,
81 require_all_not_st_var<T, L>* = nullptr>
82inline auto lb_constrain(T&& x, L&& lb) {
83 return eval(x.unaryExpr([lb](auto&& x) { return lb_constrain(x, lb); }));
84}
85
97template <typename T, typename L, require_eigen_t<T>* = nullptr,
98 require_stan_scalar_t<L>* = nullptr,
99 require_all_not_st_var<T, L>* = nullptr>
100inline auto lb_constrain(const T& x, const L& lb, return_type_t<T, L>& lp) {
101 return eval(
102 x.unaryExpr([lb, &lp](auto&& xx) { return lb_constrain(xx, lb, lp); }));
103}
104
115template <typename T, typename L, require_all_eigen_t<T, L>* = nullptr,
116 require_all_not_st_var<T, L>* = nullptr>
117inline auto lb_constrain(T&& x, L&& lb) {
118 check_matching_dims("lb_constrain", "x", x, "lb", lb);
119 return eval(x.binaryExpr(
120 lb, [](auto&& x, auto&& lb) { return lb_constrain(x, lb); }));
121}
122
134template <typename T, typename L, require_all_eigen_t<T, L>* = nullptr,
135 require_all_not_st_var<T, L>* = nullptr>
136inline auto lb_constrain(const T& x, const L& lb, return_type_t<T, L>& lp) {
137 check_matching_dims("lb_constrain", "x", x, "lb", lb);
138 return eval(x.binaryExpr(
139 lb, [&lp](auto&& xx, auto&& lbb) { return lb_constrain(xx, lbb, lp); }));
140}
141
152template <typename T, typename L, require_not_std_vector_t<L>* = nullptr>
153inline auto lb_constrain(const std::vector<T>& x, const L& lb) {
154 std::vector<plain_type_t<decltype(lb_constrain(x[0], lb))>> ret(x.size());
155 for (size_t i = 0; i < x.size(); ++i) {
156 ret[i] = lb_constrain(x[i], lb);
157 }
158 return ret;
159}
160
172template <typename T, typename L, require_not_std_vector_t<L>* = nullptr>
173inline auto lb_constrain(const std::vector<T>& x, const L& lb,
175 std::vector<plain_type_t<decltype(lb_constrain(x[0], lb))>> ret(x.size());
176 for (size_t i = 0; i < x.size(); ++i) {
177 ret[i] = lb_constrain(x[i], lb, lp);
178 }
179 return ret;
180}
181
192template <typename T, typename L>
193inline auto lb_constrain(const std::vector<T>& x, const std::vector<L>& lb) {
194 check_matching_dims("lb_constrain", "x", x, "lb", lb);
195 std::vector<plain_type_t<decltype(lb_constrain(x[0], lb[0]))>> ret(x.size());
196 for (size_t i = 0; i < x.size(); ++i) {
197 ret[i] = lb_constrain(x[i], lb[i]);
198 }
199 return ret;
200}
201
213template <typename T, typename L>
214inline auto lb_constrain(const std::vector<T>& x, const std::vector<L>& lb,
216 check_matching_dims("lb_constrain", "x", x, "lb", lb);
217 std::vector<plain_type_t<decltype(lb_constrain(x[0], lb[0]))>> ret(x.size());
218 for (size_t i = 0; i < x.size(); ++i) {
219 ret[i] = lb_constrain(x[i], lb[i], lp);
220 }
221 return ret;
222}
223
242template <bool Jacobian, typename T, typename L>
243inline auto lb_constrain(const T& x, const L& lb, return_type_t<T, L>& lp) {
244 if (Jacobian) {
245 return lb_constrain(x, lb, lp);
246 } else {
247 return lb_constrain(x, lb);
248 }
249}
250
251} // namespace math
252} // namespace stan
253
254#endif
#define unlikely(x)
addition_< as_operation_cl_t< T_a >, as_operation_cl_t< T_b > > add(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
static constexpr double NEGATIVE_INFTY
Negative infinity.
Definition constants.hpp:51
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 lb_constrain(T &&x, L &&lb)
Return the lower-bounded value for the specified unconstrained input and specified lower bound.
auto identity_constrain(T &&x, Types &&...)
Returns the result of applying the identity constraint transform to the input.
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 ...