pub const HOLOGRAPHIC_BATCH_SIMILARITY: &str = r#"
struct TDC {
tropical: f32,
dual_real: f32,
dual_dual: f32,
clifford: array<f32, 8>,
_padding: array<f32, 5>,
}
@group(0) @binding(0) var<storage, read> vectors_a: array<TDC>;
@group(0) @binding(1) var<storage, read> vectors_b: array<TDC>;
@group(0) @binding(2) var<storage, read_write> similarities: array<f32>;
@group(0) @binding(3) var<uniform> params: array<u32, 4>; // [count_a, count_b, mode, 0]
// mode: 0=pairwise (a[i] vs b[i]), 1=matrix (all pairs)
// Compute reverse of multivector (flip sign of grades 2 and 3)
fn reverse_sign(grade: u32) -> f32 {
// Grade 0: +1, Grade 1: +1, Grade 2: -1, Grade 3: -1
// For Cl(3,0): indices 0=scalar(g0), 1-3=vectors(g1), 4-6=bivectors(g2), 7=trivector(g3)
let signs = array<f32, 8>(1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0);
return signs[grade];
}
// Compute scalar product <A B̃>₀ - the proper inner product for similarity
fn scalar_product_with_reverse(a: TDC, b: TDC) -> f32 {
var result = 0.0;
// For each basis element, compute contribution to scalar part
// Using simplified formula: sum of a[i] * b[i] * reverse_sign(i) * cayley_contribution_to_scalar
// For diagonal elements (same basis): e_i * e_i contributes to scalar
for (var i = 0u; i < 8u; i = i + 1u) {
result += a.clifford[i] * b.clifford[i] * reverse_sign(i);
}
return result;
}
fn norm(v: TDC) -> f32 {
var sum = 0.0;
for (var i = 0u; i < 8u; i = i + 1u) {
sum += v.clifford[i] * v.clifford[i];
}
return sqrt(sum);
}
@compute @workgroup_size(256)
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
let idx = global_id.x;
let count_a = params[0];
let count_b = params[1];
let mode = params[2];
if (mode == 0u) {
// Pairwise mode: similarities[i] = sim(a[i], b[i])
if (idx >= count_a) {
return;
}
let a = vectors_a[idx];
let b = vectors_b[idx];
let norm_a = norm(a);
let norm_b = norm(b);
if (norm_a < 1e-10 || norm_b < 1e-10) {
similarities[idx] = 0.0;
return;
}
let inner = scalar_product_with_reverse(a, b);
similarities[idx] = inner / (norm_a * norm_b);
} else {
// Matrix mode: similarities[i * count_b + j] = sim(a[i], b[j])
let total = count_a * count_b;
if (idx >= total) {
return;
}
let i = idx / count_b;
let j = idx % count_b;
let a = vectors_a[i];
let b = vectors_b[j];
let norm_a = norm(a);
let norm_b = norm(b);
if (norm_a < 1e-10 || norm_b < 1e-10) {
similarities[idx] = 0.0;
return;
}
let inner = scalar_product_with_reverse(a, b);
similarities[idx] = inner / (norm_a * norm_b);
}
}
"#;Expand description
Batch similarity computation for holographic vectors Computes pairwise similarities using inner product with reverse: <A B̃>₀