Blame view

Pods/Surge/Source/Arithmetic.swift 9.46 KB
f2cf74c7   yangbin   1.0.20(4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
  // Copyright (c) 20142015 Mattt Thompson (http://mattt.me)
  //
  // Permission is hereby granted, free of charge, to any person obtaining a copy
  // of this software and associated documentation files (the "Software"), to deal
  // in the Software without restriction, including without limitation the rights
  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  // copies of the Software, and to permit persons to whom the Software is
  // furnished to do so, subject to the following conditions:
  //
  // The above copyright notice and this permission notice shall be included in
  // all copies or substantial portions of the Software.
  //
  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  // THE SOFTWARE.
  
  import Accelerate
  
  // MARK: Sum
  
  public func sum(_ x: [Float]) -> Float {
      var result: Float = 0.0
      vDSP_sve(x, 1, &result, vDSP_Length(x.count))
  
      return result
  }
  
  public func sum(_ x: [Double]) -> Double {
      var result: Double = 0.0
      vDSP_sveD(x, 1, &result, vDSP_Length(x.count))
  
      return result
  }
  
  // MARK: Sum of Absolute Values
  
  public func asum(_ x: [Float]) -> Float {
      return cblas_sasum(Int32(x.count), x, 1)
  }
  
  public func asum(_ x: [Double]) -> Double {
      return cblas_dasum(Int32(x.count), x, 1)
  }
  
  // MARK: Sum of Square Values
  
  public func sumsq(_ x: [Float]) -> Float {
      var result: Float = 0
      vDSP_svesq(x, 1, &result, vDSP_Length(x.count))
      return result
  }
  
  public func sumsq(_ x: [Double]) -> Double {
      var result: Double = 0
      vDSP_svesqD(x, 1, &result, vDSP_Length(x.count))
      return result
  }
  // MARK: Maximum
  
  public func max(_ x: [Float]) -> Float {
      var result: Float = 0.0
      vDSP_maxv(x, 1, &result, vDSP_Length(x.count))
  
      return result
  }
  
  public func max(_ x: [Double]) -> Double {
      var result: Double = 0.0
      vDSP_maxvD(x, 1, &result, vDSP_Length(x.count))
  
      return result
  }
  
  // MARK: Minimum
  
  public func min(_ x: [Float]) -> Float {
      var result: Float = 0.0
      vDSP_minv(x, 1, &result, vDSP_Length(x.count))
  
      return result
  }
  
  public func min(_ x: [Double]) -> Double {
      var result: Double = 0.0
      vDSP_minvD(x, 1, &result, vDSP_Length(x.count))
  
      return result
  }
  
  // MARK: Mean
  
  public func mean(_ x: [Float]) -> Float {
      var result: Float = 0.0
      vDSP_meanv(x, 1, &result, vDSP_Length(x.count))
  
      return result
  }
  
  public func mean(_ x: [Double]) -> Double {
      var result: Double = 0.0
      vDSP_meanvD(x, 1, &result, vDSP_Length(x.count))
  
      return result
  }
  
  // MARK: Mean Magnitude
  
  public func meamg(_ x: [Float]) -> Float {
      var result: Float = 0.0
      vDSP_meamgv(x, 1, &result, vDSP_Length(x.count))
  
      return result
  }
  
  public func meamg(_ x: [Double]) -> Double {
      var result: Double = 0.0
      vDSP_meamgvD(x, 1, &result, vDSP_Length(x.count))
  
      return result
  }
  
  // MARK: Mean Square Value
  
  public func measq(_ x: [Float]) -> Float {
      var result: Float = 0.0
      vDSP_measqv(x, 1, &result, vDSP_Length(x.count))
  
      return result
  }
  
  public func measq(_ x: [Double]) -> Double {
      var result: Double = 0.0
      vDSP_measqvD(x, 1, &result, vDSP_Length(x.count))
  
      return result
  }
  
  // MARK: Add
  
  public func add(_ x: [Float], y: [Float]) -> [Float] {
      var results = [Float](y)
      cblas_saxpy(Int32(x.count), 1.0, x, 1, &results, 1)
  
      return results
  }
  
  public func add(_ x: [Double], y: [Double]) -> [Double] {
      var results = [Double](y)
      cblas_daxpy(Int32(x.count), 1.0, x, 1, &results, 1)
  
      return results
  }
  
  // MARK: Subtraction
  
  public func sub(_ x: [Float], y: [Float]) -> [Float] {
      var results = [Float](y)
      catlas_saxpby(Int32(x.count), 1.0, x, 1, -1, &results, 1)
      
      return results
  }
  
  public func sub(_ x: [Double], y: [Double]) -> [Double] {
      var results = [Double](y)
      catlas_daxpby(Int32(x.count), 1.0, x, 1, -1, &results, 1)
      
      return results
  }
  
  // MARK: Multiply
  
  public func mul(_ x: [Float], y: [Float]) -> [Float] {
      var results = [Float](repeating: 0.0, count: x.count)
      vDSP_vmul(x, 1, y, 1, &results, 1, vDSP_Length(x.count))
  
      return results
  }
  
  public func mul(_ x: [Double], y: [Double]) -> [Double] {
      var results = [Double](repeating: 0.0, count: x.count)
      vDSP_vmulD(x, 1, y, 1, &results, 1, vDSP_Length(x.count))
  
      return results
  }
  
  // MARK: Divide
  
  public func div(_ x: [Float], y: [Float]) -> [Float] {
      var results = [Float](repeating: 0.0, count: x.count)
      vvdivf(&results, x, y, [Int32(x.count)])
  
      return results
  }
  
  public func div(_ x: [Double], y: [Double]) -> [Double] {
      var results = [Double](repeating: 0.0, count: x.count)
      vvdiv(&results, x, y, [Int32(x.count)])
  
      return results
  }
  
  // MARK: Modulo
  
  public func mod(_ x: [Float], y: [Float]) -> [Float] {
      var results = [Float](repeating: 0.0, count: x.count)
      vvfmodf(&results, x, y, [Int32(x.count)])
  
      return results
  }
  
  public func mod(_ x: [Double], y: [Double]) -> [Double] {
      var results = [Double](repeating: 0.0, count: x.count)
      vvfmod(&results, x, y, [Int32(x.count)])
  
      return results
  }
  
  // MARK: Remainder
  
  public func remainder(_ x: [Float], y: [Float]) -> [Float] {
      var results = [Float](repeating: 0.0, count: x.count)
      vvremainderf(&results, x, y, [Int32(x.count)])
  
      return results
  }
  
  public func remainder(_ x: [Double], y: [Double]) -> [Double] {
      var results = [Double](repeating: 0.0, count: x.count)
      vvremainder(&results, x, y, [Int32(x.count)])
  
      return results
  }
  
  // MARK: Square Root
  
  public func sqrt(_ x: [Float]) -> [Float] {
      var results = [Float](repeating: 0.0, count: x.count)
      vvsqrtf(&results, x, [Int32(x.count)])
  
      return results
  }
  
  public func sqrt(_ x: [Double]) -> [Double] {
      var results = [Double](repeating: 0.0, count: x.count)
      vvsqrt(&results, x, [Int32(x.count)])
  
      return results
  }
  
  // MARK: Dot Product
  
  public func dot(_ x: [Float], y: [Float]) -> Float {
      precondition(x.count == y.count, "Vectors must have equal count")
  
      var result: Float = 0.0
      vDSP_dotpr(x, 1, y, 1, &result, vDSP_Length(x.count))
  
      return result
  }
  
  
  public func dot(_ x: [Double], y: [Double]) -> Double {
      precondition(x.count == y.count, "Vectors must have equal count")
  
      var result: Double = 0.0
      vDSP_dotprD(x, 1, y, 1, &result, vDSP_Length(x.count))
  
      return result
  }
  
  // MARK: - Distance
  
  public func dist(_ x: [Float], y: [Float]) -> Float {
      precondition(x.count == y.count, "Vectors must have equal count")
      let sub = x - y
      var squared = [Float](repeating: 0.0, count: x.count)
      vDSP_vsq(sub, 1, &squared, 1, vDSP_Length(x.count))
      
      return sqrt(sum(squared))
  }
  
  public func dist(_ x: [Double], y: [Double]) -> Double {
      precondition(x.count == y.count, "Vectors must have equal count")
      let sub = x - y
      var squared = [Double](repeating: 0.0, count: x.count)
      vDSP_vsqD(sub, 1, &squared, 1, vDSP_Length(x.count))
      
      return sqrt(sum(squared))
  }
  
  // MARK: - Operators
  
  public func + (lhs: [Float], rhs: [Float]) -> [Float] {
      return add(lhs, y: rhs)
  }
  
  public func + (lhs: [Double], rhs: [Double]) -> [Double] {
      return add(lhs, y: rhs)
  }
  
  public func + (lhs: [Float], rhs: Float) -> [Float] {
      return add(lhs, y: [Float](repeating: rhs, count: lhs.count))
  }
  
  public func + (lhs: [Double], rhs: Double) -> [Double] {
      return add(lhs, y: [Double](repeating: rhs, count: lhs.count))
  }
  
  public func - (lhs: [Float], rhs: [Float]) -> [Float] {
      return sub(lhs, y: rhs)
  }
  
  public func - (lhs: [Double], rhs: [Double]) -> [Double] {
      return sub(lhs, y: rhs)
  }
  
  public func - (lhs: [Float], rhs: Float) -> [Float] {
      return sub(lhs, y: [Float](repeating: rhs, count: lhs.count))
  }
  
  public func - (lhs: [Double], rhs: Double) -> [Double] {
      return sub(lhs, y: [Double](repeating: rhs, count: lhs.count))
  }
  
  public func / (lhs: [Float], rhs: [Float]) -> [Float] {
      return div(lhs, y: rhs)
  }
  
  public func / (lhs: [Double], rhs: [Double]) -> [Double] {
      return div(lhs, y: rhs)
  }
  
  public func / (lhs: [Float], rhs: Float) -> [Float] {
      return div(lhs, y: [Float](repeating: rhs, count: lhs.count))
  }
  
  public func / (lhs: [Double], rhs: Double) -> [Double] {
      return div(lhs, y: [Double](repeating: rhs, count: lhs.count))
  }
  
  public func * (lhs: [Float], rhs: [Float]) -> [Float] {
      return mul(lhs, y: rhs)
  }
  
  public func * (lhs: [Double], rhs: [Double]) -> [Double] {
      return mul(lhs, y: rhs)
  }
  
  public func * (lhs: [Float], rhs: Float) -> [Float] {
      return mul(lhs, y: [Float](repeating: rhs, count: lhs.count))
  }
  
  public func * (lhs: [Double], rhs: Double) -> [Double] {
      return mul(lhs, y: [Double](repeating: rhs, count: lhs.count))
  }
  
  public func % (lhs: [Float], rhs: [Float]) -> [Float] {
      return mod(lhs, y: rhs)
  }
  
  public func % (lhs: [Double], rhs: [Double]) -> [Double] {
      return mod(lhs, y: rhs)
  }
  
  public func % (lhs: [Float], rhs: Float) -> [Float] {
      return mod(lhs, y: [Float](repeating: rhs, count: lhs.count))
  }
  
  public func % (lhs: [Double], rhs: Double) -> [Double] {
      return mod(lhs, y: [Double](repeating: rhs, count: lhs.count))
  }
  
  infix operator 
  public func  (lhs: [Double], rhs: [Double]) -> Double {
      return dot(lhs, y: rhs)
  }
  
  public func  (lhs: [Float], rhs: [Float]) -> Float {
      return dot(lhs, y: rhs)
  }