From 26df926a9cbbffd726fa5d7e53ae8af5cd1ce7db Mon Sep 17 00:00:00 2001 From: tr1ckyf0x Date: Mon, 9 Oct 2017 09:11:10 +0700 Subject: [PATCH] Converted to Swift 4 --- .../Dimensional.xcodeproj/project.pbxproj | 30 +++++-- .../xcschemes/Dimensional.xcscheme | 4 +- .../DimensionalTests/DimensionalTests.swift | 9 +- Dimensional/Vector.swift | 3 +- Sources/Matrix.swift | 22 ++--- Sources/NumericArithmeticType.swift | 85 +++++++++---------- Sources/RowView.swift | 4 +- 7 files changed, 83 insertions(+), 74 deletions(-) diff --git a/Dimensional/Dimensional.xcodeproj/project.pbxproj b/Dimensional/Dimensional.xcodeproj/project.pbxproj index 3606adb..3ff2517 100644 --- a/Dimensional/Dimensional.xcodeproj/project.pbxproj +++ b/Dimensional/Dimensional.xcodeproj/project.pbxproj @@ -207,16 +207,16 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0720; - LastUpgradeCheck = 0820; + LastUpgradeCheck = 0900; ORGANIZATIONNAME = "Jaden Geller"; TargetAttributes = { 4D59D53D1C3C757D00058827 = { CreatedOnToolsVersion = 7.2; - LastSwiftMigration = 0820; + LastSwiftMigration = 0900; }; 4D59D5471C3C757D00058827 = { CreatedOnToolsVersion = 7.2; - LastSwiftMigration = 0820; + LastSwiftMigration = 0900; }; 6B4D54F91E15F0E900BF47C3 = { CreatedOnToolsVersion = 8.2.1; @@ -320,14 +320,20 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; @@ -370,14 +376,20 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; @@ -422,7 +434,8 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -442,7 +455,8 @@ PRODUCT_BUNDLE_IDENTIFIER = com.JadenGeller.Dimensional; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.0; }; name = Release; }; @@ -454,7 +468,8 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.JadenGeller.DimensionalTests; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -466,7 +481,8 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.JadenGeller.DimensionalTests; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.0; }; name = Release; }; diff --git a/Dimensional/Dimensional.xcodeproj/xcshareddata/xcschemes/Dimensional.xcscheme b/Dimensional/Dimensional.xcodeproj/xcshareddata/xcschemes/Dimensional.xcscheme index 5706d21..fbef7e6 100644 --- a/Dimensional/Dimensional.xcodeproj/xcshareddata/xcschemes/Dimensional.xcscheme +++ b/Dimensional/Dimensional.xcodeproj/xcshareddata/xcschemes/Dimensional.xcscheme @@ -1,6 +1,6 @@ Element { precondition(self.count == vector.count, "Vectors are not the same length") return zip(self, vector).map { $0.0 * $0.1 }.reduce(Element(0), +) diff --git a/Sources/Matrix.swift b/Sources/Matrix.swift index a3da57f..c2c59d8 100644 --- a/Sources/Matrix.swift +++ b/Sources/Matrix.swift @@ -4,6 +4,7 @@ // // Created by Jaden Geller on 1/5/16. // Copyright © 2016 Jaden Geller. All rights reserved. +// Modified by Vladislav Lisyanskiy on 9/10/2017. - Converted to Swift 4. // public struct Matrix { @@ -30,11 +31,10 @@ extension Matrix: ExpressibleByArrayLiteral { public init(_ columns: ColumnView) { self.rowBacking = columns.matrix.rowBacking } - } extension Matrix { - public subscript(row: Int, column: Int) -> Member { + public subscript(_ row: Int, _ column: Int) -> Member { get { return rowBacking[row][column] } @@ -129,21 +129,20 @@ extension Matrix { return rows.count == columns.count } - public var transpose: Matrix { + public var transposed: Matrix { return Matrix(RowView(columns)) } - } extension Matrix where Member: Equatable { public var isSymmetric: Bool { - guard isSquare else { return false} - return transpose == self - + guard isSquare else { return false } + return transposed == self } - } +public typealias MatrixDimensions = (width: Int, height: Int) + extension Matrix where Member: ExpressibleByIntegerLiteral { public static func diagonal(_ dimensions: MatrixDimensions, diagonalValue: Member, defaultValue: Member) -> Matrix { var matrix = Matrix(repeating: defaultValue, dimensions: dimensions) @@ -155,10 +154,9 @@ extension Matrix where Member: ExpressibleByIntegerLiteral { public static func diagonalFromVector(_ vector: Array) -> Matrix { var matrix = Matrix(repeating: 0, dimensions: (vector.count, vector.count)) - for i in 0.. Self static func -(lhs: Self, rhs: Self) -> Self static func *(lhs: Self, rhs: Self) -> Self @@ -20,83 +21,83 @@ public protocol NumericArithmeticType: ExpressibleByIntegerLiteral { } -public protocol SignedNumericArithmeticType: NumericArithmeticType { +public protocol SignedNumericArithmetic: NumericArithmetic { prefix static func -(value: Self) -> Self } -public protocol FloatingPointArithmeticType: SignedNumericArithmeticType, Equatable, FloatingPoint { } - -extension Int8 : SignedNumericArithmeticType { } -extension Int16 : SignedNumericArithmeticType { } -extension Int32 : SignedNumericArithmeticType { } -extension Int64 : SignedNumericArithmeticType { } -extension Int : SignedNumericArithmeticType { } -extension UInt8 : NumericArithmeticType { } -extension UInt16 : NumericArithmeticType { } -extension UInt32 : NumericArithmeticType { } -extension UInt64 : NumericArithmeticType { } -extension UInt : NumericArithmeticType { } -extension Float32 : FloatingPointArithmeticType { } -extension Float64 : FloatingPointArithmeticType { } +public protocol FloatingPointArithmetic: SignedNumericArithmetic, FloatingPoint { } + +extension Int8 : SignedNumericArithmetic { } +extension Int16 : SignedNumericArithmetic { } +extension Int32 : SignedNumericArithmetic { } +extension Int64 : SignedNumericArithmetic { } +extension Int : SignedNumericArithmetic { } +extension UInt8 : NumericArithmetic { } +extension UInt16 : NumericArithmetic { } +extension UInt32 : NumericArithmetic { } +extension UInt64 : NumericArithmetic { } +extension UInt : NumericArithmetic { } +extension Float32 : FloatingPointArithmetic { } +extension Float64 : FloatingPointArithmetic { } #if arch(x86_64) || arch(i386) - extension Float80 : FloatingPointArithmeticType { } + extension Float80 : FloatingPointArithmetic { } #endif -public prefix func -(value: Matrix) -> Matrix { - return value.map{-$0} +public prefix func -(value: Matrix) -> Matrix { + return value.map {-$0} } -public func *(lhs: T, rhs: Matrix) -> Matrix { +public func *(lhs: T, rhs: Matrix) -> Matrix { return rhs.map { lhs * $0 } } -public func *(lhs: Matrix, rhs: T) -> Matrix { +public func *(lhs: Matrix, rhs: T) -> Matrix { return lhs.map { rhs * $0 } } -public func +(lhs: T, rhs: Matrix) -> Matrix { +public func +(lhs: T, rhs: Matrix) -> Matrix { return rhs.map { lhs + $0 } } -public func +(lhs: Matrix, rhs: T) -> Matrix { +public func +(lhs: Matrix, rhs: T) -> Matrix { return lhs.map { rhs + $0 } } -public func -(lhs: T, rhs: Matrix) -> Matrix { +public func -(lhs: T, rhs: Matrix) -> Matrix { return rhs.map { lhs - $0 } } -public func -(lhs: Matrix, rhs: T) -> Matrix { +public func -(lhs: Matrix, rhs: T) -> Matrix { return lhs.map { $0 - rhs} } -public func /(lhs: Matrix, rhs: T) -> Matrix { +public func /(lhs: Matrix, rhs: T) -> Matrix { return lhs.map { $0 / rhs} } -public func +(lhs: Matrix, rhs: Matrix) -> Matrix { +public func +(lhs: Matrix, rhs: Matrix) -> Matrix { precondition(lhs.dimensions == rhs.dimensions, "Cannot add matrices of different dimensions.") return lhs.zipWith(rhs, transform: +) } -public func -(lhs: Matrix, rhs: Matrix) -> Matrix { +public func -(lhs: Matrix, rhs: Matrix) -> Matrix { precondition(lhs.dimensions == rhs.dimensions, "Cannot subract matrices of different dimensions.") return lhs.zipWith(rhs, transform: -) } -public func +=(lhs: inout Matrix, rhs: Matrix) { +public func +=(lhs: inout Matrix, rhs: Matrix) { lhs = lhs + rhs } -public func -=(lhs: inout Matrix, rhs: Matrix) { +public func -=(lhs: inout Matrix, rhs: Matrix) { lhs = lhs - rhs } -public func *=(lhs: inout Matrix, rhs: Matrix) { +public func *=(lhs: inout Matrix, rhs: Matrix) { lhs = lhs * rhs } -public func *(lhs: Array, rhs: Matrix) -> Array { +public func *(lhs: Array, rhs: Matrix) -> Array { precondition(lhs.count == rhs.rows.count, "Matrix and Vector dimensions are not compatible") var result = Array() for column in rhs.columns { @@ -105,7 +106,7 @@ public func *(lhs: Array, rhs: Matrix) -> return result } -public func *(lhs: Matrix, rhs: Array) -> Array { +public func *(lhs: Matrix, rhs: Array) -> Array { precondition(rhs.count == lhs.columns.count, "Matrix and Vector dimensions are not compatible") var result = Array() for row in lhs.rows { @@ -114,16 +115,14 @@ public func *(lhs: Matrix, rhs: Array) -> return result } - - -extension Matrix where Member: NumericArithmeticType { +extension Matrix where Member: NumericArithmetic { public func dot(_ other: Matrix) -> Member { precondition(dimensions == other.dimensions, "Cannot take the dot product of matrices of different dimensions.") return zipWith(other, transform: *).reduce(0, +) } } -extension Matrix where Member: SignedNumericArithmeticType { +extension Matrix where Member: SignedNumericArithmetic { public var determinant: Member { precondition(isSquare, "Cannot find the determinant of a non-square matrix.") precondition(!isEmpty, "Cannot find the determinant of an empty matrix.") @@ -158,11 +157,11 @@ extension Matrix where Member: SignedNumericArithmeticType { } public var adjoint: Matrix { - return cofactor.transpose + return cofactor.transposed } } -extension Matrix where Member: FloatingPointArithmeticType { +extension Matrix where Member: FloatingPointArithmetic { public var cholesky:Matrix { /* Ported from LAPACK Fortran Code http://www.netlib.org/lapack/double/dpotrf.f */ precondition(!isEmpty, "Cannot find the cholesky of an empty matrix.") @@ -199,20 +198,20 @@ extension Matrix where Member: FloatingPointArithmeticType { } } -extension Matrix where Member: FloatingPointArithmeticType { +extension Matrix where Member: FloatingPointArithmetic { public var inverse: Matrix { return adjoint * (1 / determinant) } } -extension Matrix where Member: NumericArithmeticType { +extension Matrix where Member: NumericArithmetic { public func transformVector(_ vector: [Member]) -> [Member] { return rows.map{ row in zip(row, vector).map(*).reduce(0, +) } } } -public func *(lhs: Matrix, rhs: Matrix) -> Matrix { +public func *(lhs: Matrix, rhs: Matrix) -> Matrix { precondition(lhs.columns.count == rhs.rows.count, "Incompatible dimensions for matrix multiplication.") - return Matrix(lhs.rows.map(rhs.transpose.transformVector)) + return Matrix(lhs.rows.map(rhs.transposed.transformVector)) } diff --git a/Sources/RowView.swift b/Sources/RowView.swift index 30a4fbe..2e62fc2 100644 --- a/Sources/RowView.swift +++ b/Sources/RowView.swift @@ -4,6 +4,7 @@ // // Created by Jaden Geller on 1/5/16. // Copyright © 2016 Jaden Geller. All rights reserved. +// Modified by Vladislav Lisyanskiy on 9/10/2017. - Converted to Swift 4. // public struct RowView { @@ -42,7 +43,7 @@ extension RowView: CustomStringConvertible, CustomDebugStringConvertible { extension RowView: MutableCollection, RangeReplaceableCollection { public mutating func replaceSubrange(_ subRange: Range, with newElements: C) where C.Iterator.Element == [Member] { let expectedCount = matrix.count > 0 ? matrix.columns.count : (newElements.first?.count ?? 0) - newElements.forEach{ row in + newElements.forEach { row in precondition(row.count == expectedCount, "Incompatable vector size.") } matrix.rowBacking.replaceSubrange(subRange, with: newElements) @@ -70,4 +71,3 @@ extension RowView: MutableCollection, RangeReplaceableCollection { } } } -