0% found this document useful (0 votes)
7 views3 pages

Matricev F

The document defines a Matrice class to represent matrices in Python. The class can initialize matrices, access and modify elements, add, multiply, take the dot product of matrices, multiply by a scalar, calculate the transpose, and more. It also defines a MatriceC subclass for square matrices to calculate the trace.

Uploaded by

Ghaith Sellami
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Matricev F

The document defines a Matrice class to represent matrices in Python. The class can initialize matrices, access and modify elements, add, multiply, take the dot product of matrices, multiply by a scalar, calculate the transpose, and more. It also defines a MatriceC subclass for square matrices to calculate the trace.

Uploaded by

Ghaith Sellami
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

# -*- coding: utf-8 -*-

"""
Created on Mon May 6 20:15:23 2024

@author: hamdi
"""

class Matrice :
nbMat=0
def __init__(self,l,c):
try:
assert type(l)==type(c)==int
assert l > 0 and c > 0
self.l=l
self.c=c
# Utilisation d'une compréhension de liste
self.val = [[0] * c for i in range(l)]
Matrice.nbMat+=1
except AssertionError:
print("Erreur:Création impossible ...")
def __getitem__(self,t):
try:
i,j=t
assert i in range(self.l) and j in range(self.c)
return self.val[i][j]
except AssertionError:
print("Indice hors limites")
def __setitem__(self,t,v):
try:
i,j=t
assert i in range(self.l) and j in range(self.c)
assert type(v) in (int , float)
self.val[i][j]=v
except AssertionError:
print("Erreur lors de l'assignation...")
def __str__(self):
S=""
for i in range(self.l):
for j in range(self.c):
S+=str(self[i,j])+" "
S+="\n"
return S
def Remplir(self):
for i in range(self.l):
for j in range(self.c):
c=float(input("donnez l'elt{},{}:".format(i,j)))
self[i,j]=c

1
def __add__(self,other):
try:
assert isinstance(other,Matrice)
assert self.l==other.l
assert self.c == other.c
S=Matrice(self.l,self.c)
for i in range(self.l):
for j in range(self.c):
S[i,j]=self[i,j]+other[i,j]
return S
except AssertionError:
print("Erreur lors de l'addition...")
def __mul__(self,other):
try:
assert isinstance(other,Matrice)
assert self.l==other.l
assert self.c == other.c
M=Matrice(self.l,self.c)
for i in range(self.l):
for j in range(self.c):
M[i,j]=self[i,j]*other[i,j]
return M
except AssertionError:
print("Erreur lors de la multiplication...")
def dot(self,other):
try:
assert isinstance(other,Matrice)
assert self.c==other.l
P=Matrice(self.l,other.c)
for i in range(self.l):
for j in range(other.c):
for k in range(self.c):
P[i,j]+=self[i,k]+other[k,j]
return S
except AssertionError:
print("Erreur...")
def scalmul(self, scalar):
try:
assert isinstance(scalar, (int, float))
result = Matrice(self.l, self.c)
for i in range(self.l):
for j in range(self.c):
result[i, j] = self[i, j] * scalar
return result
except AssertionError:
print("Erreur lors de la multiplication scalaire...")
def transpose(self):

2
T=Matrice(self.c,self.l)
for i in range(T.l):
for j in range(T.c):
T[i,j]=self[j,i]
return T
class MatriceC(Matrice):
def __init__(self,o):
super().__init__(o,o)
def trace(self):
tr=0
for i in range(self.l):
for j in range(self.c):
if i==j:
tr+=self[i,j]
return tr

You might also like