Unified generic parameter names

This commit is contained in:
Vincent Esche 2019-10-05 15:08:41 +02:00
parent 0cb5f892ab
commit 5cb86b9e3f
3 changed files with 148 additions and 148 deletions

View File

@ -526,19 +526,19 @@ func remainderInPlace<L: UnsafeMutableMemoryAccessible, R: UnsafeMemoryAccessibl
// MARK: - Exponential
/// - Warning: does not support memory stride (assumes stride is 1).
public func exp<X: UnsafeMemoryAccessible>(_ lhs: X) -> [Float] where X.Element == Float {
public func exp<L: UnsafeMemoryAccessible>(_ lhs: L) -> [Float] where L.Element == Float {
return withArray(from: lhs) { expInPlace(&$0) }
}
/// - Warning: does not support memory stride (assumes stride is 1).
public func exp<X: UnsafeMemoryAccessible>(_ lhs: X) -> [Double] where X.Element == Double {
public func exp<L: UnsafeMemoryAccessible>(_ lhs: L) -> [Double] where L.Element == Double {
return withArray(from: lhs) { expInPlace(&$0) }
}
// MARK: - Exponential: In Place
/// - Warning: does not support memory stride (assumes stride is 1).
func expInPlace<X: UnsafeMutableMemoryAccessible>(_ lhs: inout X) where X.Element == Float {
func expInPlace<L: UnsafeMutableMemoryAccessible>(_ lhs: inout L) where L.Element == Float {
var elementCount: Int32 = numericCast(lhs.count)
withUnsafeMutableMemory(&lhs) { lm in
precondition(lm.stride == 1, "\(#function) does not support strided memory access")
@ -547,7 +547,7 @@ func expInPlace<X: UnsafeMutableMemoryAccessible>(_ lhs: inout X) where X.Elemen
}
/// - Warning: does not support memory stride (assumes stride is 1).
func expInPlace<X: UnsafeMutableMemoryAccessible>(_ lhs: inout X) where X.Element == Double {
func expInPlace<L: UnsafeMutableMemoryAccessible>(_ lhs: inout L) where L.Element == Double {
var elementCount: Int32 = numericCast(lhs.count)
withUnsafeMutableMemory(&lhs) { lm in
precondition(lm.stride == 1, "\(#function) does not support strided memory access")
@ -558,19 +558,19 @@ func expInPlace<X: UnsafeMutableMemoryAccessible>(_ lhs: inout X) where X.Elemen
// MARK: - Square Exponentiation
/// - Warning: does not support memory stride (assumes stride is 1).
public func exp2<X: UnsafeMemoryAccessible>(_ lhs: X) -> [Float] where X.Element == Float {
public func exp2<L: UnsafeMemoryAccessible>(_ lhs: L) -> [Float] where L.Element == Float {
return withArray(from: lhs) { exp2InPlace(&$0) }
}
/// - Warning: does not support memory stride (assumes stride is 1).
public func exp2<X: UnsafeMemoryAccessible>(_ lhs: X) -> [Double] where X.Element == Double {
public func exp2<L: UnsafeMemoryAccessible>(_ lhs: L) -> [Double] where L.Element == Double {
return withArray(from: lhs) { exp2InPlace(&$0) }
}
// MARK: - Square Exponentiation: In Place
/// - Warning: does not support memory stride (assumes stride is 1).
func exp2InPlace<X: UnsafeMutableMemoryAccessible>(_ lhs: inout X) where X.Element == Float {
func exp2InPlace<L: UnsafeMutableMemoryAccessible>(_ lhs: inout L) where L.Element == Float {
var elementCount: Int32 = numericCast(lhs.count)
withUnsafeMutableMemory(&lhs) { lm in
precondition(lm.stride == 1, "\(#function) does not support strided memory access")
@ -579,7 +579,7 @@ func exp2InPlace<X: UnsafeMutableMemoryAccessible>(_ lhs: inout X) where X.Eleme
}
/// - Warning: does not support memory stride (assumes stride is 1).
func exp2InPlace<X: UnsafeMutableMemoryAccessible>(_ lhs: inout X) where X.Element == Double {
func exp2InPlace<L: UnsafeMutableMemoryAccessible>(_ lhs: inout L) where L.Element == Double {
var elementCount: Int32 = numericCast(lhs.count)
withUnsafeMutableMemory(&lhs) { lm in
precondition(lm.stride == 1, "\(#function) does not support strided memory access")
@ -590,29 +590,29 @@ func exp2InPlace<X: UnsafeMutableMemoryAccessible>(_ lhs: inout X) where X.Eleme
// MARK: - Power
/// - Warning: does not support memory stride (assumes stride is 1).
public func pow<X: UnsafeMemoryAccessible, Y: UnsafeMemoryAccessible>(_ lhs: X, _ rhs: Y) -> [Float] where X.Element == Float, Y.Element == Float {
public func pow<L: UnsafeMemoryAccessible, R: UnsafeMemoryAccessible>(_ lhs: L, _ rhs: R) -> [Float] where L.Element == Float, R.Element == Float {
return withArray(from: lhs) { powInPlace(&$0, rhs) }
}
/// - Warning: does not support memory stride (assumes stride is 1).
public func pow<X: UnsafeMemoryAccessible, Y: UnsafeMemoryAccessible>(_ lhs: X, _ rhs: Y) -> [Double] where X.Element == Double, Y.Element == Double {
public func pow<L: UnsafeMemoryAccessible, R: UnsafeMemoryAccessible>(_ lhs: L, _ rhs: R) -> [Double] where L.Element == Double, R.Element == Double {
return withArray(from: lhs) { powInPlace(&$0, rhs) }
}
/// - Warning: Allocates a temporary array from `rhs` via `Array(repeating: rhs, count: lhs.count)`.
public func pow<X: UnsafeMemoryAccessible>(_ lhs: X, _ rhs: Float) -> [Float] where X.Element == Float {
public func pow<L: UnsafeMemoryAccessible>(_ lhs: L, _ rhs: Float) -> [Float] where L.Element == Float {
return withArray(from: lhs) { powInPlace(&$0, rhs) }
}
/// - Warning: Allocates a temporary array from `rhs` via `Array(repeating: rhs, count: lhs.count)`.
public func pow<X: UnsafeMemoryAccessible>(_ lhs: X, _ rhs: Double) -> [Double] where X.Element == Double {
public func pow<L: UnsafeMemoryAccessible>(_ lhs: L, _ rhs: Double) -> [Double] where L.Element == Double {
return withArray(from: lhs) { powInPlace(&$0, rhs) }
}
// MARK: - Power: In Place
/// - Warning: does not support memory stride (assumes stride is 1).
func powInPlace<X: UnsafeMutableMemoryAccessible, Y: UnsafeMemoryAccessible>(_ lhs: inout X, _ rhs: Y) where X.Element == Float, Y.Element == Float {
func powInPlace<L: UnsafeMutableMemoryAccessible, R: UnsafeMemoryAccessible>(_ lhs: inout L, _ rhs: R) where L.Element == Float, R.Element == Float {
precondition(lhs.count == rhs.count, "Collections must have the same size")
var elementCount: Int32 = numericCast(lhs.count)
withUnsafeMutableMemory(&lhs) { lm in
@ -625,7 +625,7 @@ func powInPlace<X: UnsafeMutableMemoryAccessible, Y: UnsafeMemoryAccessible>(_ l
}
/// - Warning: does not support memory stride (assumes stride is 1).
func powInPlace<X: UnsafeMutableMemoryAccessible, Y: UnsafeMemoryAccessible>(_ lhs: inout X, _ rhs: Y) where X.Element == Double, Y.Element == Double {
func powInPlace<L: UnsafeMutableMemoryAccessible, R: UnsafeMemoryAccessible>(_ lhs: inout L, _ rhs: R) where L.Element == Double, R.Element == Double {
precondition(lhs.count == rhs.count, "Collections must have the same size")
var elementCount: Int32 = numericCast(lhs.count)
withUnsafeMutableMemory(&lhs) { lm in
@ -638,13 +638,13 @@ func powInPlace<X: UnsafeMutableMemoryAccessible, Y: UnsafeMemoryAccessible>(_ l
}
/// - Warning: Allocates a temporary array from `rhs` via `Array(repeating: rhs, count: lhs.count)`.
func powInPlace<X: UnsafeMutableMemoryAccessible>(_ lhs: inout X, _ rhs: Float) where X.Element == Float {
func powInPlace<L: UnsafeMutableMemoryAccessible>(_ lhs: inout L, _ rhs: Float) where L.Element == Float {
let rhs = Array(repeating: rhs, count: lhs.count)
return powInPlace(&lhs, rhs)
}
/// - Warning: Allocates a temporary array from `rhs` via `Array(repeating: rhs, count: lhs.count)`.
func powInPlace<X: UnsafeMutableMemoryAccessible>(_ lhs: inout X, _ rhs: Double) where X.Element == Double {
func powInPlace<L: UnsafeMutableMemoryAccessible>(_ lhs: inout L, _ rhs: Double) where L.Element == Double {
let rhs = Array(repeating: rhs, count: lhs.count)
return powInPlace(&lhs, rhs)
}

View File

@ -25,12 +25,12 @@ import Accelerate
/// Elemen-wise absolute value.
///
/// - Warning: does not support memory stride (assumes stride is 1).
public func abs<C: UnsafeMemoryAccessible>(_ x: C) -> [Double] where C.Element == Double {
var results = [Double](repeating: 0.0, count: numericCast(x.count))
public func abs<L: UnsafeMemoryAccessible>(_ lhs: L) -> [Double] where L.Element == Double {
var results = [Double](repeating: 0.0, count: numericCast(lhs.count))
results.withUnsafeMutableBufferPointer { rbp in
x.withUnsafeMemory { xm in
precondition(xm.stride == 1, "\(#function) does not support strided memory access")
vvfabs(rbp.baseAddress!, xm.pointer, [numericCast(xm.count)])
lhs.withUnsafeMemory { lm in
precondition(lm.stride == 1, "\(#function) does not support strided memory access")
vvfabs(rbp.baseAddress!, lm.pointer, [numericCast(lm.count)])
}
}
return results
@ -39,12 +39,12 @@ public func abs<C: UnsafeMemoryAccessible>(_ x: C) -> [Double] where C.Element =
/// Elemen-wise absolute value.
///
/// - Warning: does not support memory stride (assumes stride is 1).
public func abs<C: UnsafeMemoryAccessible>(_ x: C) -> [Float] where C.Element == Float {
var results = [Float](repeating: 0.0, count: numericCast(x.count))
public func abs<L: UnsafeMemoryAccessible>(_ lhs: L) -> [Float] where L.Element == Float {
var results = [Float](repeating: 0.0, count: numericCast(lhs.count))
results.withUnsafeMutableBufferPointer { rbp in
x.withUnsafeMemory { xm in
precondition(xm.stride == 1, "\(#function) does not support strided memory access")
vvfabsf(rbp.baseAddress!, xm.pointer, [numericCast(xm.count)])
lhs.withUnsafeMemory { lm in
precondition(lm.stride == 1, "\(#function) does not support strided memory access")
vvfabsf(rbp.baseAddress!, lm.pointer, [numericCast(lm.count)])
}
}
return results
@ -55,12 +55,12 @@ public func abs<C: UnsafeMemoryAccessible>(_ x: C) -> [Float] where C.Element ==
/// Elemen-wise ceiling.
///
/// - Warning: does not support memory stride (assumes stride is 1).
public func ceil<C: UnsafeMemoryAccessible>(_ x: C) -> [Float] where C.Element == Float {
var results = [Float](repeating: 0.0, count: numericCast(x.count))
public func ceil<L: UnsafeMemoryAccessible>(_ lhs: L) -> [Float] where L.Element == Float {
var results = [Float](repeating: 0.0, count: numericCast(lhs.count))
results.withUnsafeMutableBufferPointer { rbp in
x.withUnsafeMemory { xm in
precondition(xm.stride == 1, "\(#function) does not support strided memory access")
vvceilf(rbp.baseAddress!, xm.pointer, [numericCast(xm.count)])
lhs.withUnsafeMemory { lm in
precondition(lm.stride == 1, "\(#function) does not support strided memory access")
vvceilf(rbp.baseAddress!, lm.pointer, [numericCast(lm.count)])
}
}
return results
@ -69,12 +69,12 @@ public func ceil<C: UnsafeMemoryAccessible>(_ x: C) -> [Float] where C.Element =
/// Elemen-wise ceiling.
///
/// - Warning: does not support memory stride (assumes stride is 1).
public func ceil<C: UnsafeMemoryAccessible>(_ x: C) -> [Double] where C.Element == Double {
var results = [Double](repeating: 0.0, count: numericCast(x.count))
public func ceil<L: UnsafeMemoryAccessible>(_ lhs: L) -> [Double] where L.Element == Double {
var results = [Double](repeating: 0.0, count: numericCast(lhs.count))
results.withUnsafeMutableBufferPointer { rbp in
x.withUnsafeMemory { xm in
precondition(xm.stride == 1, "\(#function) does not support strided memory access")
vvceil(rbp.baseAddress!, xm.pointer, [numericCast(xm.count)])
lhs.withUnsafeMemory { lm in
precondition(lm.stride == 1, "\(#function) does not support strided memory access")
vvceil(rbp.baseAddress!, lm.pointer, [numericCast(lm.count)])
}
}
return results
@ -82,28 +82,28 @@ public func ceil<C: UnsafeMemoryAccessible>(_ x: C) -> [Double] where C.Element
// MARK: - Clip
public func clip<C: UnsafeMemoryAccessible>(_ x: C, low: Float, high: Float) -> [Float] where C.Element == Float {
var results = [Float](repeating: 0.0, count: numericCast(x.count))
public func clip<L: UnsafeMemoryAccessible>(_ lhs: L, low: Float, high: Float) -> [Float] where L.Element == Float {
var results = [Float](repeating: 0.0, count: numericCast(lhs.count))
results.withUnsafeMutableBufferPointer { rbp in
x.withUnsafeMemory { xm in
lhs.withUnsafeMemory { lm in
var y = low
var z = high
withUnsafePointers(&y, &z) { y, z in
vDSP_vclip(xm.pointer, numericCast(xm.stride), y, z, rbp.baseAddress!, 1, numericCast(xm.count))
vDSP_vclip(lm.pointer, numericCast(lm.stride), y, z, rbp.baseAddress!, 1, numericCast(lm.count))
}
}
}
return results
}
public func clip<C: UnsafeMemoryAccessible>(_ x: C, low: Double, high: Double) -> [Double] where C.Element == Double {
var results = [Double](repeating: 0.0, count: numericCast(x.count))
public func clip<L: UnsafeMemoryAccessible>(_ lhs: L, low: Double, high: Double) -> [Double] where L.Element == Double {
var results = [Double](repeating: 0.0, count: numericCast(lhs.count))
results.withUnsafeMutableBufferPointer { rbp in
x.withUnsafeMemory { xm in
lhs.withUnsafeMemory { lm in
var y = low
var z = high
withUnsafePointers(&y, &z) { y, z in
vDSP_vclipD(xm.pointer, numericCast(xm.stride), y, z, rbp.baseAddress!, 1, numericCast(xm.count))
vDSP_vclipD(lm.pointer, numericCast(lm.stride), y, z, rbp.baseAddress!, 1, numericCast(lm.count))
}
}
}
@ -141,12 +141,12 @@ public func copysign<S: UnsafeMemoryAccessible, M: UnsafeMemoryAccessible>(sign:
/// Elemen-wise floor.
///
/// - Warning: does not support memory stride (assumes stride is 1).
public func floor<C: UnsafeMemoryAccessible>(_ x: C) -> [Float] where C.Element == Float {
var results = [Float](repeating: 0.0, count: numericCast(x.count))
public func floor<L: UnsafeMemoryAccessible>(_ lhs: L) -> [Float] where L.Element == Float {
var results = [Float](repeating: 0.0, count: numericCast(lhs.count))
results.withUnsafeMutableBufferPointer { rbp in
x.withUnsafeMemory { xm in
precondition(xm.stride == 1, "\(#function) does not support strided memory access")
vvfloorf(rbp.baseAddress!, xm.pointer, [numericCast(xm.count)])
lhs.withUnsafeMemory { lm in
precondition(lm.stride == 1, "\(#function) does not support strided memory access")
vvfloorf(rbp.baseAddress!, lm.pointer, [numericCast(lm.count)])
}
}
return results
@ -155,12 +155,12 @@ public func floor<C: UnsafeMemoryAccessible>(_ x: C) -> [Float] where C.Element
/// Elemen-wise floor.
///
/// - Warning: does not support memory stride (assumes stride is 1).
public func floor<C: UnsafeMemoryAccessible>(_ x: C) -> [Double] where C.Element == Double {
var results = [Double](repeating: 0.0, count: numericCast(x.count))
public func floor<L: UnsafeMemoryAccessible>(_ lhs: L) -> [Double] where L.Element == Double {
var results = [Double](repeating: 0.0, count: numericCast(lhs.count))
results.withUnsafeMutableBufferPointer { rbp in
x.withUnsafeMemory { xm in
precondition(xm.stride == 1, "\(#function) does not support strided memory access")
vvfloor(rbp.baseAddress!, xm.pointer, [numericCast(xm.count)])
lhs.withUnsafeMemory { lm in
precondition(lm.stride == 1, "\(#function) does not support strided memory access")
vvfloor(rbp.baseAddress!, lm.pointer, [numericCast(lm.count)])
}
}
return results
@ -168,21 +168,21 @@ public func floor<C: UnsafeMemoryAccessible>(_ x: C) -> [Double] where C.Element
// MARK: - Negate
public func neg<C: UnsafeMemoryAccessible>(_ x: C) -> [Float] where C.Element == Float {
var results = [Float](repeating: 0.0, count: numericCast(x.count))
public func neg<L: UnsafeMemoryAccessible>(_ lhs: L) -> [Float] where L.Element == Float {
var results = [Float](repeating: 0.0, count: numericCast(lhs.count))
results.withUnsafeMutableBufferPointer { rbp in
x.withUnsafeMemory { xm in
vDSP_vneg(xm.pointer, numericCast(xm.stride), rbp.baseAddress!, 1, numericCast(xm.count))
lhs.withUnsafeMemory { lm in
vDSP_vneg(lm.pointer, numericCast(lm.stride), rbp.baseAddress!, 1, numericCast(lm.count))
}
}
return results
}
public func neg<C: UnsafeMemoryAccessible>(_ x: C) -> [Double] where C.Element == Double {
var results = [Double](repeating: 0.0, count: numericCast(x.count))
public func neg<L: UnsafeMemoryAccessible>(_ lhs: L) -> [Double] where L.Element == Double {
var results = [Double](repeating: 0.0, count: numericCast(lhs.count))
results.withUnsafeMutableBufferPointer { rbp in
x.withUnsafeMemory { xm in
vDSP_vnegD(xm.pointer, numericCast(xm.stride), rbp.baseAddress!, 1, numericCast(xm.count))
lhs.withUnsafeMemory { lm in
vDSP_vnegD(lm.pointer, numericCast(lm.stride), rbp.baseAddress!, 1, numericCast(lm.count))
}
}
return results
@ -191,24 +191,24 @@ public func neg<C: UnsafeMemoryAccessible>(_ x: C) -> [Double] where C.Element =
// MARK: - Reciprocal
/// - Warning: does not support memory stride (assumes stride is 1).
public func rec<C: UnsafeMemoryAccessible>(_ x: C) -> [Float] where C.Element == Float {
var results = [Float](repeating: 0.0, count: numericCast(x.count))
public func rec<L: UnsafeMemoryAccessible>(_ lhs: L) -> [Float] where L.Element == Float {
var results = [Float](repeating: 0.0, count: numericCast(lhs.count))
results.withUnsafeMutableBufferPointer { rbp in
x.withUnsafeMemory { xm in
precondition(xm.stride == 1, "\(#function) does not support strided memory access")
vvrecf(rbp.baseAddress!, xm.pointer, [numericCast(xm.count)])
lhs.withUnsafeMemory { lm in
precondition(lm.stride == 1, "\(#function) does not support strided memory access")
vvrecf(rbp.baseAddress!, lm.pointer, [numericCast(lm.count)])
}
}
return results
}
/// - Warning: does not support memory stride (assumes stride is 1).
public func rec<C: UnsafeMemoryAccessible>(_ x: C) -> [Double] where C.Element == Double {
var results = [Double](repeating: 0.0, count: numericCast(x.count))
public func rec<L: UnsafeMemoryAccessible>(_ lhs: L) -> [Double] where L.Element == Double {
var results = [Double](repeating: 0.0, count: numericCast(lhs.count))
results.withUnsafeMutableBufferPointer { rbp in
x.withUnsafeMemory { xm in
precondition(xm.stride == 1, "\(#function) does not support strided memory access")
vvrec(rbp.baseAddress!, xm.pointer, [numericCast(xm.count)])
lhs.withUnsafeMemory { lm in
precondition(lm.stride == 1, "\(#function) does not support strided memory access")
vvrec(rbp.baseAddress!, lm.pointer, [numericCast(lm.count)])
}
}
return results
@ -217,24 +217,24 @@ public func rec<C: UnsafeMemoryAccessible>(_ x: C) -> [Double] where C.Element =
// MARK: - Round
/// - Warning: does not support memory stride (assumes stride is 1).
public func round<C: UnsafeMemoryAccessible>(_ x: C) -> [Float] where C.Element == Float {
var results = [Float](repeating: 0.0, count: numericCast(x.count))
public func round<L: UnsafeMemoryAccessible>(_ lhs: L) -> [Float] where L.Element == Float {
var results = [Float](repeating: 0.0, count: numericCast(lhs.count))
results.withUnsafeMutableBufferPointer { rbp in
x.withUnsafeMemory { xm in
precondition(xm.stride == 1, "\(#function) does not support strided memory access")
vvnintf(rbp.baseAddress!, xm.pointer, [numericCast(xm.count)])
lhs.withUnsafeMemory { lm in
precondition(lm.stride == 1, "\(#function) does not support strided memory access")
vvnintf(rbp.baseAddress!, lm.pointer, [numericCast(lm.count)])
}
}
return results
}
/// - Warning: does not support memory stride (assumes stride is 1).
public func round<C: UnsafeMemoryAccessible>(_ x: C) -> [Double] where C.Element == Double {
var results = [Double](repeating: 0.0, count: numericCast(x.count))
public func round<L: UnsafeMemoryAccessible>(_ lhs: L) -> [Double] where L.Element == Double {
var results = [Double](repeating: 0.0, count: numericCast(lhs.count))
results.withUnsafeMutableBufferPointer { rbp in
x.withUnsafeMemory { xm in
precondition(xm.stride == 1, "\(#function) does not support strided memory access")
vvnint(rbp.baseAddress!, xm.pointer, [numericCast(xm.count)])
lhs.withUnsafeMemory { lm in
precondition(lm.stride == 1, "\(#function) does not support strided memory access")
vvnint(rbp.baseAddress!, lm.pointer, [numericCast(lm.count)])
}
}
return results
@ -242,26 +242,26 @@ public func round<C: UnsafeMemoryAccessible>(_ x: C) -> [Double] where C.Element
// MARK: - Threshold
public func threshold<C: UnsafeMemoryAccessible>(_ x: C, low: Float) -> [Float] where C.Element == Float {
var results = [Float](repeating: 0.0, count: numericCast(x.count))
public func threshold<L: UnsafeMemoryAccessible>(_ lhs: L, low: Float) -> [Float] where L.Element == Float {
var results = [Float](repeating: 0.0, count: numericCast(lhs.count))
results.withUnsafeMutableBufferPointer { rbp in
x.withUnsafeMemory { xm in
lhs.withUnsafeMemory { lm in
var y = low
withUnsafePointer(to: &y) { y in
vDSP_vthr(xm.pointer, numericCast(xm.stride), y, rbp.baseAddress!, 1, numericCast(xm.count))
vDSP_vthr(lm.pointer, numericCast(lm.stride), y, rbp.baseAddress!, 1, numericCast(lm.count))
}
}
}
return results
}
public func threshold<C: UnsafeMemoryAccessible>(_ x: C, low: Double) -> [Double] where C.Element == Double {
var results = [Double](repeating: 0.0, count: numericCast(x.count))
public func threshold<L: UnsafeMemoryAccessible>(_ lhs: L, low: Double) -> [Double] where L.Element == Double {
var results = [Double](repeating: 0.0, count: numericCast(lhs.count))
results.withUnsafeMutableBufferPointer { rbp in
x.withUnsafeMemory { xm in
lhs.withUnsafeMemory { lm in
var y = low
withUnsafePointer(to: &y) { y in
vDSP_vthrD(xm.pointer, numericCast(xm.stride), y, rbp.baseAddress!, 1, numericCast(xm.count))
vDSP_vthrD(lm.pointer, numericCast(lm.stride), y, rbp.baseAddress!, 1, numericCast(lm.count))
}
}
}
@ -271,24 +271,24 @@ public func threshold<C: UnsafeMemoryAccessible>(_ x: C, low: Double) -> [Double
// MARK: - Truncate
/// - Warning: does not support memory stride (assumes stride is 1).
public func trunc<C: UnsafeMemoryAccessible>(_ x: C) -> [Float] where C.Element == Float {
var results = [Float](repeating: 0.0, count: numericCast(x.count))
public func trunc<L: UnsafeMemoryAccessible>(_ lhs: L) -> [Float] where L.Element == Float {
var results = [Float](repeating: 0.0, count: numericCast(lhs.count))
results.withUnsafeMutableBufferPointer { rbp in
x.withUnsafeMemory { xm in
precondition(xm.stride == 1, "\(#function) does not support strided memory access")
vvintf(rbp.baseAddress!, xm.pointer, [numericCast(xm.count)])
lhs.withUnsafeMemory { lm in
precondition(lm.stride == 1, "\(#function) does not support strided memory access")
vvintf(rbp.baseAddress!, lm.pointer, [numericCast(lm.count)])
}
}
return results
}
/// - Warning: does not support memory stride (assumes stride is 1).
public func trunc<C: UnsafeMemoryAccessible>(_ x: C) -> [Double] where C.Element == Double {
var results = [Double](repeating: 0.0, count: numericCast(x.count))
public func trunc<L: UnsafeMemoryAccessible>(_ lhs: L) -> [Double] where L.Element == Double {
var results = [Double](repeating: 0.0, count: numericCast(lhs.count))
results.withUnsafeMutableBufferPointer { rbp in
x.withUnsafeMemory { xm in
precondition(xm.stride == 1, "\(#function) does not support strided memory access")
vvint(rbp.baseAddress!, xm.pointer, [numericCast(xm.count)])
lhs.withUnsafeMemory { lm in
precondition(lm.stride == 1, "\(#function) does not support strided memory access")
vvint(rbp.baseAddress!, lm.pointer, [numericCast(lm.count)])
}
}
return results

View File

@ -22,11 +22,11 @@ import Accelerate
// MARK: - Convolution
/// Convolution of a signal [x], with a kernel [k]. The signal must be at least as long as the kernel.
public func conv<X: UnsafeMemoryAccessible, K: UnsafeMemoryAccessible>(_ x: X, _ k: K) -> [Float] where X.Element == Float, K.Element == Float {
precondition(x.count >= k.count, "Input vector [x] must have at least as many elements as the kernel, [k]")
/// Convolution of a signal [lhs], with a kernel [k]. The signal must be at least as long as the kernel.
public func conv<L: UnsafeMemoryAccessible, K: UnsafeMemoryAccessible>(_ lhs: L, _ k: K) -> [Float] where L.Element == Float, K.Element == Float {
precondition(lhs.count >= k.count, "Input vector [lhs] must have at least as many elements as the kernel, [k]")
let resultSize = numericCast(x.count) + numericCast(k.count) - 1
let resultSize = numericCast(lhs.count) + numericCast(k.count) - 1
var result = [Float](repeating: 0, count: resultSize)
result.withUnsafeMutableBufferPointer { rbp in
k.withUnsafeMemory { km in
@ -34,9 +34,9 @@ public func conv<X: UnsafeMemoryAccessible, K: UnsafeMemoryAccessible>(_ x: X, _
let xPad = repeatElement(0 as Float, count: km.count - 1)
var xPadded = [Float]()
xPadded.reserveCapacity(xPad.count + numericCast(x.count) + xPad.count)
xPadded.reserveCapacity(xPad.count + numericCast(lhs.count) + xPad.count)
xPadded.append(contentsOf: xPad)
xPadded.append(contentsOf: x)
xPadded.append(contentsOf: lhs)
xPadded.append(contentsOf: xPad)
vDSP_conv(xPadded, 1, kEnd, -numericCast(km.stride), rbp.baseAddress!, 1, numericCast(resultSize), numericCast(km.count))
@ -45,11 +45,11 @@ public func conv<X: UnsafeMemoryAccessible, K: UnsafeMemoryAccessible>(_ x: X, _
return result
}
/// Convolution of a signal [x], with a kernel [k]. The signal must be at least as long as the kernel.
public func conv<X: UnsafeMemoryAccessible, K: UnsafeMemoryAccessible>(_ x: X, _ k: K) -> [Double] where X.Element == Double, K.Element == Double {
precondition(x.count >= k.count, "Input vector [x] must have at least as many elements as the kernel, [k]")
/// Convolution of a signal [lhs], with a kernel [k]. The signal must be at least as long as the kernel.
public func conv<L: UnsafeMemoryAccessible, K: UnsafeMemoryAccessible>(_ lhs: L, _ k: K) -> [Double] where L.Element == Double, K.Element == Double {
precondition(lhs.count >= k.count, "Input vector [lhs] must have at least as many elements as the kernel, [k]")
let resultSize = numericCast(x.count) + numericCast(k.count) - 1
let resultSize = numericCast(lhs.count) + numericCast(k.count) - 1
var result = [Double](repeating: 0, count: resultSize)
result.withUnsafeMutableBufferPointer { rbp in
k.withUnsafeMemory { km in
@ -57,9 +57,9 @@ public func conv<X: UnsafeMemoryAccessible, K: UnsafeMemoryAccessible>(_ x: X, _
let xPad = repeatElement(0 as Double, count: km.count - 1)
var xPadded = [Double]()
xPadded.reserveCapacity(xPad.count + numericCast(x.count) + xPad.count)
xPadded.reserveCapacity(xPad.count + numericCast(lhs.count) + xPad.count)
xPadded.append(contentsOf: xPad)
xPadded.append(contentsOf: x)
xPadded.append(contentsOf: lhs)
xPadded.append(contentsOf: xPad)
vDSP_convD(xPadded, 1, kEnd, -numericCast(km.stride), rbp.baseAddress!, 1, numericCast(resultSize), numericCast(km.count))
@ -70,24 +70,24 @@ public func conv<X: UnsafeMemoryAccessible, K: UnsafeMemoryAccessible>(_ x: X, _
// MARK: - Cross-Correlation
/// Cross-correlation of a signal [x], with another signal [y]. The signal [y]
/// is padded so that it is the same length as [x].
public func xcorr<X: UnsafeMemoryAccessible, Y: UnsafeMemoryAccessible>(_ x: X, _ y: Y) -> [Float] where X.Element == Float, Y.Element == Float {
precondition(x.count >= y.count, "Input vector [x] must have at least as many elements as [y]")
var yPadded = [Float](y)
if x.count > y.count {
let padding = repeatElement(0 as Float, count: numericCast(x.count) - numericCast(y.count))
/// Cross-correlation of a signal [lhs], with another signal [rhs]. The signal [rhs]
/// is padded so that it is the same length as [lhs].
public func xcorr<L: UnsafeMemoryAccessible, Y: UnsafeMemoryAccessible>(_ lhs: L, _ rhs: Y) -> [Float] where L.Element == Float, Y.Element == Float {
precondition(lhs.count >= rhs.count, "Input vector [lhs] must have at least as many elements as [rhs]")
var yPadded = [Float](rhs)
if lhs.count > rhs.count {
let padding = repeatElement(0 as Float, count: numericCast(lhs.count) - numericCast(rhs.count))
yPadded.append(contentsOf: padding)
}
let resultSize = numericCast(x.count) + yPadded.count - 1
let resultSize = numericCast(lhs.count) + yPadded.count - 1
var result = [Float](repeating: 0, count: resultSize)
let xPad = repeatElement(0 as Float, count: yPadded.count-1)
var xPadded = [Float]()
xPadded.reserveCapacity(xPad.count + numericCast(x.count) + xPad.count)
xPadded.reserveCapacity(xPad.count + numericCast(lhs.count) + xPad.count)
xPadded.append(contentsOf: xPad)
xPadded.append(contentsOf: x)
xPadded.append(contentsOf: lhs)
xPadded.append(contentsOf: xPad)
result.withUnsafeMutableBufferPointer { rbp in
@ -97,24 +97,24 @@ public func xcorr<X: UnsafeMemoryAccessible, Y: UnsafeMemoryAccessible>(_ x: X,
return result
}
/// Cross-correlation of a signal [x], with another signal [y]. The signal [y]
/// is padded so that it is the same length as [x].
public func xcorr<X: UnsafeMemoryAccessible, Y: UnsafeMemoryAccessible>(_ x: X, _ y: Y) -> [Double] where X.Element == Double, Y.Element == Double {
precondition(x.count >= y.count, "Input vector [x] must have at least as many elements as [y]")
var yPadded = [Double](y)
if x.count > y.count {
let padding = repeatElement(0 as Double, count: numericCast(x.count) - numericCast(y.count))
/// Cross-correlation of a signal [lhs], with another signal [rhs]. The signal [rhs]
/// is padded so that it is the same length as [lhs].
public func xcorr<L: UnsafeMemoryAccessible, Y: UnsafeMemoryAccessible>(_ lhs: L, _ rhs: Y) -> [Double] where L.Element == Double, Y.Element == Double {
precondition(lhs.count >= rhs.count, "Input vector [lhs] must have at least as many elements as [rhs]")
var yPadded = [Double](rhs)
if lhs.count > rhs.count {
let padding = repeatElement(0 as Double, count: numericCast(lhs.count) - numericCast(rhs.count))
yPadded.append(contentsOf: padding)
}
let resultSize = numericCast(x.count) + yPadded.count - 1
let resultSize = numericCast(lhs.count) + yPadded.count - 1
var result = [Double](repeating: 0, count: resultSize)
let xPad = repeatElement(0 as Double, count: yPadded.count-1)
var xPadded = [Double]()
xPadded.reserveCapacity(xPad.count + numericCast(x.count) + xPad.count)
xPadded.reserveCapacity(xPad.count + numericCast(lhs.count) + xPad.count)
xPadded.append(contentsOf: xPad)
xPadded.append(contentsOf: x)
xPadded.append(contentsOf: lhs)
xPadded.append(contentsOf: xPad)
result.withUnsafeMutableBufferPointer { rbp in
@ -126,42 +126,42 @@ public func xcorr<X: UnsafeMemoryAccessible, Y: UnsafeMemoryAccessible>(_ x: X,
// MARK: - Auto-correlation
/// Auto-correlation of a signal [x]
public func xcorr<X: UnsafeMemoryAccessible>(_ x: X) -> [Float] where X.Element == Float {
let resultSize = 2*numericCast(x.count) - 1
/// Auto-correlation of a signal [lhs]
public func xcorr<L: UnsafeMemoryAccessible>(_ lhs: L) -> [Float] where L.Element == Float {
let resultSize = 2*numericCast(lhs.count) - 1
var result = [Float](repeating: 0, count: resultSize)
let xPad = repeatElement(0 as Float, count: numericCast(x.count) - 1)
let xPad = repeatElement(0 as Float, count: numericCast(lhs.count) - 1)
var xPadded = [Float]()
xPadded.reserveCapacity(xPad.count + numericCast(x.count) + xPad.count)
xPadded.reserveCapacity(xPad.count + numericCast(lhs.count) + xPad.count)
xPadded.append(contentsOf: xPad)
xPadded.append(contentsOf: x)
xPadded.append(contentsOf: lhs)
xPadded.append(contentsOf: xPad)
x.withUnsafeMemory { xm in
lhs.withUnsafeMemory { lm in
result.withUnsafeMutableBufferPointer { rbp in
vDSP_conv(xPadded, 1, xm.pointer, numericCast(xm.stride), rbp.baseAddress!, 1, numericCast(resultSize), numericCast(xm.count))
vDSP_conv(xPadded, 1, lm.pointer, numericCast(lm.stride), rbp.baseAddress!, 1, numericCast(resultSize), numericCast(lm.count))
}
}
return result
}
/// Auto-correlation of a signal [x]
public func xcorr<X: UnsafeMemoryAccessible>(_ x: X) -> [Double] where X.Element == Double {
let resultSize = 2*numericCast(x.count) - 1
/// Auto-correlation of a signal [lhs]
public func xcorr<L: UnsafeMemoryAccessible>(_ lhs: L) -> [Double] where L.Element == Double {
let resultSize = 2*numericCast(lhs.count) - 1
var result = [Double](repeating: 0, count: resultSize)
let xPad = repeatElement(0 as Double, count: numericCast(x.count) - 1)
let xPad = repeatElement(0 as Double, count: numericCast(lhs.count) - 1)
var xPadded = [Double]()
xPadded.reserveCapacity(xPad.count + numericCast(x.count) + xPad.count)
xPadded.reserveCapacity(xPad.count + numericCast(lhs.count) + xPad.count)
xPadded.append(contentsOf: xPad)
xPadded.append(contentsOf: x)
xPadded.append(contentsOf: lhs)
xPadded.append(contentsOf: xPad)
x.withUnsafeMemory { xm in
lhs.withUnsafeMemory { lm in
result.withUnsafeMutableBufferPointer { rbp in
vDSP_convD(xPadded, 1, xm.pointer, numericCast(xm.stride), rbp.baseAddress!, 1, numericCast(resultSize), numericCast(xm.count))
vDSP_convD(xPadded, 1, lm.pointer, numericCast(lm.stride), rbp.baseAddress!, 1, numericCast(resultSize), numericCast(lm.count))
}
}