0001    // Arithmetic.swift
0002    //
0003    // Copyright (c) 2014–2015 Mattt Thompson (http://mattt.me)
0004    //
0005    // Permission is hereby granted, free of charge, to any person obtaining a copy
0006    // of this software and associated documentation files (the "Software"), to deal
0007    // in the Software without restriction, including without limitation the rights
0008    // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0009    // copies of the Software, and to permit persons to whom the Software is
0010    // furnished to do so, subject to the following conditions:
0011    //
0012    // The above copyright notice and this permission notice shall be included in
0013    // all copies or substantial portions of the Software.
0014    //
0015    // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0016    // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0017    // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0018    // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0019    // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0020    // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
0021    // THE SOFTWARE.
0022    
0023    import Accelerate
0024    
0025    // MARK: Sum
0026    
0027    public func sum
Arithmetic.swift:272
    return sqrt(sum(squared))
(x: [Float]) -> Float { 0028 var result: Float = 0.0 0029 vDSP_sve(x, 1, &result, vDSP_Length(x.count)) 0030 0031 return result 0032 } 0033 0034 public func sum
Arithmetic.swift:281
    return sqrt(sum(squared))
(x: [Double]) -> Double { 0035 var result: Double = 0.0 0036 vDSP_sveD(x, 1, &result, vDSP_Length(x.count)) 0037 0038 return result 0039 } 0040 0041 // MARK: Sum of Absolute Values 0042 0043 public func asum(x: [Float]) -> Float { 0044 return cblas_sasum(Int32(x.count), x, 1) 0045 } 0046 0047 public func asum(x: [Double]) -> Double { 0048 return cblas_dasum(Int32(x.count), x, 1) 0049 } 0050 0051 // MARK: Maximum 0052 0053 public func max(x: [Float]) -> Float { 0054 var result: Float = 0.0 0055 vDSP_maxv(x, 1, &result, vDSP_Length(x.count)) 0056 0057 return result 0058 } 0059 0060 public func max(x: [Double]) -> Double { 0061 var result: Double = 0.0 0062 vDSP_maxvD(x, 1, &result, vDSP_Length(x.count)) 0063 0064 return result 0065 } 0066 0067 // MARK: Minimum 0068 0069 public func min(x: [Float]) -> Float { 0070 var result: Float = 0.0 0071 vDSP_minv(x, 1, &result, vDSP_Length(x.count)) 0072 0073 return result 0074 } 0075 0076 public func min(x: [Double]) -> Double { 0077 var result: Double = 0.0 0078 vDSP_minvD(x, 1, &result, vDSP_Length(x.count)) 0079 0080 return result 0081 } 0082 0083 // MARK: Mean 0084 0085 public func mean(x: [Float]) -> Float { 0086 var result: Float = 0.0 0087 vDSP_meanv(x, 1, &result, vDSP_Length(x.count)) 0088 0089 return result 0090 } 0091 0092 public func mean(x: [Double]) -> Double { 0093 var result: Double = 0.0 0094 vDSP_meanvD(x, 1, &result, vDSP_Length(x.count)) 0095 0096 return result 0097 } 0098 0099 // MARK: Mean Magnitude 0100 0101 public func meamg(x: [Float]) -> Float { 0102 var result: Float = 0.0 0103 vDSP_meamgv(x, 1, &result, vDSP_Length(x.count)) 0104 0105 return result 0106 } 0107 0108 public func meamg(x: [Double]) -> Double { 0109 var result: Double = 0.0 0110 vDSP_meamgvD(x, 1, &result, vDSP_Length(x.count)) 0111 0112 return result 0113 } 0114 0115 // MARK: Mean Square Value 0116 0117 public func measq(x: [Float]) -> Float { 0118 var result: Float = 0.0 0119 vDSP_measqv(x, 1, &result, vDSP_Length(x.count)) 0120 0121 return result 0122 } 0123 0124 public func measq(x: [Double]) -> Double { 0125 var result: Double = 0.0 0126 vDSP_measqvD(x, 1, &result, vDSP_Length(x.count)) 0127 0128 return result 0129 } 0130 0131 // MARK: Add 0132 0133 public func add
Arithmetic.swift:287
    return add(lhs, y: rhs)
