Skip to content
Snippets Groups Projects
Commit afee7e8b authored by rpages's avatar rpages
Browse files

travail

parent a2351efd
Branches master
No related tags found
No related merge requests found
class matrice():
def __init__(self,l,_format=None):
self.liste=l
a=len(l[0])
if not _format:
for i in l:
if len(i)!=a:
raise ValueError('All lines do not share same length')
self.format=(len(l),a)
else:
self.format=_format
def __repr__(self):
l=self.liste
lr=[]
for i in l:
ch='['
for j in i:
ch=ch+' {} '.format(j)
lr.append(ch+']')
return('\n'.join(lr))
def __add__(self,other):
if not isinstance(other,matrice):
raise TypeError("other(={}) n'est pas une matrice")
l1=self.liste
l2=other.liste
if not self.format==other.format:
raise ValueError('matrices do not share the same format')
n,m=self.format
l=[[l1[i][j]+l2[i][j] for j in range(m)] for i in range(n)]
return(matrice(l,(n,m)))
def __mul__(self,other):
if not isinstance(other,matrice):
raise TypeError("other(={}) n'est pas une matrice")
l1=self.liste
l2=other.liste
if not self.format[1]==other.format[0]:
raise ValueError("matrices are not of formats compatible for matrice multiplication")
n,k=self.format
k1,m=other.format
l=[[sum(l1[i][l]*l2[l][j] for l in range(k)) for j in range(m)] for
i in range(n)]
return(matrice(l,(n,m)))
......@@ -6,13 +6,38 @@ class Matrice:
self._nrows = nrows
def __repr__(self):
raise NotImplementedError
l=self.liste
lr=[]
for i in l:
ch='['
for j in i:
ch=ch+' {} '.format(j)
lr.append(ch+']')
return('\n'.join(lr))
def __add__(self, other):
raise NotImplementedError
if not isinstance(other,matrice):
raise TypeError("other(={}) n'est pas une matrice")
l1=self.liste
l2=other.liste
if not self.format==other.format:
raise ValueError('matrices do not share the same format')
n,m=self._nrows,self._ncols
l=[[l1[i][j]+l2[i][j] for j in range(m)] for i in range(n)]
return(matrice(m,n,l))
def __mul__(self, other):
raise NotImplementedError
if not isinstance(other,matrice):
raise TypeError("other(={}) n'est pas une matrice")
l1=self.liste
l2=other.liste
if not self._ncols==other._nrows:
raise ValueError("matrices are not of formats compatible for matrice multiplication")
n,k=self._nrows,self._ncols
k1,m=other._nrows,other._ncols
l=[[sum(l1[i][l]*l2[l][j] for l in range(k)) for j in range(m)] for
i in range(n)]
return(matrice(m,n,l))
def nombre_de_colonnes(self, other):
raise NotImplementedError
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment