Stan Math Library  2.12.0
reverse mode automatic differentiation
squared_distance.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_FWD_MAT_FUN_SQUARED_DISTANCE_HPP
2 #define STAN_MATH_FWD_MAT_FUN_SQUARED_DISTANCE_HPP
3 
11 
12 namespace stan {
13  namespace math {
14 
28  template<typename T, int R, int C>
29  inline fvar<T>
30  squared_distance(const Eigen::Matrix<fvar<T>, R, C>& v1,
31  const Eigen::Matrix<double, R, C>& v2) {
32  check_vector("squared_distance", "v1", v1);
33  check_vector("squared_distance", "v2", v2);
34  check_matching_sizes("squared_distance", "v1", v1, "v2", v2);
35  Eigen::Matrix<fvar<T>, R, C> v3 = subtract(v1, v2);
36  return dot_self(v3);
37  }
38 
54  template<typename T, int R1, int C1, int R2, int C2>
55  inline fvar<T>
56  squared_distance(const Eigen::Matrix<fvar<T>, R1, C1>& v1,
57  const Eigen::Matrix<double, R2, C2>& v2) {
58  check_vector("squared_distance", "v1", v1);
59  check_vector("squared_distance", "v2", v2);
60  check_matching_sizes("squared_distance", "v1", v1, "v2", v2);
61  Eigen::Matrix<double, R1, C1> t_v2 = v2.transpose();
62  Eigen::Matrix<fvar<T>, R1, C1> v3 = subtract(v1, t_v2);
63  return dot_self(v3);
64  }
65 
79  template<typename T, int R, int C>
80  inline fvar<T>
81  squared_distance(const Eigen::Matrix<double, R, C>& v1,
82  const Eigen::Matrix<fvar<T>, R, C>& v2) {
83  check_vector("squared_distance", "v1", v1);
84  check_vector("squared_distance", "v2", v2);
85  check_matching_sizes("squared_distance", "v1", v1, "v2", v2);
86  Eigen::Matrix<fvar<T>, R, C> v3 = subtract(v1, v2);
87  return dot_self(v3);
88  }
89 
105  template<typename T, int R1, int C1, int R2, int C2>
106  inline fvar<T>
107  squared_distance(const Eigen::Matrix<double, R1, C1>& v1,
108  const Eigen::Matrix<fvar<T>, R2, C2>& v2) {
109  check_vector("squared_distance", "v1", v1);
110  check_vector("squared_distance", "v2", v2);
111  check_matching_sizes("squared_distance", "v1", v1, "v2", v2);
112  Eigen::Matrix<double, R2, C2> t_v1 = v1.transpose();
113  Eigen::Matrix<fvar<T>, R2, C2> v3 = subtract(t_v1, v2);
114  return dot_self(v3);
115  }
129  template<typename T, int R, int C>
130  inline fvar<T>
131  squared_distance(const Eigen::Matrix<fvar<T>, R, C>& v1,
132  const Eigen::Matrix<fvar<T>, R, C>& v2) {
133  check_vector("squared_distance", "v1", v1);
134  check_vector("squared_distance", "v2", v2);
135  check_matching_sizes("squared_distance", "v1", v1, "v2", v2);
136  Eigen::Matrix<fvar<T>, R, C> v3 = subtract(v1, v2);
137  return dot_self(v3);
138  }
139 
155  template<typename T, int R1, int C1, int R2, int C2>
156  inline fvar<T>
157  squared_distance(const Eigen::Matrix<fvar<T>, R1, C1>& v1,
158  const Eigen::Matrix<fvar<T>, R2, C2>& v2) {
159  check_vector("squared_distance", "v1", v1);
160  check_vector("squared_distance", "v2", v2);
161  check_matching_sizes("squared_distance", "v1", v1, "v2", v2);
162  Eigen::Matrix<fvar<T>, R2, C2> t_v1 = v1.transpose();
163  Eigen::Matrix<fvar<T>, R2, C2> v3 = subtract(t_v1, v2);
164  return dot_self(v3);
165  }
166 
167  }
168 }
169 #endif
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.
Eigen::Matrix< typename boost::math::tools::promote_args< T1, T2 >::type, R, C > subtract(const Eigen::Matrix< T1, R, C > &m1, const Eigen::Matrix< T2, R, C > &m2)
Return the result of subtracting the second specified matrix from the first specified matrix...
Definition: subtract.hpp:27
fvar< T > dot_self(const Eigen::Matrix< fvar< T >, R, C > &v)
Definition: dot_self.hpp:16
fvar< T > squared_distance(const Eigen::Matrix< fvar< T >, R, C > &v1, const Eigen::Matrix< double, R, C > &v2)
Returns the squared distance between the specified vectors of the same dimensions.
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.

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