1#ifndef STAN_MATH_PRIM_CONSTRAINT_LUB_CONSTRAIN_HPP
2#define STAN_MATH_PRIM_CONSTRAINT_LUB_CONSTRAIN_HPP
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>
50 if (
unlikely(is_ub_inf && is_lb_inf)) {
95template <
typename T,
typename L,
typename U,
typename Lp,
102 if (
unlikely(is_ub_inf && is_lb_inf)) {
110 const auto diff = ub - lb;
119template <
typename T,
typename L,
typename U, require_eigen_t<T>* =
nullptr,
120 require_all_stan_scalar_t<L, U>* =
nullptr,
121 require_not_var_t<return_type_t<T, L, U>>* =
nullptr>
124 x.unaryExpr([ub, lb](
auto&& xx) { return lub_constrain(xx, lb, ub); }));
130template <
typename T,
typename L,
typename U,
typename Lp,
136 return eval(x.unaryExpr(
137 [lb, ub, &lp](
auto&& xx) { return lub_constrain(xx, lb, ub, lp); }));
144template <
typename T,
typename L,
typename U,
148inline auto lub_constrain(
const T& x,
const L& lb,
const U& ub) {
150 return eval(x.binaryExpr(
151 lb, [ub](
auto&& x,
auto&& lb) { return lub_constrain(x, lb, ub); }));
158template <
typename T,
typename L,
typename U,
typename Lp,
159 require_all_eigen_t<T, L>* =
nullptr,
160 require_stan_scalar_t<U>* =
nullptr,
161 require_not_var_t<return_type_t<T, L, U>>* =
nullptr,
162 require_convertible_t<return_type_t<T, L, U>, Lp>* =
nullptr>
163inline auto lub_constrain(
const T& x,
const L& lb,
const U& ub, Lp& lp) {
165 return eval(x.binaryExpr(lb, [ub, &lp](
auto&& x,
auto&& lb) {
166 return lub_constrain(x, lb, ub, lp);
174template <
typename T,
typename L,
typename U,
175 require_all_eigen_t<T, U>* =
nullptr,
176 require_stan_scalar_t<L>* =
nullptr,
177 require_not_var_t<return_type_t<T, L, U>>* =
nullptr>
178inline auto lub_constrain(
const T& x,
const L& lb,
const U& ub) {
180 return eval(x.binaryExpr(
181 ub, [lb](
auto&& x,
auto&& ub) { return lub_constrain(x, lb, ub); }));
188template <
typename T,
typename L,
typename U,
typename Lp,
189 require_all_eigen_t<T, U>* =
nullptr,
190 require_stan_scalar_t<L>* =
nullptr,
191 require_not_var_t<return_type_t<T, L, U>>* =
nullptr,
192 require_convertible_t<return_type_t<T, L, U>, Lp>* =
nullptr>
193inline auto lub_constrain(
const T& x,
const L& lb,
const U& ub, Lp& lp) {
195 return eval(x.binaryExpr(ub, [lb, &lp](
auto&& x,
auto&& ub) {
196 return lub_constrain(x, lb, ub, lp);
203template <
typename T,
typename L,
typename U,
204 require_all_eigen_t<T, L, U>* =
nullptr,
205 require_not_var_t<return_type_t<T, L, U>>* =
nullptr>
213 for (Eigen::Index j = 0; j < x_ref.cols(); ++j) {
214 for (Eigen::Index i = 0; i < x_ref.rows(); ++i) {
216 x_ref.coeff(i, j), lb_ref.coeff(i, j), ub_ref.coeff(i, j));
225template <
typename T,
typename L,
typename U,
typename Lp,
236 for (Eigen::Index j = 0; j < x_ref.cols(); ++j) {
237 for (Eigen::Index i = 0; i < x_ref.rows(); ++i) {
239 x_ref.coeff(i, j), lb_ref.coeff(i, j), ub_ref.coeff(i, j), lp);
248template <
typename T,
typename L,
typename U,
250inline auto lub_constrain(
const std::vector<T>& x,
const L& lb,
const U& ub) {
253 for (
size_t i = 0; i < x.size(); ++i) {
262template <
typename T,
typename L,
typename U,
typename Lp,
269 for (
size_t i = 0; i < x.size(); ++i) {
278template <
typename T,
typename L,
typename U,
281 const std::vector<U>& ub) {
285 for (
size_t i = 0; i < x.size(); ++i) {
294template <
typename T,
typename L,
typename U,
typename Lp,
298 const std::vector<U>& ub, Lp& lp) {
302 for (
size_t i = 0; i < x.size(); ++i) {
311template <
typename T,
typename L,
typename U,
318 for (
size_t i = 0; i < x.size(); ++i) {
327template <
typename T,
typename L,
typename U,
typename Lp,
331 const U& ub, Lp& lp) {
335 for (
size_t i = 0; i < x.size(); ++i) {
344template <
typename T,
typename L,
typename U>
346 const std::vector<U>& ub) {
351 for (
size_t i = 0; i < x.size(); ++i) {
360template <
typename T,
typename L,
typename U,
typename Lp,
363 const std::vector<U>& ub, Lp& lp) {
368 for (
size_t i = 0; i < x.size(); ++i) {
399template <
bool Jacobian,
typename T,
typename L,
typename U,
typename Lp,
402 if constexpr (Jacobian) {
412template <
typename T,
typename L,
typename U>
414 return lub_constrain(x, std::get<0>(bounds), std::get<1>(bounds));
420template <
typename T,
typename L,
typename U,
typename Lp,
422inline auto lub_constrain(
const T& x,
const std::tuple<L, U>& bounds, Lp& lp) {
423 return lub_constrain(x, std::get<0>(bounds), std::get<1>(bounds), lp);
429template <
bool Jacobian,
typename T,
typename L,
typename U,
typename Lp,
431inline auto lub_constrain(
const T& x,
const std::tuple<L, U>& bounds, Lp& lp) {
432 return lub_constrain<Jacobian>(x, std::get<0>(bounds), std::get<1>(bounds),
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.
require_all_t< is_eigen< std::decay_t< Types > >... > require_all_eigen_t
Require all of the types satisfy is_eigen.
require_t< is_eigen< std::decay_t< T > > > require_eigen_t
Require type satisfies 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.
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 ...