I am working on an algorithm which calculates the determinant of any n*n matrix, here is my code:
Laplace[matrix_List] := Module[{a = matrix, newmatrix, result = 0},
If [Length[a] == 1, result = Total[Total[a]],
For [i = 1, i <= Length[a], i++,
newmatrix = Drop[a, {i}, {1}];
result = result + (-1)^(i + 1) *
Total[Total[Take[a, {i}, {1}]]]*
Laplace[newmatrix];
]
]; result]
It works recursively, it works for a 2*2 matrix(I have checked with Det[]), but it doesn't work for any matrix of higher degree than 2!
I would like to solve this solution myself - I want to implement this myself, rather than simply using Det
- but I would appreciate it if someone could explain what is wrong with the recursion here?
You should not calculate the determinant in a recursive way, it takes a lot of time. The simplest method is to take the first column and see if there is an element different from 0. If there isn't then the determinant is equal to 0. Otherwise take that element and for every line in the matrix different from that of the chosen element substract the line of the chosen element multiplied with the symetric of the first element of the current line. That substraction should leave you with a line which has 0 as its first element. Then you can eliminate the first column and the line of the chosen element and multiply the n-1 order determinant with (-1)^(line_index+column_index)*chosen_element.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With