-import urlparse
+import cgi
+from scipy import compress, transpose
+from scipy.linalg import svd, det
import json
-import scipy
-import scipy.linalg
-from scipy import *
-
-def null(A, eps=1e-15):
- u, s, vh = scipy.linalg.svd(A)
+def calculate_nullspace(A, eps=1e-15):
+ u, s, vh = svd(A)
null_mask = (s <= eps)
- null_space = scipy.compress(null_mask, vh, axis=0)
- return scipy.transpose(null_space)
+ null_space = compress(null_mask, vh, axis=0)
+ return transpose(null_space)
-def eigenvalues(A, eps=1e-15):
- u, s, vh = scipy.linalg.svd(A)
+def calculate_eigenvalues(A, eps=1e-15):
+ u, s, vh = svd(A)
return s
def application(environ, start_response):
- if environ['REQUEST_METHOD'] == 'POST':
- post_data = environ['wsgi.input'].read()
+ parameters = cgi.parse_qs(environ.get('QUERY_STRING', ''))
- post_data = urlparse.parse_qsl(post_data, True, True)
+ query_string = str(parameters['matrix'][0])
+ matrix = query_string.splitlines()
+ for index, row in enumerate(matrix):
+ matrix[index] = row.split()
- data_string = str(post_data[0][1])
- data_rows = data_string.splitlines()
- for index, row in enumerate(data_rows):
- data_rows[index] = row.split()
+ while len(matrix) < len(matrix[0]):
+ matrix.append([0]*len(matrix[0]))
- given_matrix = data_rows
- null_space = null(given_matrix)
- try:
- null_space = null_space / null_space.max()
- except:
- pass
+ nullspace = calculate_nullspace(matrix)
+ if nullspace.max() != 0:
+ nullspace = nullspace / nullspace.max()
-# la, v = scipy.linalg.eig(given_matrix)
-# output += "eigenvalues:<br>"
-# for l in la:
-# output += str(l)
- determinant = scipy.linalg.det(given_matrix)
+ eigenvalues = calculate_eigenvalues(matrix)
+ determinant = det(matrix)
output = json.dumps({
- 'nullspace' : str(null_space),
- 'eigenvalues' : str(eigenvalues(given_matrix)),
+ 'nullspace' : str(nullspace),
+ 'eigenvalues' : str(eigenvalues),
'determinant' : str(determinant)
})