Arithmetic.swift:295
    return add(lhs, y: [Float](count: lhs.count, repeatedValue: rhs))
(x: [Float], y: [Float]) -> [Float] { 0134 var results = [Float](y) 0135 cblas_saxpy(Int32(x.count), 1.0, x, 1, &results, 1) 0136 0137 return results 0138 } 0139 0140 public func add
Arithmetic.swift:291
    return add(lhs, y: rhs)
Arithmetic.swift:299
    return add(lhs, y: [Double](count: lhs.count, repeatedValue: rhs))
(x: [Double], y: [Double]) -> [Double] { 0141 var results = [Double](y) 0142 cblas_daxpy(Int32(x.count), 1.0, x, 1, &results, 1) 0143 0144 return results 0145 } 0146 0147 // MARK: Subtraction 0148 0149 public func sub
Arithmetic.swift:303
    return sub(lhs, y: rhs)
Arithmetic.swift:311
    return sub(lhs, y: [Float](count: lhs.count, repeatedValue: rhs))
(x: [Float], y: [Float]) -> [Float] { 0150 var results = [Float](y) 0151 catlas_saxpby(Int32(x.count), 1.0, x, 1, -1, &results, 1) 0152 0153 return results 0154 } 0155 0156 public func sub
Arithmetic.swift:307
    return sub(lhs, y: rhs)
Arithmetic.swift:315
    return sub(lhs, y: [Double](count: lhs.count, repeatedValue: rhs))
(x: [Double], y: [Double]) -> [Double] { 0157 var results = [Double](y) 0158 catlas_daxpby(Int32(x.count), 1.0, x, 1, -1, &results, 1) 0159 0160 return results 0161 } 0162 0163 // MARK: Multiply 0164 0165 public func mul
Arithmetic.swift:335
    return mul(lhs, y: rhs)
Arithmetic.swift:343
    return mul(lhs, y: [Float](count: lhs.count, repeatedValue: rhs))
(x: [Float], y: [Float]) -> [Float] { 0166 var results = [Float](count: x.count, repeatedValue: 0.0) 0167 vDSP_vmul(x, 1, y, 1, &results, 1, vDSP_Length(x.count)) 0168 0169 return results 0170 } 0171 0172 public func mul
Arithmetic.swift:339
    return mul(lhs, y: rhs)
Arithmetic.swift:347
    return mul(lhs, y: [Double](count: lhs.count, repeatedValue: rhs))
(x: [Double], y: [Double]) -> [Double] { 0173 var results = [Double](count: x.count, repeatedValue: 0.0) 0174 vDSP_vmulD(x, 1, y, 1, &results, 1, vDSP_Length(x.count)) 0175 0176 return results 0177 } 0178 0179 // MARK: Divide 0180 0181 public func div
Arithmetic.swift:319
    return div(lhs, y: rhs)
Arithmetic.swift:327
    return div(lhs, y: [Float](count: lhs.count, repeatedValue: rhs))
(x: [Float], y: [Float]) -> [Float] { 0182 var results = [Float](count: x.count, repeatedValue: 0.0) 0183 vvdivf(&results, x, y, [Int32(x.count)]) 0184 0185 return results 0186 } 0187 0188 public func div
Arithmetic.swift:323
    return div(lhs, y: rhs)
Arithmetic.swift:331
    return div(lhs, y: [Double](count: lhs.count, repeatedValue: rhs))
(x: [Double], y: [Double]) -> [Double] { 0189 var results = [Double](count: x.count, repeatedValue: 0.0) 0190 vvdiv(&results, x, y, [Int32(x.count)]) 0191 0192 return results 0193 } 0194 0195 // MARK: Modulo 0196 0197 public func mod
Arithmetic.swift:351
    return mod(lhs, y: rhs)
