Stan Math Library  2.20.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 
9 
10 namespace stan {
11 namespace math {
12 
26 template <typename T, int R, int C>
27 inline fvar<T> squared_distance(const Eigen::Matrix<fvar<T>, R, C>& v1,
28  const Eigen::Matrix<double, R, C>& v2) {
29  check_vector("squared_distance", "v1", v1);
30  check_vector("squared_distance", "v2", v2);
31  check_matching_sizes("squared_distance", "v1", v1, "v2", v2);
32  Eigen::Matrix<fvar<T>, R, C> v3 = subtract(v1, v2);
33  return dot_self(v3);
34 }
35 
51 template <typename T, int R1, int C1, int R2, int C2>
52 inline fvar<T> squared_distance(const Eigen::Matrix<fvar<T>, R1, C1>& v1,
53  const Eigen::Matrix<double, R2, C2>& v2) {
54  check_vector("squared_distance", "v1", v1);
55  check_vector("squared_distance", "v2", v2);
56  check_matching_sizes("squared_distance", "v1", v1, "v2", v2);
57  Eigen::Matrix<double, R1, C1> t_v2 = v2.transpose();
58  Eigen::Matrix<fvar<T>, R1, C1> v3 = subtract(v1, t_v2);
59  return dot_self(v3);
60 }
61 
75 template <typename T, int R, int C>
76 inline fvar<T> squared_distance(const Eigen::Matrix<double, R, C>& v1,
77  const Eigen::Matrix<fvar<T>, R, C>& v2) {
78  check_vector("squared_distance", "v1", v1);
79  check_vector("squared_distance", "v2", v2);
80  check_matching_sizes("squared_distance", "v1", v1, "v2", v2);
81  Eigen::Matrix<fvar<T>, R, C> v3 = subtract(v1, v2);
82  return dot_self(v3);
83 }
84 
100 template <typename T, int R1, int C1, int R2, int C2>
101 inline fvar<T> squared_distance(const Eigen::Matrix<double, R1, C1>& v1,
102  const Eigen::Matrix<fvar<T>, R2, C2>& v2) {
103  check_vector("squared_distance", "v1", v1);
104  check_vector("squared_distance", "v2", v2);
105  check_matching_sizes("squared_distance", "v1", v1, "v2", v2);
106  Eigen::Matrix<double, R2, C2> t_v1 = v1.transpose();
107  Eigen::Matrix<fvar<T>, R2, C2> v3 = subtract(t_v1, v2);
108  return dot_self(v3);
109 }
123 template <typename T, int R, int C>
124 inline fvar<T> squared_distance(const Eigen::Matrix<fvar<T>, R, C>& v1,
125  const Eigen::Matrix<fvar<T>, R, C>& v2) {
126  check_vector("squared_distance", "v1", v1);
127  check_vector("squared_distance", "v2", v2);
128  check_matching_sizes("squared_distance", "v1", v1, "v2", v2);
129  Eigen::Matrix<fvar<T>, R, C> v3 = subtract(v1, v2);
130  return dot_self(v3);
131 }
132 
148 template <typename T, int R1, int C1, int R2, int C2>
149 inline fvar<T> squared_distance(const Eigen::Matrix<fvar<T>, R1, C1>& v1,
150  const Eigen::Matrix<fvar<T>, R2, C2>& v2) {
151  check_vector("squared_distance", "v1", v1);
152  check_vector("squared_distance", "v2", v2);
153  check_matching_sizes("squared_distance", "v1", v1, "v2", v2);
154  Eigen::Matrix<fvar<T>, R2, C2> t_v1 = v1.transpose();
155  Eigen::Matrix<fvar<T>, R2, C2> v3 = subtract(t_v1, v2);
156  return dot_self(v3);
157 }
158 
159 } // namespace math
160 } // namespace stan
161 #endif
auto subtract(const matrix_cl &A, const matrix_cl &B)
Matrix subtraction on the OpenCL device Subtracts the second matrix from the first matrix and stores ...
Definition: subtract.hpp:28
void check_vector(const char *function, const char *name, const Eigen::Matrix< T, R, C > &x)
Check if the matrix is either a row vector or column vector.
fvar< T > dot_self(const Eigen::Matrix< fvar< T >, R, C > &v)
Definition: dot_self.hpp:13
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.
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.
This template class represents scalars used in forward-mode automatic differentiation, which consist of values and directional derivatives of the specified template type.
Definition: fvar.hpp:41

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