Automatic Differentiation
 
Loading...
Searching...
No Matches
fmin.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_REV_FMIN_HPP
2#define STAN_MATH_OPENCL_REV_FMIN_HPP
3#ifdef STAN_OPENCL
4
15
16namespace stan {
17namespace math {
18
27template <typename T_a, typename T_b,
28 require_all_prim_or_rev_kernel_expression_t<T_a, T_b>* = nullptr,
29 require_any_var_t<T_a, T_b>* = nullptr,
30 require_any_not_stan_scalar_t<T_a, T_b>* = nullptr>
31inline var_value<matrix_cl<double>> fmin(T_a&& a, T_b&& b) {
32 using std::isnan;
33 const arena_t<T_a>& a_arena = std::forward<T_a>(a);
34 const arena_t<T_b>& b_arena = std::forward<T_b>(b);
35
36 matrix_cl<double> res_val = fmin(value_of(a_arena), value_of(b_arena));
37
38 return make_callback_var(
39 res_val,
40 [a_arena, b_arena](const vari_value<matrix_cl<double>>& res) mutable {
41 auto both_nan = isnan(value_of(a_arena)) && isnan(value_of(b_arena));
42 auto a_is_min = value_of(a_arena) < value_of(b_arena);
43 auto a_deriv
44 = select(both_nan, NOT_A_NUMBER, select(a_is_min, res.adj(), 0.0));
45 auto b_deriv
46 = select(both_nan, NOT_A_NUMBER, select(!a_is_min, res.adj(), 0.0));
47
48 adjoint_results(a_arena, b_arena) += expressions(a_deriv, b_deriv);
49 });
50}
51
52} // namespace math
53} // namespace stan
54
55#endif
56#endif
Represents an arithmetic matrix on the OpenCL device.
Definition matrix_cl.hpp:47
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.
static constexpr double NOT_A_NUMBER
(Quiet) not-a-number value.
Definition constants.hpp:56
adjoint_results_cl< T_results... > adjoint_results(T_results &&... results)
Deduces types for constructing adjoint_results_cl object.
fvar< T > fmin(const fvar< T > &x1, const fvar< T > &x2)
Definition fmin.hpp:14
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
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 ...
bool isnan(const stan::math::var &a)
Checks if the given number is NaN.
Definition std_isnan.hpp:18