CoCalc Shared FilesPivotGauss.sagews
Author: Morgan Morancey
Views : 25
# 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)


[ 1 1 1] [ 1 -1 2] [ 2 -1 1] (2.00000000000000, 9.00000000000000, 7.00000000000000)
# 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)

2
# 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

[ 1.00000000000000 1.00000000000000 1.00000000000000] [ 2.00000000000000 -1.00000000000000 1.00000000000000] [ 1.00000000000000 -1.00000000000000 2.00000000000000] [ 1.00000000000000 1.00000000000000 1.00000000000000] [ 1.00000000000000 -1.00000000000000 2.00000000000000] [ 2.00000000000000 -1.00000000000000 1.00000000000000]
# 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)

$\displaystyle \left(\begin{array}{rrr} 1 & 1 & 1 \\ 1 & -1 & 2 \\ 2 & -1 & 1 \end{array}\right)$
$\displaystyle \left(\begin{array}{rrr} 1 & 1 & 1 \\ 0 & -2 & 1 \\ 2 & -1 & 1 \end{array}\right)$
$\displaystyle \left(\begin{array}{rrr} 1 & 1 & 1 \\ 0 & -2 & 1 \\ 0 & -3 & -1 \end{array}\right)$
# 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

True
# 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))

$\displaystyle \left(\begin{array}{rrrr} 1 & 1 & 1 & 2 \\ 1 & -1 & 2 & 9 \\ 2 & -1 & 1 & 7 \end{array}\right)$
# 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)

2
# 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)

$\displaystyle \left(\begin{array}{rrr} 1 & 1 & 1 \\ 2 & -1 & 1 \\ 1 & -1 & 2 \end{array}\right)$
$\displaystyle \left(\begin{array}{rrr} 1 & 1 & 1 \\ 1 & -1 & 2 \\ 2 & -1 & 1 \end{array}\right)$
# 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)

$\displaystyle \left(\begin{array}{rrr} 1 & 1 & 1 \\ 1 & -1 & 2 \\ 2 & -1 & 1 \end{array}\right)$
$\displaystyle \left(\begin{array}{rrr} 1 & 1 & 1 \\ 0 & -2 & 1 \\ 2 & -1 & 1 \end{array}\right)$
$\displaystyle \left(\begin{array}{rrr} 1 & 1 & 1 \\ 0 & -2 & 1 \\ 0 & -3 & -1 \end{array}\right)$
# 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)

True
$\displaystyle \left(2,\,9,\,7\right)$