X-Git-Url: https://disinclined.org/git/?a=blobdiff_plain;f=matrix.py;fp=matrix.py;h=41713e2a952e1611f4392fdf59e29be8790814fb;hb=7a41639f1e784c13e5614239bb91d48e8a2d55fe;hp=5ae2eba965994938ea283013d490bdf07b5a9878;hpb=cf491ed085c7fb21ab8d94eb7f5c48c17504b525;p=matrixnullspace.com.git 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) })