Skip to content

Commit

Permalink
cleanup includes and try both sparse and regular methods for eigenval…
Browse files Browse the repository at this point in the history
…ue/eigenvector calculation
  • Loading branch information
carlsonp committed Mar 6, 2014
1 parent 3c2a2b1 commit be37687
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
from scipy import linalg
from numpy import array, warnings, ComplexWarning, absolute, amax, amin, where, nditer, random

import scipy.sparse.linalg as sparse
import numpy as np
import sys

#Ignore conversion from imaginary to real number
warnings.simplefilter("ignore", ComplexWarning)
np.warnings.simplefilter("ignore", np.ComplexWarning)

# Data from previous Java example
# data = array([ [1, 1, 1, 0, 0, 0, 0],
# data = np.array([ [1, 1, 1, 0, 0, 0, 0],
# [1, 1, 1, 0, 0, 0, 0],
# [1, 1, 1, 1, 1, 1, 1],
# [0, 0, 1, 1, 1, 0, 0],
# [0, 0, 1, 1, 1, 0, 0],
# [0, 0, 1, 0, 0, 1, 1],
# [0, 0, 1, 0, 0, 1, 1] ] )

# New example data
# data = array([ [1, 0, 1, 0, 1, 1, 0, 0, 0],
#New example data
# data = np.array([ [1, 0, 1, 0, 1, 1, 0, 0, 0],
# [0, 1, 1, 0, 0, 0, 1, 0, 0],
# [1, 1, 1, 1, 0, 0, 0, 1, 0],
# [0, 0, 1, 1, 1, 0, 0, 0, 1],
Expand All @@ -26,23 +27,27 @@
# [0, 0, 0, 1, 0, 0, 0, 1, 1] ] )

# Generate random data (to test how long this will take on larger datasets)
data = random.randint(2, size=(1000, 1000))
data = np.random.randint(2, size=(1000, 1000))
data = (data + data.T)/2 #make it symmetrical
print "Finished generating data."

eigenvalues,eigenvectors = linalg.eig(data)
#use this for non-sparse matrices
#eigenvalues,eigenvectors = linalg.eig(data)
#on sparse matrices, this should be faster
eigenvalues,eigenvectors = sparse.eigs(data.astype(float))
print "Eigenvalues:"
print eigenvalues.round().astype(int)

print "Eigenvalues after absolute value:"
abs_eigenvalues = absolute(eigenvalues.round().astype(int))
abs_eigenvalues = np.absolute(eigenvalues.round().astype(int))
print abs_eigenvalues

print "Principle Eigenvalue (largest):"
principle_eigenvalue = amax(abs_eigenvalues)
principle_eigenvalue = np.amax(abs_eigenvalues)
print principle_eigenvalue

print "Principle Eigenvalue Index:"
principle_index = where(abs_eigenvalues == principle_eigenvalue)[0][0]
principle_index = np.where(abs_eigenvalues == principle_eigenvalue)[0][0]
print principle_index

print "Eigenvectors:"
Expand All @@ -59,13 +64,13 @@

print "Normalized Eigenvectors (Gould Index values for each node):"
normalized_eigenvectors = []
for x in nditer(eigenvectors_index):
for x in np.nditer(eigenvectors_index):
normalized_eigenvectors.append(round(x * (1/sum_eig), 3))
print normalized_eigenvectors

print "Largest Gould Index:"
print amax(normalized_eigenvectors)
print np.amax(normalized_eigenvectors)

print "Smallest Gould Index:"
print amin(normalized_eigenvectors)
print np.amin(normalized_eigenvectors)

0 comments on commit be37687

Please sign in to comment.