hypot(_:_:result:)
Calculates the single-precision hypotenuses of right triangles with legs that are the lengths of corresponding elements of the two input vectors.
Declaration
static func hypot<T, U, V>(_ x: T, _ y: U, result: inout V) where T : AccelerateBuffer, U : AccelerateBuffer, V : AccelerateMutableBuffer, T.Element == Float, U.Element == Float, V.Element == FloatParameters
- x:
An array that contains the lengths of the first set of legs of the triangles.
- y:
An array that contains the lengths of the second set of legs of the triangles.
- result:
An array that receives the result of the calculation.
Discussion
This function calculates the square roots of the sum of the squares of corresponding elements of vectors x and y, using the following operation:
for (n = 0; n < N; ++n)
C[n] = sqrt(x[n]*x[n] + y[n]*y[n]);For example, the following code calculates the hypotenuse of four Pythagorean triples:
let x: [Float] = [3, 6, 5, 9]
let y: [Float] = [4, 8, 12, 12]
let hypotenuses = [Float](
unsafeUninitializedCapacity: x.count) {
buffer, initializedCount in
vDSP.hypot(x, y,
result: &buffer)
initializedCount = x.count
}
// Prints "[5.0, 10.0, 13.0, 15.0]".
print(hypotenuses)