SharedPivotGauss.sagewsOpen in CoCalc
# 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)
(111112211)\displaystyle \left(\begin{array}{rrr} 1 & 1 & 1 \\ 1 & -1 & 2 \\ 2 & -1 & 1 \end{array}\right)
(111021211)\displaystyle \left(\begin{array}{rrr} 1 & 1 & 1 \\ 0 & -2 & 1 \\ 2 & -1 & 1 \end{array}\right)
(111021031)\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))
(111211292117)\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)
(111211112)\displaystyle \left(\begin{array}{rrr} 1 & 1 & 1 \\ 2 & -1 & 1 \\ 1 & -1 & 2 \end{array}\right)
(111112211)\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)
(111112211)\displaystyle \left(\begin{array}{rrr} 1 & 1 & 1 \\ 1 & -1 & 2 \\ 2 & -1 & 1 \end{array}\right)
(111021211)\displaystyle \left(\begin{array}{rrr} 1 & 1 & 1 \\ 0 & -2 & 1 \\ 2 & -1 & 1 \end{array}\right)
(111021031)\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
(2,9,7)\displaystyle \left(2,\,9,\,7\right)
f=12/500
z=1.96
n=500
q=100
t=z*sqrt(f*(1-f))/sqrt(n-1)

a=q/(f+t)
b=q/(f-t)
a.n()
b.n()
2671.74134611723 9459.63971169641