From c83f890f338f10f25f8224899f7904bb2041ac35 Mon Sep 17 00:00:00 2001 From: Vincent Esche Date: Fri, 30 Aug 2019 12:27:53 +0200 Subject: [PATCH] =?UTF-8?q?Moved=20functions/operators=20with=20`lhs:=20Sc?= =?UTF-8?q?alar`=20into=20separate=20=E2=80=98Scalar.swift=E2=80=99=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/Surge/Arithmetic.swift | 16 ------- Sources/Surge/Matrix.swift | 26 ----------- Sources/Surge/Scalar.swift | 81 +++++++++++++++++++++++++++++++++ Sources/Surge/Vector.swift | 17 +------ Surge.xcodeproj/project.pbxproj | 10 ++++ 5 files changed, 92 insertions(+), 58 deletions(-) create mode 100644 Sources/Surge/Scalar.swift diff --git a/Sources/Surge/Arithmetic.swift b/Sources/Surge/Arithmetic.swift index 9bdc077..32bffb5 100644 --- a/Sources/Surge/Arithmetic.swift +++ b/Sources/Surge/Arithmetic.swift @@ -260,22 +260,6 @@ public func * (lhs: L, rhs: Double) -> [Double] where return mul(lhs, rhs) } -public func mul(_ lhs: Float, _ rhs: R) -> [Float] where R.Element == Float { - return mul([Float](repeating: lhs, count: numericCast(rhs.count)), rhs) -} - -public func mul(_ lhs: Double, _ rhs: R) -> [Double] where R.Element == Double { - return mul([Double](repeating: lhs, count: numericCast(rhs.count)), rhs) -} - -public func * (lhs: Float, rhs: R) -> [Float] where R.Element == Float { - return mul(lhs, rhs) -} - -public func * (lhs: Double, rhs: R) -> [Double] where R.Element == Double { - return mul(lhs, rhs) -} - // MARK: Multiplication: In Place func mulInPlace(_ lhs: inout L, _ rhs: R) where L.Element == Float, R.Element == Float { diff --git a/Sources/Surge/Matrix.swift b/Sources/Surge/Matrix.swift index 3b7ae58..3872c35 100644 --- a/Sources/Surge/Matrix.swift +++ b/Sources/Surge/Matrix.swift @@ -322,32 +322,6 @@ public func - (lhs: Matrix, rhs: Matrix) -> Matrix { return sub(lhs, rhs) } -public func mul(_ alpha: Float, _ x: Matrix) -> Matrix { - var results = x - results.grid.withUnsafeMutableBufferPointer { pointer in - cblas_sscal(Int32(x.grid.count), alpha, pointer.baseAddress!, 1) - } - - return results -} - -public func mul(_ alpha: Double, _ x: Matrix) -> Matrix { - var results = x - results.grid.withUnsafeMutableBufferPointer { pointer in - cblas_dscal(Int32(x.grid.count), alpha, pointer.baseAddress!, 1) - } - - return results -} - -public func * (lhs: Float, rhs: Matrix) -> Matrix { - return mul(lhs, rhs) -} - -public func * (lhs: Double, rhs: Matrix) -> Matrix { - return mul(lhs, rhs) -} - public func mul(_ x: Matrix, _ y: Matrix) -> Matrix { precondition(x.columns == y.rows, "Matrix dimensions not compatible with multiplication") if x.rows == 0 || x.columns == 0 || y.columns == 0 { diff --git a/Sources/Surge/Scalar.swift b/Sources/Surge/Scalar.swift new file mode 100644 index 0000000..4c947c2 --- /dev/null +++ b/Sources/Surge/Scalar.swift @@ -0,0 +1,81 @@ +// Copyright © 2014-2019 the Surge contributors +// +// 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: Multiplication + +public func mul(_ lhs: Float, _ rhs: R) -> [Float] where R.Element == Float { + return mul([Float](repeating: lhs, count: numericCast(rhs.count)), rhs) +} + +public func mul(_ lhs: Double, _ rhs: R) -> [Double] where R.Element == Double { + return mul([Double](repeating: lhs, count: numericCast(rhs.count)), rhs) +} + +public func * (lhs: Float, rhs: R) -> [Float] where R.Element == Float { + return mul(lhs, rhs) +} + +public func * (lhs: Double, rhs: R) -> [Double] where R.Element == Double { + return mul(lhs, rhs) +} + +public func mul(_ x: Float, _ y: Vector) -> Vector { + return Vector(mul(x, y.scalars)) +} + +public func mul(_ x: Double, _ y: Vector) -> Vector { + return Vector(mul(x, y.scalars)) +} + +public func * (lhs: Float, rhs: Vector) -> Vector { + return mul(lhs, rhs) +} + +public func * (lhs: Double, rhs: Vector) -> Vector { + return mul(lhs, rhs) +} + +public func mul(_ alpha: Float, _ x: Matrix) -> Matrix { + var results = x + results.grid.withUnsafeMutableBufferPointer { pointer in + cblas_sscal(Int32(x.grid.count), alpha, pointer.baseAddress!, 1) + } + + return results +} + +public func mul(_ alpha: Double, _ x: Matrix) -> Matrix { + var results = x + results.grid.withUnsafeMutableBufferPointer { pointer in + cblas_dscal(Int32(x.grid.count), alpha, pointer.baseAddress!, 1) + } + + return results +} + +public func * (lhs: Float, rhs: Matrix) -> Matrix { + return mul(lhs, rhs) +} + +public func * (lhs: Double, rhs: Matrix) -> Matrix { + return mul(lhs, rhs) +} diff --git a/Sources/Surge/Vector.swift b/Sources/Surge/Vector.swift index 93e2a59..076b502 100644 --- a/Sources/Surge/Vector.swift +++ b/Sources/Surge/Vector.swift @@ -255,26 +255,11 @@ public func -= (lhs: inout Vector, rhs: Float) { public func -= (lhs: inout Vector, rhs: Double) { return subInPlace(&lhs, rhs) + } // MARK: Multiplication -public func mul(_ x: Float, _ y: Vector) -> Vector { - return Vector(mul(x, y.scalars)) -} - -public func mul(_ x: Double, _ y: Vector) -> Vector { - return Vector(mul(x, y.scalars)) -} - -public func * (lhs: Float, rhs: Vector) -> Vector { - return mul(lhs, rhs) -} - -public func * (lhs: Double, rhs: Vector) -> Vector { - return mul(lhs, rhs) -} - public func mul(_ x: Vector, _ y: Float) -> Vector { return Vector(mul(x.scalars, y)) } diff --git a/Surge.xcodeproj/project.pbxproj b/Surge.xcodeproj/project.pbxproj index f2a76c3..ec54fcd 100644 --- a/Surge.xcodeproj/project.pbxproj +++ b/Surge.xcodeproj/project.pbxproj @@ -104,6 +104,10 @@ CAEC79C02319275000516E10 /* OperatorPrecedences.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAEC79B72319244D00516E10 /* OperatorPrecedences.swift */; }; CAEC79C12319275000516E10 /* OperatorPrecedences.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAEC79B72319244D00516E10 /* OperatorPrecedences.swift */; }; CAEC79C22319275100516E10 /* OperatorPrecedences.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAEC79B72319244D00516E10 /* OperatorPrecedences.swift */; }; + CAEC79C423192FE300516E10 /* Scalar.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAEC79C323192FE300516E10 /* Scalar.swift */; }; + CAEC79C5231930EC00516E10 /* Scalar.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAEC79C323192FE300516E10 /* Scalar.swift */; }; + CAEC79C6231930ED00516E10 /* Scalar.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAEC79C323192FE300516E10 /* Scalar.swift */; }; + CAEC79C7231930ED00516E10 /* Scalar.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAEC79C323192FE300516E10 /* Scalar.swift */; }; CAFE5DA522F9EC1D00A34887 /* XCTAssert+Surge.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAFE5DA422F9EC1D00A34887 /* XCTAssert+Surge.swift */; }; CAFE5DA622F9EC1D00A34887 /* XCTAssert+Surge.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAFE5DA422F9EC1D00A34887 /* XCTAssert+Surge.swift */; }; CAFE5DA722F9EC1D00A34887 /* XCTAssert+Surge.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAFE5DA422F9EC1D00A34887 /* XCTAssert+Surge.swift */; }; @@ -180,6 +184,7 @@ 61E930C12070B69300694FCB /* UnsafeMutableMemory.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UnsafeMutableMemory.swift; sourceTree = ""; }; 61E930C72070BCCD00694FCB /* ArraySliceUnsafeMemory.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ArraySliceUnsafeMemory.swift; sourceTree = ""; }; CAEC79B72319244D00516E10 /* OperatorPrecedences.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OperatorPrecedences.swift; sourceTree = ""; }; + CAEC79C323192FE300516E10 /* Scalar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Scalar.swift; sourceTree = ""; }; CAFE5DA422F9EC1D00A34887 /* XCTAssert+Surge.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "XCTAssert+Surge.swift"; sourceTree = ""; }; CAFE5DA822F9ED3A00A34887 /* Vector.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Vector.swift; sourceTree = ""; }; CAFE5DAD22F9ED4900A34887 /* VectorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VectorTests.swift; sourceTree = ""; }; @@ -275,6 +280,7 @@ 61E930C12070B69300694FCB /* UnsafeMutableMemory.swift */, CAFE5DA822F9ED3A00A34887 /* Vector.swift */, CAEC79B72319244D00516E10 /* OperatorPrecedences.swift */, + CAEC79C323192FE300516E10 /* Scalar.swift */, ); path = Surge; sourceTree = ""; @@ -719,6 +725,7 @@ CAFE5DAA22F9ED3A00A34887 /* Vector.swift in Sources */, 61E930BE2070104600694FCB /* ArrayUnsafeMemory.swift in Sources */, 61E930B9207002EA00694FCB /* UnsafeMemory.swift in Sources */, + CAEC79C5231930EC00516E10 /* Scalar.swift in Sources */, 614AD3401FC0AF72002BFE1C /* Power.swift in Sources */, 6152A42220719E9200043627 /* Statistics.swift in Sources */, CAEC79C02319275000516E10 /* OperatorPrecedences.swift in Sources */, @@ -761,6 +768,7 @@ CAFE5DAB22F9ED3A00A34887 /* Vector.swift in Sources */, 61E930BF2070104600694FCB /* ArrayUnsafeMemory.swift in Sources */, 61E930BA207002EA00694FCB /* UnsafeMemory.swift in Sources */, + CAEC79C6231930ED00516E10 /* Scalar.swift in Sources */, 614AD3711FC0B0CC002BFE1C /* Power.swift in Sources */, 6152A42320719E9200043627 /* Statistics.swift in Sources */, CAEC79C12319275000516E10 /* OperatorPrecedences.swift in Sources */, @@ -803,6 +811,7 @@ CAFE5DAC22F9ED3A00A34887 /* Vector.swift in Sources */, 61E930C02070104600694FCB /* ArrayUnsafeMemory.swift in Sources */, 61E930BB207002EA00694FCB /* UnsafeMemory.swift in Sources */, + CAEC79C7231930ED00516E10 /* Scalar.swift in Sources */, 614AD3931FC0B134002BFE1C /* Power.swift in Sources */, 6152A42420719E9200043627 /* Statistics.swift in Sources */, CAEC79C22319275100516E10 /* OperatorPrecedences.swift in Sources */, @@ -845,6 +854,7 @@ CAFE5DA922F9ED3A00A34887 /* Vector.swift in Sources */, 61E930BD2070104600694FCB /* ArrayUnsafeMemory.swift in Sources */, 61E930B8207002EA00694FCB /* UnsafeMemory.swift in Sources */, + CAEC79C423192FE300516E10 /* Scalar.swift in Sources */, 6153945E1F762B59002A4AD2 /* Power.swift in Sources */, 6152A42120719E9200043627 /* Statistics.swift in Sources */, CAEC79BF2319274F00516E10 /* OperatorPrecedences.swift in Sources */,