Contents

vDSP_distancesq

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

Declaration

extern void vDSP_distancesq(const float *__A, vDSP_Stride __IA, const float *__B, vDSP_Stride __IB, float *__C, vDSP_Length __N);

Parameters

  • __A:

    A vector that contains the coordinates of the first point.

  • __IA:

    The distance between the elements in the input vector A.

  • __B:

    A vector that contains the coordinates of the second point.

  • __IB:

    The distance between the elements in the input vector B.

  • __C:

    On output, the square of the Euclidean distance between the two points.

  • __N:

    The number of elements to process.

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 stride = 1
    
    let pointA: [Float] = [-1, 0, -2, 1]
    let pointB: [Float] = [ 2, 1,  4, 3]
    
    var distanceSquared = Float()
    
    vDSP_distancesq(pointA, stride,
                    pointB, stride,
                    &distanceSquared,
                    vDSP_Length(pointA.count))
    
    print("distance²", distanceSquared) // Prints "distance² 50.0"

See Also

Vector distance computation