Reformatting code
This commit is contained in:
parent
12c4af1846
commit
c32a8ed722
1 changed files with 36 additions and 17 deletions
49
gauss.py
49
gauss.py
|
@ -1,44 +1,63 @@
|
|||
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
|
||||
|
||||
pprint(A)
|
||||
|
||||
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))
|
||||
if oy < len(A)-1:
|
||||
A,b = gauss(A,b,offset=(ox+1,oy+1))
|
||||
|
||||
b[oy] /= A[oy][ox]
|
||||
x = b[oy]
|
||||
for y in range(oy):
|
||||
b[y] -= A[y][ox]*x
|
||||
|
||||
return A,b
|
||||
|
||||
|
||||
def vprint(A):
|
||||
print("Vector ----")
|
||||
for i in range(len(A)):
|
||||
print("%3d" % A[i])
|
||||
print("%10.4f" % A[i])
|
||||
print("-----")
|
||||
|
||||
def pprint(A):
|
||||
print("Matrix ----")
|
||||
for i in range(len(A)):
|
||||
print( " ".join(["%3d" % (A[i][j]) for j in range(len(A[i])) ]) )
|
||||
print( " ".join(["%10.4f" % (A[i][j]) for j in range(len(A[i])) ]) )
|
||||
print("-----")
|
||||
|
||||
A = [ [ 0, 0, 0,-1,-1, 1 ],
|
||||
[ -1, 1, 0, 0, 0, 0 ],
|
||||
[ 0, -1, 1, 0, 0, 0 ],
|
||||
[-1000, 0, 0, 1, 0, 0 ],
|
||||
[ 0, -1000, 0, 0, 1, 0 ],
|
||||
[ 0, 0, 0, 0, 0, 1 ] ]
|
||||
|
||||
A = [ [ 1, 1 ],
|
||||
[ 1,-1 ] ]
|
||||
|
||||
b = [ 5,
|
||||
3 ]
|
||||
b = [ 0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
5 ]
|
||||
|
||||
pprint(A)
|
||||
vprint(b)
|
||||
|
||||
|
||||
A, b = gauss(A,b)
|
||||
# _, x = gauss(A,b)
|
||||
|
||||
pprint(A)
|
||||
vprint(b)
|
||||
from numpy.linalg import solve
|
||||
x = solve(A,b)
|
||||
|
||||
vprint(x)
|
||||
|
|
Loading…
Reference in a new issue