| Stan Math Library 2.11.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/mat/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 stan::math::fvar<T>
30 squared_distance(const Eigen::Matrix<stan::math::fvar<T>, R, C>& v1,
31 const Eigen::Matrix<double, R, C>& v2) {
32 stan::math::check_vector("squared_distance", "v1", v1);
33 stan::math::check_vector("squared_distance", "v2", v2);
34 stan::math::check_matching_sizes("squared_distance",
35 "v1", v1,
36 "v2", v2);
37 Eigen::Matrix<stan::math::fvar<T>, R, C> v3
38 = stan::math::subtract(v1, v2);
39 return stan::math::dot_self(v3);
40 }
41
57 template<typename T, int R1, int C1, int R2, int C2>
58 inline stan::math::fvar<T>
59 squared_distance(const Eigen::Matrix<stan::math::fvar<T>, R1, C1>& v1,
60 const Eigen::Matrix<double, R2, C2>& v2) {
61 stan::math::check_vector("squared_distance", "v1", v1);
62 stan::math::check_vector("squared_distance", "v2", v2);
63 stan::math::check_matching_sizes("squared_distance",
64 "v1", v1,
65 "v2", v2);
66 Eigen::Matrix<double, R1, C1> t_v2 = v2.transpose();
67 Eigen::Matrix<stan::math::fvar<T>, R1, C1> v3
68 = stan::math::subtract(v1, t_v2);
69 return stan::math::dot_self(v3);
70 }
71
85 template<typename T, int R, int C>
86 inline stan::math::fvar<T>
87 squared_distance(const Eigen::Matrix<double, R, C>& v1,
88 const Eigen::Matrix<stan::math::fvar<T>, R, C>& v2) {
89 stan::math::check_vector("squared_distance", "v1", v1);
90 stan::math::check_vector("squared_distance", "v2", v2);
91 stan::math::check_matching_sizes("squared_distance",
92 "v1", v1,
93 "v2", v2);
94 Eigen::Matrix<stan::math::fvar<T>, R, C> v3
95 = stan::math::subtract(v1, v2);
96 return stan::math::dot_self(v3);
97 }
98
114 template<typename T, int R1, int C1, int R2, int C2>
115 inline stan::math::fvar<T>
116 squared_distance(const Eigen::Matrix<double, R1, C1>& v1,
117 const Eigen::Matrix<stan::math::fvar<T>, R2, C2>& v2) {
118 stan::math::check_vector("squared_distance", "v1", v1);
119 stan::math::check_vector("squared_distance", "v2", v2);
120 stan::math::check_matching_sizes("squared_distance",
121 "v1", v1,
122 "v2", v2);
123 Eigen::Matrix<double, R2, C2> t_v1 = v1.transpose();
124 Eigen::Matrix<stan::math::fvar<T>, R2, C2> v3
125 = stan::math::subtract(t_v1, v2);
126 return stan::math::dot_self(v3);
127 }
141 template<typename T, int R, int C>
142 inline stan::math::fvar<T>
143 squared_distance(const Eigen::Matrix<stan::math::fvar<T>, R, C>& v1,
144 const Eigen::Matrix<stan::math::fvar<T>, R, C>& v2) {
145 stan::math::check_vector("squared_distance", "v1", v1);
146 stan::math::check_vector("squared_distance", "v2", v2);
147 stan::math::check_matching_sizes("squared_distance",
148 "v1", v1,
149 "v2", v2);
150 Eigen::Matrix<stan::math::fvar<T>, R, C> v3
151 = stan::math::subtract(v1, v2);
152 return stan::math::dot_self(v3);
153 }
154
170 template<typename T, int R1, int C1, int R2, int C2>
171 inline stan::math::fvar<T>
172 squared_distance(const Eigen::Matrix<stan::math::fvar<T>, R1, C1>& v1,
173 const Eigen::Matrix<stan::math::fvar<T>, R2, C2>& v2) {
174 stan::math::check_vector("squared_distance", "v1", v1);
175 stan::math::check_vector("squared_distance", "v2", v2);
176 stan::math::check_matching_sizes("squared_distance",
177 "v1", v1,
178 "v2", v2);
179 Eigen::Matrix<stan::math::fvar<T>, R2, C2> t_v1 = v1.transpose();
180 Eigen::Matrix<stan::math::fvar<T>, R2, C2> v3
181 = stan::math::subtract(t_v1, v2);
182 return stan::math::dot_self(v3);
183 }
184 }
185 }
186 #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
stan::math::fvar< T > squared_distance(const Eigen::Matrix< stan::math::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:13

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