euclideanDistanceGPU function

GPUTensor<Scalar> euclideanDistanceGPU(
  1. GPUTensor<Vector> a,
  2. GPUTensor<Vector> b,
  3. CommandBuffer tape
)

Calculates the straight-line Euclidean distance between two vectors. Computes the element-wise difference between vector a and b, squares the differences, sums them, and extracts the square root. The operations are appended to tape for execution.

Implementation

GPUTensor<Scalar> euclideanDistanceGPU(
    GPUTensor<Vector> a, GPUTensor<Vector> b, CommandBuffer tape) {
  // sqrt( sum( (a - b)^2 ) )
  GPUTensor<Vector> diff = subtractGPU<Vector>(a, b, tape);
  GPUTensor<Vector> squaredDiff = powGPU<Vector>(diff, 2.0, tape);
  GPUTensor<Scalar> sumOfSquares = sumGPU(squaredDiff, tape);
  GPUTensor<Scalar> out = sqrtGPU<Scalar>(sumOfSquares, tape);
  out.subMap["diff"] = diff;
  out.subMap["squaredDiff"] = squaredDiff;
  out.subMap["sumOfSquares"] = sumOfSquares;
  return out;
}