kdtree_simd/
distance.rs

1//! Defines different distance metrics, in simplest case it defines the
2//! euclidean distance which is no more than the square root of the sum of the
3//! squares of the distances in each dimension.
4
5use num_traits::Float;
6use simd_euclidean::Vectorized;
7
8/// Returns the squared euclidean distance between two points. When you only
9/// need to compare distances, rather than having the exact distance between
10/// the points, this metric is benefitial because it avoids the expensive square
11/// root computation.
12///
13/// # Examples
14///
15/// ```rust
16/// use kdtree::distance::squared_euclidean;
17///
18/// assert!(0.0 == squared_euclidean(&[0.0, 0.0], &[0.0, 0.0]));
19/// assert!(2.0 == squared_euclidean(&[0.0, 0.0], &[1.0, 1.0]));
20/// assert!(1.0 == squared_euclidean(&[0.0, 0.0], &[1.0, 0.0]));
21/// ```
22///
23/// # Panics
24///
25/// Only in debug mode, the length of the slices at input will be compared.
26/// If they do not match, there will be a panic:
27///
28/// ```rust,should_panic
29/// # use kdtree::distance::squared_euclidean;
30/// // this is broken
31/// let _ = squared_euclidean(&[0.0, 0.0], &[1.0, 0.0, 0.0]);
32/// ```
33pub fn squared_euclidean_f32(a: &[f32], b: &[f32]) -> f32 {
34    debug_assert_eq!(a.len(), b.len());
35    Vectorized::distance(a, b)
36}
37
38pub fn squared_euclidean_f64(a: &[f64], b: &[f64]) -> f64 {
39    debug_assert_eq!(a.len(), b.len());
40    Vectorized::distance(a, b)
41}