1 #ifndef STAN_MATH_PRIM_SCAL_FUN_LUB_CONSTRAIN_HPP
2 #define STAN_MATH_PRIM_SCAL_FUN_LUB_CONSTRAIN_HPP
7 #include <boost/math/tools/promotion.hpp>
42 template <
typename T,
typename TL,
typename TU>
44 typename boost::math::tools::promote_args<T, TL, TU>::type
48 if (lb == -std::numeric_limits<double>::infinity())
50 if (ub == std::numeric_limits<double>::infinity())
55 T exp_minus_x =
exp(-x);
56 inv_logit_x = 1.0 / (1.0 + exp_minus_x);
58 if ((x < std::numeric_limits<double>::infinity())
59 && (inv_logit_x == 1))
60 inv_logit_x = 1 - 1
e-15;
63 inv_logit_x = 1.0 - 1.0 / (1.0 + exp_x);
65 if ((x > -std::numeric_limits<double>::infinity())
69 return lb + (ub - lb) * inv_logit_x;
113 template <
typename T,
typename TL,
typename TU>
114 typename boost::math::tools::promote_args<T, TL, TU>::type
120 s <<
"domain error in lub_constrain; lower bound = " << lb
121 <<
" must be strictly less than upper bound = " << ub;
124 if (lb == -std::numeric_limits<double>::infinity())
126 if (ub == std::numeric_limits<double>::infinity())
130 T exp_minus_x =
exp(-x);
131 inv_logit_x = 1.0 / (1.0 + exp_minus_x);
132 lp +=
log(ub - lb) - x - 2 *
log1p(exp_minus_x);
134 if ((x < std::numeric_limits<double>::infinity())
135 && (inv_logit_x == 1))
136 inv_logit_x = 1 - 1
e-15;
139 inv_logit_x = 1.0 - 1.0 / (1.0 + exp_x);
140 lp +=
log(ub - lb) + x - 2 *
log1p(exp_x);
142 if ((x > -std::numeric_limits<double>::infinity())
143 && (inv_logit_x== 0))
146 return lb + (ub - lb) * inv_logit_x;
bool check_less(const char *function, const char *name, const T_y &y, const T_high &high)
Return true if y is strictly less than high.
fvar< T > log(const fvar< T > &x)
T lb_constrain(const T x, const TL lb)
Return the lower-bounded value for the specified unconstrained input and specified lower bound...
fvar< T > exp(const fvar< T > &x)
void domain_error(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
Throw a domain error with a consistently formatted message.
double e()
Return the base of the natural logarithm.
fvar< T > log1p(const fvar< T > &x)
boost::math::tools::promote_args< T, TU >::type ub_constrain(const T x, const TU ub)
Return the upper-bounded value for the specified unconstrained scalar and upper bound.
boost::math::tools::promote_args< T, TL, TU >::type lub_constrain(const T x, TL lb, TU ub)
Return the lower- and upper-bounded scalar derived by transforming the specified free scalar given th...