Improved formatting

# Conflicts:
#	Sources/Surge/Linear Algebra/Matrix.swift
This commit is contained in:
Vincent Esche 2019-10-05 16:08:38 +02:00
parent 8fdbdf5cbf
commit 540307b52a
10 changed files with 60 additions and 50 deletions

View File

@ -44,7 +44,7 @@ public struct Matrix<Scalar> where Scalar: FloatingPoint, Scalar: ExpressibleByF
for (i, row) in contents.enumerated() {
precondition(row.count == columns, "All rows should have the same number of columns")
grid.replaceSubrange(i*columns ..< (i + 1)*columns, with: row)
grid.replaceSubrange(i * columns..<(i + 1) * columns, with: row)
}
}
@ -210,7 +210,7 @@ extension Matrix: CustomStringConvertible {
var description = ""
for i in 0..<rows {
let contents = (0..<columns).map({ "\(self[i, $0])" }).joined(separator: "\t")
let contents = (0..<columns).map { "\(self[i, $0])" }.joined(separator: "\t")
switch (i, rows) {
case (0, 1):
@ -273,7 +273,7 @@ extension Matrix: Collection {
}
extension Matrix: Equatable {}
public func ==<T> (lhs: Matrix<T>, rhs: Matrix<T>) -> Bool {
public func == <T>(lhs: Matrix<T>, rhs: Matrix<T>) -> Bool {
return lhs.rows == rhs.rows && lhs.columns == rhs.columns && lhs.grid == rhs.grid
}
@ -604,7 +604,7 @@ public func inv(_ lhs: Matrix<Float>) -> Matrix<Float> {
var nc = __CLPK_integer(lhs.columns)
withUnsafeMutablePointers(&nc, &lwork, &error) { nc, lwork, error in
withUnsafeMutableMemory(&ipiv, &work, &(results.grid)) { ipiv, work, grid in
withUnsafeMutableMemory(&ipiv, &work, &results.grid) { ipiv, work, grid in
sgetrf_(nc, nc, grid.pointer, nc, ipiv.pointer, error)
sgetri_(nc, grid.pointer, nc, ipiv.pointer, work.pointer, lwork, error)
}
@ -627,7 +627,7 @@ public func inv(_ lhs: Matrix<Double>) -> Matrix<Double> {
var nc = __CLPK_integer(lhs.columns)
withUnsafeMutablePointers(&nc, &lwork, &error) { nc, lwork, error in
withUnsafeMutableMemory(&ipiv, &work, &(results.grid)) { ipiv, work, grid in
withUnsafeMutableMemory(&ipiv, &work, &results.grid) { ipiv, work, grid in
dgetrf_(nc, nc, grid.pointer, nc, ipiv.pointer, error)
dgetri_(nc, grid.pointer, nc, ipiv.pointer, work.pointer, lwork, error)
}
@ -675,7 +675,7 @@ public func det(_ lhs: Matrix<Float>) -> Float? {
var info = __CLPK_integer()
var m = __CLPK_integer(lhs.rows)
var n = __CLPK_integer(lhs.columns)
_ = withUnsafeMutableMemory(&pivots, &(decomposed.grid)) { ipiv, grid in
_ = withUnsafeMutableMemory(&pivots, &decomposed.grid) { ipiv, grid in
withUnsafeMutablePointers(&m, &n, &info) { m, n, info in
sgetrf_(m, n, grid.pointer, m, ipiv.pointer, info)
}
@ -703,7 +703,7 @@ public func det(_ lhs: Matrix<Double>) -> Double? {
var info = __CLPK_integer()
var m = __CLPK_integer(lhs.rows)
var n = __CLPK_integer(lhs.columns)
_ = withUnsafeMutableMemory(&pivots, &(decomposed.grid)) { ipiv, grid in
_ = withUnsafeMutableMemory(&pivots, &decomposed.grid) { ipiv, grid in
withUnsafeMutablePointers(&m, &n, &info) { m, n, info in
dgetrf_(m, n, grid.pointer, m, ipiv.pointer, info)
}
@ -730,7 +730,7 @@ public func det(_ lhs: Matrix<Double>) -> Double? {
// See Intel's documentation on column-major results for sample code that this
// is based on:
// https://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_lapack_examples/dgeev.htm
private func buildEigenVector<Scalar: FloatingPoint & ExpressibleByFloatLiteral>(eigenValueImaginaryParts: [Scalar], eigenVectorWork: [Scalar], result: inout [[(Scalar, Scalar)]]) {
private func buildEigenVector<Scalar>(eigenValueImaginaryParts: [Scalar], eigenVectorWork: [Scalar], result: inout [[(Scalar, Scalar)]]) where Scalar: FloatingPoint & ExpressibleByFloatLiteral {
// row and col count are the same because result must be square.
let rowColCount = result.count
@ -739,12 +739,12 @@ private func buildEigenVector<Scalar: FloatingPoint & ExpressibleByFloatLiteral>
while col < rowColCount {
if eigenValueImaginaryParts[col] == 0.0 {
// v is column-major
result[row][col] = (eigenVectorWork[row+rowColCount*col], 0.0)
result[row][col] = (eigenVectorWork[row + rowColCount * col], 0.0)
col += 1
} else {
// v is column-major
result[row][col] = (eigenVectorWork[row+col*rowColCount], eigenVectorWork[row+rowColCount*(col+1)])
result[row][col+1] = (eigenVectorWork[row+col*rowColCount], -eigenVectorWork[row+rowColCount*(col+1)])
result[row][col] = (eigenVectorWork[row + col * rowColCount], eigenVectorWork[row + rowColCount * (col + 1)])
result[row][col + 1] = (eigenVectorWork[row + col * rowColCount], -eigenVectorWork[row + rowColCount * (col + 1)])
col += 2
}
}

View File

@ -71,7 +71,7 @@ extension Vector: ExpressibleByArrayLiteral {
extension Vector: CustomStringConvertible {
public var description: String {
return self.scalars.map({ "\($0)" }).joined(separator: "\t")
return self.scalars.map { "\($0)" }.joined(separator: "\t")
}
}
@ -112,7 +112,7 @@ extension Vector: Collection {
// MARK: - Equatable
extension Vector: Equatable {}
public func ==<T> (lhs: Vector<T>, rhs: Vector<T>) -> Bool {
public func == <T>(lhs: Vector<T>, rhs: Vector<T>) -> Bool {
return lhs.scalars == rhs.scalars
}

View File

@ -42,10 +42,10 @@ extension FloatingPoint {
static func randomNormalized() -> Self {
switch self {
case is Float.Type:
let value = Float.random(in: (0.0)...(1.0))
let value = Float.random(in: 0.0...1.0)
return unsafeBitCast(value, to: self)
case is Double.Type:
let value = Double.random(in: (0.0)...(1.0))
let value = Double.random(in: 0.0...1.0)
return unsafeBitCast(value, to: self)
case _:
fatalError("Only supported by `Float` and `Double`")
@ -259,11 +259,11 @@ extension XCTestCase {
let lhs = produceLhs()
closure({ innerClosure in
closure { innerClosure in
startMeasuring()
let _ = innerClosure(lhs)
stopMeasuring()
})
}
}
func measure_inout_array<T, U>(
@ -275,13 +275,13 @@ extension XCTestCase {
let lhs = produceLhs()
closure({ innerClosure in
closure { innerClosure in
var lhs = lhs
startMeasuring()
let _ = innerClosure(&lhs)
stopMeasuring()
})
}
}
func measure_array_array<T, U>(
@ -295,11 +295,11 @@ extension XCTestCase {
let lhs = produceLhs()
let rhs = produceRhs()
closure({ innerClosure in
closure { innerClosure in
startMeasuring()
let _ = innerClosure(lhs, rhs)
stopMeasuring()
})
}
}
func measure_inout_array_array<T, U>(
@ -313,13 +313,13 @@ extension XCTestCase {
let lhs = produceLhs()
let rhs = produceRhs()
closure({ innerClosure in
closure { innerClosure in
var lhs = lhs
startMeasuring()
let _ = innerClosure(&lhs, rhs)
stopMeasuring()
})
}
}
func measure_array_scalar<T, U>(
@ -333,11 +333,11 @@ extension XCTestCase {
let lhs = produceLhs()
let rhs = produceRhs()
closure({ innerClosure in
closure { innerClosure in
startMeasuring()
let _ = innerClosure(lhs, rhs)
stopMeasuring()
})
}
}
func measure_inout_array_scalar<T, U>(
@ -351,13 +351,13 @@ extension XCTestCase {
let lhs = produceLhs()
let rhs = produceRhs()
closure({ innerClosure in
closure { innerClosure in
var lhs = lhs
startMeasuring()
let _ = innerClosure(&lhs, rhs)
stopMeasuring()
})
}
}
func measure_vector_matrix<T, U>(
@ -371,10 +371,10 @@ extension XCTestCase {
let lhs = produceLhs()
let rhs = produceRhs()
closure({ innerClosure in
closure { innerClosure in
startMeasuring()
let _ = innerClosure(lhs, rhs)
stopMeasuring()
})
}
}
}

View File

@ -414,7 +414,7 @@ class ArithmeticTests: XCTestCase {
let actual: Scalar = Surge.dist(lhs, rhs)
let expected: Scalar = sqrt(zip(lhs, rhs).map({ $0 - $1 }).map({ $0 * $0 }).reduce(0.0, +))
let expected: Scalar = sqrt(zip(lhs, rhs).map { $0 - $1 }.map { $0 * $0 }.reduce(0.0, +))
XCTAssertEqual(actual, expected, accuracy: 1e-6)
}
@ -427,7 +427,7 @@ class ArithmeticTests: XCTestCase {
let actual: Scalar = Surge.dist(lhs, rhs)
let expected: Scalar = sqrt(zip(lhs, rhs).map({ $0 - $1 }).map({ $0 * $0 }).reduce(0.0, +))
let expected: Scalar = sqrt(zip(lhs, rhs).map { $0 - $1 }.map { $0 * $0 }.reduce(0.0, +))
XCTAssertEqual(actual, expected, accuracy: 1e-6)
}

View File

@ -26,7 +26,7 @@ class AuxiliaryTests: XCTestCase {
let n = 10_000
func test_copysign() {
let signs = Array((0..<n).map({ $0 % 2 == 0 ? 1.0 : -1.0 }))
let signs = Array((0..<n).map { $0 % 2 == 0 ? 1.0 : -1.0 })
var magnitudes = [Double]()
for _ in (0..<n).enumerated() {

View File

@ -27,6 +27,7 @@ class ConvolutionTests: XCTestCase {
let doubleAccuracy: Double = 1e-11
// MARK: - Test Arrays - Float
let a1f: [Float] = [0, 0, 1, 0, 0]
let a2f: [Float] = [1, 0, 0]
let b1f: [Float] = [0, 2, 3, 1, 5, 6]
@ -39,6 +40,7 @@ class ConvolutionTests: XCTestCase {
let e2f: [Float] = [0, 0, 0]
// MARK: - Test Arrays - Double
let a1d: [Double] = [0, 0, 1, 0, 0]
let a2d: [Double] = [1, 0, 0]
let b1d: [Double] = [0, 2, 3, 1, 5, 6]
@ -51,6 +53,7 @@ class ConvolutionTests: XCTestCase {
let e2d: [Double] = [0, 0, 0]
// MARK: - Convolution - Float
func test_conv_float() {
let a3f: [Float] = [0, 0, 1, 0, 0, 0, 0]
let b3f: [Float] = [0, 0, 0, -2, -3, -1, -5, -6]
@ -66,6 +69,7 @@ class ConvolutionTests: XCTestCase {
}
// MARK: - Convolution - Double
func test_conv_double() {
let a3d: [Double] = [0, 0, 1, 0, 0, 0, 0]
let b3d: [Double] = [0, 0, 0, -2, -3, -1, -5, -6]
@ -81,6 +85,7 @@ class ConvolutionTests: XCTestCase {
}
// MARK: - Cross-Correlation - Float
func test_xcorr_float() {
let a3f: [Float] = [0, 0, 0, 0, 0, 0, 1, 0, 0]
let b3f: [Float] = [0, 0, 0, 0, -2, -3, -1, -5, -6, 0, 0]
@ -96,6 +101,7 @@ class ConvolutionTests: XCTestCase {
}
// MARK: - Cross-Correlation - Double
func test_xcorr_double() {
let a3d: [Double] = [0, 0, 0, 0, 0, 0, 1, 0, 0]
let b3d: [Double] = [0, 0, 0, 0, -2, -3, -1, -5, -6, 0, 0]
@ -111,6 +117,7 @@ class ConvolutionTests: XCTestCase {
}
// MARK: - Auto-Correlation - Float
func test_acorr_float() {
let a3f: [Float] = [0, 0, 0, 0, 1, 0, 0, 0, 0]
let b3f: [Float] = [0, 12, 28, 23, 44, 75, 44, 23, 28, 12, 0]
@ -126,6 +133,7 @@ class ConvolutionTests: XCTestCase {
}
// MARK: - Auto-Correlation - Double
func test_acorr_double() {
let a3d: [Double] = [0, 0, 0, 0, 1, 0, 0, 0, 0]
let b3d: [Double] = [0, 12, 28, 23, 44, 75, 44, 23, 28, 12, 0]

View File

@ -18,9 +18,10 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
@testable import Surge
import XCTest
@testable import Surge
// swiftlint:disable nesting type_body_length
class MatrixTests: XCTestCase {
@ -628,10 +629,10 @@ class MatrixTests: XCTestCase {
typealias Scalar = Float
let lhs: Matrix<Scalar> = [
[1]
[1],
]
let rhs: Matrix<Scalar> = [
[]
[],
]
let result = lhs * rhs
@ -644,10 +645,10 @@ class MatrixTests: XCTestCase {
typealias Scalar = Double
let lhs: Matrix<Scalar> = [
[1]
[1],
]
let rhs: Matrix<Scalar> = [
[]
[],
]
let result = lhs * rhs
@ -705,7 +706,7 @@ class MatrixTests: XCTestCase {
]
let rhs: Matrix<Scalar> = [
[-1, 3/2],
[-1, 3 / 2],
[1, -1],
]
@ -727,7 +728,7 @@ class MatrixTests: XCTestCase {
]
let rhs: Matrix<Scalar> = [
[-1, 3/2],
[-1, 3 / 2],
[1, -1],
]
@ -1003,7 +1004,7 @@ class MatrixTests: XCTestCase {
}) == nil
}
func eigen_decomposition_trivial_generic<T: FloatingPoint & ExpressibleByFloatLiteral>(defaultAccuracy: T, eigendecompostionFn: ((Matrix<T>) throws -> MatrixEigenDecompositionResult<T>)) throws {
func eigen_decomposition_trivial_generic<T: FloatingPoint & ExpressibleByFloatLiteral>(defaultAccuracy: T, eigendecompostionFn: (Matrix<T>) throws -> MatrixEigenDecompositionResult<T>) throws {
let matrix = Matrix<T>([
[1, 0, 0],
[0, 2, 0],
@ -1034,7 +1035,7 @@ class MatrixTests: XCTestCase {
}
// Example from https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.eig.html
func eigen_decomposition_complex_results_numpy_example_generic<T: FloatingPoint & ExpressibleByFloatLiteral>(defaultAccuracy: T, eigendecompostionFn: ((Matrix<T>) throws -> MatrixEigenDecompositionResult<T>)) throws {
func eigen_decomposition_complex_results_numpy_example_generic<T: FloatingPoint & ExpressibleByFloatLiteral>(defaultAccuracy: T, eigendecompostionFn: (Matrix<T>) throws -> MatrixEigenDecompositionResult<T>) throws {
let matrix = Matrix<T>([
[1, -1],
[1, 1],
@ -1064,13 +1065,13 @@ class MatrixTests: XCTestCase {
// Example from Intel's DGEEV documentation
// https://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_lapack_examples/dgeev.htm
func eigen_decomposition_complex_results_dgeev_example_generic<T: FloatingPoint & ExpressibleByFloatLiteral>(defaultAccuracy: T, eigendecompostionFn: ((Matrix<T>) throws -> MatrixEigenDecompositionResult<T>)) throws {
func eigen_decomposition_complex_results_dgeev_example_generic<T: FloatingPoint & ExpressibleByFloatLiteral>(defaultAccuracy: T, eigendecompostionFn: (Matrix<T>) throws -> MatrixEigenDecompositionResult<T>) throws {
let matrix = Matrix<T>(rows: 5, columns: 5, grid: [
-1.01, 0.86, -4.60, 3.31, -4.81,
3.98, 0.53, -7.04, 5.29, 3.55,
3.30, 8.26, -3.89, 8.20, -1.51,
4.43, 4.96, -7.66, -7.33, 6.18,
7.31, -6.43, -6.16, 2.47, 5.58
7.31, -6.43, -6.16, 2.47, 5.58,
])
let ed = try eigendecompostionFn(matrix)

View File

@ -18,9 +18,10 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
@testable import Surge
import XCTest
@testable import Surge
// swiftlint:disable nesting
class ScalarTests: XCTestCase {

View File

@ -40,10 +40,10 @@ extension FloatingPoint {
static func randomNormalized() -> Self {
switch self {
case is Float.Type:
let value = Float.random(in: (0.0)...(1.0))
let value = Float.random(in: 0.0...1.0)
return unsafeBitCast(value, to: self)
case is Double.Type:
let value = Double.random(in: (0.0)...(1.0))
let value = Double.random(in: 0.0...1.0)
return unsafeBitCast(value, to: self)
case _:
fatalError("Only supported by `Float` and `Double`")

View File

@ -93,7 +93,7 @@ private func checkArray<T, U>(
switch checkValue(actualValue, expectedValue, accuracy: accuracy) {
case .success:
continue
case .failure(let error):
case let .failure(error):
return .failure(.content(index: index, content: error))
}
}
@ -116,7 +116,7 @@ private func checkGrid<T, U, V>(
switch checkArray(actualArray, expectedArray, accuracy: accuracy) {
case .success:
continue
case .failure(let error):
case let .failure(error):
return .failure(.content(index: index, content: error))
}
}
@ -204,7 +204,7 @@ func XCTAssertEqual1D<T>(
let result = checkArray(actual, expected, accuracy: accuracy ?? 0.0)
guard case .failure(let error) = result else {
guard case let .failure(error) = result else {
return
}
@ -273,7 +273,7 @@ func XCTAssertEqual2D<T, U>(
let result = checkGrid(actual, expected, accuracy: accuracy ?? 0.0)
guard case .failure(let error) = result else {
guard case let .failure(error) = result else {
return
}