Add `muladd…` to `Vector<Scalar>` and corresponding tests

This commit is contained in:
Vincent Esche 2019-09-24 23:18:05 +02:00
parent 462c1e40dc
commit dcebafd1f3
2 changed files with 102 additions and 0 deletions

View File

@ -257,6 +257,34 @@ public func -= (lhs: inout Vector<Double>, rhs: Double) {
return subInPlace(&lhs, rhs)
}
// MARK: - Multiply Addition
public func muladd(_ lhs: Vector<Float>, _ rhs: Vector<Float>, _ alpha: Float) -> Vector<Float> {
var result = lhs
muladdInPlace(&result, rhs, alpha)
return result
}
public func muladd(_ lhs: Vector<Double>, _ rhs: Vector<Double>, _ alpha: Double) -> Vector<Double> {
var result = lhs
muladdInPlace(&result, rhs, alpha)
return result
}
// MARK: - Multiply Addition: In Place
func muladdInPlace(_ lhs: inout Vector<Float>, _ rhs: Vector<Float>, _ alpha: Float) {
precondition(lhs.dimensions == rhs.dimensions, "Vector dimensions not compatible with addition")
return muladdInPlace(&lhs.scalars, rhs.scalars, alpha)
}
func muladdInPlace(_ lhs: inout Vector<Double>, _ rhs: Vector<Double>, _ alpha: Double) {
precondition(lhs.dimensions == rhs.dimensions, "Vector dimensions not compatible with addition")
return muladdInPlace(&lhs.scalars, rhs.scalars, alpha)
}
// MARK: - Multiplication
public func mul(_ lhs: Vector<Float>, _ rhs: Float) -> Vector<Float> {

View File

@ -323,6 +323,80 @@ class VectorTests: XCTestCase {
XCTAssertEqual(actual, expected, accuracy: 1e-8)
}
// MARK: - Multiply Addition
func test_muladd_vector_vector_double() {
typealias Scalar = Double
let vector: Vector<Scalar> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let scalar: Scalar = 2.0
var actual: Vector<Scalar> = []
measure {
actual = Surge.muladd(vector, vector, scalar)
}
let expected: Vector<Scalar> = [3, 6, 9, 12, 15, 18, 21, 24, 27, 30]
XCTAssertEqual(actual, expected, accuracy: 1e-8)
}
func test_muladd_vector_vector_float() {
typealias Scalar = Float
let vector: Vector<Scalar> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let scalar: Scalar = 2.0
var actual: Vector<Scalar> = []
measure {
actual = Surge.muladd(vector, vector, scalar)
}
let expected: Vector<Scalar> = [3, 6, 9, 12, 15, 18, 21, 24, 27, 30]
XCTAssertEqual(actual, expected, accuracy: 1e-8)
}
// MARK: - Multiply Addition: In Place
func test_muladd_in_place_vector_vector_double() {
typealias Scalar = Double
let vector: Vector<Scalar> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let scalar: Scalar = 2.0
var actual: Vector<Scalar> = []
measureMetrics([.wallClockTime], automaticallyStartMeasuring: false) {
actual = vector
startMeasuring()
Surge.muladdInPlace(&actual, vector, scalar)
stopMeasuring()
}
let expected: Vector<Scalar> = [3, 6, 9, 12, 15, 18, 21, 24, 27, 30]
XCTAssertEqual(actual, expected, accuracy: 1e-8)
}
func test_muladd_in_place_vector_vector_float() {
typealias Scalar = Float
let vector: Vector<Scalar> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let scalar: Scalar = 2.0
var actual: Vector<Scalar> = []
measureMetrics([.wallClockTime], automaticallyStartMeasuring: false) {
actual = vector
startMeasuring()
Surge.muladdInPlace(&actual, vector, scalar)
stopMeasuring()
}
let expected: Vector<Scalar> = [3, 6, 9, 12, 15, 18, 21, 24, 27, 30]
XCTAssertEqual(actual, expected, accuracy: 1e-8)
}
// MARK: - Multiplication
func test_mul_vector_scalar_double() {