<!--
{
  "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/SparseSolve(_:_:_:_:)-9yhgp",
  "metadataVersion" : "0.1.0",
  "role" : "Function",
  "symbol" : {
    "kind" : "Function",
    "modules" : [
      "Accelerate"
    ],
    "preciseIdentifier" : "c:@F@SparseSolve#$@SA@SparseIterativeMethod#BFv(#b#$@E@CBLAS_TRANSPOSE#$@SA@DenseVector_Float#S4_)#S4_#S4_#"
  },
  "title" : "SparseSolve(_:_:_:_:)"
}
-->

# SparseSolve(_:_:_:_:)

Solves the equation *Ax = b* for vectors of single-precision values, treating *A* as an operator and using the specified iterative method.

```
func SparseSolve(_ method: SparseIterativeMethod, _ ApplyOperator: @escaping (Bool, CBLAS_TRANSPOSE, DenseVector_Float, DenseVector_Float) -> Void, _ b: DenseVector_Float, _ x: DenseVector_Float) -> SparseIterativeStatus_t
```

## Parameters

`method`

The iterative method.

`ApplyOperator`

The apply operator block to run. The block takes the following parameters:

- accumulate: Indicates whether to perform `y` `+= op(A)`x (if `true`), or `Y = op(A)X` (if `false`).
- trans: Indicates whether `op(A)` is the application of *A* if `CblasNoTrans`, or *Aᵀ* if `CblasTrans`
- x: The vector to multiply.
- y: The vector for accumulating or storing the result.

`b`

The vector *b*.

`x`

The matrix *x*.

## Return Value

A [`SparseIterativeStatus_t`](/documentation/Accelerate/SparseIterativeStatus_t) enumeration that represents the status of the iterative solve.

## Discussion

Use this function to solve a system of linear equations using a factored coefficient matrix. In cases where the matrix *A* isn’t explicitly available or you need control over the multiplication, this function allows you to provide an apply block.

The following figure shows two systems of equations where the coefficient matrix is sparse:

![A mathematical equation that has one set of three simultaneous equations on the left. Each equation has three unknowns. The same set of simultaneous equations appears on the right as a single matrix equation, A x equals B. The single matrix equation consists of a three-by-three matrix multiplied by a three-element column matrix that equals a three-element column matrix.](images/com.apple.accelerate/media-3703930@2x.png)

The following code solves this system using the least squares minimum residual method:

```swift
/// Create the coefficient matrix _A_.
let rowIndices: [Int32] =    [ 0,  1, 1,  2]
let columnIndices: [Int32] = [ 2,  0, 2,  1]
let aValues: [Float] =      [10, 20, 5, 50]

let A = SparseConvertFromCoordinate(3, 3,
                                    4, 1,
                                    SparseAttributes_t(),
                                    rowIndices, columnIndices,
                                    aValues)
defer {
    SparseCleanup(A)
}

/// Create the right-hand-side vector, _b_.
var bValues: [Float] = [30, 35, 100]
var xValues = [Float](repeating: .nan, count: bValues.count)

/// Create the apply operator block.
func applyOperator(accumulate: Bool,
                   trans: CBLAS_TRANSPOSE,
                   x: DenseVector_Float,
                   y: DenseVector_Float) {

    switch(accumulate, trans == CblasTrans) {
        case (false, false):
            SparseMultiply(A, x, y)
        case (false, true):
            SparseMultiply(SparseGetTranspose(A), x, y)
        case (true, false):
            SparseMultiplyAdd(A, x, y)
        case (true, true):
            SparseMultiplyAdd(SparseGetTranspose(A), x, y)
    }
}

bValues.withUnsafeMutableBufferPointer { bPtr in
    xValues.withUnsafeMutableBufferPointer { xPtr in
        
        let b = DenseVector_Float(count: 3,
                                  data: bPtr.baseAddress!)
        
        let x = DenseVector_Float(count: 3,
                                  data: xPtr.baseAddress!)
        
        SparseSolve(SparseLSMR(),
                    applyOperator,
                    b, x)
    }
}
```

On return, x`Values` contains the values `[1.0, 2.0, 3.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)