Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
squared_distance.hpp
Go to the documentation of this file.
1 #ifndef STAN__AGRAD__REV__MATRIX__SQUARED_DISTANCE_HPP
2 #define STAN__AGRAD__REV__MATRIX__SQUARED_DISTANCE_HPP
3 
4 #include <vector>
5 
6 #include <stan/agrad/rev/var.hpp>
10 
17 
18 
19 namespace stan {
20 
21  namespace agrad {
22 
23  namespace {
24 
25  class squared_distance_vv_vari : public vari {
26  protected:
27  vari** v1_;
28  vari** v2_;
29  size_t length_;
30 
31  template <int R1,int C1,int R2,int C2>
32  inline static double
33  var_squared_distance(const Eigen::Matrix<var,R1,C1> &v1,
34  const Eigen::Matrix<var,R2,C2> &v2) {
35  using Eigen::Matrix;
37  typedef typename index_type<Matrix<var,R1,R2> >::type idx_t;
38  double result = 0;
39  for (idx_t i = 0; i < v1.size(); i++) {
40  double diff = v1(i).vi_->val_ - v2(i).vi_->val_;
41  result += diff*diff;
42  }
43  return result;
44  }
45  public:
46  template<int R1,int C1,int R2,int C2>
47  squared_distance_vv_vari(const Eigen::Matrix<var,R1,C1> &v1,
48  const Eigen::Matrix<var,R2,C2> &v2)
49  : vari(var_squared_distance(v1, v2)), length_(v1.size())
50  {
51  v1_ = (vari**)memalloc_.alloc(length_*sizeof(vari*));
52  for (size_t i = 0; i < length_; i++)
53  v1_[i] = v1(i).vi_;
54 
55  v2_ = (vari**)memalloc_.alloc(length_*sizeof(vari*));
56  for (size_t i = 0; i < length_; i++)
57  v2_[i] = v2(i).vi_;
58  }
59  virtual void chain() {
60  for (size_t i = 0; i < length_; i++) {
61  double di = 2 * adj_ * (v1_[i]->val_ - v2_[i]->val_);
62  v1_[i]->adj_ += di;
63  v2_[i]->adj_ -= di;
64  }
65  }
66  };
67  class squared_distance_vd_vari : public vari {
68  protected:
69  vari** v1_;
70  double* v2_;
71  size_t length_;
72 
73  template<int R1,int C1,int R2,int C2>
74  inline static double
75  var_squared_distance(const Eigen::Matrix<var,R1,C1> &v1,
76  const Eigen::Matrix<double,R2,C2> &v2) {
77 
78  using Eigen::Matrix;
80  typedef typename index_type<Matrix<double,R1,C1> >::type idx_t;
81 
82  double result = 0;
83  for (idx_t i = 0; i < v1.size(); i++) {
84  double diff = v1(i).vi_->val_ - v2(i);
85  result += diff*diff;
86  }
87  return result;
88  }
89  public:
90  template<int R1,int C1,int R2,int C2>
91  squared_distance_vd_vari(const Eigen::Matrix<var,R1,C1> &v1,
92  const Eigen::Matrix<double,R2,C2> &v2)
93  : vari(var_squared_distance(v1, v2)), length_(v1.size())
94  {
95  v1_ = (vari**)memalloc_.alloc(length_*sizeof(vari*));
96  for (size_t i = 0; i < length_; i++)
97  v1_[i] = v1(i).vi_;
98 
99  v2_ = (double*)memalloc_.alloc(length_*sizeof(double));
100  for (size_t i = 0; i < length_; i++)
101  v2_[i] = v2(i);
102  }
103  virtual void chain() {
104  for (size_t i = 0; i < length_; i++) {
105  v1_[i]->adj_ += 2 * adj_ * (v1_[i]->val_ - v2_[i]);
106  }
107  }
108  };
109  }
110 
111  template<int R1,int C1,int R2, int C2>
112  inline var squared_distance(const Eigen::Matrix<var, R1, C1>& v1,
113  const Eigen::Matrix<var, R2, C2>& v2) {
114  stan::math::check_vector("squared_distance(%1%)",v1,"v1",(double*)0);
115  stan::math::check_vector("squared_distance(%1%)",v2,"v2",(double*)0);
116  stan::math::check_matching_sizes("squared_distance(%1%)",v1,"v1",
117  v2,"v2",(double*)0);
118  return var(new squared_distance_vv_vari(v1,v2));
119  }
120  template<int R1,int C1,int R2, int C2>
121  inline var squared_distance(const Eigen::Matrix<var, R1, C1>& v1,
122  const Eigen::Matrix<double, R2, C2>& v2) {
123  stan::math::check_vector("squared_distance(%1%)",v1,"v1",(double*)0);
124  stan::math::check_vector("squared_distance(%1%)",v2,"v2",(double*)0);
125  stan::math::check_matching_sizes("squared_distance(%1%)",v1,"v1",
126  v2,"v2",(double*)0);
127  return var(new squared_distance_vd_vari(v1,v2));
128  }
129  template<int R1,int C1,int R2, int C2>
130  inline var squared_distance(const Eigen::Matrix<double, R1, C1>& v1,
131  const Eigen::Matrix<var, R2, C2>& v2) {
132  stan::math::check_vector("squared_distance(%1%)",v1,"v1",(double*)0);
133  stan::math::check_vector("squared_distance(%1%)",v2,"v2",(double*)0);
134  stan::math::check_matching_sizes("squared_distance(%1%)",v1,"v1",
135  v2,"v2",(double*)0);
136  return var(new squared_distance_vd_vari(v2,v1));
137  }
138  }
139 }
140 #endif
memory::stack_alloc memalloc_
Definition: var_stack.cpp:16
bool check_vector(const char *function, const Eigen::Matrix< T, R, C > &x, const char *name, T_result *result)
Primary template class for the metaprogram to compute the index type of a container.
Definition: index_type.hpp:21
vari ** v2_
size_t length_
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator...
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:27
int size(const std::vector< T > &x)
Definition: size.hpp:11
var squared_distance(const Eigen::Matrix< var, R1, C1 > &v1, const Eigen::Matrix< var, R2, C2 > &v2)
bool check_matching_sizes(const char *function, const T_y1 &y1, const char *name1, const T_y2 &y2, const char *name2, T_result *result)
vari ** v1_

     [ Stan Home Page ] © 2011–2014, Stan Development Team.