Automatic Differentiation
 
Loading...
Searching...
No Matches
squared_distance.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_REV_SQUARED_DISTANCE_HPP
2#define STAN_MATH_OPENCL_REV_SQUARED_DISTANCE_HPP
3#ifdef STAN_OPENCL
4
15
16namespace stan {
17namespace math {
18
28template <
29 typename T_a, typename T_b,
30 require_all_nonscalar_prim_or_rev_kernel_expression_t<T_a, T_b>* = nullptr,
31 require_any_var_t<T_a, T_b>* = nullptr>
32inline var_value<double> squared_distance(T_a&& a, T_b&& b) {
33 arena_t<T_a> a_arena = std::forward<T_a>(a);
34 arena_t<T_b> b_arena = std::forward<T_b>(b);
35
36 return make_callback_var(
37 squared_distance(value_of(a_arena), value_of(b_arena)),
38 [a_arena, b_arena](const vari_value<double>& res) mutable {
39 auto res_two_mult_diff = elt_multiply(
40 res.adj(), 2.0 * (value_of(a_arena) - value_of(b_arena)));
41 adjoint_results(a_arena, b_arena)
42 += expressions(res_two_mult_diff, -res_two_mult_diff);
43 });
44}
45
46} // namespace math
47} // namespace stan
48
49#endif
50#endif
elt_multiply_< as_operation_cl_t< T_a >, as_operation_cl_t< T_b > > elt_multiply(T_a &&a, T_b &&b)
expressions_cl< T_expressions... > expressions(T_expressions &&... expressions)
Deduces types for constructing expressions_cl object.
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 squared_distance(const T_a &a, const T_b &b)
Returns the squared distance.
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 ...