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².
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();BilinearBilinear form computation: aᵀ × C × b where C is a metric tensor.2pub 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².
OutputOutput type for results.bilinearComputes the bilinear form aᵀ × C × b.fn bilinear(a: &[Self], b: &[Self], c: &[Self]) -> Result<Self::Output, Error> a - First vector of length nb - Second vector of length nc - Metric tensor, symmetric [n,n] matrix in row-major order, flattened to
length n²Error::InvalidShape for empty vectors, Error::ShapeMismatch if lengths
are incompatible, or Error::KernelFailed carrying the kernel's status.
MahalanobisMahalanobis distance: √((a−b)ᵀ × C × (a−b)).2pub 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².
impl Mahalanobis for f64impl Mahalanobis for f32impl Mahalanobis for f16impl Mahalanobis for bf16OutputOutput type for results.mahalanobisComputes the Mahalanobis distance √((a−b)ᵀ × C × (a−b)).fn mahalanobis(a: &[Self], b: &[Self], c: &[Self]) -> Result<Self::Output, Error> a - First vector of length nb - Second vector of length nc - Inverse covariance matrix, a symmetric [n,n] matrix in row-major order,
flattened to length n²Error::InvalidShape for empty vectors, Error::ShapeMismatch if lengths
are incompatible, or Error::KernelFailed carrying the kernel's status.