Advertisement
freswinn

Python: Recursive function to find the determinant of a square matrix of any size

Sep 4th, 2021 (edited)
1,576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.68 KB | None | 0 0
  1. from math import *
  2.  
  3. # Change this matrix here to get a new determinant; make sure it's square!
  4.  
  5. input_matrix = [ [3,4,5], [5,12,13], [1,0,-1] ]
  6.  
  7. # The determinant of a matrix is only applicable to a square matrix; thus, the given_matrix array must be representative of a square matrix.
  8. # The given_matrix array should contain arrays, each of which represent one row of a square matrix.
  9. # Thus, there should be as many elements in each of those arrays as there are arrays.
  10.  
  11.  
  12. ######## I would actually like to note:
  13. # I made this script on an assumption of a recursive pattern existing to find the determinant of any-dimension matrix.
  14. # I am not claiming it holds, mathematically, because I don't actually know if it does!
  15. # This was not so much a project to build a tool for solving a matrix, but a test for my ability to make a recursive function.
  16. # (Also, it seems to run out of steam if you give it a matrix that's too big and I don't know how to get around that)
  17. ######## Thank you.
  18.  
  19.  
  20. def find_determinant( given_matrix ):
  21.  
  22.     print("function running on given_matrix: " + str(given_matrix))
  23.     #error checking first!
  24.     for i in given_matrix:
  25.     #making sure all of the elements of the array are also arrays
  26.         # if not isinstance(i, list):
  27.         #   print(str(i) + " not an array")
  28.         #   return null                             ### TO-DO: should try to get this error-check working for the data type of given_matrix at some point, but I'm tired.
  29.     #making sure the matrix is a square
  30.         if len(i) != len(given_matrix):
  31.             print(str(given_matrix) + " not a square matrix")
  32.             return null
  33.  
  34.  
  35.     matrix_size = len(given_matrix[0])                  #finds the size of the matrix; the goal is to recursively reduce to 2x2 matrices and find their determinants
  36.     if matrix_size == 2:
  37.         return ( (given_matrix[0][0] * given_matrix[1][1]) - (given_matrix[0][1] * given_matrix[1][0]) )    #det(A) = ad-bc
  38.  
  39.  
  40.     determinant = 0.0
  41.     terms = []
  42.     matrix_of_minors = []
  43.  
  44.     for n in range(matrix_size):
  45.         terms.append( given_matrix[0][n] * pow(-1,n) )      #establishes the scalars and cofactors of our terms
  46.         minor = []
  47.    
  48.         for i in range(matrix_size):        #row
  49.             minor_row = []
  50.    
  51.             for j in range(matrix_size):    #column
  52.                 if i != 0 and j != n: minor_row.append(given_matrix[i][j])
  53.    
  54.             if len(minor_row) == matrix_size-1:
  55.                 minor.append(minor_row)
  56.                 minor_row = []
  57.    
  58.         if len(minor) == matrix_size-1:
  59.             matrix_of_minors.append(minor)
  60.             minor = []
  61.  
  62.     for i in range(matrix_size):
  63.         matrix_of_minors[i] = find_determinant( matrix_of_minors[i] )   #recursion!
  64.     for i in range(matrix_size):
  65.         determinant += terms[i] * matrix_of_minors[i]
  66.    
  67.     #print(str(determinant))
  68.     return determinant
  69.  
  70. print("The determinant is: " + find_determinant(input_matrix))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement