Moved functions/operators with `lhs: Scalar` into separate ‘Scalar.swift’ file

This commit is contained in:
Vincent Esche 2019-08-30 12:27:53 +02:00
parent 8dc7cbc180
commit c83f890f33
5 changed files with 92 additions and 58 deletions

View File

@ -260,22 +260,6 @@ public func * <L: UnsafeMemoryAccessible>(lhs: L, rhs: Double) -> [Double] where
return mul(lhs, rhs)
}
public func mul<R: UnsafeMemoryAccessible>(_ lhs: Float, _ rhs: R) -> [Float] where R.Element == Float {
return mul([Float](repeating: lhs, count: numericCast(rhs.count)), rhs)
}
public func mul<R: UnsafeMemoryAccessible>(_ lhs: Double, _ rhs: R) -> [Double] where R.Element == Double {
return mul([Double](repeating: lhs, count: numericCast(rhs.count)), rhs)
}
public func * <R: UnsafeMemoryAccessible>(lhs: Float, rhs: R) -> [Float] where R.Element == Float {
return mul(lhs, rhs)
}
public func * <R: UnsafeMemoryAccessible>(lhs: Double, rhs: R) -> [Double] where R.Element == Double {
return mul(lhs, rhs)
}
// MARK: Multiplication: In Place
func mulInPlace<L: UnsafeMutableMemoryAccessible, R: UnsafeMemoryAccessible>(_ lhs: inout L, _ rhs: R) where L.Element == Float, R.Element == Float {

View File

@ -322,32 +322,6 @@ public func - (lhs: Matrix<Double>, rhs: Matrix<Double>) -> Matrix<Double> {
return sub(lhs, rhs)
}
public func mul(_ alpha: Float, _ x: Matrix<Float>) -> Matrix<Float> {
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<Double>) -> Matrix<Double> {
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<Float>) -> Matrix<Float> {
return mul(lhs, rhs)
}
public func * (lhs: Double, rhs: Matrix<Double>) -> Matrix<Double> {
return mul(lhs, rhs)
}
public func mul(_ x: Matrix<Float>, _ y: Matrix<Float>) -> Matrix<Float> {
precondition(x.columns == y.rows, "Matrix dimensions not compatible with multiplication")
if x.rows == 0 || x.columns == 0 || y.columns == 0 {

View File

@ -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<R: UnsafeMemoryAccessible>(_ lhs: Float, _ rhs: R) -> [Float] where R.Element == Float {
return mul([Float](repeating: lhs, count: numericCast(rhs.count)), rhs)
}
public func mul<R: UnsafeMemoryAccessible>(_ lhs: Double, _ rhs: R) -> [Double] where R.Element == Double {
return mul([Double](repeating: lhs, count: numericCast(rhs.count)), rhs)
}
public func * <R: UnsafeMemoryAccessible>(lhs: Float, rhs: R) -> [Float] where R.Element == Float {
return mul(lhs, rhs)
}
public func * <R: UnsafeMemoryAccessible>(lhs: Double, rhs: R) -> [Double] where R.Element == Double {
return mul(lhs, rhs)
}
public func mul(_ x: Float, _ y: Vector<Float>) -> Vector<Float> {
return Vector(mul(x, y.scalars))
}
public func mul(_ x: Double, _ y: Vector<Double>) -> Vector<Double> {
return Vector(mul(x, y.scalars))
}
public func * (lhs: Float, rhs: Vector<Float>) -> Vector<Float> {
return mul(lhs, rhs)
}
public func * (lhs: Double, rhs: Vector<Double>) -> Vector<Double> {
return mul(lhs, rhs)
}
public func mul(_ alpha: Float, _ x: Matrix<Float>) -> Matrix<Float> {
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<Double>) -> Matrix<Double> {
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<Float>) -> Matrix<Float> {
return mul(lhs, rhs)
}
public func * (lhs: Double, rhs: Matrix<Double>) -> Matrix<Double> {
return mul(lhs, rhs)
}

View File

@ -255,26 +255,11 @@ public func -= (lhs: inout Vector<Float>, rhs: Float) {
public func -= (lhs: inout Vector<Double>, rhs: Double) {
return subInPlace(&lhs, rhs)
}
// MARK: Multiplication
public func mul(_ x: Float, _ y: Vector<Float>) -> Vector<Float> {
return Vector(mul(x, y.scalars))
}
public func mul(_ x: Double, _ y: Vector<Double>) -> Vector<Double> {
return Vector(mul(x, y.scalars))
}
public func * (lhs: Float, rhs: Vector<Float>) -> Vector<Float> {
return mul(lhs, rhs)
}
public func * (lhs: Double, rhs: Vector<Double>) -> Vector<Double> {
return mul(lhs, rhs)
}
public func mul(_ x: Vector<Float>, _ y: Float) -> Vector<Float> {
return Vector(mul(x.scalars, y))
}

View File

@ -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 = "<group>"; };
61E930C72070BCCD00694FCB /* ArraySliceUnsafeMemory.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ArraySliceUnsafeMemory.swift; sourceTree = "<group>"; };
CAEC79B72319244D00516E10 /* OperatorPrecedences.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OperatorPrecedences.swift; sourceTree = "<group>"; };
CAEC79C323192FE300516E10 /* Scalar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Scalar.swift; sourceTree = "<group>"; };
CAFE5DA422F9EC1D00A34887 /* XCTAssert+Surge.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "XCTAssert+Surge.swift"; sourceTree = "<group>"; };
CAFE5DA822F9ED3A00A34887 /* Vector.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Vector.swift; sourceTree = "<group>"; };
CAFE5DAD22F9ED4900A34887 /* VectorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VectorTests.swift; sourceTree = "<group>"; };
@ -275,6 +280,7 @@
61E930C12070B69300694FCB /* UnsafeMutableMemory.swift */,
CAFE5DA822F9ED3A00A34887 /* Vector.swift */,
CAEC79B72319244D00516E10 /* OperatorPrecedences.swift */,
CAEC79C323192FE300516E10 /* Scalar.swift */,
);
path = Surge;
sourceTree = "<group>";
@ -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 */,