1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| class Matrice:
def __init__(self,lignes,colonnes,L=[]):
self.m=lignes
self.n=colonnes
if len(L)!=self.m*self.n:
self.coeffs=self.m*self.n*[0]
else:
self.coeffs=L
def coeff(self,i,j):
return self.coeffs[i*self.n+j]
def affiche(self):
for i in range(0,self.m):
for j in range(0,self.n):
print "%6.2f"%self.coeff(i,j),
print
print
def __mul__(self,other):
if self.n != other.m:
print "Multiplicaton impossible"
return
R=Matrice(self.m,other.n)
for i in range (0,R.m):
for j in range (0,R.n):
c=0
for k in range(0,self.n):
c+=self.coeff(i,k)*other.coeff(k,j)
R.coeffs[i*R.n+j]=c
return R
def main():
M=Matrice(2,2,[1,2,3,4])
K=M*M
K.affiche()
if __name__ == '__main__':
main() |
Partager