From 1e96fab7309d96943bf29415b51b2f24fce46676 Mon Sep 17 00:00:00 2001 From: Ilya Mikhaltsou Date: Sat, 5 May 2018 09:54:56 -0700 Subject: [PATCH] Changed element-wise operators from + to .+ to resolve #58 (#80) --- Sources/Surge/Arithmetic.swift | 55 +++++++++++++++++++++------------- Sources/Surge/Matrix.swift | 4 +-- Sources/Surge/Statistics.swift | 4 +-- 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/Sources/Surge/Arithmetic.swift b/Sources/Surge/Arithmetic.swift index cc5c6c37..84d744d4 100644 --- a/Sources/Surge/Arithmetic.swift +++ b/Sources/Surge/Arithmetic.swift @@ -260,7 +260,7 @@ public func dot(_ x: X, _ public func dist(_ x: X, _ y: Y) -> Float where X.Element == Float, Y.Element == Float { precondition(x.count == y.count, "Vectors must have equal count") - let sub = x - y + let sub = x .- y var squared = [Float](repeating: 0.0, count: numericCast(x.count)) squared.withUnsafeMutableBufferPointer { bufferPointer in vDSP_vsq(sub, 1, bufferPointer.baseAddress!, 1, numericCast(x.count)) @@ -270,7 +270,7 @@ public func dist(_ x: X, _ public func dist(_ x: X, _ y: Y) -> Double where X.Element == Double, Y.Element == Double { precondition(x.count == y.count, "Vectors must have equal count") - let sub = x - y + let sub = x .- y var squared = [Double](repeating: 0.0, count: numericCast(x.count)) squared.withUnsafeMutableBufferPointer { bufferPointer in vDSP_vsqD(sub, 1, bufferPointer.baseAddress!, 1, numericCast(x.count)) @@ -282,7 +282,10 @@ public func dist(_ x: X, _ // MARK: Elemen-wise addition -public func += (lhs: inout L, rhs: R) where L.Element == Float, R.Element == Float { +infix operator .+: AdditionPrecedence +infix operator .+=: AssignmentPrecedence + +public func .+= (lhs: inout L, rhs: R) where L.Element == Float, R.Element == Float { lhs.withUnsafeMutableMemory { lm in rhs.withUnsafeMemory { rm in vDSP_vadd(lm.pointer, lm.stride, rm.pointer, rm.stride, lm.pointer, lm.stride, numericCast(lm.count)) @@ -290,7 +293,7 @@ public func += (lhs } } -public func += (lhs: inout L, rhs: R) where L.Element == Double, R.Element == Double { +public func .+= (lhs: inout L, rhs: R) where L.Element == Double, R.Element == Double { lhs.withUnsafeMutableMemory { lm in rhs.withUnsafeMemory { rm in vDSP_vaddD(lm.pointer, lm.stride, rm.pointer, rm.stride, lm.pointer, lm.stride, numericCast(lm.count)) @@ -298,11 +301,11 @@ public func += (lhs } } -public func + (lhs: L, rhs: R) -> [Float] where L.Element == Float, R.Element == Float { +public func .+ (lhs: L, rhs: R) -> [Float] where L.Element == Float, R.Element == Float { return add(lhs, rhs) } -public func + (lhs: L, rhs: R) -> [Double] where L.Element == Double, R.Element == Double { +public func .+ (lhs: L, rhs: R) -> [Double] where L.Element == Double, R.Element == Double { return add(lhs, rhs) } @@ -332,7 +335,10 @@ public func + (lhs: L, rhs: Double) -> [Double] where // MARK: Element-wise subtraction -public func -= (lhs: inout L, rhs: R) where L.Element == Float, R.Element == Float { +infix operator .-: AdditionPrecedence +infix operator .-=: AssignmentPrecedence + +public func .-= (lhs: inout L, rhs: R) where L.Element == Float, R.Element == Float { lhs.withUnsafeMutableMemory { lm in rhs.withUnsafeMemory { rm in vDSP_vsub(rm.pointer, rm.stride, lm.pointer, lm.stride, lm.pointer, lm.stride, numericCast(lm.count)) @@ -340,7 +346,7 @@ public func -= (lhs } } -public func -= (lhs: inout L, rhs: R) where L.Element == Double, R.Element == Double { +public func .-= (lhs: inout L, rhs: R) where L.Element == Double, R.Element == Double { lhs.withUnsafeMutableMemory { lm in rhs.withUnsafeMemory { rm in vDSP_vsubD(rm.pointer, rm.stride, lm.pointer, lm.stride, lm.pointer, lm.stride, numericCast(lm.count)) @@ -348,11 +354,11 @@ public func -= (lhs } } -public func - (lhs: L, rhs: R) -> [Float] where L.Element == Float, R.Element == Float { +public func .- (lhs: L, rhs: R) -> [Float] where L.Element == Float, R.Element == Float { return sub(lhs, rhs) } -public func - (lhs: L, rhs: R) -> [Double] where L.Element == Double, R.Element == Double { +public func .- (lhs: L, rhs: R) -> [Double] where L.Element == Double, R.Element == Double { return sub(lhs, rhs) } @@ -382,7 +388,10 @@ public func - (lhs: L, rhs: Double) -> [Double] where // MARK: Element-wise division -public func /= (lhs: inout L, rhs: R) where L.Element == Float, R.Element == Float { +infix operator ./: MultiplicationPrecedence +infix operator ./=: AssignmentPrecedence + +public func ./= (lhs: inout L, rhs: R) where L.Element == Float, R.Element == Float { lhs.withUnsafeMutableMemory { lm in rhs.withUnsafeMemory { rm in vDSP_vdiv(lm.pointer, lm.stride, rm.pointer, rm.stride, lm.pointer, lm.stride, numericCast(lm.count)) @@ -390,7 +399,7 @@ public func /= (lhs } } -public func /= (lhs: inout L, rhs: R) where L.Element == Double, R.Element == Double { +public func ./= (lhs: inout L, rhs: R) where L.Element == Double, R.Element == Double { lhs.withUnsafeMutableMemory { lm in rhs.withUnsafeMemory { rm in vDSP_vdivD(lm.pointer, lm.stride, rm.pointer, rm.stride, lm.pointer, lm.stride, numericCast(lm.count)) @@ -398,11 +407,11 @@ public func /= (lhs } } -public func / (lhs: L, rhs: R) -> [Float] where L.Element == Float, R.Element == Float { +public func ./ (lhs: L, rhs: R) -> [Float] where L.Element == Float, R.Element == Float { return div(lhs, rhs) } -public func / (lhs: L, rhs: R) -> [Double] where L.Element == Double, R.Element == Double { +public func ./ (lhs: L, rhs: R) -> [Double] where L.Element == Double, R.Element == Double { return div(lhs, rhs) } @@ -432,7 +441,10 @@ public func / (lhs: L, rhs: Double) -> [Double] where // MARK: Element-wise multiplication -public func *= (lhs: inout L, rhs: R) where L.Element == Float, R.Element == Float { +infix operator .*: MultiplicationPrecedence +infix operator .*=: AssignmentPrecedence + +public func .*= (lhs: inout L, rhs: R) where L.Element == Float, R.Element == Float { lhs.withUnsafeMutableMemory { lm in rhs.withUnsafeMemory { rm in vDSP_vmul(lm.pointer, lm.stride, rm.pointer, rm.stride, lm.pointer, lm.stride, numericCast(lm.count)) @@ -440,7 +452,7 @@ public func *= (lhs } } -public func *= (lhs: inout L, rhs: R) where L.Element == Double, R.Element == Double { +public func .*= (lhs: inout L, rhs: R) where L.Element == Double, R.Element == Double { lhs.withUnsafeMutableMemory { lm in rhs.withUnsafeMemory { rm in vDSP_vmulD(lm.pointer, lm.stride, rm.pointer, rm.stride, lm.pointer, lm.stride, numericCast(lm.count)) @@ -448,11 +460,11 @@ public func *= (lhs } } -public func * (lhs: L, rhs: R) -> [Float] where L.Element == Float, R.Element == Float { +public func .* (lhs: L, rhs: R) -> [Float] where L.Element == Float, R.Element == Float { return mul(lhs, rhs) } -public func * (lhs: L, rhs: R) -> [Double] where L.Element == Double, R.Element == Double { +public func .* (lhs: L, rhs: R) -> [Double] where L.Element == Double, R.Element == Double { return mul(lhs, rhs) } @@ -482,11 +494,14 @@ public func * (lhs: L, rhs: Double) -> [Double] where // MARK: Modulo -public func % (lhs: L, rhs: R) -> [Float] where L.Element == Float, R.Element == Float { +infix operator .%: MultiplicationPrecedence +infix operator .%=: AssignmentPrecedence + +public func .% (lhs: L, rhs: R) -> [Float] where L.Element == Float, R.Element == Float { return mod(lhs, rhs) } -public func % (lhs: L, rhs: R) -> [Double] where L.Element == Double, R.Element == Double { +public func .% (lhs: L, rhs: R) -> [Double] where L.Element == Double, R.Element == Double { return mod(lhs, rhs) } diff --git a/Sources/Surge/Matrix.swift b/Sources/Surge/Matrix.swift index 56a55ae3..7e1a29cc 100644 --- a/Sources/Surge/Matrix.swift +++ b/Sources/Surge/Matrix.swift @@ -259,14 +259,14 @@ public func mul(_ x: Matrix, _ y: Matrix) -> Matrix { public func elmul(_ x: Matrix, _ y: Matrix) -> Matrix { precondition(x.rows == y.rows && x.columns == y.columns, "Matrix must have the same dimensions") var result = Matrix(rows: x.rows, columns: x.columns, repeatedValue: 0.0) - result.grid = x.grid * y.grid + result.grid = x.grid .* y.grid return result } public func elmul(_ x: Matrix, _ y: Matrix) -> Matrix { precondition(x.rows == y.rows && x.columns == y.columns, "Matrix must have the same dimensions") var result = Matrix(rows: x.rows, columns: x.columns, repeatedValue: 0.0) - result.grid = x.grid * y.grid + result.grid = x.grid .* y.grid return result } diff --git a/Sources/Surge/Statistics.swift b/Sources/Surge/Statistics.swift index ccc8486c..5577df1e 100644 --- a/Sources/Surge/Statistics.swift +++ b/Sources/Surge/Statistics.swift @@ -238,7 +238,7 @@ public func linregress(_ x precondition(x.count == y.count, "Vectors must have equal count") let meanx = mean(x) let meany = mean(y) - let meanxy = mean(x * y) + let meanxy = mean(x .* y) let meanx_sqr = measq(x) let slope = (meanx * meany - meanxy) / (meanx * meanx - meanx_sqr) @@ -256,7 +256,7 @@ public func linregress(_ x precondition(x.count == y.count, "Vectors must have equal count") let meanx = mean(x) let meany = mean(y) - let meanxy = mean(x * y) + let meanxy = mean(x .* y) let meanx_sqr = measq(x) let slope = (meanx * meany - meanxy) / (meanx * meanx - meanx_sqr)