Rust · Dense Distances

Dense Distances

use numkong::Angular;use numkong::Euclidean;use numkong::SpatialSimilarity; 8.0.0-rc.1 · b1f866ab

The dense spatial family covers sqeuclidean, euclidean, and angular. The main value over naive loops is the combination of SIMD and safer accumulation policy.

use numkong::Euclidean;

let a = [1_i8, 2, 3, 4];
let b = [4_i8, 3, 2, 1];

let distance = i8::euclidean(&a, &b).unwrap(); // widened output, not int8
println!("{distance}");

API

TraitAngularComputes the angular distance, also called cosine distance, between two vectors.3
spatial.rs:360 ↗
pub trait Angular: StorageElement

d = 1 − (a · b) / (‖a‖ × ‖b‖)

Range: [0, 2]. Fails with Error::ShapeMismatch if lengths differ, or Error::KernelFailed carrying the kernel's status.

Implemented for: f64, f32, f16, bf16, i8, u8, e4m3, e5m2, e2m3, e3m2, i4x2, and u4x2.

Implementors

OutputResult type of the angular distance.
spatial.rs:362 ↗
type Output;
angularComputes the angular distance between two equally long slices.
spatial.rs:364 ↗
fn angular(a: &[Self], b: &[Self]) -> Result<Self::Output, Error>
cosineAlias for angular.
spatial.rs:367 ↗
fn cosine(a: &[Self], b: &[Self]) -> Result<Self::Output, Error>
TraitEuclideanComputes the Euclidean distance, the L2 norm of the difference, between two vectors.4
spatial.rs:625 ↗
pub trait Euclidean: StorageElement

d = √(∑ᵢ (aᵢ − bᵢ)²)

Range: [0, ∞). Fails with [Error::ShapeMismatch] if lengths differ, or Error::KernelFailed carrying the kernel's status.

Implemented for: f64, f32, f16, bf16, i8, u8, e4m3, e5m2, e2m3, e3m2, i4x2, and u4x2.

Implementors

SqEuclideanOutputResult type of the squared Euclidean distance.
spatial.rs:627 ↗
type SqEuclideanOutput;
EuclideanOutputResult type of the Euclidean distance.
spatial.rs:629 ↗
type EuclideanOutput;
sqeuclideanSquared Euclidean distance, i.e.
spatial.rs:632 ↗
fn sqeuclidean(a: &[Self], b: &[Self]) -> Result<Self::SqEuclideanOutput, Error>

L2². Faster than euclidean for comparisons.

euclideanEuclidean distance, i.e.
spatial.rs:635 ↗
fn euclidean(a: &[Self], b: &[Self]) -> Result<Self::EuclideanOutput, Error>

L2. True metric distance.

TraitSpatialSimilaritySpatialSimilarity bundles spatial distance metrics: Dot, Angular, and Euclidean.
spatial.rs:1113 ↗
pub trait SpatialSimilarity: Dot + Angular + Euclidean

Implementors