Rust · Curved Metrics

Curved Metrics

use numkong::Bilinear;use numkong::Mahalanobis; 8.0.0-rc.1 · b1f866ab

Curved-space kernels combine vectors with an extra metric tensor or covariance inverse.

use numkong::{Bilinear, Mahalanobis, f32c};

// Complex bilinear form: aᴴ M b
let a = [f32c { re: 1.0, im: 0.0 }; 16];
let b = [f32c { re: 0.0, im: 1.0 }; 16];
let metric = [f32c { re: 1.0, im: 0.0 }; 16 * 16];
let bilinear = f32c::bilinear(&a, &b, &metric).unwrap();

// Real Mahalanobis distance: √((a−b)ᵀ M⁻¹ (a−b))
let x = [1.0_f32; 32];
let y = [2.0_f32; 32];
let mut inv_cov = vec![0.0_f32; 32 * 32];
for i in 0..32 { inv_cov[i * 32 + i] = 1.0; } // identity matrix
let distance = f32::mahalanobis(&x, &y, &inv_cov).unwrap();

API

TraitBilinearBilinear form computation: aᵀ × C × b where C is a metric tensor.2
curved.rs:138 ↗
pub trait Bilinear: StorageElement

Computes the bilinear form of two vectors a and b with respect to a symmetric matrix C, given in row-major order as a flat slice of length n².

Implementors

OutputOutput type for results.
curved.rs:140 ↗
type Output;

f32 widens to f64; f16/bf16 use f32.

bilinearComputes the bilinear form aᵀ × C × b.
curved.rs:153 ↗
fn bilinear(a: &[Self], b: &[Self], c: &[Self]) -> Result<Self::Output, Error>

Arguments

  • a - First vector of length n
  • b - Second vector of length n
  • c - Metric tensor, symmetric [n,n] matrix in row-major order, flattened to length n²

Errors

Error::InvalidShape for empty vectors, Error::ShapeMismatch if lengths are incompatible, or Error::KernelFailed carrying the kernel's status.

TraitMahalanobisMahalanobis distance: √((a−b)ᵀ × C × (a−b)).2
curved.rs:420 ↗
pub trait Mahalanobis: StorageElement

Computes the Mahalanobis distance between two vectors a and b with respect to an inverse covariance matrix C, given in row-major order as a flat slice of length n².

Implementors

OutputOutput type for results.
curved.rs:422 ↗
type Output;

f32 widens to f64; f16/bf16 use f32.

mahalanobisComputes the Mahalanobis distance √((a−b)ᵀ × C × (a−b)).
curved.rs:435 ↗
fn mahalanobis(a: &[Self], b: &[Self], c: &[Self]) -> Result<Self::Output, Error>

Arguments

  • a - First vector of length n
  • b - Second vector of length n
  • c - Inverse covariance matrix, a symmetric [n,n] matrix in row-major order, flattened to length n²

Errors

Error::InvalidShape for empty vectors, Error::ShapeMismatch if lengths are incompatible, or Error::KernelFailed carrying the kernel's status.