def __init__(self,n,p): #initialisation de la matrice
self.nl=n
self.nc=p
mat=[]
for i in range(n):
ligne = []
for j in range(p):
ligne.append(0)
mat.append(ligne)
self.matrix=mat
def load(self,tableau): #permet de convertir un 2d-array en matrice
if len(tableau) != self.nl or len(tableau) != self.nc:
return ("error : Bad dimensions")
for i in range(self.nl):
for j in range(self.nc):
self.matrix[i][j]=tableau[i][j]
def __getitem__(self,index):
return self.matrix[index]
def __setitem__(self,index,value):
self.matrix[index]=value
def __repr__(self):#permet de représenter une matrice
repres=""
for i in range(self.nl):
repres=repres+str(self.matrix[i])
if i != self.nl-1:
repres=repres+"\n"
return repres
def __str__(self):# permet d'afficher en format matrice
mat=""
if self.matrix != []:
if self.nl ==1 :
return str(self.matrix)
else:
for i in range(self.nl):
mat+= str(self.matrix[i]) + "\n"
return mat
else:
return "[]"
def matri(self):#permet de rentrer les valeurs dans une matrice à partir de l'utilisateur
mat =[]
for i in range(self.nl):
lig1=[]
for j in range(self.nc):
print("Entrer l'élément Xij ? i: ", i+1, " j:", j+1)
lig1.append(int(input()))
mat.append(lig1)
self.matrix=mat
def __mul__(self,other):#permet de multiplier une matrice avec une autre
matreturn = matrix(self.nl,self.nc)
if isinstance(other,matrix):
if (self.nc != other.nl):
return ("error: bad dimension")#renvoie "mauvaise dimension" dans le cas de deux matrice qui ne peuvent se multiplier
for i in range(self.nl):
for j in range(self.nc):
x=0
for ind in range(self.nc):
x=x+self[i][ind]*other[ind][j]
matreturn[i][j]=x
else:
for i in range(self.nl):
for j in range(self.nc):
matreturn[i][j]=other*self[i][j]
return matreturn
def __len__(self):#renvoie la longueur d'une matrice carré
return self.nl
def nbrelemt(self): #renvoie le nombre total d'éléments le rappeler nbr element
return self.nl*self.nc
def deter(A,n):#renvoie le déterminant de la matrice
det=0
t = [[0 for i in range (len(A))] for j in range (len (A))]
if n==1:
return A[0][0]
elif n==2:
det=(A[0][0]*A[1][1]-A[0][1]*A[1][0])
return det
else :
for p in range(n):
h=0
k=0
for i in range(1,n):
for j in range(n):
if j==p:
continue
t.matrice[h][k]=A[i][j]
k=k+1
if k==n-1:
h=h+1
k=0
det=det+A[0][p]*((-1)**p)*det(t,n-1);
return det
def transpose(self): #renvoie la matrice transposée
t = matrix(self.nl,self.nc)
for i in range(self.nc):
for j in range(self.nl):
t[i][j]= self[j][i]
return t
def coeff(self,i,j):#renvoie les coefficients de la matrice
return self[i][j]
def multcoef(A,r):# permet de calculer la multiplication d'un matrice avec un nombre réel
"Donne le produit d'une matrice A par le coefficient r"
N =len(A)
print(N)
Mat =matrix(A.nl,A.nc)
for i in range(1,A.nl):
for j in range(A.nc):
Mat[i][j]=A[0][0]*N
for i in range(N):
for j in range(N):
Mat[i][j]=A[0][0]*N
for j in range(N):
Mat[i][j]=r*A[i][j]
return Mat
def suppLigne(self,i):#renvoie la matrice avec la suppression d'une ligne
matSortie = matrix(self.nl - 1, self.nc)
for x in range (self.nl - 1) :
for y in range (self.nc) :
if x < i :
matSortie[x][y] =self.matrix[x][y]
else :
matSortie[x][y] = self.matrix[x+1][y]
print(matSortie)
return matSortie
def suppColonne(self,j):# renvoie la matrice avec la suppression d'une colonne
matSortie = matrix(self.nl, self.nc - 1)
for x in range (self.nl) :
for y in range (self.nc - 1) :
if y < j :
matSortie[x][y] =self.matrix[x][y]
else :
matSortie[x][y] = self.matrix[x][y+1]
print(matSortie)
return matSortie
def matriceSuppLC(self,i,j):
return self.suppLigne(i).suppColonne(j)
def cofacteur(self,i,j):#renvoie les cofacteurs de la matrice
return (-1)**(i+j)*self.matriceSuppLC(i,j).deter(self, self.nl)
def comatrice(self):#renvoie la comatrice d'une matrice
comatr = matrix(self.nc,self.nl)
if self.nl==1 :
return self.matrix[0]
if self.nl==2 :
comatr.matrix[0][1] = -self.matrix[1][0]
comatr.matrix[1][0] = -self.matrix[0][1]
comatr.matrix[1][1] = self.matrix[0][0]
comatr.matrix[0][0] = self.matrix[1][1]
else :
for i in range (self.nc):
for j in range(self.nl):
comatr[i][j] = self.cofacteur(i,j)#rentre chaque cofacteur en i,j pour remplir la comatrice
print(self.cofacteur(i,j))
return comatr
def inverse(A):#renvoie l'inverse de la matrice
"Donne l'inverse d'une matrice carrée A"
B=matrix(A.nl,A.nc)
inverse=matrix(A.nl,A.nc)
d=A.deter(A.nl)
if d==0:
return 'La Matrice n\'est pas inversible'
else:
for i in range(1,A.nl):
for j in range(A.nc):
B =(A.comatrice()).multcoef(1/d)
inverse=B.transpose()
return inverse
Comme ça