︠5803d416-240f-4072-8f40-851e77ba06ac︠
# Copie de la matrice
def copiematrice(A):
n = A.nrows()
p = A.ncols()
taille = n*p
B = Matrix(QQ,n,p,[0 for k in range(taille)])
for i in range(n):
for j in range(p):
B[i,j]=A[i,j]
return B
A = matrix(RR,[[1, 1, 1], [1, -1, 2], [2, -1, 1]])
copiematrice(A)
def copievecteur(Y):
n=len(Y)
n
Z = vector(RR,[0 for j in range(n)])
for i in range(n):
Z[i]=Y[i]
return Z
Y = vector(RR,[2,9,7])
copievecteur(Y)
︡6c2046e4-f027-46dc-84f2-b3274cbbaba9︡{"stdout":"[ 1 1 1]\n[ 1 -1 2]\n[ 2 -1 1]\n"}︡{"stdout":"(2.00000000000000, 9.00000000000000, 7.00000000000000)\n"}︡{"done":true}︡
︠174d8d87-7b4e-496e-94e7-926d677ad767︠
# Recherche du pivot
def indicepivot(A,j):
n = A.nrows()
p = A.ncols()
i=j
max = 0
for k in [j..n-1]:
if abs(A[k,j])>max:
max=abs(A[k,j])
i=k
return i
indicepivot(A,0)
︡2be25368-38e8-440c-b1bc-f76a751ef976︡{"stdout":"2\n"}︡{"done":true}︡
︠4d48b246-da32-4882-9710-8b2f5d49c0b6︠
# Echange de lignes
def echangelignes(A,i,j):
temp = A[i,:]
A[i,:]=A[j,:]
A[j,:]=temp
echangelignes(A,1,2)
A
echangelignes(A,1,2)
A
︡6b278b07-98c3-4b3d-99ff-c62a347f896f︡{"stdout":"[ 1.00000000000000 1.00000000000000 1.00000000000000]\n[ 2.00000000000000 -1.00000000000000 1.00000000000000]\n[ 1.00000000000000 -1.00000000000000 2.00000000000000]\n"}︡{"stdout":"[ 1.00000000000000 1.00000000000000 1.00000000000000]\n[ 1.00000000000000 -1.00000000000000 2.00000000000000]\n[ 2.00000000000000 -1.00000000000000 1.00000000000000]\n"}︡{"done":true}︡
︠9b0a15f7-e65f-48aa-99fa-efcc1291b0ce︠
# Operations elementaires
def transvection(A,k,i,alpha):
temp=A[k,:]
A[k,:]=temp-alpha*A[i,:]
B=copiematrice(A)
show(B)
transvection(B,1,0,1)
show(B)
transvection(B,2,0,2)
show(B)
︡8021821c-bc4c-474d-b3ce-5951ecb56a82︡{"html":"
$\\displaystyle \\left(\\begin{array}{rrr}\n1 & 1 & 1 \\\\\n1 & -1 & 2 \\\\\n2 & -1 & 1\n\\end{array}\\right)$
"}︡{"html":"$\\displaystyle \\left(\\begin{array}{rrr}\n1 & 1 & 1 \\\\\n0 & -2 & 1 \\\\\n2 & -1 & 1\n\\end{array}\\right)$
"}︡{"html":"$\\displaystyle \\left(\\begin{array}{rrr}\n1 & 1 & 1 \\\\\n0 & -2 & 1 \\\\\n0 & -3 & -1\n\\end{array}\\right)$
"}︡{"done":true}︡
︠9430fc13-fb16-49e4-8fdf-03c7004ffec7︠
# Algorithme du pivot
def PivotGauss(A,Y):
B=copiematrice(A)
Z=copievecteur(Y)
n=B.nrows()
#assert len(Z)==n
p=B.ncols()
for j in [0..n-2]:
i=indicepivot(B,j)
echangelignes(B,i,j)
z=Z[i]
Z[i]=Z[j]
Z[j]=z
for i in [j+1..n-1]:
pivot=B[i,j]/B[j,j]
transvection(B,i,j,pivot)
Z[i]=Z[i]-pivot*Z[j]
X = vector(RR,[0]*n)
for i in range(n-1,-1,-1):
X[i] = (Z[i] - sum(B[i,j]*X[j] for j in range(i+1,n)))/B[i,i]
return X
X=PivotGauss(A,Y)
A*X==Y
︡0c98421e-9a62-441c-a02d-2b9c8ae4d0b4︡{"stdout":"True\n"}︡{"done":true}︡
︠1959ce3c-a8c8-44e5-9b0e-1c609dc8ed4ds︠
# Version 2 avec assemblage de la matrice (A | Y)
def MatriceGauss(A,Y):
n = A.nrows()
p = A.ncols()
B = Matrix(QQ,n,p+1)
for i in range(n):
for j in range(p):
B[i,j]=A[i,j]
B[i,p]=Y[i]
return B
A = matrix(QQ,[[1, 1, 1], [1, -1, 2], [2, -1, 1]])
Y = vector(QQ,[2,9,7])
show(MatriceGauss(A,Y))
︡84601dda-65c7-43fc-a5a6-8d3b439a74e7︡{"html":"$\\displaystyle \\left(\\begin{array}{rrrr}\n1 & 1 & 1 & 2 \\\\\n1 & -1 & 2 & 9 \\\\\n2 & -1 & 1 & 7\n\\end{array}\\right)$
"}︡{"done":true}︡
︠07ab5b45-4e66-4d9b-a029-9258b915cd07s︠
# Recherche du pivot
def indicepivot(A,j):
n = A.nrows()
p = A.ncols()
i=j
max = 0
for k in [j..n-1]:
if abs(A[k,j])>max:
max=abs(A[k,j])
i=k
return i
indicepivot(A,0)
︡142e2491-68b8-42b6-876d-19f3fac542c7︡{"stdout":"2\n"}︡{"done":true}︡
︠0136ce19-4d52-48cf-88df-4bad527a8c5bs︠
# Echange de lignes
def echangelignes(A,i,j):
temp = A[i,:]
A[i,:]=A[j,:]
A[j,:]=temp
echangelignes(A,1,2)
show(A)
echangelignes(A,1,2)
show(A)
︡c7a3a71b-ee22-4b37-9f29-d9177790f296︡{"html":"$\\displaystyle \\left(\\begin{array}{rrr}\n1 & 1 & 1 \\\\\n2 & -1 & 1 \\\\\n1 & -1 & 2\n\\end{array}\\right)$
"}︡{"html":"$\\displaystyle \\left(\\begin{array}{rrr}\n1 & 1 & 1 \\\\\n1 & -1 & 2 \\\\\n2 & -1 & 1\n\\end{array}\\right)$
"}︡{"done":true}︡
︠e0524c18-5090-485e-a10d-a2018b43f203s︠
# Operations elementaires
def transvection(A,k,i,alpha):
temp=A[k,:]
A[k,:]=temp-alpha*A[i,:]
B=copy(A)
show(B)
transvection(B,1,0,1)
show(B)
transvection(B,2,0,2)
show(B)
︡e3275f0b-8ea7-45c7-9adb-a281fc9b44e9︡{"html":"$\\displaystyle \\left(\\begin{array}{rrr}\n1 & 1 & 1 \\\\\n1 & -1 & 2 \\\\\n2 & -1 & 1\n\\end{array}\\right)$
"}︡{"html":"$\\displaystyle \\left(\\begin{array}{rrr}\n1 & 1 & 1 \\\\\n0 & -2 & 1 \\\\\n2 & -1 & 1\n\\end{array}\\right)$
"}︡{"html":"$\\displaystyle \\left(\\begin{array}{rrr}\n1 & 1 & 1 \\\\\n0 & -2 & 1 \\\\\n0 & -3 & -1\n\\end{array}\\right)$
"}︡{"done":true}︡
︠b1b081bf-24bd-4440-b56b-60346d5a8bdas︠
# Algorithme du pivot
def PivotGauss2(A,Y):
B=MatriceGauss(A,Y)
n=A.nrows()
p=A.ncols()
for j in [0..n-2]:
i=indicepivot(B,j)
echangelignes(B,i,j)
for i in [j+1..n-1]:
pivot=B[i,j]/B[j,j]
transvection(B,i,j,pivot)
X = vector(QQ,[0]*n)
Z = vector(B[:,p])
for i in range(n-1,-1,-1):
X[i] = (Z[i] - sum(B[i,j]*X[j] for j in range(i+1,n)))/B[i,i]
return X
X=PivotGauss2(A,Y)
A*X==Y
show(A*X)
︡38d49aef-614b-4f64-a7a5-576d964fafc5︡{"stdout":"True\n"}︡{"html":"$\\displaystyle \\left(2,\\,9,\\,7\\right)$
"}︡{"done":true}︡
︠088c00b6-7031-4f29-aa7a-0ffb2a6210c0︠