---
title: "SparseSolve(_:_:_:_:_:)"
framework: accelerate
role: symbol
role_heading: Function
path: "accelerate/sparsesolve(_:_:_:_:_:)-6i1nx"
---

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

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

## Declaration

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

## Parameters

- `method`: The iterative method.
- `ApplyOperator`: The apply operator block to run. The block takes the following parameters:
- `b`: The vector b.
- `x`: The matrix x.
- `Preconditioner`: The preconditioner to apply.

## Return Value

Return Value A SparseIterativeStatus_t enumeration that represents the status of the iterative solve.

## Discussion

Discussion Use this function to solve a system of linear equations using a factored coefficient matrix. Preconditioning the coefficient matrix can reduce the number of iterations the function requires to converge the system. 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:

The following code solves this system by applying a diagonal scaling preconditioner and using the least squares minimum residual method: /// Create the coefficient matrix _A_. let rowIndices: [Int32] =    [ 0,  1, 1,  2] let columnIndices: [Int32] = [ 2,  0, 2,  1] let aValues: [Double] =      [10, 20, 5, 50]

let A = SparseConvertFromCoordinate(3, 3,                                     4, 1,                                     SparseAttributes_t(),                                     rowIndices, columnIndices,                                     aValues) let preconditioner = SparseCreatePreconditioner(SparsePreconditionerDiagScaling,                                                 A)

defer {     SparseCleanup(A)     SparseCleanup(preconditioner) }

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

/// Create the apply operator block. func applyOperator(accumulate: Bool,                    trans: CBLAS_TRANSPOSE,                    x: DenseVector_Double,                    y: DenseVector_Double) {     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_Double(count: 3,                                    data: bPtr.baseAddress!)                  let x = DenseVector_Double(count: 3,                                    data: xPtr.baseAddress!)                  SparseSolve(SparseLSMR(),                     applyOperator,                     b, x,                     preconditioner)     } } On return, xValues contains the values [1.0, 2.0, 3.0].

## See Also

### Iterative sparse solve functions with preconditioner

- [SparseSolve(_:_:_:_:_:)](accelerate/sparsesolve(_:_:_:_:_:)-1qwax.md)
- [SparseSolve(_:_:_:_:_:)](accelerate/sparsesolve(_:_:_:_:_:)-3aphv.md)
- [SparseSolve(_:_:_:_:_:)](accelerate/sparsesolve(_:_:_:_:_:)-5vs11.md)
- [SparseSolve(_:_:_:_:_:)](accelerate/sparsesolve(_:_:_:_:_:)-9nzvm.md)
- [SparseSolve(_:_:_:_:_:)](accelerate/sparsesolve(_:_:_:_:_:)-7wnum.md)
