1#ifndef STAN_MATH_REV_FUN_SQUARED_DISTANCE_HPP
2#define STAN_MATH_REV_FUN_SQUARED_DISTANCE_HPP
25 [a, b](
const auto& vi)
mutable {
26 const double diff = 2.0 * (a.val() - b.val());
27 a.adj() += vi.adj_ * diff;
28 b.adj() -= vi.adj_ * diff;
39 [a, b](
const auto& vi)
mutable {
40 a.adj() += vi.adj_ * 2.0 * (a.val() - b);
61 typename EigVecVar1,
typename EigVecVar2,
79 vector_d di = 2 * adj_ * (v1_map.val() - v2_map.val());
92 template <
typename EigVecVar,
typename EigVecArith,
100 v1_ =
reinterpret_cast<vari**
>(
102 v2_ =
reinterpret_cast<double*
>(
104 Eigen::Map<vector_vi>(
v1_,
length_) = v1.vi();
111 += 2 * adj_ * (v1_map.val() - Eigen::Map<vector_d>(
v2_,
length_));
117 typename EigVecVar1,
typename EigVecVar2,
124template <
typename EigVecVar,
typename EigVecArith,
132template <
typename EigVecArith,
typename EigVecVar,
155template <
typename T1,
typename T2, require_all_vector_t<T1, T2>* =
nullptr,
156 require_any_var_vector_t<T1, T2>* =
nullptr>
165 double res_val = 0.0;
166 for (
size_t i = 0; i < arena_A.size(); ++i) {
167 const double diff = arena_A.val().coeff(i) - arena_B.val().coeff(i);
168 res_diff.coeffRef(i) = diff;
169 res_val += diff * diff;
172 res_val, [arena_A, arena_B, res_diff](
const auto& res)
mutable {
173 const double res_adj = 2.0 * res.adj();
174 for (
size_t i = 0; i < arena_A.size(); ++i) {
175 const double diff = res_adj * res_diff.coeff(i);
176 arena_A.adj().coeffRef(i) += diff;
177 arena_B.adj().coeffRef(i) -= diff;
184 double res_val = 0.0;
185 for (
size_t i = 0; i < arena_A.size(); ++i) {
186 const double diff = arena_A.val().coeff(i) - arena_B.coeff(i);
187 res_diff.coeffRef(i) = diff;
188 res_val += diff * diff;
191 res_val, [arena_A, arena_B, res_diff](
const auto& res)
mutable {
192 arena_A.adj() += 2.0 * res.adj() * res_diff;
198 double res_val = 0.0;
199 for (
size_t i = 0; i < arena_A.size(); ++i) {
200 const double diff = arena_A.coeff(i) - arena_B.val().coeff(i);
201 res_diff.coeffRef(i) = diff;
202 res_val += diff * diff;
205 res_val, [arena_A, arena_B, res_diff](
const auto& res)
mutable {
206 arena_B.adj() -= 2.0 * res.adj() * res_diff;
squared_distance_vd_vari(const EigVecVar &v1, const EigVecArith &v2)
squared_distance_vv_vari(const EigVecVar1 &v1, const EigVecVar2 &v2)
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator.
require_all_t< container_type_check_base< is_eigen_vector, value_type_t, TypeCheck, Check >... > require_all_eigen_vector_vt
Require all of the types satisfy is_eigen_vector.
require_t< container_type_check_base< is_eigen_vector, value_type_t, TypeCheck, Check... > > require_eigen_vector_vt
Require type satisfies is_eigen_vector.
auto as_column_vector_or_scalar(T &&a)
as_column_vector_or_scalar of a kernel generator expression.
int64_t size(const T &m)
Returns the size (number of the elements) of a matrix_cl or var_value<matrix_cl<T>>.
Eigen::Matrix< double, Eigen::Dynamic, 1 > vector_d
Type for (column) vector of double values.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
void check_matching_sizes(const char *function, const char *name1, const T_y1 &y1, const char *name2, const T_y2 &y2)
Check if two structures at the same size.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
void check_finite(const char *function, const char *name, const T_y &y)
Return true if all values in y are finite.
auto squared_distance(const T_a &a, const T_b &b)
Returns the squared distance.
internal::callback_vari< plain_type_t< T >, F > * make_callback_vari(T &&value, F &&functor)
Creates a new vari with given value and a callback that implements the reverse pass (chain).
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 ...
Metaprogramming struct to detect whether a given type is constant in the mathematical sense (not the ...
static thread_local AutodiffStackStorage * instance_