Arithmetic.swift:359
    return mod(lhs, y: [Float](count: lhs.count, repeatedValue: rhs))
(x: [Float], y: [Float]) -> [Float] { 0198 var results = [Float](count: x.count, repeatedValue: 0.0) 0199 vvfmodf(&results, x, y, [Int32(x.count)]) 0200 0201 return results 0202 } 0203 0204 public func mod
Arithmetic.swift:355
    return mod(lhs, y: rhs)
Arithmetic.swift:363
    return mod(lhs, y: [Double](count: lhs.count, repeatedValue: rhs))
(x: [Double], y: [Double]) -> [Double] { 0205 var results = [Double](count: x.count, repeatedValue: 0.0) 0206 vvfmod(&results, x, y, [Int32(x.count)]) 0207 0208 return results 0209 } 0210 0211 // MARK: Remainder 0212 0213 public func remainder(x: [Float], y: [Float]) -> [Float] { 0214 var results = [Float](count: x.count, repeatedValue: 0.0) 0215 vvremainderf(&results, x, y, [Int32(x.count)]) 0216 0217 return results 0218 } 0219 0220 public func remainder(x: [Double], y: [Double]) -> [Double] { 0221 var results = [Double](count: x.count, repeatedValue: 0.0) 0222 vvremainder(&results, x, y, [Int32(x.count)]) 0223 0224 return results 0225 } 0226 0227 // MARK: Square Root 0228 0229 public func sqrt
FFT.swift:41
    vDSP_vsmul(sqrt(magnitudes), 1, [2.0 / Float(input.count)], &normalizedMagnitudes, 1, vDSP_Length(input.count))
(x: [Float]) -> [Float] { 0230 var results = [Float](count: x.count, repeatedValue: 0.0) 0231 vvsqrtf(&results, x, [Int32(x.count)]) 0232 0233 return results 0234 } 0235 0236 public func sqrt
FFT.swift:62
    vDSP_vsmulD(sqrt(magnitudes), 1, [2.0 / Double(input.count)], &normalizedMagnitudes, 1, vDSP_Length(input.count))
(x: [Double]) -> [Double] { 0237 var results = [Double](count: x.count, repeatedValue: 0.0) 0238 vvsqrt(&results, x, [Int32(x.count)]) 0239 0240 return results 0241 } 0242 0243 // MARK: Dot Product 0244 0245 public func dot
Arithmetic.swift:372
    return dot(lhs, y: rhs)
(x: [Float], y: [Float]) -> Float { 0246 precondition(x.count == y.count, "Vectors must have equal count") 0247 0248 var result: Float = 0.0 0249 vDSP_dotpr(x, 1, y, 1, &result, vDSP_Length(x.count)) 0250 0251 return result 0252 } 0253 0254 0255 public func dot
Arithmetic.swift:368
    return dot(lhs, y: rhs)
