From 331f7bc2b3214a1424dba4ea1eec6d1187589f13 Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Thu, 23 Apr 2015 06:19:23 -0700 Subject: [PATCH] [Issue #14] Getting PR to compile Refactoring and reformatting --- Source/Matrix.swift | 76 ++++++++++++++------------------------------- 1 file changed, 24 insertions(+), 52 deletions(-) diff --git a/Source/Matrix.swift b/Source/Matrix.swift index eff22d2e..223d7f41 100644 --- a/Source/Matrix.swift +++ b/Source/Matrix.swift @@ -43,8 +43,8 @@ public struct Matrix { self.init(rows: m, columns: n, repeatedValue: repeatedValue) - FOR (IOW) in enumerate(contents) { - grid.replaceRange(i*n.. GeneratorOf> { + public func generate() -> GeneratorOf> { let endIndex = rows * columns var nextRowStartIndex = 0 - return GeneratorOf> { + return GeneratorOf> { if nextRowStartIndex == endIndex { return nil } @@ -216,47 +216,21 @@ public func transpose(x: Matrix) -> Matrix { return results } -public func eigenDecompostion(x: Matrix) ->(Matrix, [Float]) -{ - precondition(x.rows == x.columns, "Matrix must be square") - - var mat:[__CLPK_floatreal] = x.grid - - var N = __CLPK_integer(sqrt(Float(x.grid.count))) - var pivots = [__CLPK_integer](count: Int(N), repeatedValue: 0) - var workspaceQuery: Float = 0.0 - var error : __CLPK_integer = 0 - var lwork = __CLPK_integer(-1) - // Real parts of eigenvalues - var wr = [Float](count: Int(N), repeatedValue: 0) - // Imaginary parts of eigenvalues - var wi = [Float](count: Int(N), repeatedValue: 0) - - // Left eigenvectors - var vl = [Float](count: Int(N*N), repeatedValue: 0) - // Right eigenvectors - var vr = [Float](count: Int(N*N), repeatedValue: 0) - - // The first iteration is used to get the size of the workspace from the workspace query - dgeev_(UnsafeMutablePointer(("V" as NSString).UTF8String), UnsafeMutablePointer(("V" as NSString).UTF8String), &N, &mat, &N, &wr, &wi, &vl, &N, &vr, &N, &workspaceQuery, &lwork, &error) - - var workspace = [Double](count: Int(workspaceQuery), repeatedValue: 0.0) - lwork = __CLPK_integer(workspaceQuery) - - // Actual iteration after obtaining workspace size - dgeev_(UnsafeMutablePointer(("V" as NSString).UTF8String), UnsafeMutablePointer,("V" as NSString).UTF8String), &N, &mat, &N, &wr, &wi, &vl, &N, &vr, &N, &workspaceQuery, &lwork, &error) +public func eigendecompostion(x: Matrix) ->(Matrix, [Float]) { + var input = Matrix(rows: x.rows, columns: x.columns, repeatedValue: 0.0) + input.grid = map(x.grid) {return Double($0)} + let (eigenVectors, components) = eigendecompostion(input) - var eigenVectors: Matrix = Matrix(rows: Int(N), columns: Int(N), repeatedValue: 0.0) - eigenVectors.grid = vr + var output = Matrix(rows: eigenVectors.rows, columns: eigenVectors.columns, repeatedValue: 0.0) + output.grid = map(eigenVectors.grid) {return Float($0)} - return (eigenVectors, wr) + return (output, map(components) {return Float($0)}) } -public func eigenDecompostion(x: Matrix) ->(Matrix, [Double]) -{ +public func eigendecompostion(x: Matrix) ->(Matrix, [Double]) { precondition(x.rows == x.columns, "Matrix must be square") - var mat:[__CLPK_doublereal] = x.grid + var mat: [__CLPK_doublereal] = x.grid var N = __CLPK_integer(sqrt(Double(x.grid.count))) var pivots = [__CLPK_integer](count: Int(N), repeatedValue: 0) @@ -264,24 +238,22 @@ public func eigenDecompostion(x: Matrix) ->(Matrix, [Double]) 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 = [Double](count: Int(N*N), repeatedValue: 0) - // Right eigenvectors - var vr = [Double](count: Int(N*N), repeatedValue: 0) + var vl = [Double](count: Int(N * N), repeatedValue: 0) + var vr = [Double](count: Int(N * N), repeatedValue: 0) - // The first iteration is used to get the size of the workspace from the workspace query - dgeev_(UnsafeMutablePointer(("V" as NSString).UTF8String), UnsafeMutablePointer(("V" as NSString).UTF8String), &N, &mat, &N, &wr, &wi, &vl, &N, &vr, &N, &workspaceQuery, &lwork, &error) + "V".withCString { (V) -> Void in + dgeev_(UnsafeMutablePointer(V), UnsafeMutablePointer(V), &N, &mat, &N, &wr, &wi, &vl, &N, &vr, &N, &workspaceQuery, &lwork, &error) + } var workspace = [Double](count: Int(workspaceQuery), repeatedValue: 0.0) lwork = __CLPK_integer(workspaceQuery) - // Actual run after obtaining workspace size - dgeev_(UnsafeMutablePointer(("V" as NSString).UTF8String), UnsafeMutablePointer,("V" as NSString).UTF8String), &N, &mat, &N, &wr, &wi, &vl, &N, &vr, &N, &workspaceQuery, &lwork, &error) + "V".withCString { (V) -> Void in + dgeev_(UnsafeMutablePointer(V), UnsafeMutablePointer(V), &N, &mat, &N, &wr, &wi, &vl, &N, &vr, &N, &workspaceQuery, &lwork, &error) + } var eigenVectors: Matrix = Matrix(rows: Int(N), columns: Int(N), repeatedValue: 0.0) eigenVectors.grid = vr