Parent macro-issue: #12
Depends on: #13 (Phase 0)
Goal
Make WiringMetric::QuadranceGaussian fully operational end-to-end: remove all sqrt calls from the k-NN hot path and the Laplacian kernel computation path. This is the first phase that changes runtime behaviour and becomes the live default.
Background
The current compute_bhattacharyya_weights() exponent is already (μ_i − μ_j)² / (σ_i² + σ_j²) — a squared distance — so no sqrt is analytically required. The only sqrt calls in the pipeline today are:
- k-NN gate:
distance = sqrt(sum_sq) used for threshold comparisons.
- Normalisation steps:
‖x‖ computed to produce unit vectors before cosine.
- Logging/diagnostics: sqrt for human-readable distance display.
Since sqrt is monotone, d < r ↔ d² < r² — the substitution is mathematically exact for all ordering operations. The quadrance Gaussian kernel:
w_ij = exp( −Q(i,j) / 2σ² )
= exp( −‖μ_i − μ_j‖² / 2σ² )
is the canonical heat-kernel form in the Graph Wiring / Laplace–Beltrami literature and carries identical convergence guarantees to the BC formulation.
Detailed changes
1. Kernel dispatcher in Stage C
let weights = match config.wiring_metric {
WiringMetric::QuadranceGaussian { sigma_sq } =>
compute_quadrance_weights(¢roids, sigma_sq), // new default
WiringMetric::QuadranceRational { .. } =>
unimplemented!("Phase 4"),
WiringMetric::BhattacharyyaCosine =>
compute_bc_weights(¢roids), // legacy
};
compute_quadrance_weights() calls surfface_geometry::quadrance() — zero sqrt in the hot path.
2. k-NN gate audit
3. sigma_sq auto-calibration
The BC kernel has implicit bandwidth from (μ_i−μ_j)²/(σ_i²+σ_j²). QuadranceGaussian needs σ² set to match. Implement:
/// Estimate sigma_sq for QuadranceGaussian from empirical mean quadrance
/// of k-NN pairs — Scott's rule adapted to quadrance.
/// Runs once at index build time; stored in serialised LaplacianConfig.
pub fn estimate_sigma_sq(knn_quadrances: &[f32]) -> f32 {
knn_quadrances.iter().sum::<f32>() / (2.0 * knn_quadrances.len() as f32)
}
Benchmarks (CVE corpus, N = 313 841, F = 384)
| Metric |
BC baseline |
QuadranceGaussian |
Target |
| k-NN build time |
baseline |
≥ 10 % faster |
✔ |
| Laplacian build time |
baseline |
≥ 5 % faster |
✔ |
| Serialised index size |
baseline |
≤ baseline + 1 % |
✔ |
| TauModeScore KS vs BC |
— |
< 0.01 |
✔ |
| Cross-platform bit-exact |
no |
yes |
✔ |
Acceptance criteria
Parent macro-issue: #12
Depends on: #13 (Phase 0)
Goal
Make
WiringMetric::QuadranceGaussianfully operational end-to-end: remove allsqrtcalls from the k-NN hot path and the Laplacian kernel computation path. This is the first phase that changes runtime behaviour and becomes the live default.Background
The current
compute_bhattacharyya_weights()exponent is already(μ_i − μ_j)² / (σ_i² + σ_j²)— a squared distance — so no sqrt is analytically required. The only sqrt calls in the pipeline today are:distance = sqrt(sum_sq)used for threshold comparisons.‖x‖computed to produce unit vectors before cosine.Since
sqrtis monotone,d < r↔d² < r²— the substitution is mathematically exact for all ordering operations. The quadrance Gaussian kernel:is the canonical heat-kernel form in the Graph Wiring / Laplace–Beltrami literature and carries identical convergence guarantees to the BC formulation.
Detailed changes
1. Kernel dispatcher in Stage C
compute_quadrance_weights()callssurfface_geometry::quadrance()— zero sqrt in the hot path.2. k-NN gate audit
f32::sqrt/f64::sqrtcall insurfface-coreandarrowspace.sqrt(sum_sq) < rwithsum_sq < r * rat every comparison site.quadrance=; never in the comparison path.**/wiring*.rsand**/knn*.rs.3.
sigma_sqauto-calibrationThe BC kernel has implicit bandwidth from
(μ_i−μ_j)²/(σ_i²+σ_j²). QuadranceGaussian needs σ² set to match. Implement:Benchmarks (CVE corpus, N = 313 841, F = 384)
Acceptance criteria
cargo benchshows ≥ 10 % speed improvement in k-NN build on CVE.sqrtcalls in k-NN and kernel hot path (CI grep enforced).estimate_sigma_sq()documented and tested on a synthetic Gaussian point cloud.TauModeScoreKS-statistic vs BC baseline < 0.01 on CVE.