<!--
{
  "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(_:_:_:)-9kraw",
  "metadataVersion" : "0.1.0",
  "role" : "Function",
  "symbol" : {
    "kind" : "Function",
    "modules" : [
      "Accelerate"
    ],
    "preciseIdentifier" : "c:@F@SparseMultiply#$@SA@SparseMatrix_Float#$@SA@DenseMatrix_Float#S1_#"
  },
  "title" : "SparseMultiply(_:_:_:)"
}
-->

# SparseMultiply(_:_:_:)

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

```
func SparseMultiply(_ A: SparseMatrix_Float, _ X: DenseMatrix_Float, _ Y: DenseMatrix_Float)
```

## Parameters

`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

Use this function to multiply a sparse matrix by a dense matrix. The following equation is an example of a matrix-matrix multiplication where the first matrix is sparse:

![A mathematical formula that describes the matrix multiplication, Y equals A X. A four-by-four matrix multiplied by a four-by-two matrix equals a four-by-two matrix.](images/com.apple.accelerate/media-3703076@2x.png)

Call [`SparseMultiply(_:_:_:)`](/documentation/Accelerate/SparseMultiply(_:_:_:)-8d9lh) 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: [Float] = [1.0, 4.0, 13.0, 16.0]

// The sparse matrix `A` in `Y=AX`.
let A = SparseConvertFromCoordinate(rowCount, columnCount,
                                    blockCount, blockSize,
                                    SparseAttributes_t(),
                                    rowIndices, columnIndices,
                                    data)
defer {
    SparseCleanup(A)
}

// The values for `X` in `Y=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=AX`.
let yValues = [Float](unsafeUninitializedCapacity: xValues.count) {
    resultBuffer, count in
    
    xValues.withUnsafeMutableBufferPointer { denseMatrixPtr in
        // The dense matrix `X` in `Y=AX`.
        let X = DenseMatrix_Float(rowCount: 4,
                                  columnCount: 2,
                                  columnStride: 4,
                                  attributes: SparseAttributes_t(),
                                  data: denseMatrixPtr.baseAddress!)
        
        // The dense matrix `Y` in `Y=AX`.
        let Y = DenseMatrix_Float(rowCount: 4,
                                  columnCount: 2,
                                  columnStride: 4,
                                  attributes: SparseAttributes_t(),
                                  data: resultBuffer.baseAddress!)
        
        SparseMultiply(A, X, Y)
    }
    
    count = xValues.count
}

print(yValues) // Prints "[140.0, 0.0, 0.0, 200.0, 1400.0, 0.0, 0.0, 2000.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)