<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: -",
    "macOS: 10.15.0 -",
    "tvOS: 13.0.0 -",
    "visionOS: -",
    "watchOS: 6.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Accelerate",
  "identifier" : "/documentation/Accelerate/vDSP/addSubtract(_:_:addResult:subtractResult:)-avzd",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "Accelerate"
    ],
    "preciseIdentifier" : "s:10Accelerate4vDSPO11addSubtract__0C6Result08subtractE0yx_q_q0_zq1_ztAA0A6BufferRzAaGR_AA0a7MutableG0R0_AaHR1_Sd7ElementRtzSdAIRt_SdAIRt0_SdAIRt1_r2_lFZ"
  },
  "title" : "addSubtract(_:_:addResult:subtractResult:)"
}
-->

# addSubtract(_:_:addResult:subtractResult:)

Calculates the double-precision element-wise sum and subtraction of two vectors.

```
static func addSubtract<S, T, U, V>(_ vectorA: S, _ vectorB: T, addResult: inout U, subtractResult: inout V) where S : AccelerateBuffer, T : AccelerateBuffer, U : AccelerateMutableBuffer, V : AccelerateMutableBuffer, S.Element == Double, T.Element == Double, U.Element == Double, V.Element == Double
```

## Parameters

`vectorA`

The first input vector, `A`.

`vectorB`

The second input vector, `B`.

`addResult`

The addition output vector, `O0`.

`subtractResult`

The subtraction output vector, `O1`.

## Discussion

This function calculates the addition and subtraction of the first `N` elements of input vectors `A` and `B`, and writes the result to output vector `C`.

```swift
 for (i = 0; i < N; ++i)
 {
     i1 = I1[i*I1S];
     i0 = I0[i*I0S];
     O0[i*O0S] = i0 + i1;
     O1[i*O1S] = i0 - i1;
 }
```

![A diagram showing the operation of this function. There are three rows. The top row represents the input vectors, A and B, with three boxes of each. The middle row represents the operation, addition and subtraction, with three boxes of each. The bottom row represents the output vectors, O0 and O1, as three boxes of each. The diagram has connecting lines from the input vectors to the operations, and from the operations to the output vectors.](images/com.apple.accelerate/media-4336936@2x.png)

The following code shows an example of using this function:

```swift
    let count = 5
    
    let a: [Double] = [10, 20, 30, 40, 50]
    let b: [Double] = [ 1,  2,  3,  4,  5]
    
    var o0 = [Double](repeating: 0, count: count)
    var o1 = [Double](repeating: 0, count: count)
    
    vDSP.addSubtract(a, b, addResult: &o0, subtractResult: &o1)
    
    // Prints the addition result: "[11.0, 22.0, 33.0, 44.0, 55.0]".
    print(o0)
    
    // Prints the subtraction result: "[9.0, 18.0, 27.0, 36.0, 45.0]".
    print(o1)
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)