Automatic Differentiation
 
Loading...
Searching...
No Matches
distance.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_DISTANCE_HPP
2#define STAN_MATH_PRIM_FUN_DISTANCE_HPP
3
10#include <cmath>
11
12namespace stan {
13namespace math {
14
25template <typename T1, typename T2,
26 require_all_stan_scalar_t<T1, T2>* = nullptr>
27inline return_type_t<T1, T2> distance(const T1& x1, const T2& x2) {
28 check_finite("distance", "x1", x1);
29 check_finite("distance", "x2", x2);
30 return abs(x1 - x2);
31}
32
46template <typename T1, typename T2, require_all_vector_t<T1, T2>* = nullptr>
47inline return_type_t<T1, T2> distance(const T1& x1, const T2& x2) {
48 using std::sqrt;
49 check_matching_sizes("distance", "x1", x1, "x2", x2);
50 return sqrt(squared_distance(x1, x2));
51}
52
53} // namespace math
54} // namespace stan
55
56#endif
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
fvar< T > abs(const fvar< T > &x)
Definition abs.hpp:15
auto distance(const T_a &a, const T_b &b)
Returns the distance between the specified vectors.
Definition distance.hpp:33
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.
fvar< T > sqrt(const fvar< T > &x)
Definition sqrt.hpp:17
void check_finite(const char *function, const char *name, const T_y &y)
Return true if all values in y are finite.
auto squared_distance(const T_a &a, const T_b &b)
Returns the squared distance.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...