Automatic Differentiation
 
Loading...
Searching...
No Matches
lb_free.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_CONSTRAINT_LB_FREE_HPP
2#define STAN_MATH_PRIM_CONSTRAINT_LB_FREE_HPP
3
11#include <cmath>
12
13namespace stan {
14namespace math {
15
28template <typename T, typename L, require_not_std_vector_t<T>* = nullptr,
29 require_stan_scalar_t<L>* = nullptr>
30inline auto lb_free(T&& y, L&& lb) {
31 if (value_of_rec(lb) == NEGATIVE_INFTY) {
32 return identity_free(y, lb);
33 } else {
34 auto&& y_ref = to_ref(std::forward<T>(y));
35 auto&& lb_ref = to_ref(std::forward<L>(lb));
36 check_greater_or_equal("lb_free", "Lower bounded variable", y_ref, lb_ref);
37 return eval(log(subtract(std::forward<decltype(y_ref)>(y_ref),
38 std::forward<decltype(lb_ref)>(lb_ref))));
39 }
40}
41
56template <typename T, typename L, require_all_eigen_t<T, L>* = nullptr>
57inline auto lb_free(T&& y, L&& lb) {
58 auto&& y_ref = to_ref(std::forward<T>(y));
59 auto&& lb_ref = to_ref(std::forward<L>(lb));
60 promote_scalar_t<return_type_t<T, L>, T> ret(y.rows(), y.cols());
61 for (Eigen::Index j = 0; j < y.cols(); ++j) {
62 for (Eigen::Index i = 0; i < y.rows(); ++i) {
63 ret.coeffRef(i, j) = lb_free(y_ref.coeff(i, j), lb_ref.coeff(i, j));
64 }
65 }
66 return ret;
67}
68
83template <typename T, typename L, require_not_std_vector_t<L>* = nullptr>
84inline auto lb_free(const std::vector<T> y, const L& lb) {
85 auto&& lb_ref = to_ref(lb);
86 std::vector<decltype(lb_free(y[0], lb_ref))> ret(y.size());
87 std::transform(y.begin(), y.end(), ret.begin(),
88 [&lb_ref](auto&& yy) { return lb_free(yy, lb_ref); });
89 return ret;
90}
91
106template <typename T, typename L>
107inline auto lb_free(const std::vector<T> y, const std::vector<L>& lb) {
108 std::vector<decltype(lb_free(y[0], lb[0]))> ret(y.size());
109 std::transform(y.begin(), y.end(), lb.begin(), ret.begin(),
110 [](auto&& yy, auto&& lbb) { return lb_free(yy, lbb); });
111 return ret;
112}
113
114} // namespace math
115} // namespace stan
116#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.
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
fvar< T > log(const fvar< T > &x)
Definition log.hpp:18
auto identity_free(T &&x, Types &&...)
Returns the result of applying the inverse of the identity constraint transform to the input.
static constexpr double NEGATIVE_INFTY
Negative infinity.
Definition constants.hpp:51
void check_greater_or_equal(const char *function, const char *name, const T_y &y, const T_low &low, Idxs... idxs)
Throw an exception if y is not greater or equal than low.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
auto lb_free(T &&y, L &&lb)
Return the unconstrained value that produces the specified lower-bound constrained value.
Definition lb_free.hpp:30
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...