Advertisement
furas

Maple - zestaw 6 - zadanie 1

Jun 12th, 2015
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.00 KB | None | 0 0
  1. # http://pl.wikipedia.org/wiki/Rozwini%C4%99cie_Laplace%E2%80%99a
  2.  
  3. #-------------------------
  4. # zadanie 1
  5. #-------------------------
  6.  
  7. def det(matrix):
  8.    
  9.     n = len(matrix)
  10.    
  11.     if n == 1:
  12.         return matrix[0][0]
  13.    
  14.     result = 0
  15.    
  16.     for column in range(n):
  17.         # powielanie macierzy bez zerowego wiersza
  18.         submatrix = [matrix[i][:] for i in range(1,n)]
  19.        
  20.         # usuwanie z podmacierzy wybranej kolumny
  21.         for i in range(n-1):
  22.             del submatrix[i][column]
  23.  
  24.         # wyliczanie rekurencyjnie
  25.         result += matrix[0][column] * (-1)**column * det(submatrix)
  26.  
  27.         #print column, submatrix, matrix[0][column], (-1)**column
  28.                
  29.     return result
  30.  
  31. #-------------------------
  32. # przyklad m4 z wikipedii - pod hasłem "Rozwinięcie Laplace'a"
  33. m4 = [
  34.     [0,1,2,7],
  35.     [1,2,3,4],
  36.     [5,6,7,8],
  37.     [-1,1,-1,1]
  38. ]
  39.  
  40. m2 = [ [7,8], [-1,1] ]
  41.  
  42. m1 = [[-3]]
  43.  
  44. print det(m1) # -3
  45. print det(m2) # 15
  46. print det(m4) # -64
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement