Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An algorithm on mathematica to calculate the determinant of a n*n matrix:

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?

like image 754
John Avatar asked Jan 28 '12 11:01

John


1 Answers

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.

like image 182
Ion Vlad-Doru Avatar answered Sep 30 '22 14:09

Ion Vlad-Doru