hypot(x0:x1:y0:y1:result:)
Calculates the single-precision hypotenuses of right triangles with legs that are the differences of corresponding elements of two pairs of vectors.
Declaration
static func hypot<R, S, T, U, V>(x0: R, x1: S, y0: T, y1: U, result: inout V) where R : AccelerateBuffer, S : AccelerateBuffer, T : AccelerateBuffer, U : AccelerateBuffer, V : AccelerateMutableBuffer, R.Element == Float, S.Element == Float, T.Element == Float, U.Element == Float, V.Element == FloatParameters
- x0:
An array that contains the first values of the first set of legs of the triangles.
- x1:
An array that contains the second values of the first set of legs of the triangles.
- y0:
An array that contains the first values of the second set of legs of the triangles.
- y1:
An array that contains the second values of the second set of legs of the triangles.
- result:
An array that receives the result of the calculation.
Discussion
This function calculates the length of the hypotenuse of n number of triangles, where n is the number of elements in the supplied vectors. The differences between corresponding elements of vectors x0 and x1 and vectors y0 and y1 define the lengths of the two legs of each triangle.
The functions use the following operation:
for (n = 0; n < N; ++n)
E[n] = sqrt((x0[n]-x1[n])**2 + (y0[n]-y1[n])**2);For example, the following code calculates the hypotenuse of four Pythagorean triples:
let x0: [Float] = [3, 6, 5, 9]
let x1: [Float] = [0, 0, 0, 0]
let y0: [Float] = [0, 0, 0, 0]
let y1: [Float] = [4, 8, 12, 12]
let hypotenuses = [Float](
unsafeUninitializedCapacity: x0.count) {
buffer, initializedCount in
vDSP.hypot(x0: x0, x1: x1,
y0: y0, y1: y1,
result: &buffer)
initializedCount = x0.count
}
// Prints "[5.0, 10.0, 13.0, 15.0]".
print(hypotenuses)