Automatic Differentiation
 
Loading...
Searching...
No Matches
ub_constrain.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_REV_CONSTRAINT_UB_CONSTRAIN_HPP
2#define STAN_MATH_OPENCL_REV_CONSTRAINT_UB_CONSTRAIN_HPP
3#ifdef STAN_OPENCL
4
15
16namespace stan {
17namespace math {
18
35template <typename T_x, typename T_ub,
36 require_all_prim_or_rev_kernel_expression_t<T_x, T_ub>* = nullptr,
37 require_any_var_t<T_x, T_ub>* = nullptr,
38 require_any_not_stan_scalar_t<T_x, T_ub>* = nullptr>
39inline var_value<matrix_cl<double>> ub_constrain(T_x&& x, T_ub&& ub) {
40 arena_t<T_x> x_arena = std::forward<T_x>(x);
41 arena_t<T_ub> ub_arena = std::forward<T_ub>(ub);
42
43 return make_callback_var(
44 ub_constrain(value_of(x_arena), value_of(ub_arena)),
45 [x_arena, ub_arena](vari_value<matrix_cl<double>>& res) mutable {
46 auto ub_inf = value_of(ub_arena) == INFTY;
47 adjoint_results(x_arena, ub_arena) += expressions(
48 select(ub_inf, res.adj(),
49 -elt_multiply(res.adj(), exp(value_of(x_arena)))),
50 select(ub_inf, 0, res.adj()));
51 });
52}
53
71template <typename T_x, typename T_ub,
75inline var_value<matrix_cl<double>> ub_constrain(T_x&& x, T_ub&& ub, var& lp) {
76 arena_t<T_x> x_arena = std::forward<T_x>(x);
77 arena_t<T_ub> ub_arena = std::forward<T_ub>(ub);
78
79 double lp_inc = 0;
81 = ub_constrain(value_of(x_arena), value_of(ub_arena), lp_inc);
82 lp += lp_inc;
83
84 return make_callback_var(
85 std::move(res),
86 [x_arena, ub_arena, lp](vari_value<matrix_cl<double>>& res) mutable {
87 auto ub_inf = value_of(ub_arena) == INFTY;
88 adjoint_results(x_arena, ub_arena) += expressions(
89 select(ub_inf, res.adj(),
90 -elt_multiply(res.adj(), exp(value_of(x_arena))) + lp.adj()),
91 select(ub_inf, 0, res.adj()));
92 });
93}
94
95} // namespace math
96} // namespace stan
97
98#endif
99#endif
Represents an arithmetic matrix on the OpenCL device.
Definition matrix_cl.hpp:47
elt_multiply_< as_operation_cl_t< T_a >, as_operation_cl_t< T_b > > elt_multiply(T_a &&a, T_b &&b)
select_< as_operation_cl_t< T_condition >, as_operation_cl_t< T_then >, as_operation_cl_t< T_else > > select(T_condition &&condition, T_then &&then, T_else &&els)
Selection operation on kernel generator expressions.
Definition select.hpp:148
expressions_cl< T_expressions... > expressions(T_expressions &&... expressions)
Deduces types for constructing expressions_cl object.
require_all_t< is_prim_or_rev_kernel_expression< std::decay_t< Types > >... > require_all_prim_or_rev_kernel_expression_t
Require type satisfies is_prim_or_rev_kernel_expression.
require_any_not_t< is_stan_scalar< std::decay_t< Types > >... > require_any_not_stan_scalar_t
Require at least one of the types do not satisfy is_stan_scalar.
require_any_t< is_var< std::decay_t< Types > >... > require_any_var_t
Require any of the types satisfy is_var.
Definition is_var.hpp:39
adjoint_results_cl< T_results... > adjoint_results(T_results &&... results)
Deduces types for constructing adjoint_results_cl object.
var_value< plain_type_t< T > > make_callback_var(T &&value, F &&functor)
Creates a new var initialized with a callback_vari with a given value and reverse-pass callback funct...
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
auto ub_constrain(T &&x, U &&ub)
Return the upper-bounded value for the specified unconstrained matrix and upper bound.
static constexpr double INFTY
Positive infinity.
Definition constants.hpp:46
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:15
typename internal::arena_type_impl< std::decay_t< T > >::type arena_t
Determines a type that can be used in place of T that does any dynamic allocations on the AD stack.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...