From 7a41639f1e784c13e5614239bb91d48e8a2d55fe Mon Sep 17 00:00:00 2001 From: Dylan Lloyd Date: Sun, 14 Jul 2013 00:23:47 -0400 Subject: [PATCH] clean up matrix.py & square matrices still need error handling... --- index.php | 25 +++++++++++------------- js/matrix.js | 2 +- matrix.py | 54 ++++++++++++++++++++++------------------------------ style.css | 4 ++++ 4 files changed, 39 insertions(+), 46 deletions(-) diff --git a/index.php b/index.php index d8f84ba..cf3daa0 100644 --- a/index.php +++ b/index.php @@ -9,24 +9,21 @@
- -

matrix.py

- Find the kernel, determinant and eigenvalues of a given matrix. - -
+

matrix.py

+
+ Find the kernel, determinant and eigenvalues of a given matrix. +
+
./calculate -
- - - -
- - -
diff --git a/js/matrix.js b/js/matrix.js index 83e8e27..4fcfde4 100644 --- a/js/matrix.js +++ b/js/matrix.js @@ -13,7 +13,7 @@ $(document).ready(function(){ }); return; } - $.post('/calculate', { 'matrix' : $('#matrix').val() }) + $.get('/calculate', { 'matrix' : $('#matrix').val() }) .done(function(data) { $('#results').fadeOut('slow', function() { $('#results').html('').append( diff --git a/matrix.py b/matrix.py index 5ae2eba..41713e2 100755 --- a/matrix.py +++ b/matrix.py @@ -1,47 +1,39 @@ -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:
" -# 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) }) diff --git a/style.css b/style.css index c9eb003..1e770c9 100644 --- a/style.css +++ b/style.css @@ -31,6 +31,10 @@ body { width: 960px; } +#description { + margin-bottom: 30px; +} + form { float: left; } -- 2.30.2