(x: [Double], y: [Double]) -> Double { 0256 precondition(x.count == y.count, "Vectors must have equal count") 0257 0258 var result: Double = 0.0 0259 vDSP_dotprD(x, 1, y, 1, &result, vDSP_Length(x.count)) 0260 0261 return result 0262 } 0263 0264 // MARK: - Distance 0265 0266 public func dist(x: [Float], y: [Float]) -> Float { 0267 precondition(x.count == y.count, "Vectors must have equal count") 0268 let sub = x - y 0269 var squared = [Float](count: x.count, repeatedValue: 0.0) 0270 vDSP_vsq(sub, 1, &squared, 1, vDSP_Length(x.count)) 0271 0272 return sqrt(sum(squared)) 0273 } 0274 0275 public func dist(x: [Double], y: [Double]) -> Double { 0276 precondition(x.count == y.count, "Vectors must have equal count") 0277 let sub = x - y 0278 var squared = [Double](count: x.count, repeatedValue: 0.0) 0279 vDSP_vsqD(sub, 1, &squared, 1, vDSP_Length(x.count)) 0280 0281 return sqrt(sum(squared)) 0282 } 0283 0284 // MARK: - Operators 0285 0286 public func + (lhs: [Float], rhs: [Float]) -> [Float] { 0287 return add(lhs, y: rhs) 0288 } 0289 0290 public func + (lhs: [Double], rhs: [Double]) -> [Double] { 0291 return add(lhs, y: rhs) 0292 } 0293 0294 public func + (lhs: [Float], rhs: Float) -> [Float] { 0295 return add(lhs, y: [Float](count: lhs.count, repeatedValue: rhs)) 0296 } 0297 0298 public func + (lhs: [Double], rhs: Double) -> [Double] { 0299 return add(lhs, y: [Double](count: lhs.count, repeatedValue: rhs)) 0300 } 0301 0302 public func - (lhs: [Float], rhs: [Float]) -> [Float] { 0303 return sub(lhs, y: rhs) 0304 } 0305 0306 public func - (lhs: [Double], rhs: [Double]) -> [Double] { 0307 return sub(lhs, y: rhs) 0308 } 0309 0310 public func - (lhs: [Float], rhs: Float) -> [Float] { 0311 return sub(lhs, y: [Float](count: lhs.count, repeatedValue: rhs)) 0312 } 0313 0314 public func - (lhs: [Double], rhs: Double) -> [Double] { 0315 return sub(lhs, y: [Double](count: lhs.count, repeatedValue: rhs)) 0316 } 0317 0318 public func / (lhs: [Float], rhs: [Float]) -> [Float] { 0319 return div(lhs, y: rhs) 0320 } 0321 0322 public func / (lhs: [Double], rhs: [Double]) -> [Double] { 0323 return div(lhs, y: rhs) 0324 } 0325 0326 public func / (lhs: [Float], rhs: Float) -> [Float] { 0327 return div(lhs, y: [Float](count: lhs.count, repeatedValue: rhs)) 0328 } 0329 0330 public func / (lhs: [Double], rhs: Double) -> [Double] { 0331 return div(lhs, y: [Double](count: lhs.count, repeatedValue: rhs)) 0332 } 0333 0334 public func * (lhs: [Float], rhs: [Float]) -> [Float] { 0335 return mul(lhs, y: rhs) 0336 } 0337 0338 public func * (lhs: [Double], rhs: [Double]) -> [Double] { 0339 return mul(lhs, y: rhs) 0340 } 0341 0342 public func * (lhs: [Float], rhs: Float) -> [Float] { 0343 return mul(lhs, y: [Float](count: lhs.count, repeatedValue: rhs)) 0344 } 0345 0346 public func * (lhs: [Double], rhs: Double) -> [Double] { 0347 return mul(lhs, y: [Double](count: lhs.count, repeatedValue: rhs)) 0348 } 0349 0350 public func % (lhs: [Float], rhs: [Float]) -> [Float] { 0351 return mod(lhs, y: rhs) 0352 } 0353 0354 public func % (lhs: [Double], rhs: [Double]) -> [Double] { 0355 return mod(lhs, y: rhs) 0356 } 0357 0358 public func % (lhs: [Float], rhs: Float) -> [Float] { 0359 return mod(lhs, y: [Float](count: lhs.count, repeatedValue: rhs)) 0360 } 0361 0362 public func % (lhs: [Double], rhs: Double) -> [Double] { 0363 return mod(lhs, y: [Double](count: lhs.count, repeatedValue: rhs)) 0364 } 0365 0366 infix operator • {} 0367 public func • (lhs: [Double], rhs: [Double]) -> Double { 0368 return dot(lhs, y: rhs) 0369 } 0370 0371 public func • (lhs: [Float], rhs: [Float]) -> Float { 0372 return dot(lhs, y: rhs) 0373 } 0374