| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
4 #include <stan/math/prim/mat/fun/Eigen.hpp>
5 #include <stan/math/prim/mat/err/check_vector.hpp>
6 #include <stan/math/prim/arr/err/check_matching_sizes.hpp>
7 #include <stan/math/fwd/core/operator_subtraction.hpp>
8 #include <stan/math/fwd/mat/fun/dot_self.hpp>
9 #include <stan/math/fwd/mat/fun/to_fvar.hpp>
10 #include <stan/math/prim/mat/fun/subtract.hpp>
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
stan::math::check_vector
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.
Definition: check_vector.hpp:34
stan
Definition: log_sum_exp.hpp:8
stan::math::subtract
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
operator_subtraction.hpp
check_matching_sizes.hpp
stan::math::dot_self
fvar< T > dot_self(const Eigen::Matrix< fvar< T >, R, C > &v)
Definition: dot_self.hpp:16
stan::math::squared_distance
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.
Definition: squared_distance.hpp:30
dot_self.hpp
stan::math::check_matching_sizes
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.
Definition: check_matching_sizes.hpp:29
Eigen.hpp
check_vector.hpp
subtract.hpp
to_fvar.hpp
stan::math::fvar
Definition: fvar.hpp:12

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