简体   繁体   中英

Trouble with vDSP in Swift to multiply, transpose, and invert matrices

I'm having trouble writing out the Normal Equation in Linear Regression: (X_Transpose * X)_Inverted * X_Transpose * y in Swift using Accelerate. Can anyone maybe help me out with what's wrong with my code here?

class NormalEquation {
    
    public private(set) var thetas: [Double]

    init(X: [[Double]], y: [Double]) {
        // Get matrix dimensions row x col
        let rows = Int32(X.count)
        let cols = Int32(X.first!.count)

        let X_1 = X.flatMap({ $0 })
        // Create X transposed
        var X_T = X_1
        vDSP_mtransD(X_1, vDSP_Stride(1), &X_T, vDSP_Stride(1), vDSP_Length(cols), vDSP_Length(rows))
        // Multiply X_T and X together and store it in X_I (not yet inverted)
        var X_I: [Double] = Array(repeating: 0.0, count: Int(cols) * Int(cols))
        vDSP_mmulD(X_T, vDSP_Stride(1), X_1, vDSP_Stride(1), &X_I, vDSP_Stride(1), vDSP_Length(cols), vDSP_Length(cols), vDSP_Length(rows))

        // Inversion of X_I in two steps
        var error: Int32 = 0
        var pivot: [Int32] = Array(repeating: 0, count: Int(cols))
        var workspace: [Double] = Array(repeating: 0.0, count: Int(cols))
        // LU Factorization
        var n1: Int32 = cols, n2: Int32 = cols, n3: Int32 = cols
        dgetrf_(&n1, &n2, &X_I, &n3, &pivot, &error)
        // Check for error
        if error != 0 { fatalError() }
        // Do matrix inversion
        n1 = cols; n2 = cols; n3 = cols
        dgetri_(&n1, &X_I, &n2, &pivot, &workspace, &n3, &error)
        // Check for error
        if error != 0 { fatalError() }
        // Now multiply our inverted matrix by the transpose
        var X_M = X_T
        vDSP_mmulD(X_I, vDSP_Stride(1), X_T, vDSP_Stride(1), &X_M, vDSP_Stride(1), vDSP_Length(cols), vDSP_Length(rows), vDSP_Length(cols))
        // Now multiply our mult matrix by y to get our final
        var X_F = X_M
        vDSP_mmulD(X_M, vDSP_Stride(1), y, vDSP_Stride(1), &X_F, vDSP_Stride(1), vDSP_Length(rows), vDSP_Length(rows), vDSP_Length(rows))

        self.thetas = X_F
        print("Thetas: \(self.thetas)")
    }
}

Never mind! Found Ozgur Sahin's AMAZING GitHub matrix type code, actually incredibly, here's the link: https://github.com/hollance/Matrix/blob/master/Matrix.swift .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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