Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # http://pl.wikipedia.org/wiki/Rozwini%C4%99cie_Laplace%E2%80%99a
- #-------------------------
- # zadanie 1
- #-------------------------
- def det(matrix):
- n = len(matrix)
- if n == 1:
- return matrix[0][0]
- result = 0
- for column in range(n):
- # powielanie macierzy bez zerowego wiersza
- submatrix = [matrix[i][:] for i in range(1,n)]
- # usuwanie z podmacierzy wybranej kolumny
- for i in range(n-1):
- del submatrix[i][column]
- # wyliczanie rekurencyjnie
- result += matrix[0][column] * (-1)**column * det(submatrix)
- #print column, submatrix, matrix[0][column], (-1)**column
- return result
- #-------------------------
- # przyklad m4 z wikipedii - pod hasłem "Rozwinięcie Laplace'a"
- m4 = [
- [0,1,2,7],
- [1,2,3,4],
- [5,6,7,8],
- [-1,1,-1,1]
- ]
- m2 = [ [7,8], [-1,1] ]
- m1 = [[-3]]
- print det(m1) # -3
- print det(m2) # 15
- print det(m4) # -64
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement