clean up matrix.py & square matrices
authorDylan Lloyd <dylan@dylansserver.com>
Sun, 14 Jul 2013 04:23:47 +0000 (00:23 -0400)
committerDylan Lloyd <dylan@dylansserver.com>
Sun, 14 Jul 2013 04:23:47 +0000 (00:23 -0400)
still need error handling...

index.php
js/matrix.js
matrix.py
style.css

index d8f84ba..cf3daa0 100644 (file)
--- a/index.php
+++ b/index.php
@@ -9,24 +9,21 @@
 </head>
 <body>
   <div id='container'>
-
-  <h1>matrix.py</h1>
-  Find the kernel, determinant and eigenvalues of a given matrix.
-
-  <form>
+    <h1>matrix.py</h1>
+    <div id='description'>
+      Find the kernel, determinant and eigenvalues of a given matrix.
+    </div>
+    <form>
       <textarea id="matrix">2 3 5
 -4 2 3
 0 0 0</textarea></br>
       <a id='submit' href='#'>./calculate</a>
-  </form>
-
-  <ul id='results'></ul>
-
-  <div id='clear'></div>
-
-  <div id="footer">
-      <h2><a href="http://dylansserver.com">dylansserver.com</a>
+    </form>
+    <ul id='results'></ul>
+    <div id='clear'></div>
+    <div id="footer">
+        <h2><a href="http://dylansserver.com">dylansserver.com</a>
+    </div>
   </div>
-</div>
 </body>
 </html>
index 83e8e27..4fcfde4 100644 (file)
@@ -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(
index 5ae2eba..41713e2 100755 (executable)
--- 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:<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)
         })
 
index c9eb003..1e770c9 100644 (file)
--- a/style.css
+++ b/style.css
@@ -31,6 +31,10 @@ body {
     width: 960px;
 }
 
+#description {
+    margin-bottom: 30px;
+}
+
 form {
     float: left;
 }