Advertisement
TornioSubito

Class Matrix

Jun 29th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. class Matrix:
  2. def __init__(self,numrows,numcolumns):
  3. #@param numrows : int
  4. #@param numcolumns : int
  5. self.row=numrows
  6. self.col=numcolumns
  7. self.mat=[]
  8. for i in range(0,numrows):
  9. self.mat.append([])
  10. for j in range(0,numcolumns):
  11. self.mat[i].append(0)
  12.  
  13. def minor(self,j):
  14. #@ come riga di riferimento si prende i=0
  15. #@param j : int (colonna da cancellare)
  16. #@return matrix (minore della matrice ottenuto cancellando riga 0 e colonna j)
  17. self.mat.remove(self.mat[0])
  18. for h in range (0,self.col-1):
  19. self.mat[h].pop(j)
  20. print_matrix(self.mat)
  21. return self.mat
  22.  
  23. def det(self,D=0):
  24. #@ (metodo basato su ricorsione, essendo lo sviluppo di Laplace una sommatoria)
  25. #@ return int : determinante della matrice
  26. assert self.row==self.col
  27. if self.row==1:
  28. return self.mat[0]
  29. elif self.row==2:
  30. D=D+((self.mat[0][0]*self.mat[1][1])-(self.mat[0][1]*self.mat[1][0]))
  31. return D
  32. else:
  33. #@sviluppo di Laplace
  34. for j in range (0,self.row):
  35. D=D+(pow((-1),(j+2))*self.mat[0][j]*det(self.minor(self,j))) #@Qui mi da errore, non riesco ad impostare la ricorsiva
  36. return D
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement