This code finds the determinant of square matrices by reducing the size of the matrix from NxN to (N-1)x(N-1), until the final matrix is 2x2. (Using recursion is inefficient and slow for actually computing determinants, use some online tool instead. I wrote it just for the fun of doing it recursively)
My favorite algorithm for finding the determinant of matrices, is using Laplace Expansion. For matrices bigger than
As it's my favorite method, i tried to code a program that finds the determinant by doing it using said method. (It also uses recursion, which i find really cool, even though i am great at breaking things with it.)
To better illustrate how the program works, we are going to compute the determinant of a general
Doing the same thing with a 4x4 matrix, youwould first reduce every matrix to 3x3, and then to calculate its determinant, reduce it further to 2x2.
The basic idea is: 'Hold' the term
Then, you do it for all the terms on the first row, but alternate the signs. In the end you should have:
Where
You could do it with other rows, not only the first one, but then you would need to change the signs accordingly, which is a little bit harder. So as a quick introduction, stick to
Running the code as is, will compute the determinant of some random matrix, without showing you the 'sub-matrices', which makes this code basically pointless and inefficient, since there are quicker and easier algorithms to compute the determinant.
If you want to see the sub-matrices, open the 'determinant_functions.py' file, and uncomment the print
statement on line 35.
Also, if you, for some reason, wants to use this code to compute your determinants, you should change the matrix first, by uncommenting the 'matrix_to_calculate' and changing it as you wish.
Since it is not very useful outside of having fun with it, i don't think i'll be developing some sort of GUI, or easier-to-use way to run it, so for the time, we'll have to stick with changing the source code and running it with your favorite IDE/Editor/Terminal.
To run it from terminal, try:
$ python determinant_calculator.py
Or
$ python3 determinant_calculator.py