Function distances::vectors::cosine

source ·
pub fn cosine<T: Number, U: Float>(x: &[T], y: &[T]) -> U
Expand description

Computes the Cosine distance between two vectors.

The cosine distance is defined as 1.0 - c where c is the cosine similarity.

The cosine similarity is defined as the dot product of the two vectors divided by the product of their magnitudes.

See the crate::vectors module documentation for information on this function’s potentially unexpected behaviors

§Arguments

  • x: A slice of numbers.
  • y: A slice of numbers.

§Examples

use distances::vectors::cosine;

let x: Vec<f32> = vec![1.0, 0.0, 0.0];
let y: Vec<f32> = vec![0.0, 1.0, 0.0];

let distance: f32 = cosine(&x, &y);

assert!((distance - 1.0).abs() < f32::EPSILON);

§References