1 #ifndef STAN_MATH_REV_MAT_FUN_SQUARED_DISTANCE_HPP
2 #define STAN_MATH_REV_MAT_FUN_SQUARED_DISTANCE_HPP
22 class squared_distance_vv_vari :
public vari {
28 template <
int R1,
int C1,
int R2,
int C2>
30 var_squared_distance(
const Eigen::Matrix<var, R1, C1> &v1,
31 const Eigen::Matrix<var, R2, C2> &v2) {
34 typedef typename index_type<Matrix<var, R1, R2> >::type idx_t;
36 for (idx_t i = 0; i < v1.size(); i++) {
37 double diff = v1(i).vi_->val_ - v2(i).vi_->val_;
44 template<
int R1,
int C1,
int R2,
int C2>
45 squared_distance_vv_vari(
const Eigen::Matrix<var, R1, C1> &v1,
46 const Eigen::Matrix<var, R2, C2> &v2)
47 : vari(var_squared_distance(v1, v2)),
length_(v1.
size()) {
50 for (
size_t i = 0; i <
length_; i++)
54 .
alloc(length_*
sizeof(vari*)));
55 for (
size_t i = 0; i <
length_; i++)
58 virtual void chain() {
59 for (
size_t i = 0; i <
length_; i++) {
60 double di = 2 * adj_ * (
v1_[i]->val_ -
v2_[i]->val_);
66 class squared_distance_vd_vari :
public vari {
72 template<
int R1,
int C1,
int R2,
int C2>
74 var_squared_distance(
const Eigen::Matrix<var, R1, C1> &v1,
75 const Eigen::Matrix<double, R2, C2> &v2) {
78 typedef typename index_type<Matrix<double, R1, C1> >::type idx_t;
81 for (idx_t i = 0; i < v1.size(); i++) {
82 double diff = v1(i).vi_->val_ - v2(i);
89 template<
int R1,
int C1,
int R2,
int C2>
90 squared_distance_vd_vari(
const Eigen::Matrix<var, R1, C1> &v1,
91 const Eigen::Matrix<double, R2, C2> &v2)
92 : vari(var_squared_distance(v1, v2)), length_(v1.
size()) {
94 .
alloc(length_*
sizeof(vari*)));
95 for (
size_t i = 0; i <
length_; i++)
99 .
alloc(length_*
sizeof(
double)));
100 for (
size_t i = 0; i <
length_; i++)
103 virtual void chain() {
104 for (
size_t i = 0; i <
length_; i++) {
105 v1_[i]->adj_ += 2 * adj_ * (
v1_[i]->val_ -
v2_[i]);
111 template<
int R1,
int C1,
int R2,
int C2>
113 const Eigen::Matrix<var, R2, C2>& v2) {
119 return var(
new squared_distance_vv_vari(v1, v2));
121 template<
int R1,
int C1,
int R2,
int C2>
123 const Eigen::Matrix<double, R2, C2>& v2) {
129 return var(
new squared_distance_vd_vari(v1, v2));
131 template<
int R1,
int C1,
int R2,
int C2>
133 const Eigen::Matrix<var, R2, C2>& v2) {
139 return var(
new squared_distance_vd_vari(v2, v1));
bool check_vector(const char *function, const char *name, const Eigen::Matrix< T, R, C > &x)
Return true if the matrix is either a row vector or column vector.
static stack_alloc memalloc_
boost::math::tools::promote_args< T1, T2 >::type squared_distance(const Eigen::Matrix< T1, R1, C1 > &v1, const Eigen::Matrix< T2, R2, C2 > &v2)
Returns the squared distance between the specified vectors.
Independent (input) and dependent (output) variables for gradients.
Primary template class for the metaprogram to compute the index type of a container.
bool check_matching_sizes(const char *function, const char *name1, const T_y1 &y1, const char *name2, const T_y2 &y2)
Return true if two structures at the same size.
int size(const std::vector< T > &x)
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator...