Skip to content

Commit

Permalink
Add vDSP.add between vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
ptoffy committed Jan 16, 2025
1 parent 19d4987 commit f64ae7b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ The package is structured as follows:
- [x] `vDSP_vsortD` - Sort a vector
- [x] `vDSP_vrampD` - Ramp a vector
- [ ] `vDSP.sum` - Sum of a vector
- [ ] `vDSP.add` - Add two vectors
- [x] `vDSP.add`:
- [x] `vDSP.add` - Add two vectors
- [x] `vDSP.add` - Add a scalar to a vector
- [ ] `vDSP.subtract` - Subtract two vectors
- [ ] `vDSP.multiply` - Multiply two vectors
- `Statistical` - Statistical operations:
Expand Down
24 changes: 24 additions & 0 deletions Sources/AccelerateLinux/VectorOps/vDSP+add.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,30 @@ extension vDSP {
}
}
}

/// Returns the double-precision element-wise sum of two vectors.
/// - Parameters:
/// - vectorA: The first input vector, A.
/// - vectorB: The second input vector, B.
/// - Returns: The output vector, C.
public static func add<T, U>(
_ vectorA: T,
_ vectorB: U
) -> [Double] where T: AccelerateBuffer, U: AccelerateBuffer, T.Element == Double, U.Element == Double {
precondition(vectorA.count == vectorB.count, "Vectors must have the same count.")
return .init(unsafeUninitializedCapacity: vectorA.count) { buffer, initializedCount in
vectorA.withUnsafeBufferPointer { vecAPtr in
vectorB.withUnsafeBufferPointer { vecBPtr in
var i = 0
while i < vectorA.count {
buffer[i] = vecAPtr[i] + vecBPtr[i]
i += 1
}
initializedCount = vectorA.count
}
}
}
}
}

#endif
10 changes: 10 additions & 0 deletions Tests/AccelerateLinuxTests/VectorTests/vDSP+addTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,14 @@ struct vDSPaddTests {

#expect(c == [11.0, 22.0, 33.0, 44.0, 55.0])
}

@Test("vDSP.add vector + vector")
func vDSPaddVectorVector() {
let a: [Double] = [1, 2, 3, 4, 5]
let b: [Double] = [10, 20, 30, 40, 50]

let c = vDSP.add(a, b)

#expect(c == [11.0, 22.0, 33.0, 44.0, 55.0])
}
}

0 comments on commit f64ae7b

Please sign in to comment.