Contents

distanceSquared(_:_:)

Returns the single-precision distance squared between two points in n-dimensional space.

Declaration

static func distanceSquared<U, V>(_ pointA: U, _ pointB: V) -> Float where U : AccelerateBuffer, V : AccelerateMutableBuffer, U.Element == Float, V.Element == Float

Parameters

  • pointA:

    An array that contains the coordinates of the first point.

  • pointB:

    An array that contains the coordinates of the second point.

Discussion

This function calculates the sum of squares of the differences of corresponding elements of vectors A and B, using the following operation:

C[0] = sum((A[n] - B[n]) ** 2, 0 <= n < N); 

For example, the following code calculates the distance squared between the two four-dimensional points pointA and pointB:

    let pointA: [Float] = [-1, 0, -2, 1]
    let pointB: [Float] = [ 2, 1,  4, 3]
    
    let distanceSquared = vDSP.distanceSquared(pointA, pointB)

    print("distance²", distanceSquared) // Prints "distance² 50.0".

See Also

Type Methods