added readme and notice of scipy dependency
[matrixnullspace.com.git] / matrix.py
1 import urlparse
2
3 import scipy
4 import scipy.linalg
5 from scipy import *
6
7 def null(A, eps=1e-15):
8 u, s, vh = scipy.linalg.svd(A)
9 null_mask = (s <= eps)
10 null_space = scipy.compress(null_mask, vh, axis=0)
11 return scipy.transpose(null_space)
12
13 def eigenvalues(A, eps=1e-15):
14 u, s, vh = scipy.linalg.svd(A)
15 return s
16
17 def application(environ, start_response):
18 if environ['REQUEST_METHOD'] == 'POST':
19 post_data = environ['wsgi.input'].read()
20
21 output = "<pre>"
22 #output += post_data
23 output += "<br>"
24
25 post_data = urlparse.parse_qsl(post_data, True, True)
26
27 data_string = str(post_data[0][1])
28 data_rows = data_string.splitlines()
29 for index, row in enumerate(data_rows):
30 data_rows[index] = row.split()
31
32 given_matrix = data_rows
33 null_space = null(given_matrix)
34 try:
35 null_space = null_space / null_space.max()
36 except:
37 pass
38
39 # la, v = scipy.linalg.eig(given_matrix)
40 # output += "eigenvalues:<br>"
41 # for l in la:
42 # output += str(l)
43 determinant = scipy.linalg.det(given_matrix)
44
45 output += "<h1>"
46 output += "nullspace:<br>"
47 output += str(null_space)
48 output += "<br><br>"
49 output += "eigenvalues:<br>"
50 output += str(eigenvalues(given_matrix))
51 output += "<br><br>"
52 output += "determinant:<br>"
53 output += str(determinant)
54 # output += "<br><br>"
55 # output += "eigenvalues:<br>"
56 # output += str(l1)
57 # output += "<br><br>"
58 # output += "eigenvectors:<br>"
59 # output += str(v[:,0][0])
60 output += "</h1>"
61
62 # send results
63 output_len = sum(len(line) for line in output)
64 start_response('200 OK', [('Content-type', 'text/html'),
65 ('Content-Length', str(output_len))])
66 return output