简体   繁体   English

使用加速框架的Swift中的矩阵求逆

[英]Matrix inversion in Swift using Accelerate Framework

Following the good instructions that I found here: https://github.com/haginile/SwiftAccelerate I verified that matrix inversion works. 按照我在这里找到的好的指示: https//github.com/haginile/SwiftAccelerate我验证了矩阵反转是有效的。 In fact it did for the example given. 事实上它确实给出了给出的例子。 But I get a EXC_BAD_ACCESS error for any other matrix (bigger than 2x2) for example the following 2D matrix (converted as a 1D array) has been tested in matlab and python successfully and it does not work 但是我得到任何其他矩阵(大于2x2)的EXC_BAD_ACCESS错误,例如下面的2D矩阵(转换为1D数组)已成功在matlab和python中测试过,它不起作用

m = [0.55481645013013, -1.15522603580724, 0.962090414322894, -0.530226035807236, 0.168545207161447, -0.38627124296868, 0.93401699437494, -0.999999999999995, 0.684016994374945, -0.23176274578121, 0.123606797749979, -0.323606797749979, 0.432893622827287, -0.323606797749979, 0.123606797749979, 0.231762745781211, -0.684016994374948, 1.0, -0.934016994374947, 0.386271242968684, 0.168545207161448, -0.530226035807237, 0.962090414322895, -1.15522603580724, 0.554816450130132]

Its inverted matrix should be 它的倒置矩阵应该是

inv(AA)

ans =

  Columns 1 through 3

          -262796763616197          -656991909040516          4.90007819375216
          -162417332048282          -406043330120712          14.6405748712708
         0.718958226823704          7.87760147961979          30.4010295628018
           162417332048287           406043330120730          46.1614842543337
           262796763616208           656991909040536          55.9019809318537

  Columns 4 through 5

          -656991909040528           262796763616211
          -406043330120721           162417332048287
         -4.28281034550088        -0.718958226823794
           406043330120704          -162417332048283
           656991909040497          -262796763616196

Could you please give me another way of matrix inversion in Swift? 你能否在Swift中给我另一种矩阵求逆方法? Or explain me how to fix this? 或者解释一下如何解决这个问题? I really don't understand why it does not work. 我真的不明白为什么它不起作用。

It doesn't work because the instructions that you found are not so good. 它不起作用,因为您找到的说明不太好。 Specifically, both pivots and workspace need to be Arrays, not scalar values; 具体来说,枢轴和工作空间都需要是数组,而不是标量值; it was only working for two-by-two matrices by random chance. 它只是偶然机会用于2乘2矩阵。

Here's a modified version of the invert function that allocates the workspaces correctly: 这是invert函数的修改版本,可以正确分配工作空间:

func invert(matrix : [Double]) -> [Double] {
  var inMatrix = matrix
  var N = __CLPK_integer(sqrt(Double(matrix.count)))
  var pivots = [__CLPK_integer](count: Int(N), repeatedValue: 0)
  var workspace = [Double](count: Int(N), repeatedValue: 0.0)
  var error : __CLPK_integer = 0
  dgetrf_(&N, &N, &inMatrix, &N, &pivots, &error)
  dgetri_(&N, &inMatrix, &N, &pivots, &workspace, &N, &error)
  return inMatrix
}

I should also note that your 5x5 matrix is extremely ill-conditioned, so even when you can compute the "inverse" the error of that computation will be very large, and the inverse really shouldn't be used. 我还应该注意到你的5x5矩阵是非常恶劣的,所以即使你可以计算“逆”,该计算的误差也会非常大,并且实际上不应该使用逆。

A Swift 4 version: 一个Swift 4版本:

func invert(matrix : [Double]) -> [Double] {
    var inMatrix = matrix
    var N = __CLPK_integer(sqrt(Double(matrix.count)))
    var pivots = [__CLPK_integer](repeating: 0, count: Int(N))
    var workspace = [Double](repeating: 0.0, count: Int(N))
    var error : __CLPK_integer = 0

    withUnsafeMutablePointer(to: &N) {
        dgetrf_($0, $0, &inMatrix, $0, &pivots, &error)
        dgetri_($0, &inMatrix, $0, &pivots, &workspace, $0, &error)
    }
    return inMatrix
}

I have written a library for linear algebra in Swift. 我在Swift中编写了一个线性代数库。 I call this library swix and it includes functions to invert matrices (this function is called inv ). 我将此库称为swix,它包含反转矩阵的函数(此函数称为inv )。

Example use case: 用例示例:

var b = ones(10)
var A = rand((10, 10))
var AI = inv(A)
var x = AI.dot(b)

Source: https://github.com/stsievert/swix 资料来源: https//github.com/stsievert/swix

Documentation: http://scottsievert.com/swix/ 文档: http//scottsievert.com/swix/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM