Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Creating your own function Determinant in Mathematica

  1. Dec 14, 2011 #1
    Hi guys.

    So I got this assignment, where I have to create my own function, which can calculate the determinant of any n x n matrix.

    The general formula we've been given is (recursive formula): [itex]det(A) = Sum[n,i=1] (-1)^{1+i} * a_{1 i} * det(A_{1 i})[/itex] where n is the length of the matrix.

    The first lines are quite easy to compute: [itex]det(A) = Sum[n,i=1] (-1)^{1+i} * a_{1 i}[/itex]

    However finding the determinant just seems like an impossible thing to do. It's fairly easy to calculate with a 2 x 2 matrix, but once it exceeds 2 x 2, you just get a new matrix instead of a number. I know you have to make some loop of some kind, but I just can't figure out how. We are, of course, not allowed to use the built in Det[...] function in any way. Otherwise it would had been completed in a few seconds

    I know you have to use the recursive function inside the function, however that just brings up a million errores :)

    So I hope there was some expert out there, who could give a hint or some help of some kind, so I get going with this assignment.

    On the beforehand thanks.
     
  2. jcsd
  3. Dec 14, 2011 #2
    I can give you a plan:

    First thing: scrap recursion initially just to get the mechanism down

    Second thing: initially focus entirely on just a 3x3 matrix

    Third: Your method above is using the technique of expansion by minors which means given a matrix, you extract the sub-matrix by removing the i'th row and j'th column. You know how to do that? Yeah, me neither. Need to figure that out. For example, if I have the test case:

    mymatrix={{1,2,3},{4,5,6},{7,8,9}}

    and I want to extract the minor which is the first row and first column removed. What's the best way to do that? I don't know. Could just brute-force get it but I bet there is a more elegant way to get it.

    Now, once you have that, just code a regular program to find the determinant using the technique. Then modify your code for a 4x4 then higher, then try to modify your code for nxn matrix. So now at least you have the principle down. Now it's time to look into recursion in Mathematica. Start with the simple factorial formula:

    f[x_] := If[x == 1, 1, x f[x - 1]]
    f[5]

    Notice how we have the test to stop recursion when x=1. In your case, would it not be when the minor is a 2x2 matrix? Now, is there any way in the world we can modify that simple recursive formula to do just a 3x3 matrix even if it means the code is initially crummy and won't (initially) work for any higher order? Try that for a while and if you run into problems, then need to put that up for a while and find other examples of recursion in Mathematica and study them, tinker with those for a while, then come back to this problem.
    Also look at Wikipedia:
    http://en.wikipedia.org/wiki/Determinant
    under "Laplace's formula for adjuate matrix". Bet you can code that for just 3x3.
     
    Last edited: Dec 14, 2011
  4. Dec 14, 2011 #3
    Alright, that wasn't too bad. Can you first code a routine to extract the minors? Like for example I have:

    mymatrix = {{1, -2, 3}, {4, 5, -6}, {-7, 8, 9}};

    and I write a routine to get the minors called getMinors:

    myminors=getMinors[mymatrix]

    to get:

    myminors={{{5, -6}, {8, 9}}, {{4, -6}, {-7, 9}}, {{4, 5}, {-7, 8}}}

    You write that routine and post it and if you want, I'll help you write the recursive routine to get the determinant which uses getMinors or you can post your code. It should only be a few lines and we'd call it like:

    getMyDeterminant[mymatrix]
     
  5. Dec 15, 2011 #4
    Wow that helped a lot :D

    Tried to break it up, as you said. Also did look into recursive functions more specifically, which gave me a much better idea on, how it actually worked.

    So got it working now. Thanks a lot for the help :)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Creating your own function Determinant in Mathematica
  1. Mathematica functions (Replies: 1)

Loading...