Skip to content

Commit f64ae7b

Browse files
committed
Add vDSP.add between vectors
1 parent 19d4987 commit f64ae7b

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ The package is structured as follows:
6666
- [x] `vDSP_vsortD` - Sort a vector
6767
- [x] `vDSP_vrampD` - Ramp a vector
6868
- [ ] `vDSP.sum` - Sum of a vector
69-
- [ ] `vDSP.add` - Add two vectors
69+
- [x] `vDSP.add`:
70+
- [x] `vDSP.add` - Add two vectors
71+
- [x] `vDSP.add` - Add a scalar to a vector
7072
- [ ] `vDSP.subtract` - Subtract two vectors
7173
- [ ] `vDSP.multiply` - Multiply two vectors
7274
- `Statistical` - Statistical operations:

Sources/AccelerateLinux/VectorOps/vDSP+add.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,30 @@ extension vDSP {
2323
}
2424
}
2525
}
26+
27+
/// Returns the double-precision element-wise sum of two vectors.
28+
/// - Parameters:
29+
/// - vectorA: The first input vector, A.
30+
/// - vectorB: The second input vector, B.
31+
/// - Returns: The output vector, C.
32+
public static func add<T, U>(
33+
_ vectorA: T,
34+
_ vectorB: U
35+
) -> [Double] where T: AccelerateBuffer, U: AccelerateBuffer, T.Element == Double, U.Element == Double {
36+
precondition(vectorA.count == vectorB.count, "Vectors must have the same count.")
37+
return .init(unsafeUninitializedCapacity: vectorA.count) { buffer, initializedCount in
38+
vectorA.withUnsafeBufferPointer { vecAPtr in
39+
vectorB.withUnsafeBufferPointer { vecBPtr in
40+
var i = 0
41+
while i < vectorA.count {
42+
buffer[i] = vecAPtr[i] + vecBPtr[i]
43+
i += 1
44+
}
45+
initializedCount = vectorA.count
46+
}
47+
}
48+
}
49+
}
2650
}
2751

2852
#endif

Tests/AccelerateLinuxTests/VectorTests/vDSP+addTests.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,14 @@ struct vDSPaddTests {
1212

1313
#expect(c == [11.0, 22.0, 33.0, 44.0, 55.0])
1414
}
15+
16+
@Test("vDSP.add vector + vector")
17+
func vDSPaddVectorVector() {
18+
let a: [Double] = [1, 2, 3, 4, 5]
19+
let b: [Double] = [10, 20, 30, 40, 50]
20+
21+
let c = vDSP.add(a, b)
22+
23+
#expect(c == [11.0, 22.0, 33.0, 44.0, 55.0])
24+
}
1525
}

0 commit comments

Comments
 (0)