---
title: "SparseMultiplyAdd(_:_:_:_:)"
framework: accelerate
role: symbol
role_heading: Function
path: "accelerate/sparsemultiplyadd(_:_:_:_:)-n61k"
---

# SparseMultiplyAdd(_:_:_:_:)

Performs the multiply operation Y += alpha * AX on a sparse matrix of single-precision, floating-point values.

## Declaration

```swift
func SparseMultiplyAdd(_ alpha: Float, _ A: SparseMatrix_Float, _ X: DenseMatrix_Float, _ Y: DenseMatrix_Float)
```

## Parameters

- `alpha`: The scalar value alpha in Y += alpha * AX.
- `A`: The sparse matrix A in Y += AX.
- `X`: The dense matrix X in Y += AX.
- `Y`: The dense matrix Y in Y += AX.

## Discussion

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

Call SparseMultiplyAdd(_:_:_:_:) to calculate the result. 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: [Float] = [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: [Float] = [10.0, -1.0, -1.0, 10.0,                         100.0, -1.0, -1.0, 100.0]

/// The values for _Y_ in _Y += alpha * AX_. var yValues = [Float](repeating: 1,                       count: xValues.count)

let alpha: Float = 2.0

yValues.withUnsafeMutableBufferPointer { yValuesPtr in     xValues.withUnsafeMutableBufferPointer { denseMatrixPtr in         /// The _X_ in _Y += alpha * AX_.         let X = DenseMatrix_Float(rowCount: 4,                                   columnCount: 2,                                   columnStride: 4,                                   attributes: SparseAttributes_t(),                                   data: denseMatrixPtr.baseAddress!)                  /// The _Y_ in _Y += alpha * AX_.         let Y = DenseMatrix_Float(rowCount: 4,                                   columnCount: 2,                                   columnStride: 4,                                   attributes: SparseAttributes_t(),                                   data: yValuesPtr.baseAddress!)                  SparseMultiplyAdd(alpha, A, X, Y)     } }

// On return, `yValues` contains: //      `[ 281.0, 1.0, 1.0,  401.0, //        2801.0, 1.0, 1.0, 4001.0]`

## See Also

### Multiply-add functions

- [SparseMultiplyAdd(_:_:_:)](accelerate/sparsemultiplyadd(_:_:_:)-lgm5.md)
- [SparseMultiplyAdd(_:_:_:)](accelerate/sparsemultiplyadd(_:_:_:)-8pgpq.md)
- [SparseMultiplyAdd(_:_:_:_:)](accelerate/sparsemultiplyadd(_:_:_:_:)-86gfz.md)
