1
我在加速框架中使用LAPACK中的dgeev
算法來計算矩陣的特徵值和特徵向量。這是我的代碼:Swift中加速框架的問題
var matrix:[__CLPK_doublereal] = [1,2,3,4,5,6,7,8,9]
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
var lwork = __CLPK_integer(-1)
// Real parts of eigenvalues
var wr = [Double](count: Int(N), repeatedValue: 0)
// Imaginary parts of eigenvalues
var wi = [Double](count: Int(N), repeatedValue: 0)
// Left eigenvectors
var vl = [__CLPK_doublereal](count: Int(N*N), repeatedValue: 0)
// Right eigenvectors
var vr = [__CLPK_doublereal](count: Int(N*N), repeatedValue: 0)
dgeev_(UnsafeMutablePointer(("V" as NSString).UTF8String), UnsafeMutablePointer(("V" as NSString).UTF8String), &N, &matrix, &N, &wr, &wi, &vl, &N, &vr, &N, &workspace, &lwork, &error)
println("\(wr), \(vl), \(vr)")
這隻打印包含零的數組,這意味着它們不被函數修改。我究竟做錯了什麼?
更新1
我現在有這樣的:
var matrix:[__CLPK_doublereal] = [1,2,3,4,5,6,7,8,9]
var N = __CLPK_integer(sqrt(Double(matrix.count)))
var pivots = [__CLPK_integer](count: Int(N), repeatedValue: 0)
var workspaceQuery = [Double](count: 1, repeatedValue: 0.0)
var error : __CLPK_integer = 0
var lwork = __CLPK_integer(-1)
// Real parts of eigenvalues
var wr = [Double](count: Int(N), repeatedValue: 0)
// Imaginary parts of eigenvalues
var wi = [Double](count: Int(N), repeatedValue: 0)
// Left eigenvectors
var vl = [__CLPK_doublereal](count: Int(N*N), repeatedValue: 0)
// Right eigenvectors
var vr = [__CLPK_doublereal](count: Int(N*N), repeatedValue: 0)
dgeev_(UnsafeMutablePointer(("V" as NSString).UTF8String), UnsafeMutablePointer(("V" as NSString).UTF8String), &N, &matrix, &N, &wr, &wi, &vl, &N, &vr, &N, &workspaceQuery, &lwork, &error)
var workspace = [Double](count: Int(workspaceQuery[0]), repeatedValue: 0.0)
dgeev_(UnsafeMutablePointer(("V" as NSString).UTF8String), UnsafeMutablePointer(("V" as NSString).UTF8String), &N, &matrix, &N, &wr, &wi, &vl, &N, &vr, &N, &workspace, &lwork, &error)
println("\(wr), \(vl), \(vr)")
不過它打印零。
你值得你名稱:)我剛剛得出同樣的結論... –
那麼,應該在第二次電話會議中「lwork」的價值是什麼? –
@YoussefSami:我認爲它應該是工作區的實際大小:'lwork = __CLPK_integer(workspace.count)'。我發現這個C代碼的例子,它也演示了這個用法:https://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_lapack_examples/dgeev_ex.c.htm –