Automatic Differentiation
 
Loading...
Searching...
No Matches
ub_free.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_CONSTRAINT_UB_FREE_HPP
2#define STAN_MATH_PRIM_CONSTRAINT_UB_FREE_HPP
3
9#include <cmath>
10
11namespace stan {
12namespace math {
13
33template <typename T, typename U, require_not_std_vector_t<T>* = nullptr,
34 require_stan_scalar_t<U>* = nullptr>
35inline auto ub_free(T&& y, U&& ub) {
36 if (value_of_rec(ub) == INFTY) {
37 return identity_free(y, ub);
38 } else {
39 auto&& y_ref = to_ref(std::forward<T>(y));
40 auto&& ub_ref = to_ref(std::forward<U>(ub));
41 check_less_or_equal("ub_free", "Upper bounded variable", value_of(y_ref),
42 value_of(ub_ref));
43 return eval(log(subtract(std::forward<decltype(ub_ref)>(ub_ref),
44 std::forward<decltype(y_ref)>(y_ref))));
45 }
46}
47
62template <typename T, typename U, require_all_eigen_t<T, U>* = nullptr>
63inline auto ub_free(T&& y, U&& ub) {
64 check_matching_dims("ub_free", "y", y, "ub", ub);
65 auto&& y_ref = to_ref(std::forward<T>(y));
66 auto&& ub_ref = to_ref(std::forward<U>(ub));
67 promote_scalar_t<return_type_t<T, U>, T> ret(y.rows(), y.cols());
68 for (Eigen::Index j = 0; j < y.cols(); ++j) {
69 for (Eigen::Index i = 0; i < y.rows(); ++i) {
70 ret.coeffRef(i, j) = ub_free(y_ref.coeff(i, j), ub_ref.coeff(i, j));
71 }
72 }
73 return ret;
74}
75
90template <typename T, typename U, require_not_std_vector_t<U>* = nullptr>
91inline auto ub_free(const std::vector<T> y, const U& ub) {
92 auto&& ub_ref = to_ref(ub);
93 std::vector<decltype(ub_free(y[0], ub))> ret(y.size());
94 for (Eigen::Index i = 0; i < y.size(); ++i) {
95 ret[i] = ub_free(y[i], ub_ref);
96 }
97 return ret;
98}
99
114template <typename T, typename U>
115inline auto ub_free(const std::vector<T> y, const std::vector<U>& ub) {
116 check_matching_dims("ub_free", "y", y, "ub", ub);
117 std::vector<decltype(ub_free(y[0], ub[0]))> ret(y.size());
118 for (Eigen::Index i = 0; i < y.size(); ++i) {
119 ret[i] = ub_free(y[i], ub[i]);
120 }
121 return ret;
122}
123} // namespace math
124} // namespace stan
125#endif
subtraction_< as_operation_cl_t< T_a >, as_operation_cl_t< T_b > > subtract(T_a &&a, T_b &&b)
double value_of_rec(const fvar< T > &v)
Return the value of the specified variable.
void check_less_or_equal(const char *function, const char *name, const T_y &y, const T_high &high, Idxs... idxs)
Throw an exception if y is not less than high.
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 ...
Definition eval.hpp:20
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
fvar< T > log(const fvar< T > &x)
Definition log.hpp:15
auto identity_free(T &&x, Types &&...)
Returns the result of applying the inverse of the identity constraint transform to the input.
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 ub_free(T &&y, U &&ub)
Return the free scalar that corresponds to the specified upper-bounded value with respect to the spec...
Definition ub_free.hpp:35
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
static constexpr double INFTY
Positive infinity.
Definition constants.hpp:46
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...