#DETERMINTANTE #print MatrixMinor([[1,0,1],[2,-1,3],[1,4,2]],0,0) def MatrixMinor(x,i,j): y = x[:] del(y[i]) y = zip(*y) del(y[j]) return zip(*y) def MatrixDet(x): l=len(x) if l==1: return x[0][0] j=0 return sum([(-1)**(i+j)*x[i][j]*MatrixDet(MatrixMinor(x,i,j)) for i in range (l)]) #CREA MATRICE def MatrixNew (NC,NR,val=0): return[[val for col in range (NC)] for row in range (NR)] def MatrixTranspose (M): NR = len(M) NC = len(M[0]) return [[M[c][r] for c in range(NC)] for r in range(NR)] #return[[val for col in range (NC)] for row in range (NR)] #MATRICE INVERSA def MatriceInversa(M): col = len(M) row = len(M[0]) if col!=row: raise Exception("Errore Matrice non quadrata") if MatrixDet(M)==0: raise Exception("Det = 0 non invertibile") if MatrixDet(M)!=0: c=MatrixDet(M) #assegna a una variabile il determinante B = MatrixNew (row,row) #col = row for i in range (col): #trova matrice cofattore for j in range (col): B[i][j]=(-1)**(i+j)*MatrixDet(MatrixMinor(M,i,j)) B = MatrixTranspose(B) #fai trasposta print B for i in range (row): for j in range (row): B[i][j] = B[i][j]/float(c) #divide per il determinante print B[i][j] #print "sono il determinante:" #print c #print B return B print MatriceInversa([[1,0,1],[2,-1,3],[1,4,2]])