February 2018
Intermediate to advanced
378 pages
10h 14m
English
Now we will take a look at how linear algebraic operations can be performed:
// Add two vectors. Equivalent to zip(a, b).map(+) func vecAdd(_ a: [Double], _ b: [Double]) -> [Double] { let count = a.count assert(count == b.count, "Vectors must be of equal length.") var c = [Double](repeating: 0.0, count: count) vDSP_vaddD(a, 1, b, 1, &c, 1, vDSP_Length(count)) return c } // Subtract vector b from vector a. Equivalent to zip(a, b).map(-) func vecSubtract(_ a: [Double], _ b: [Double]) -> [Double] { let count = a.count assert(count == b.count, "Vectors must be of equal length.") var c = [Double](repeating: 0.0, count: count) vDSP_vsubD(b, 1, a, 1, &c, 1, vDSP_Length(count)) return c } // Multiply two vectors elementwise. ...Read now
Unlock full access