From cdb787d492e240200fcf433e177555de9e666120 Mon Sep 17 00:00:00 2001 From: Valentin Gehrke Date: Tue, 30 Jun 2015 21:53:40 +0200 Subject: [PATCH] =?UTF-8?q?Gauss=20algorithmus=20zum=20L=C3=B6sen=20von=20?= =?UTF-8?q?Ax=3Db?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gauss.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 gauss.py diff --git a/gauss.py b/gauss.py new file mode 100644 index 0000000..8bc6d0a --- /dev/null +++ b/gauss.py @@ -0,0 +1,44 @@ +def gauss(A,b,offset=(0,0)): + ox,oy = offset + if len(A)-1 < oy: + return A,b + + for i in range(oy,len(A)): + if A[i][ox] != 0: + if i > oy: + A[i], A[oy] = A[oy], A[i] + b[i], b[oy] = b[oy], b[i] + break + + for i in range(oy+1,len(A)): + if A[i][ox] != 0: + c = A[i][0]/A[oy][0] + A[i] = [ (A[i][x] - A[oy][x]*c) for x in range(ox,len(A[i])) ] + b[i] = b[i] - b[oy]*c + + return gauss(A,b,offset=(ox+1,oy+1)) + + +def vprint(A): + for i in range(len(A)): + print("%3d" % A[i]) + +def pprint(A): + for i in range(len(A)): + print( " ".join(["%3d" % (A[i][j]) for j in range(len(A[i])) ]) ) + + +A = [ [ 1, 1 ], + [ 1,-1 ] ] + +b = [ 5, + 3 ] + +pprint(A) +vprint(b) + + +A, b = gauss(A,b) + +pprint(A) +vprint(b)