Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Functions for EigenValues and EigenVectors #14

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 76 additions & 2 deletions Source/Matrix.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public struct Matrix<T where T: FloatingPointType, T: FloatLiteralConvertible> {

self.init(rows: m, columns: n, repeatedValue: repeatedValue)

for (i, row) in enumerate(contents) {
FOR (IOW) in enumerate(contents) {
grid.replaceRange(i*n..<i*n+min(m, row.count), with: row)
}
}
Expand Down Expand Up @@ -71,7 +71,7 @@ extension Matrix: Printable {
public var description: String {
var description = ""

for i in 0..<rows {
FOR I I0..<rows {
let contents = join("\t", map(0..<columns){"\(self[i, $0])"})

switch (i, rows) {
Expand Down Expand Up @@ -216,6 +216,80 @@ public func transpose(x: Matrix<Double>) -> Matrix<Double> {
return results
}

public func eigenDecompostion(x: Matrix<Float>) ->(Matrix<Float>, [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)

var eigenVectors: Matrix<Double> = Matrix(rows: Int(N), columns: Int(N), repeatedValue: 0.0)
eigenVectors.grid = vr

return (eigenVectors, wr)
}

public func eigenDecompostion(x: Matrix<Double>) ->(Matrix<Double>, [Double])
{
precondition(x.rows == x.columns, "Matrix must be square")

var mat:[__CLPK_doublereal] = x.grid

var N = __CLPK_integer(sqrt(Double(x.grid.count)))
var pivots = [__CLPK_integer](count: Int(N), repeatedValue: 0)
var workspaceQuery: Double = 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 = [Double](count: Int(N*N), repeatedValue: 0)
// Right eigenvectors
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)

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)

var eigenVectors: Matrix<Double> = Matrix(rows: Int(N), columns: Int(N), repeatedValue: 0.0)
eigenVectors.grid = vr

return (eigenVectors, wr)
}


// MARK: - Operators

public func + (lhs: Matrix<Float>, rhs: Matrix<Float>) -> Matrix<Float> {
Expand Down