Skip to content

Instantly share code, notes, and snippets.

@ukiyoevega
Created March 20, 2017 06:34
Show Gist options
  • Save ukiyoevega/b0b9f6afc708c30e5235f31fdc61ebee to your computer and use it in GitHub Desktop.
Save ukiyoevega/b0b9f6afc708c30e5235f31fdc61ebee to your computer and use it in GitHub Desktop.
invert matrix with Accelerate framework in Swift
func invert(matrix : [Double]) -> [Double] {
var inMatrix = matrix
var N = __CLPK_integer(sqrt(Double(matrix.count)))
var pivots = Array(repeating: 0 as __CLPK_integer, count: Int(N))
var workspace = Array(repeating: 0.0 as Double, count: Int(N))
var error : __CLPK_integer = 0
dgetrf_(&N, &N, &inMatrix, &N, &pivots, &error)
dgetri_(&N, &inMatrix, &N, &pivots, &workspace, &N, &error)
return inMatrix
}
let matrix: [Double] = [2.0, 5.0, -3.0, -7.0]
let invertedMatrix = invert(matrix: matrix)
@gmcusaro
Copy link

How to upgrade to swift 5?
Schermata 2021-06-26 alle 11 06 11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment