2015-06-30 21:53:40 +02:00
|
|
|
def gauss(A,b,offset=(0,0)):
|
|
|
|
ox,oy = offset
|
|
|
|
for i in range(oy,len(A)):
|
|
|
|
if A[i][ox] != 0:
|
2015-10-06 04:44:06 +02:00
|
|
|
A[i], A[oy] = A[oy], A[i]
|
|
|
|
b[i], b[oy] = b[oy], b[i]
|
2015-06-30 21:53:40 +02:00
|
|
|
break
|
|
|
|
|
2015-10-06 04:44:06 +02:00
|
|
|
pprint(A)
|
|
|
|
|
2015-06-30 21:53:40 +02:00
|
|
|
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
|
|
|
|
|
2015-10-06 04:44:06 +02:00
|
|
|
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
|
2015-06-30 21:53:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
def vprint(A):
|
2015-10-06 04:44:06 +02:00
|
|
|
print("Vector ----")
|
2015-06-30 21:53:40 +02:00
|
|
|
for i in range(len(A)):
|
2015-10-06 04:44:06 +02:00
|
|
|
print("%10.4f" % A[i])
|
|
|
|
print("-----")
|
2015-06-30 21:53:40 +02:00
|
|
|
|
|
|
|
def pprint(A):
|
2015-10-06 04:44:06 +02:00
|
|
|
print("Matrix ----")
|
2015-06-30 21:53:40 +02:00
|
|
|
for i in range(len(A)):
|
2015-10-06 04:44:06 +02:00
|
|
|
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 ] ]
|
|
|
|
|
|
|
|
b = [ 0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
5 ]
|
2015-06-30 21:53:40 +02:00
|
|
|
|
|
|
|
pprint(A)
|
|
|
|
vprint(b)
|
|
|
|
|
|
|
|
|
2015-10-06 04:44:06 +02:00
|
|
|
# _, x = gauss(A,b)
|
2015-06-30 21:53:40 +02:00
|
|
|
|
2015-10-06 04:44:06 +02:00
|
|
|
from numpy.linalg import solve
|
|
|
|
x = solve(A,b)
|
|
|
|
|
|
|
|
vprint(x)
|