Function

vDSP_distancesq(_:_:_:_:_:_:)

Calculates the distance squared between two single-precision points in n-dimensional space, using the specified strides.

Declaration

func vDSP_distancesq(_ __A: UnsafePointer<Float>, _ __IA: vDSP_Stride, _ __B: UnsafePointer<Float>, _ __IB: vDSP_Stride, _ __C: UnsafeMutablePointer<Float>, _ __N: vDSP_Length)

Parameters

__A

Single-precision real input vector.

__IA

Stride for A.

__B

Single-precision real input vector.

__IB

Stride for B.

__C

Single-precision real output value, the square of the Euclidean distance between the two points

__N

The number of elements to process in both input vectors.

Discussion

The vDSP_distancesq(_:_:_:_:_:_:) and vDSP_distancesqD(_:_:_:_:_:_:) functions interpret the first N elements of A and B as defining two points in N-dimensional space, and write the square of the Euclidean distance between the two points to C:

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

For example, the following code defines a and b as two points in four-dimensional space:

let a: [Float] = [-1, 0, -2, 1]
let b: [Float] = [2, 1, 4, 3]

To calculate the square of the distance between the two points, pass them to vDSP_distancesq(_:_:_:_:_:_:):

var c: Float = .nan

let n = vDSP_Length(a.count)
let stride = vDSP_Stride(1)

vDSP_distancesq(a, stride,
                b, stride,
                &c,
                n)

print("distance²", c) // Prints "distance² 50.0"

On return, the output scalar contains the distance squared, 50.

See Also

Vector-to-Vector Distance Computation

static func distanceSquared<U, V>(U, V) -> Double

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

static func distanceSquared<U, V>(U, V) -> Float

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

func vDSP_distancesqD(UnsafePointer<Double>, vDSP_Stride, UnsafePointer<Double>, vDSP_Stride, UnsafeMutablePointer<Double>, vDSP_Length)

Calculates the distance squared between two double-precision points in n-dimensional space, using the specified strides.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software