1#ifndef STAN_MATH_PRIM_FUN_LUB_CONSTRAIN_HPP
2#define STAN_MATH_PRIM_FUN_LUB_CONSTRAIN_HPP
43template <
typename T,
typename L,
typename U,
44 require_all_stan_scalar_t<T, L, U>* =
nullptr,
45 require_not_var_t<return_type_t<T, L, U>>* =
nullptr>
49 if (
unlikely(is_ub_inf && is_lb_inf)) {
94template <
typename T,
typename L,
typename U,
100 if (
unlikely(is_ub_inf && is_lb_inf)) {
108 const auto diff = ub - lb;
117template <
typename T,
typename L,
typename U, require_eigen_t<T>* =
nullptr,
118 require_all_stan_scalar_t<L, U>* =
nullptr,
119 require_not_var_t<return_type_t<T, L, U>>* =
nullptr>
122 x.unaryExpr([ub, lb](
auto&& xx) { return lub_constrain(xx, lb, ub); }));
128template <
typename T,
typename L,
typename U, require_eigen_t<T>* =
nullptr,
129 require_all_stan_scalar_t<L, U>* =
nullptr,
130 require_not_var_t<return_type_t<T, L, U>>* =
nullptr>
133 return eval(x.unaryExpr(
134 [lb, ub, &lp](
auto&& xx) { return lub_constrain(xx, lb, ub, lp); }));
141template <
typename T,
typename L,
typename U,
145inline auto lub_constrain(
const T& x,
const L& lb,
const U& ub) {
147 return eval(x.binaryExpr(
148 lb, [ub](
auto&& x,
auto&& lb) { return lub_constrain(x, lb, ub); }));
155template <
typename T,
typename L,
typename U,
156 require_all_eigen_t<T, L>* =
nullptr,
157 require_stan_scalar_t<U>* =
nullptr,
158 require_not_var_t<return_type_t<T, L, U>>* =
nullptr>
159inline auto lub_constrain(
const T& x,
const L& lb,
const U& ub,
160 return_type_t<T, L, U>& lp) {
162 return eval(x.binaryExpr(lb, [ub, &lp](
auto&& x,
auto&& lb) {
163 return lub_constrain(x, lb, ub, lp);
171template <
typename T,
typename L,
typename U,
172 require_all_eigen_t<T, U>* =
nullptr,
173 require_stan_scalar_t<L>* =
nullptr,
174 require_not_var_t<return_type_t<T, L, U>>* =
nullptr>
175inline auto lub_constrain(
const T& x,
const L& lb,
const U& ub) {
177 return eval(x.binaryExpr(
178 ub, [lb](
auto&& x,
auto&& ub) { return lub_constrain(x, lb, ub); }));
185template <
typename T,
typename L,
typename U,
186 require_all_eigen_t<T, U>* =
nullptr,
187 require_stan_scalar_t<L>* =
nullptr,
188 require_not_var_t<return_type_t<T, L, U>>* =
nullptr>
189inline auto lub_constrain(
const T& x,
const L& lb,
const U& ub,
190 return_type_t<T, L, U>& lp) {
192 return eval(x.binaryExpr(ub, [lb, &lp](
auto&& x,
auto&& ub) {
193 return lub_constrain(x, lb, ub, lp);
200template <
typename T,
typename L,
typename U,
201 require_all_eigen_t<T, L, U>* =
nullptr,
202 require_not_var_t<return_type_t<T, L, U>>* =
nullptr>
210 for (Eigen::Index j = 0; j < x_ref.cols(); ++j) {
211 for (Eigen::Index i = 0; i < x_ref.rows(); ++i) {
213 x_ref.coeff(i, j), lb_ref.coeff(i, j), ub_ref.coeff(i, j));
222template <
typename T,
typename L,
typename U,
225inline auto lub_constrain(
const T& x,
const L& lb,
const U& ub,
232 promote_scalar_t<return_type_t<T, L, U>, T> x_ret(x.rows(), x.cols());
233 for (Eigen::Index j = 0; j < x_ref.cols(); ++j) {
234 for (Eigen::Index i = 0; i < x_ref.rows(); ++i) {
236 x_ref.coeff(i, j), lb_ref.coeff(i, j), ub_ref.coeff(i, j), lp);
245template <
typename T,
typename L,
typename U,
246 require_all_not_std_vector_t<L, U>* =
nullptr>
247inline auto lub_constrain(
const std::vector<T>& x,
const L& lb,
const U& ub) {
250 for (
size_t i = 0; i < x.size(); ++i) {
259template <
typename T,
typename L,
typename U,
265 for (
size_t i = 0; i < x.size(); ++i) {
274template <
typename T,
typename L,
typename U,
277 const std::vector<U>& ub) {
281 for (
size_t i = 0; i < x.size(); ++i) {
290template <
typename T,
typename L,
typename U,
293 const std::vector<U>& ub,
298 for (
size_t i = 0; i < x.size(); ++i) {
307template <
typename T,
typename L,
typename U,
314 for (
size_t i = 0; i < x.size(); ++i) {
323template <
typename T,
typename L,
typename U,
330 for (
size_t i = 0; i < x.size(); ++i) {
339template <
typename T,
typename L,
typename U>
341 const std::vector<U>& ub) {
346 for (
size_t i = 0; i < x.size(); ++i) {
355template <
typename T,
typename L,
typename U>
357 const std::vector<U>& ub,
363 for (
size_t i = 0; i < x.size(); ++i) {
393template <
bool Jacobian,
typename T,
typename L,
typename U>
require_all_t< is_eigen< std::decay_t< Types > >... > require_all_eigen_t
Require all of the types satisfy is_eigen.
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.
fvar< T > abs(const fvar< T > &x)
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 ...
T value_of(const fvar< T > &v)
Return the value of the specified variable.
fvar< T > log(const fvar< T > &x)
static constexpr double NEGATIVE_INFTY
Negative infinity.
auto multiply(const Mat1 &m1, const Mat2 &m2)
Return the product of the specified matrices.
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)
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
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.
auto identity_constrain(T &&x, Types &&...)
Returns the result of applying the identity constraint transform to the input.
static constexpr double INFTY
Positive infinity.
typename plain_type< T >::type plain_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...