Skip to content

Commit

Permalink
Changed element-wise operators from + to .+ to resolve #58 (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
morpheby authored and alejandro-isaza committed May 5, 2018
1 parent bb3e9b4 commit 1e96fab
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 24 deletions.
55 changes: 35 additions & 20 deletions Sources/Surge/Arithmetic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public func dot<X: UnsafeMemoryAccessible, Y: UnsafeMemoryAccessible>(_ x: X, _

public func dist<X: UnsafeMemoryAccessible, Y: UnsafeMemoryAccessible>(_ 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))
Expand All @@ -270,7 +270,7 @@ public func dist<X: UnsafeMemoryAccessible, Y: UnsafeMemoryAccessible>(_ x: X, _

public func dist<X: UnsafeMemoryAccessible, Y: UnsafeMemoryAccessible>(_ 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))
Expand All @@ -282,27 +282,30 @@ public func dist<X: UnsafeMemoryAccessible, Y: UnsafeMemoryAccessible>(_ x: X, _

// MARK: Elemen-wise addition

public func += <L: UnsafeMutableMemoryAccessible, R: UnsafeMemoryAccessible>(lhs: inout L, rhs: R) where L.Element == Float, R.Element == Float {
infix operator .+: AdditionPrecedence
infix operator .+=: AssignmentPrecedence

public func .+= <L: UnsafeMutableMemoryAccessible, R: UnsafeMemoryAccessible>(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))
}
}
}

public func += <L: UnsafeMutableMemoryAccessible, R: UnsafeMemoryAccessible>(lhs: inout L, rhs: R) where L.Element == Double, R.Element == Double {
public func .+= <L: UnsafeMutableMemoryAccessible, R: UnsafeMemoryAccessible>(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))
}
}
}

public func + <L: UnsafeMemoryAccessible, R: UnsafeMemoryAccessible>(lhs: L, rhs: R) -> [Float] where L.Element == Float, R.Element == Float {
public func .+ <L: UnsafeMemoryAccessible, R: UnsafeMemoryAccessible>(lhs: L, rhs: R) -> [Float] where L.Element == Float, R.Element == Float {
return add(lhs, rhs)
}

public func + <L: UnsafeMemoryAccessible, R: UnsafeMemoryAccessible>(lhs: L, rhs: R) -> [Double] where L.Element == Double, R.Element == Double {
public func .+ <L: UnsafeMemoryAccessible, R: UnsafeMemoryAccessible>(lhs: L, rhs: R) -> [Double] where L.Element == Double, R.Element == Double {
return add(lhs, rhs)
}

Expand Down Expand Up @@ -332,27 +335,30 @@ public func + <L: UnsafeMemoryAccessible>(lhs: L, rhs: Double) -> [Double] where

// MARK: Element-wise subtraction

public func -= <L: UnsafeMutableMemoryAccessible, R: UnsafeMemoryAccessible>(lhs: inout L, rhs: R) where L.Element == Float, R.Element == Float {
infix operator .-: AdditionPrecedence
infix operator .-=: AssignmentPrecedence

public func .-= <L: UnsafeMutableMemoryAccessible, R: UnsafeMemoryAccessible>(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))
}
}
}

public func -= <L: UnsafeMutableMemoryAccessible, R: UnsafeMemoryAccessible>(lhs: inout L, rhs: R) where L.Element == Double, R.Element == Double {
public func .-= <L: UnsafeMutableMemoryAccessible, R: UnsafeMemoryAccessible>(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))
}
}
}

public func - <L: UnsafeMemoryAccessible, R: UnsafeMemoryAccessible>(lhs: L, rhs: R) -> [Float] where L.Element == Float, R.Element == Float {
public func .- <L: UnsafeMemoryAccessible, R: UnsafeMemoryAccessible>(lhs: L, rhs: R) -> [Float] where L.Element == Float, R.Element == Float {
return sub(lhs, rhs)
}

public func - <L: UnsafeMemoryAccessible, R: UnsafeMemoryAccessible>(lhs: L, rhs: R) -> [Double] where L.Element == Double, R.Element == Double {
public func .- <L: UnsafeMemoryAccessible, R: UnsafeMemoryAccessible>(lhs: L, rhs: R) -> [Double] where L.Element == Double, R.Element == Double {
return sub(lhs, rhs)
}

Expand Down Expand Up @@ -382,27 +388,30 @@ public func - <L: UnsafeMemoryAccessible>(lhs: L, rhs: Double) -> [Double] where

// MARK: Element-wise division

public func /= <L: UnsafeMutableMemoryAccessible, R: UnsafeMemoryAccessible>(lhs: inout L, rhs: R) where L.Element == Float, R.Element == Float {
infix operator ./: MultiplicationPrecedence
infix operator ./=: AssignmentPrecedence

public func ./= <L: UnsafeMutableMemoryAccessible, R: UnsafeMemoryAccessible>(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))
}
}
}

public func /= <L: UnsafeMutableMemoryAccessible, R: UnsafeMemoryAccessible>(lhs: inout L, rhs: R) where L.Element == Double, R.Element == Double {
public func ./= <L: UnsafeMutableMemoryAccessible, R: UnsafeMemoryAccessible>(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))
}
}
}

