Skip to main content

pdist

Function pdist 

Source
pub fn pdist<T, F>(
    x: &ArrayBase<OwnedRepr<T>, Dim<[usize; 2]>>,
    metric: F,
) -> ArrayBase<OwnedRepr<T>, Dim<[usize; 2]>>
where T: Float + Debug, F: Fn(&[T], &[T]) -> T,
Expand description

Compute a distance matrix between two sets of points

§Arguments

  • x_a - First set of points
  • xb - Second set of points
  • metric - Distance metric to use

§Returns

  • Distance matrix with shape (x_a.nrows(), xb.nrows())

§Examples

use scirs2_spatial::distance::{pdist, euclidean};
use scirs2_core::ndarray::array;

let points = array![[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]];
let dist_matrix = pdist(&points, euclidean);

assert_eq!(dist_matrix.shape(), &[3, 3]);
assert!((dist_matrix[(0, 1)] - 1.0f64).abs() < 1e-6);
assert!((dist_matrix[(0, 2)] - 1.0f64).abs() < 1e-6);
assert!((dist_matrix[(1, 2)] - std::f64::consts::SQRT_2).abs() < 1e-6);