Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am trying to implement a function that finds the determinant of a random matrix consisting of integers. This is the code I have written so far, but I am stuck. Suggestions ? I want to find a solution without Det[M].

M := RandomInteger[{-10, 10}, {8, 8}]

det[M_ /; Dimensions[M][[1]] == Dimensions[M][[2]]] := Module[{i, d},
If[N == 2,
Drop[Mi, {1, 1}, {1, 1}][[1, 1]]*Drop[Mi, {1, 1}, {1, 1}][[2, 2]] -
 Drop[Mi, {1, 1}, {1, 1}][[1, 2]]*
 Drop[Mi, {1, 1}, {1, 1}][[2, 1]], 
For[i = 1, i <= Length[M], i++, 
d = d + (-1)^(1 + i) *M[[1, i]]*
   Minors[Drop[M, {1, 1}, {i, i}], 
      Length[Drop[M, {1, 1}, {i, i}]]][[1]][[1]]]]; 
Return[d, Module]]
det1[Mi_ /; Dimensions[Mi][[1]] == Dimensions[Mi][[2]]] := 
Module[{det1},
det1 = Sum[
If[det1 == 1, 
 Break[], (-1)^(1 + j) *Mi[[1, j]]*det1[Drop[Mi, {1}, {j}]]], {j, 
 1, Length[Mi]}]; Return[det1 // MatrixForm, Module]]
det1[M_ /; Dimensions[M][[1]] == Dimensions[M][[2]]] :=
Module[{i, d = M},
For[i = 1, i <= Length[d], i++,
If[Length[M] == 1, Goto[end]
 If[Length[M] == 2, 
  d = Drop[d, {1}, {1}][[1, 1]]*Drop[d, {1}, {1}][[2, 2]] - 
    Drop[d, {1}, {1}][[1, 2]]*Drop[d, {1}, {1}][[2, 1]]; 
  Goto[end], d = d + (-1)^(1 + i) *d[[1, i]]*Drop[d, {1}, {i}]];
Label[end]; Return[d, Module]]]]
det1[M_ /; Dimensions[M][[1]] == Dimensions[M][[2]]] :=
Module[{i, d = M},
For[i = 1, i <= Length[d], i++,
 Switch[d, 1, Goto[end], 2, 
d = Drop[d, {1}, {1}][[1, 1]]*Drop[d, {1}, {1}][[2, 2]] - 
  Drop[d, {1}, {1}][[1, 2]]*Drop[d, {1}, {1}][[2, 1]]; 
Goto[end], _,
d = d + (-1)^(1 + i) *d[[1, i]]*Drop[d, {1}, {i}]];
 Label[end]; Return[d, Module]]]
Return[Determinant[Mi]]
share|improve this question
1  
How about Det[M] ? –  kirma Dec 19 '13 at 11:18
    
I want to find a solution without Det[M] –  Im a robot Dec 19 '13 at 11:41
1  
Looking at your first attempt, some of the problems are: N is undefined (also N is a built-in function), Mi is undefined, d is undefined. Drop[Mi, {1, 1}, {1, 1}][[1, 1]] is a very long-winded way to do Mi[[2, 2]]. Return is unnecessary (just put d as the last expression in the module). –  Simon Woods Dec 19 '13 at 13:32
    
I wont even try to read code with Goto's... –  george2079 Dec 19 '13 at 22:06
    
Question sounds like a homework exercise. –  murray Dec 19 '13 at 22:26
add comment

3 Answers

up vote 2 down vote accepted

A recursive implementation..

  mm = RandomInteger[{-10, 10}, {#, #}] &@9;
  det[m_?MatrixQ /; (Equal @@ Dimensions@m && Length@m > 1 )] :=
  Sum[
      (-1)^(i + 1) m[[1, i]]
      det[m[[2 ;;, Drop[Range[Length[m]], {i}]]] ] ,
      {i, Length[m]}];
  det[m_List  /; Dimensions[m] == {1, 1} ] := First@First@m
  det[mm] // Timing (* {8.346054, 1259312020} *)
  Det[mm] // Timing (* {0., 1259312020} *)

For academic illustration only..

share|improve this answer
add comment

What about using the Leibniz formula? Here is my approach that should work for any n x n Matrix:

Generate the Matrix:

Clear[m];
m = RandomInteger[{-10, 10}, {4, 4}];

Determine its dimension:

If[
 Length[Union[Dimensions[m]]] == 1,
 n = Union[Dimensions[m]][[1]]
 ]

Determine the permutations for the Leibniz formula:

perm = Permutations[
   Range[1, n]
   ];

Calculate the determinant:

det = Sum[
  Signature[perm[[i]]] * Product[m[[k, perm[[i, k]]  ]], {k, 1, Length[perm[[i]]]}
    ],  {i, 1, Length[perm]}]

Done.

Why are you not using the Det-function?

share|improve this answer
    
Thank you. much appreciated! –  Im a robot Dec 19 '13 at 17:36
    
Im trying to time different approches i have now 3 different methods. –  Im a robot Dec 19 '13 at 17:51
add comment

The product of the eigenvalues is equal to the determinant, so you could program:

n = RandomInteger[{0, 10}, {5, 5}];
Chop[N@Eigenvalues[n] /. List -> Times]

Note that

Chop[N@Eigenvalues[n] /. List -> Times] == Det[n]
True
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.