Skip to content

Instantly share code, notes, and snippets.

@scroobius-pip
Forked from willhbr/matrix.py
Last active October 17, 2016 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scroobius-pip/8d2c02ae38703cc8de8af1b97b0c6c3b to your computer and use it in GitHub Desktop.
Save scroobius-pip/8d2c02ae38703cc8de8af1b97b0c6c3b to your computer and use it in GitHub Desktop.
Python Matrix Determinant Calculator
#! py
def solve(matrix, mul):
width = len(matrix)
if width == 1:
return mul * matrix[0][0]
else:
sign = -1
total = 0
for i in range(width):
m = []
for j in range(1, width):
buff = []
for k in range(width):
if k != i:
buff.append(matrix[j][k])
m.append(buff)
sign *= -1
total += mul * solve(m, sign * matrix[0][i])
return total
matrix = [[1,-2,3],[0,-3,-4],[0,0,-3]]
print(solve(matrix, 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment