From 7c1b6ea3fc3c0fff4ab5491e156fa2bcda07d1d2 Mon Sep 17 00:00:00 2001 From: Alejandro Isaza Date: Sun, 24 Sep 2017 23:07:45 -0700 Subject: [PATCH] Add linter, fix linter warnigns, uncomment tests --- .swiftlint.yml | 9 +++ README.md | 3 +- Sources/Surge/Arithmetic.swift | 1 - Sources/Surge/Convolution.swift | 12 +-- Sources/Surge/Matrix.swift | 25 +++---- Sources/Surge/Pointers.swift | 2 - Sources/Surge/Power.swift | 4 +- Surge.playground/Contents.swift | 2 +- Surge.xcodeproj/project.pbxproj | 19 +++++ ...24ADB003-3B8E-4FE6-8EC0-4AE9E46ABA69.plist | 73 +++++++++++++++++++ Tests/SurgeTests/ArithmeticTests.swift | 6 +- Tests/SurgeTests/AuxiliaryTests.swift | 2 +- Tests/SurgeTests/ConvolutionTests.swift | 61 ++++++++-------- Tests/SurgeTests/ExponentialTests.swift | 4 +- Tests/SurgeTests/HyperbolicTests.swift | 34 ++++----- Tests/SurgeTests/MatrixTests.swift | 30 ++++---- Tests/SurgeTests/PowerTests.swift | 4 +- Tests/SurgeTests/TrigonometricTests.swift | 34 ++++----- Tests/SurgeTests/XCTestCase+Surge.swift | 16 ++-- 19 files changed, 219 insertions(+), 122 deletions(-) create mode 100644 .swiftlint.yml diff --git a/.swiftlint.yml b/.swiftlint.yml new file mode 100644 index 0000000..6cae92e --- /dev/null +++ b/.swiftlint.yml @@ -0,0 +1,9 @@ +disabled_rules: + - file_length + - identifier_name + - line_length + - trailing_comma + +opt_in_rules: + - closure_spacing + diff --git a/README.md b/README.md index e7c81ba..47800b5 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ -# Surge [![Build Status](https://travis-ci.org/mattt/Surge.svg?branch=master)](https://travis-ci.org/mattt/Surge) [![GitHub license](https://img.shields.io/badge/license-MIT-lightgrey.svg)](https://github.com/mattt/Surge/blob/master/LICENSE) [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) +# Surge +[![Build Status](https://travis-ci.org/mattt/Surge.svg?branch=master)](https://travis-ci.org/mattt/Surge) [![GitHub license](https://img.shields.io/badge/license-MIT-lightgrey.svg)](https://github.com/mattt/Surge/blob/master/LICENSE) [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) Surge is a Swift library that uses the Accelerate framework to provide high-performance functions for matrix math, digital signal processing, and image manipulation. diff --git a/Sources/Surge/Arithmetic.swift b/Sources/Surge/Arithmetic.swift index c2437f2..eac12d0 100644 --- a/Sources/Surge/Arithmetic.swift +++ b/Sources/Surge/Arithmetic.swift @@ -390,7 +390,6 @@ public func dot(_ x: X, _ y: Y return result } - public func dot(_ x: X, _ y: Y) -> Double where X.Iterator.Element == Double, Y.Iterator.Element == Double { precondition(x.count == y.count, "Vectors must have equal count") diff --git a/Sources/Surge/Convolution.swift b/Sources/Surge/Convolution.swift index 2c2ae25..b7c2e34 100644 --- a/Sources/Surge/Convolution.swift +++ b/Sources/Surge/Convolution.swift @@ -26,7 +26,7 @@ import Accelerate // Convolution of a signal [x], with a kernel [k]. The signal must be at least as long as the kernel. public func conv(_ x: X, _ k: K) -> [Float] where X.Iterator.Element == Float, K.Iterator.Element == Float { precondition(x.count >= k.count, "Input vector [x] must have at least as many elements as the kernel, [k]") - + let resultSize = numericCast(x.count) + numericCast(k.count) - 1 var result = [Float](repeating: 0, count: resultSize) result.withUnsafeMutableBufferPointer { rbp in @@ -80,7 +80,7 @@ public func xcorr(_ x: X, _ y: let padding = repeatElement(0 as Float, count: numericCast(x.count) - numericCast(y.count)) yPadded.append(contentsOf: padding) } - + let resultSize = numericCast(x.count) + yPadded.count - 1 var result = [Float](repeating: 0, count: resultSize) let xPad = repeatElement(0 as Float, count: yPadded.count-1) @@ -94,7 +94,7 @@ public func xcorr(_ x: X, _ y: result.withUnsafeMutableBufferPointer { rbp in vDSP_conv(xPadded, 1, yPadded, 1, rbp.baseAddress!, 1, vDSP_Length(resultSize), vDSP_Length(yPadded.count)) } - + return result } @@ -107,7 +107,7 @@ public func xcorr(_ x: X, _ y: let padding = repeatElement(0 as Double, count: numericCast(x.count) - numericCast(y.count)) yPadded.append(contentsOf: padding) } - + let resultSize = numericCast(x.count) + yPadded.count - 1 var result = [Double](repeating: 0, count: resultSize) let xPad = repeatElement(0 as Double, count: yPadded.count-1) @@ -121,7 +121,7 @@ public func xcorr(_ x: X, _ y: result.withUnsafeMutableBufferPointer { rbp in vDSP_convD(xPadded, 1, yPadded, 1, rbp.baseAddress!, 1, vDSP_Length(resultSize), vDSP_Length(yPadded.count)) } - + return result } @@ -165,6 +165,6 @@ public func xcorr(_ x: X) -> [Double] where X.Iterator. vDSP_convD(xPadded, 1, xp, 1, rbp.baseAddress!, 1, vDSP_Length(resultSize), vDSP_Length(xc)) } } - + return result } diff --git a/Sources/Surge/Matrix.swift b/Sources/Surge/Matrix.swift index 9c2f39e..dc1aa64 100644 --- a/Sources/Surge/Matrix.swift +++ b/Sources/Surge/Matrix.swift @@ -57,7 +57,7 @@ public struct Matrix where Scalar: FloatingPoint, Scalar: ExpressibleByF grid[(row * columns) + column] = newValue } } - + public subscript(row row: Int) -> [Scalar] { get { assert(row < rows) @@ -65,7 +65,7 @@ public struct Matrix where Scalar: FloatingPoint, Scalar: ExpressibleByF let endIndex = row * columns + columns return Array(grid[startIndex.. where Scalar: FloatingPoint, Scalar: ExpressibleByF grid.replaceSubrange(startIndex.. [Scalar] { get { var result = [Scalar](repeating: 0.0, count: rows) @@ -84,7 +84,7 @@ public struct Matrix where Scalar: FloatingPoint, Scalar: ExpressibleByF } return result } - + set { assert(column < columns) assert(newValue.count == rows) @@ -107,7 +107,7 @@ extension Matrix: CustomStringConvertible { var description = "" for i in 0.. (lhs: Matrix, rhs: Matrix) -> Bool { return lhs.rows == rhs.rows && lhs.columns == rhs.columns && lhs.grid == rhs.grid } - // MARK: - public func add(_ x: Matrix, _ y: Matrix) -> Matrix { @@ -268,7 +267,7 @@ public func exp(_ x: Matrix) -> Matrix { } public func sum(_ x: Matrix, axies: MatrixAxies = .column) -> Matrix { - + switch axies { case .column: var result = Matrix(rows: 1, columns: x.columns, repeatedValue: 0.0) @@ -276,7 +275,7 @@ public func sum(_ x: Matrix, axies: MatrixAxies = .column) -> Matrix(rows: x.rows, columns: 1, repeatedValue: 0.0) for i in 0.., axies: MatrixAxies = .column) -> Matrix) -> Matrix { +public func inv(_ x: Matrix) -> Matrix { precondition(x.rows == x.columns, "Matrix must be square") var results = x @@ -386,14 +385,14 @@ public func / (lhs: Matrix, rhs: Matrix) -> Matrix { public func / (lhs: Matrix, rhs: Double) -> Matrix { var result = Matrix(rows: lhs.rows, columns: lhs.columns, repeatedValue: 0.0) - result.grid = lhs.grid / rhs; - return result; + result.grid = lhs.grid / rhs + return result } public func / (lhs: Matrix, rhs: Float) -> Matrix { var result = Matrix(rows: lhs.rows, columns: lhs.columns, repeatedValue: 0.0) - result.grid = lhs.grid / rhs; - return result; + result.grid = lhs.grid / rhs + return result } postfix operator ′ diff --git a/Sources/Surge/Pointers.swift b/Sources/Surge/Pointers.swift index 793245f..b407b3b 100644 --- a/Sources/Surge/Pointers.swift +++ b/Sources/Surge/Pointers.swift @@ -52,7 +52,6 @@ public func withUnsafeMutablePointersAndCountsTo } } - // MARK: 2 Parameter /// Invokes the given closure with pointers to the given arguments (2 parameter version). @@ -129,7 +128,6 @@ public func withUnsafeMutablePointersAndCountsTo(_ x: X, _ y: Y) -> [Float] where X.Iterator.Element == Float, Y.Iterator.Element == Float { var results = [Float](repeating: 0.0, count: numericCast(x.count)) results.withUnsafeMutableBufferPointer { pointer in - withUnsafePointersAndCountsTo(x, y) { xp, xc, yp, yc in + withUnsafePointersAndCountsTo(x, y) { xp, xc, yp, _ in vvpowf(pointer.baseAddress!, xp, yp, [Int32(xc)]) } } @@ -35,7 +35,7 @@ public func pow(_ x: X, _ y: Y public func pow(_ x: X, _ y: Y) -> [Double] where X.Iterator.Element == Double, Y.Iterator.Element == Double { var results = [Double](repeating: 0.0, count: numericCast(x.count)) results.withUnsafeMutableBufferPointer { pointer in - withUnsafePointersAndCountsTo(x, y) { xp, xc, yp, yc in + withUnsafePointersAndCountsTo(x, y) { xp, xc, yp, _ in vvpow(pointer.baseAddress!, xp, yp, [Int32(xc)]) } } diff --git a/Surge.playground/Contents.swift b/Surge.playground/Contents.swift index eea3835..e06d4d8 100644 --- a/Surge.playground/Contents.swift +++ b/Surge.playground/Contents.swift @@ -27,7 +27,7 @@ let count = 64 let frequency = 4.0 let amplitude = 3.0 -let x = (0.. + AuxiliaryTests + + test_copysign() + + com.apple.XCTPerformanceMetric_WallClockTime + + baselineAverage + 3.4883e-05 + baselineIntegrationDisplayName + Sep 24, 2017, 11:04:17 PM + + + ExponentialTests test_exp() @@ -62,6 +75,36 @@ HyperbolicTests + test_acosh() + + com.apple.XCTPerformanceMetric_WallClockTime + + baselineAverage + 0.00014254 + baselineIntegrationDisplayName + Sep 24, 2017, 11:04:17 PM + + + test_asinh() + + com.apple.XCTPerformanceMetric_WallClockTime + + baselineAverage + 8.0483e-05 + baselineIntegrationDisplayName + Sep 24, 2017, 11:04:17 PM + + + test_atanh() + + com.apple.XCTPerformanceMetric_WallClockTime + + baselineAverage + 0.00012056 + baselineIntegrationDisplayName + Sep 24, 2017, 11:04:17 PM + + test_cosh() com.apple.XCTPerformanceMetric_WallClockTime @@ -95,6 +138,36 @@ TrigonometricTests + test_acos() + + com.apple.XCTPerformanceMetric_WallClockTime + + baselineAverage + 0.00015664 + baselineIntegrationDisplayName + Sep 24, 2017, 11:06:27 PM + + + test_asin() + + com.apple.XCTPerformanceMetric_WallClockTime + + baselineAverage + 0.00012768 + baselineIntegrationDisplayName + Sep 24, 2017, 11:06:27 PM + + + test_atan() + + com.apple.XCTPerformanceMetric_WallClockTime + + baselineAverage + 0.00014418 + baselineIntegrationDisplayName + Sep 24, 2017, 11:06:27 PM + + test_cos() com.apple.XCTPerformanceMetric_WallClockTime diff --git a/Tests/SurgeTests/ArithmeticTests.swift b/Tests/SurgeTests/ArithmeticTests.swift index efcb1d8..686cacc 100644 --- a/Tests/SurgeTests/ArithmeticTests.swift +++ b/Tests/SurgeTests/ArithmeticTests.swift @@ -26,7 +26,7 @@ class ArithmeticTests: XCTestCase { let n = 100000 func test_sum() { - let values = (0...n).map{ _ in Double(arc4random()) / Double(UInt32.max) } + let values = (0...n).map { _ in Double(arc4random()) / Double(UInt32.max) } var actual = 0.0 measure { actual = sum(values) @@ -41,7 +41,7 @@ class ArithmeticTests: XCTestCase { } func test_sum_slice() { - let values = (0...n).map{ _ in Double(arc4random()) / Double(UInt32.max) } + let values = (0...n).map { _ in Double(arc4random()) / Double(UInt32.max) } var actual = 0.0 measure { actual = sum(values[0.. = Matrix([[1, 2, 3, 4], [5,6,7,8], [9, 10, 11, 12]]) + + var matrix: Matrix = Matrix([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) func testInit() { let m1 = Matrix([[1.0, 2.0]]) @@ -35,32 +35,32 @@ class MatrixTests: XCTestCase { } func testSubscriptRow() { - XCTAssertEqual(matrix[row: 0], [1,2,3,4]) + XCTAssertEqual(matrix[row: 0], [1, 2, 3, 4]) XCTAssertEqual(matrix[row: 1], [5, 6, 7, 8]) } - + func testSubscriptColumn() { - XCTAssertEqual(matrix[column: 0], [1,5,9]) - XCTAssertEqual(matrix[column: 1], [2,6,10]) + XCTAssertEqual(matrix[column: 0], [1, 5, 9]) + XCTAssertEqual(matrix[column: 1], [2, 6, 10]) } - + func testSetRow() { matrix[row: 0] = [13.0, 14.0, 15.0, 16.0] - XCTAssertTrue(matrix==Matrix([[13,14,15,16],[5,6,7,8], [9, 10, 11, 12]])) + XCTAssertTrue(matrix==Matrix([[13, 14, 15, 16], [5, 6, 7, 8], [9, 10, 11, 12]])) } - + func testSetColumn() { - matrix[column: 0] = [20,30,40] - XCTAssertEqual(matrix, Matrix([[20,2,3,4],[30,6,7,8], [40, 10, 11, 12]])) + matrix[column: 0] = [20, 30, 40] + XCTAssertEqual(matrix, Matrix([[20, 2, 3, 4], [30, 6, 7, 8], [40, 10, 11, 12]])) } - + func testMatrixPower() { let expectedResult = Matrix([[1, 4, 9, 16], [25, 36, 49, 64], [81, 100, 121, 144]]) XCTAssertEqual(pow(matrix, 2), expectedResult) } - + func testElementWiseMultiplication() { - let matrix2 = Matrix([[2,3,4,5], [6,7,8,9], [10, 11, 12, 13]]) - XCTAssertEqual(elmul(matrix, matrix2), Matrix([[2,6,12,20], [30,42,56,72], [90, 110, 132,156]])) + let matrix2 = Matrix([[2, 3, 4, 5], [6, 7, 8, 9], [10, 11, 12, 13]]) + XCTAssertEqual(elmul(matrix, matrix2), Matrix([[2, 6, 12, 20], [30, 42, 56, 72], [90, 110, 132, 156]])) } } diff --git a/Tests/SurgeTests/PowerTests.swift b/Tests/SurgeTests/PowerTests.swift index a290069..ad0c9b0 100644 --- a/Tests/SurgeTests/PowerTests.swift +++ b/Tests/SurgeTests/PowerTests.swift @@ -23,9 +23,9 @@ import XCTest import Surge class PowerTests: XCTestCase { - + let vector = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] - + func testPower() { let powered = pow(vector, 2.0) XCTAssertEqual(powered, [1.0, 4.0, 9.0, 16.0, 25.0, 36.0]) diff --git a/Tests/SurgeTests/TrigonometricTests.swift b/Tests/SurgeTests/TrigonometricTests.swift index 5faeac2..5cfd122 100644 --- a/Tests/SurgeTests/TrigonometricTests.swift +++ b/Tests/SurgeTests/TrigonometricTests.swift @@ -26,32 +26,32 @@ class TrigonometricTests: XCTestCase { let n = 10000 func test_sin() { - let values = (0...n).map{_ in drand48() * Double.pi} + let values = (0...n).map { _ in drand48() * Double.pi } measureAndValidateMappedFunctionWithAccuracy(source: values, member: sin, mapped: sin, accuracy: 0.0001) } func test_cos() { - let values = (0...n).map{_ in drand48() * Double.pi} + let values = (0...n).map { _ in drand48() * Double.pi } measureAndValidateMappedFunctionWithAccuracy(source: values, member: cos, mapped: cos, accuracy: 0.0001) } func test_tan() { - let values = (0...n).map{_ in drand48() * Double.pi} + let values = (0...n).map { _ in drand48() * Double.pi } measureAndValidateMappedFunctionWithAccuracy(source: values, member: tan, mapped: tan, accuracy: 0.0001) } -// func test_asin() { -// let values = map(0...n){_ in drand48()} -// measureAndValidateMappedFunctionWithAccuracy(values, member: asin, mapped: asin, accuracy: 0.0001) -// } -// -// func test_acos() { -// let values = map(0...n){_ in drand48()} -// measureAndValidateMappedFunctionWithAccuracy(values, member: acos, mapped: acos, accuracy: 0.0001) -// } -// -// func test_atan() { -// let values = map(0...n){_ in drand48()} -// measureAndValidateMappedFunctionWithAccuracy(values, member: atan, mapped: atan, accuracy: 0.0001) -// } + func test_asin() { + let values = (0...n).map { _ in drand48() } + measureAndValidateMappedFunctionWithAccuracy(source: values, member: asin, mapped: asin, accuracy: 0.0001) + } + + func test_acos() { + let values = (0...n).map { _ in drand48() } + measureAndValidateMappedFunctionWithAccuracy(source: values, member: acos, mapped: acos, accuracy: 0.0001) + } + + func test_atan() { + let values = (0...n).map { _ in drand48() } + measureAndValidateMappedFunctionWithAccuracy(source: values, member: atan, mapped: atan, accuracy: 0.0001) + } } diff --git a/Tests/SurgeTests/XCTestCase+Surge.swift b/Tests/SurgeTests/XCTestCase+Surge.swift index 23a173e..039abfd 100644 --- a/Tests/SurgeTests/XCTestCase+Surge.swift +++ b/Tests/SurgeTests/XCTestCase+Surge.swift @@ -22,29 +22,29 @@ import Foundation import XCTest extension XCTestCase { - func measureAndValidateMappedFunctionWithAccuracy(source: C, member: (C.Iterator.Element) -> (C.Iterator.Element), mapped: @escaping (C) -> ([C.Iterator.Element]), accuracy: C.Iterator.Element) where C.Iterator.Element: ExpressibleByFloatLiteral & FloatingPoint { + func measureAndValidateMappedFunctionWithAccuracy(source: C, member: (C.Iterator.Element) -> (C.Iterator.Element), mapped: @escaping (C) -> ([C.Iterator.Element]), accuracy: C.Iterator.Element) where C.Iterator.Element: ExpressibleByFloatLiteral & FloatingPoint { var expected = source.map(member) var actual: [C.Iterator.Element] = [] self.measure { actual = mapped(source) } - - for (i, _) in source.enumerated() { - XCTAssertEqual(actual[i], expected[i], accuracy: accuracy) + + for (i, j) in zip(actual.indices, expected.indices) { + XCTAssertEqual(actual[i], expected[j], accuracy: accuracy) } } - + func XCTAssertArrayFloatEqualWithAccuracy(calcArray: [Float], _ testArray: [Float], _ accuracy: Float) { assert(calcArray.count == testArray.count, "XCTAssertArrayFloatEqualWithAccuracy arrays must be same size") - for i:Int in 0..