1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
from __future__ import division
import numpy as np
def gaussjordan(M):
M[0][0]=float(M[0][0])
M=np.array(M)
for i in range(len(M)):
# 1) recherche de la valeur maximale
ligne_pivot=i
for k in range(i,len(M)):
if abs(M[k][i])>abs(M[ligne_pivot][i]):
ligne_pivot=k
# 2) permutation de la ligne contenant le pivot
M[i],M[ligne_pivot]=M[ligne_pivot],([0 for j in range(len(M[0]))]+M[i])
# 3) normalisation en divisant par le pivot
M[i]=M[i]/M[i][i]
# 4) application du pivot a toutes les lignes
if i!=len(M):
for n in range(i+1,len(M)):
M[n]+= (-M[i]*M[n][i])
# 5) triangularisation (matrice inferieure remplie de zeros)
for e in range(len(M)-1, -1, -1):
tot=len(M)-e-1
for g in range(1,tot+1):
M[e]+=(-M[e]*M[e][e+g]*M[e+g])
return M
A=[[1, -2, 2, 5],[3, -6, -4, 1],[4, 3, 1, 3]]
B=[[1, 1, 2, 5],[1, -1, -1, 1],[1, 0, 1, 3]] |
Partager