pairwise_sqdist

std.geometry.pairwise_sqdist · Level L0

Squared distances between every point of X and every point of Y, with one matrix product.

Dᵢⱼ = ‖xᵢ‖² + ‖yⱼ‖² − 2·xᵢ·yⱼ

Signature

pairwise_sqdist(X: f64[m, d], Y: f64[k, d]) → f64[m, k]

Structure

The function as NOVA stores it: one box per input, operation and output, and arrows that carry values. A double border marks a call to another library function; select it to open that function.

Xf64[m, d]Yf64[k, d]MultiplyXXMultiplyYYTransposeYtReduceSumx2ReduceSumy2MatMulXY2.0Reshapey2rMultiplyXY2Addn2SubtractDDf64[m, k]
  • input
  • operation
  • constant
  • call
  • output

Verification

  • Signature proven by NOVA’s shape solver, for every size.
  • Equal to the reference ((X[:, None, :] - Y[None, :, :]) ** 2).sum(-1) in exact rational arithmetic, on all 40 test cases.
  • All 572 float64 results inside the running error bound; the closest uses 48% of it.
  • Interpreter and NumPy backend return bit-identical results.
Accuracy in detail
correctly rounded (the float64 nearest the exact value)
65%
bit-equal to the NumPy formula in float64
58%
largest error, in units in the last place
5.8e+3

Large ulp counts appear only where cancellation drives a result toward zero; the absolute error is still inside the bound.

Note

The expanded form is fast but cancels when two points nearly coincide; the verification bound shows exactly how much. For one pair, sqdist is more accurate.

Identity

Calls
—
Called by
—
sha256:b8a714fe8e09a2d5aa1cbdb19899ada7a9dca369cf7ae00ffb6befb142841356

The semantic hash of the graph. It changes when the program changes, and never when only its documentation does.