Using vDSP for vector-based arithmetic
Increase the performance of common mathematical tasks with vDSP vector-vector and vector-scalar operations.
Overview
vDSP provides a suite of general-purpose, high-performance arithmetic functions that are alternatives to for loops and map when applying operations on collections of floating-point values.
For example, the code below multiplies the element-wise sum of two arrays by a scalar value:
let a: [Float] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let b: [Float] = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
let c: Float = 2
let d = zip(a, b).map {
($0.0 + $0.1) * c
}
// The `d` equals [22.0, 44.0, 66.0, 88.0, 110.0, 132.0, 154.0, 176.0, 198.0, 220.0].The vDSP version of the same operation runs significantly faster.
Many vDSP functions have a variant that returns the result.
Available arithmetic functions
The following table summarizes the basic arithmetic functions available in vDSP. All functions are available in single- and double-precision variants.
In the Operation column, a subscript (for example, a[i]) indicates a vector, and no subscript (for example, a) indicates a scalar value.
The C function column links to the equivalent low-level C interface. The Swift vDSP overlay (for example, multiply(_:_:)) has no Objective-C interface; from C, call the underlying vDSP_* function shown here. Each C function also has a double-precision variant with a D suffix (for example, vDSP_vsaddD).
Operation | Function | C function |
|---|---|---|
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
|