Skip to content

Matrix Determinant #235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions Matrix/Matrix Determinant/Matrix-Determinant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
def determinant(matrix): #Optimized solution
#your code here
result = 0
l = len(matrix)

#base case when length of matrix is 1
if l == 1:
return matrix[0][0]

#base case when length of matrix is 2
if l == 2:
return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]

#for length of matrix > 2
for j in range(0, l):
# create a sub matrix to find the determinant
if l!=2:
sub_matrix = []
sub_matrix = [(row[0:j]+row[j+1:]) for row in matrix[1:]]
result = result + (-1)**j * matrix[0][j] * determinant(sub_matrix)
return result

def det(matrix): #My (perhaps easier to understand) solution
if len(matrix)==1: #Base Case
return matrix[0][0]
elif len(matrix)==2: #Base Case
return matrix[0][0]*matrix[1][1] - matrix[1][0]*matrix[0][1]

else:
sub_m = [] #List containing sub matrixes
for z in range(len(matrix[0])):
sub_m.append([]) #Create an empty list for all sub matrixes
for y in range(1,len(matrix)):
sub_m[z].append([]) #Create rows for each sub matrix
for x in range(len(matrix[y])):
if x != z: #Check if sub matrix doesn't include the row of the multiplying value
sub_m[z][y-1].append(matrix[y][x]) #Append individual values to sub matrix
total = 0
for x in range(len(matrix[0])): #cycle through matrix[0] values
if matrix[0][x] != 0:
total += ((-1) ** x) * matrix[0][x] * det(sub_m[x]) #multiply them by the determinant of the sub matrix, creationg the recursion
else:
continue
return total #in the end returns the determninant



17 changes: 17 additions & 0 deletions Matrix/Matrix Determinant/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Matrix Determinant

The determinant of a matrix is a function of the entries of a square matrix.

A Matrix Determinant can be calculated like so:

![](https://www.chilimath.com/wp-content/uploads/2022/05/determinant-3x3-matrix-example-4.png)

The input should be a 2 dimensional square array (of ```nxn``` size)

The output should be an integer that is the matrix determinant

# Recursive Method

1) Create a fuction that takes the matrix as the only argument
2) Return the determinant for length 1x1 (```matrix[0][0]```) and 2x2 (```(matrix[0][0]*matrix[1][1]) - (matrix[1][0]*matrix[0][1])```) arrays directly
3) Cycle trough the first row of the array (```x``` 1-4 for a length 4 array) and sum/subtract the product of each value and the determinant of the sub array (```x1*determinant - x2*determinant + x3*determinant - x4*determinant``` for a length 4 matrix) and repeat the cycle for the ensuing sub arrays of decreasing length