<!--
{
  "availability" : [
    "iOS: 11.0.0 -",
    "iPadOS: 11.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.13.0 -",
    "tvOS: 11.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 4.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Accelerate",
  "identifier" : "/documentation/Accelerate/SparseMultiply(_:_:_:_:)-76o5l",
  "metadataVersion" : "0.1.0",
  "role" : "Function",
  "symbol" : {
    "kind" : "Function",
    "modules" : [
      "Accelerate"
    ],
    "preciseIdentifier" : "c:@F@SparseMultiply#d#$@SA@SparseMatrix_Double#$@SA@DenseVector_Double#S1_#"
  },
  "title" : "SparseMultiply(_:_:_:_:)"
}
-->

# SparseMultiply(_:_:_:_:)

Performs the multiply operation *y* *= alpha * Ax* on a vector of double-precision, floating-point values.

```
func SparseMultiply(_ alpha: Double, _ A: SparseMatrix_Double, _ x: DenseVector_Double, _ y: DenseVector_Double)
```

## Parameters

`alpha`

The scalar value *alpha* in *y* *= alpha * Ax*.

`A`

The sparse matrix *A* in *y* *= Ax*.

`x`

The dense vector *x* in *y* *= Ax*.

`y`

The dense vector *y* in *y* *= Ax*.

## Discussion

Use this function to multiply a scalar by a sparse matrix, and then by a dense vector. The following equation is an example of a scalar-matrix-vector multiplication where the matrix is sparse:

![A mathematical formula that describes the matrix multiplication, y equals alpha times A x. A scalar value multiplied by a four-by-four matrix multiplied by a four-element column matrix equals a four-element column matrix.](images/com.apple.accelerate/media-3703082@2x.png)

Call [`SparseMultiply(_:_:_:_:)`](/documentation/Accelerate/SparseMultiply(_:_:_:_:)-76o5l) to calculate the result.

```swift
let rowCount = Int32(4)
let columnCount = Int32(4)
let blockCount = 4
let blockSize = UInt8(1)
let rowIndices: [Int32] = [0, 3, 0, 3]
let columnIndices: [Int32] = [0, 0, 3, 3]
let data = [1.0, 4.0, 13.0, 16.0]

/// The _A_ in _y = alpha * Ax_.
let A = SparseConvertFromCoordinate(rowCount, columnCount,
                                    blockCount, blockSize,
                                    SparseAttributes_t(),
                                    rowIndices, columnIndices,
                                    data)
defer {
    SparseCleanup(A)
}

/// The values for _x_ in _y = alpha * Ax_.
var xValues = [10.0, -1.0, -1.0, 10.0]

let alpha = 2.0

/// The values for _y_ in _y = alpha * Ax_.
let yValues = [Double](unsafeUninitializedCapacity: xValues.count) {
    resultBuffer, count in
    
    xValues.withUnsafeMutableBufferPointer { xValuesPtr in
        /// The _x_ in _y = alpha * Ax_.
        let x = DenseVector_Double(count: 4, 
                                   data: xValuesPtr.baseAddress!)
        
        /// The _y_ in _y = alpha * Ax_.
        let y = DenseVector_Double(count: 4,
                                   data: resultBuffer.baseAddress!)
        
        SparseMultiply(alpha, A, x, y)
    }
    
    count = xValues.count
}

/// On return, `yValues` contains:
///      `[ 280.0, 0.0, 0.0,  400.0 ]`
```

---

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)