<!--
{
  "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(_:_:_:_:)-6bmr8",
  "metadataVersion" : "0.1.0",
  "role" : "Function",
  "symbol" : {
    "kind" : "Function",
    "modules" : [
      "Accelerate"
    ],
    "preciseIdentifier" : "c:@F@SparseSolve#$@SA@SparseOpaqueFactorization_Float#$@SA@DenseVector_Float#S1_#*v#"
  },
  "title" : "SparseSolve(_:_:_:_:)"
}
-->

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

Solves the system *Ax = b* using the supplied single-precision factorization of *A*, without any internal memory allocations.

```
func SparseSolve(_ Factored: SparseOpaqueFactorization_Float, _ b: DenseVector_Float, _ x: DenseVector_Float, _ workspace: UnsafeMutableRawPointer)
```

## Parameters

`Factored`

The factored matrix to solve.

`b`

The vector *b*.

`x`

The vector *x*.

`workspace`

The scratch space of size [`solveWorkspaceRequiredStatic`](/documentation/Accelerate/SparseOpaqueFactorization_Float/solveWorkspaceRequiredStatic) `+ nrhs *` [`solveWorkspaceRequiredPerRHS`](/documentation/Accelerate/SparseOpaqueFactorization_Float/solveWorkspaceRequiredPerRHS).

## Discussion

Use this function to solve a system of linear equations using a factored coefficient matrix. In cases where your code calls the function frequently, create and manage the workspace that the Sparse Solvers library uses and reuse it across function calls. Reusing a workspace prevents the Sparse Solvers library from allocating the temporary storage with each call.

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-3703896@2x.png)

The following code solves this system with a QR factorization of the coefficient matrix:

```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)

/// Factorize _A_.
let factorization = SparseFactor(SparseFactorizationQR, A)

defer {
    SparseCleanup(A)
    SparseCleanup(factorization)
}

/// Create the workspace.
let byteCount = factorization.solveWorkspaceRequiredStatic +
                    factorization.solveWorkspaceRequiredPerRHS
let workspace = UnsafeMutableRawPointer.allocate(
    byteCount: byteCount,
    alignment: MemoryLayout<Float>.alignment)
defer {
    workspace.deallocate()
}

/// Create the right-hand-side vector, _b_.
var bValues: [Float] = [30, 35, 100]
let n = bValues.count

/// Solve the system.
let xValues = [Float](unsafeUninitializedCapacity: n) {
    buffer, count in
    
    bValues.withUnsafeMutableBufferPointer { bPtr in
        
        let b = DenseVector_Float(count: 3,
                                  data: bPtr.baseAddress!)
        let x = DenseVector_Float(count: 3,
                                  data: buffer.baseAddress!)
        
        SparseSolve(factorization, b, x,
                    workspace)
        
        count = n
    }
}
```

On return, x`Values` contains the values `[1.0, 2.0, 3.0]`.

If the factorization is *A = QR*, the function returns the solution of minimum norm *‖ x ‖₂* for underdetermined systems.

If the factorization is *A = QR*, the function returns the least squares solution *minₓ ‖ AX - B ‖₂* for overdetermined systems.

If the factorization is [`SparseFactorizationCholeskyAtA`](/documentation/Accelerate/SparseFactorizationCholeskyAtA), the factorization is of *AᵀA*, and the solution that returns is for the system *AᵀAX = B*.

---

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)