Rust · Dot Products

Dot Products

use numkong::Dot;use numkong::VDot; 8.0.0-rc.1 · b1f866ab

Dot products span real, complex, quantized, and packed storage types.

use numkong::{Dot, VDot, f32c};

let a = [f32c { re: 1.0, im: 2.0 }, f32c { re: 3.0, im: 4.0 }];
let b = [f32c { re: 5.0, im: 6.0 }, f32c { re: 7.0, im: 8.0 }];

let dot = f32c::dot(&a, &b).unwrap();
let vdot = f32c::vdot(&a, &b).unwrap(); // like numpy.vdot, conjugated

println!("{dot:?} {vdot:?}");

API

TraitDotComputes the dot product — the inner product — between two vectors.3
dot.rs:261 ↗
pub trait Dot: StorageElement

d = ∑ᵢ aᵢ × bᵢ

Range: unbounded. 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, u4x2, u1x8.

On u1x8 this is the binary inner product — the number of positions where both bits are set — which is the intersection count that Tanimoto similarity divides by the union. Use crate::Jaccard when you want that ratio.

Examples

use numkong::Dot;
let first = vec![1.0_f32, 2.0, 3.0];
let second = vec![4.0_f32, 5.0, 6.0];
let result = f32::dot(&first, &second).unwrap();
assert!((result - 32.0).abs() < 1e-5);

Implementors

OutputResult type of the dot product.
dot.rs:263 ↗
type Output;
dotComputes the dot product of two equally long slices.
dot.rs:265 ↗
fn dot(first: &[Self], second: &[Self]) -> Result<Self::Output, Error>
innerAlias for dot.
dot.rs:268 ↗
fn inner(first: &[Self], second: &[Self]) -> Result<Self::Output, Error>
TraitVDotComputes the conjugating dot product, the Hermitian inner product.1
dot.rs:640 ↗
pub trait VDot: Dot

For real-valued types this is identical to Dot::dot. For complex-valued types it computes ∑ᵢ conj(aᵢ) × bᵢ, which is the convention used in quantum mechanics and most linear-algebra textbooks — BLAS cdotc, NumPy vdot.

Unlike an ordinary complex dot product, the Hermitian form produces a proper inner product: vdot(x, x) is always a non-negative real number equal to ‖x‖², and vdot(x, y) == conj(vdot(y, x)). This matters in retrieval and signal-processing pipelines where cancellation between a vector and its conjugate should leave the norm intact.

Implementors

vdotHermitian inner product.
dot.rs:644 ↗
fn vdot(first: &[Self], second: &[Self]) -> Result<Self::Output, Error>

On real-valued types this falls back to Dot::dot; on complex types it returns ∑ᵢ conj(aᵢ) × bᵢ computed in the widened accumulator described by Dot::Output.