public func / <L: UnsafeMemoryAccessible, R: UnsafeMemoryAccessible>(lhs: L, rhs: R) -> [Float] where L.Element == Float, R.Element == Float {
public func ./ <L: UnsafeMemoryAccessible, R: UnsafeMemoryAccessible>(lhs: L, rhs: R) -> [Float] where L.Element == Float, R.Element == Float {
return div(lhs, rhs)
}

public func / <L: UnsafeMemoryAccessible, R: UnsafeMemoryAccessible>(lhs: L, rhs: R) -> [Double] where L.Element == Double, R.Element == Double {
public func ./ <L: UnsafeMemoryAccessible, R: UnsafeMemoryAccessible>(lhs: L, rhs: R) -> [Double] where L.Element == Double, R.Element == Double {
return div(lhs, rhs)
}

Expand Down Expand Up @@ -432,27 +441,30 @@ public func / <L: UnsafeMemoryAccessible>(lhs: L, rhs: Double) -> [Double] where

// MARK: Element-wise multiplication

public func *= <L: UnsafeMutableMemoryAccessible, R: UnsafeMemoryAccessible>(lhs: inout L, rhs: R) where L.Element == Float, R.Element == Float {
infix operator .*: MultiplicationPrecedence
infix operator .*=: AssignmentPrecedence

public func .*= <L: UnsafeMutableMemoryAccessible, R: UnsafeMemoryAccessible>(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))
}
}
}

public func *= <L: UnsafeMutableMemoryAccessible, R: UnsafeMemoryAccessible>(lhs: inout L, rhs: R) where L.Element == Double, R.Element == Double {
public func .*= <L: UnsafeMutableMemoryAccessible, R: UnsafeMemoryAccessible>(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))
}
}
}

public func * <L: UnsafeMemoryAccessible, R: UnsafeMemoryAccessible>(lhs: L, rhs: R) -> [Float] where L.Element == Float, R.Element == Float {
public func .* <L: UnsafeMemoryAccessible, R: UnsafeMemoryAccessible>(lhs: L, rhs: R) -> [Float] where L.Element == Float, R.Element == Float {
return mul(lhs, rhs)
}

public func * <L: UnsafeMemoryAccessible, R: UnsafeMemoryAccessible>(lhs: L, rhs: R) -> [Double] where L.Element == Double, R.Element == Double {
public func .* <L: UnsafeMemoryAccessible, R: UnsafeMemoryAccessible>(lhs: L, rhs: R) -> [Double] where L.Element == Double, R.Element == Double {
return mul(lhs, rhs)
}

Expand Down Expand Up @@ -482,11 +494,14 @@ public func * <L: UnsafeMemoryAccessible>(lhs: L, rhs: Double) -> [Double] where

// MARK: Modulo

public func % <L: UnsafeMemoryAccessible, R: UnsafeMemoryAccessible>(lhs: L, rhs: R) -> [Float] where L.Element == Float, R.Element == Float {
infix operator .%: MultiplicationPrecedence
infix operator .%=: AssignmentPrecedence

public func .% <L: UnsafeMemoryAccessible, R: UnsafeMemoryAccessible>(lhs: L, rhs: R) -> [Float] where L.Element == Float, R.Element == Float {
return mod(lhs, rhs)
}

public func % <L: UnsafeMemoryAccessible, R: UnsafeMemoryAccessible>(lhs: L, rhs: R) -> [Double] where L.Element == Double, R.Element == Double {
public func .% <L: UnsafeMemoryAccessible, R: UnsafeMemoryAccessible>(lhs: L, rhs: R) -> [Double] where L.Element == Double, R.Element == Double {
return mod(lhs, rhs)
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/Surge/Matrix.swift
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,14 @@ public func mul(_ x: Matrix<Double>, _ y: Matrix<Double>) -> Matrix<Double> {
public func elmul(_ x: Matrix<Double>, _ y: Matrix<Double>) -> Matrix<Double> {
precondition(x.rows == y.rows && x.columns == y.columns, "Matrix must have the same dimensions")
var result = Matrix<Double>(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<Float>, _ y: Matrix<Float>) -> Matrix<Float> {
precondition(x.rows == y.rows && x.columns == y.columns, "Matrix must have the same dimensions")
var result = Matrix<Float>(rows: x.rows, columns: x.columns, repeatedValue: 0.0)
result.grid = x.grid * y.grid
result.grid = x.grid .* y.grid
return result
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/Surge/Statistics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public func linregress<X: UnsafeMemoryAccessible, Y: UnsafeMemoryAccessible>(_ 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)
Expand All @@ -256,7 +256,7 @@ public func linregress<X: UnsafeMemoryAccessible, Y: UnsafeMemoryAccessible>(_ 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)
Expand Down

1 comment on commit 1e96fab

@johndpope
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @morpheby - I found this broken code - morpheby/Seconn-Swift#1
is there a simple fix for this syntax? thanks in advance.
https://github.com/morpheby/Seconn-Swift/blob/master/Sources/Seconn/HiddenLayer.swift#L117

Please sign in to comment.