<!--
{
  "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/SparseRefactor(_:_:_:)-6ttkd",
  "metadataVersion" : "0.1.0",
  "role" : "Function",
  "symbol" : {
    "kind" : "Function",
    "modules" : [
      "Accelerate"
    ],
    "preciseIdentifier" : "c:@F@SparseRefactor#$@SA@SparseMatrix_Double#*$@SA@SparseOpaqueFactorization_Double#$@SA@SparseNumericFactorOptions#"
  },
  "title" : "SparseRefactor(_:_:_:)"
}
-->

# SparseRefactor(_:_:_:)

Computes a factorization of the specified double-precision matrix using an existing factorization’s storage and specified options.

```
func SparseRefactor(_ Matrix: SparseMatrix_Double, _ Factorization: UnsafeMutablePointer<SparseOpaqueFactorization_Double>, _ nfoptions: SparseNumericFactorOptions)
```

## Parameters

`Matrix`

The matrix that contains numerical data to recompute.

`Factorization`

On input, the factorization to recompute. On output, the recomputed result.

`nfoptions`

The numeric factor options, such as the scaling method to use.

## Discussion

Use this function to calculate the factorization of a sparse matrix to pass to the direct solve functions using an existing factorization’s storage. The specified matrix must have the same sparsity structure as the matrix of the original factorization.

This function provides behavior similar to [`SparseFactor(_:_:_:_:_:)`](/documentation/Accelerate/SparseFactor(_:_:_:_:_:)-68hki) by reusing explicit storage that you supply to [`SparseFactor(_:_:_:_:_:)`](/documentation/Accelerate/SparseFactor(_:_:_:_:_:)-68hki) as the argument `factorStorage`. However, in addition to providing a simplified call sequence, this call can also reuse any additional storage that you allocate to accommodate delayed pivots.

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

![A mathematical equation that has two stacked sets of three simultaneous equations on the left. Each equation has three unknowns. The same sets of simultaneous equations appear on the right as two matrix equations, A x equals B. Each 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-3703913@2x.png)

The following code solves these two systems with refactoring. After factorizing and solving for the coefficient matrix *A0*, the code refactors and solves for matrix *A1*:

```swift
// Define the sparsity pattern of matrices `A0` and `A1`.
let rowIndices: [Int32] =    [ 0, 1, 1, 2]
let columnIndices: [Int32] = [ 2, 0, 2, 1]

// Create the single-precision coefficient matrix `A0` from coordinate (COO) arrays.
let a0Values: [Double] = [10, 20, 5, 50]
let A0 = SparseConvertFromCoordinate(3, 3,
                                     4, 1,
                                     SparseAttributes_t(),
                                     rowIndices, columnIndices,
                                     a0Values)

// Compute the factors of `A0`.
var factorization = SparseFactor(SparseFactorizationQR, A0)

// Solve `A0 · x = b0` in place.
var b0Values: [Double] = [30, 35, 100]
b0Values.withUnsafeMutableBufferPointer { bPtr in
    let xb = DenseVector_Double(count: 3,
                                data: bPtr.baseAddress!)
    
    SparseSolve(factorization, xb)
}

// Create the double-precision coefficient matrix `A1` from coordinate (COO) arrays.
let a1Values: [Double] = [5, 10, 2.5, 25]
let A1 = SparseConvertFromCoordinate(3, 3,
                                     4, 1,
                                     SparseAttributes_t(),
                                     rowIndices, columnIndices,
                                     a1Values)

// Refactor `A1` into the existing factorization.
let numericFactorOptions = SparseNumericFactorOptions()
SparseRefactor(A1, &factorization,
               numericFactorOptions)

// Solve `A1 · x = b1` in place.
var b1Values: [Double] = [60, 70, 200]
b1Values.withUnsafeMutableBufferPointer { bPtr in
    let xb = DenseVector_Double(count: 3,
                                data: bPtr.baseAddress!)
    
    SparseSolve(factorization, xb)
}

print(b0Values) // Prints "[1.0, 2.0, 3.0]".
print(b1Values) // Prints "[4.0, 8.0, 12.0]".

SparseCleanup(A0)
SparseCleanup(A1)
SparseCleanup(factorization)
```

On return, `b0Values` contains the values `[1.0, 2.0, 3.0]`, and `b1Values` contains the values `[4.0, 8.0, 12.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)