Loading [MathJax]/extensions/TeX/AMSmath.js
Automatic Differentiation
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
59template <typename T, typename L, typename Lp,
63inline auto lb_constrain(const T& x, const L& lb, Lp& lp) {
64 if (value_of_rec(lb) == NEGATIVE_INFTY) {
65 return identity_constrain(x, lb);
66 } else {
67 lp += x;
68 return add(exp(x), lb);
69 }
70}
71
82template <typename T, typename L, require_eigen_t<T>* = nullptr,
83 require_stan_scalar_t<L>* = nullptr,
84 require_all_not_st_var<T, L>* = nullptr>
85inline auto lb_constrain(T&& x, L&& lb) {
86 return eval(x.unaryExpr([lb](auto&& x) { return lb_constrain(x, lb); }));
87}
88
101template <typename T, typename L, typename Lp, require_eigen_t<T>* = nullptr,
102 require_stan_scalar_t<L>* = nullptr,
103 require_all_not_st_var<T, L>* = nullptr,
104 require_convertible_t<return_type_t<T, L>, Lp>* = nullptr>
105
106inline auto lb_constrain(const T& x, const L& lb, Lp& lp) {
107 return eval(
108 x.unaryExpr([lb, &lp](auto&& xx) { return lb_constrain(xx, lb, lp); }));
109}
110
121template <typename T, typename L, require_all_eigen_t<T, L>* = nullptr,
122 require_all_not_st_var<T, L>* = nullptr>
123inline auto lb_constrain(T&& x, L&& lb) {
124 check_matching_dims("lb_constrain", "x", x, "lb", lb);
125 return eval(x.binaryExpr(
126 lb, [](auto&& x, auto&& lb) { return lb_constrain(x, lb); }));
127}
128
141template <typename T, typename L, typename Lp,
142 require_all_eigen_t<T, L>* = nullptr,
143 require_all_not_st_var<T, L>* = nullptr,
144 require_convertible_t<return_type_t<T, L>, Lp>* = nullptr>
145inline auto lb_constrain(const T& x, const L& lb, Lp& lp) {
146 check_matching_dims("lb_constrain", "x", x, "lb", lb);
147 return eval(x.binaryExpr(
148 lb, [&lp](auto&& xx, auto&& lbb) { return lb_constrain(xx, lbb, lp); }));
149}
150
161template <typename T, typename L, require_not_std_vector_t<L>* = nullptr>
162inline auto lb_constrain(const std::vector<T>& x, const L& lb) {
163 std::vector<plain_type_t<decltype(lb_constrain(x[0], lb))>> ret(x.size());
164 for (size_t i = 0; i < x.size(); ++i) {
165 ret[i] = lb_constrain(x[i], lb);
166 }
167 return ret;
168}
169
182template <typename T, typename L, typename Lp,
185inline auto lb_constrain(const std::vector<T>& x, const L& lb, Lp& lp) {
186 std::vector<plain_type_t<decltype(lb_constrain(x[0], lb))>> ret(x.size());
187 for (size_t i = 0; i < x.size(); ++i) {
188 ret[i] = lb_constrain(x[i], lb, lp);
189 }
190 return ret;
191}
192
203template <typename T, typename L>
204inline auto lb_constrain(const std::vector<T>& x, const std::vector<L>& lb) {
205 check_matching_dims("lb_constrain", "x", x, "lb", lb);
206 std::vector<plain_type_t<decltype(lb_constrain(x[0], lb[0]))>> ret(x.size());
207 for (size_t i = 0; i < x.size(); ++i) {
208 ret[i] = lb_constrain(x[i], lb[i]);
209 }
210 return ret;
211}
212
225template <typename T, typename L, typename Lp,
227inline auto lb_constrain(const std::vector<T>& x, const std::vector<L>& lb,
228 Lp& lp) {
229 check_matching_dims("lb_constrain", "x", x, "lb", lb);
230 std::vector<plain_type_t<decltype(lb_constrain(x[0], lb[0]))>> ret(x.size());
231 for (size_t i = 0; i < x.size(); ++i) {
232 ret[i] = lb_constrain(x[i], lb[i], lp);
233 }
234 return ret;
235}
236
256template <bool Jacobian, typename T, typename L, typename Lp,
258inline auto lb_constrain(const T& x, const L& lb, Lp& lp) {
259 if constexpr (Jacobian) {
260 return lb_constrain(x, lb, lp);
261 } else {
262 return lb_constrain(x, lb);
263 }
264}
265
266} // namespace math
267} // namespace stan
268
269#endif
#define unlikely(x)
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.
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_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
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 ...