Creating your own function Determinant in Mathematica

Click For Summary

Discussion Overview

The discussion revolves around creating a custom function in Mathematica to calculate the determinant of an n x n matrix. Participants explore the recursive nature of the determinant calculation, share strategies for handling matrix minors, and discuss the challenges of implementing the function without using built-in methods.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related
  • Mathematical reasoning

Main Points Raised

  • One participant describes the recursive formula for the determinant and expresses difficulty in implementing it for matrices larger than 2 x 2.
  • Another participant suggests starting with a simpler approach by focusing on a 3 x 3 matrix and extracting minors, indicating uncertainty about the best method to do so.
  • A later reply proposes coding a routine to extract minors from a matrix and offers to assist with writing the recursive determinant function once that is accomplished.
  • One participant reports success in breaking down the problem and gaining a better understanding of recursive functions after following the advice given.

Areas of Agreement / Disagreement

Participants generally agree on the approach of breaking the problem down into smaller parts, particularly focusing on extracting minors and understanding recursion. However, there is no consensus on the specific implementation details or methods to achieve the final determinant function.

Contextual Notes

Participants express uncertainty about the best ways to extract minors and implement recursion, indicating that the discussion is exploratory and lacks definitive solutions.

Who May Find This Useful

Readers interested in programming in Mathematica, particularly those working on assignments related to linear algebra and matrix operations, may find this discussion helpful.

santais
Messages
18
Reaction score
0
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): det(A) = Sum[n,i=1] (-1)^{1+i} * a_{1 i} * det(A_{1 i}) where n is the length of the matrix.

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

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.
 
Physics news on Phys.org
santais said:
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): det(A) = Sum[n,i=1] (-1)^{1+i} * a_{1 i} * det(A_{1 i}) where n is the length of the matrix.

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

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.

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:
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]
 
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 :)
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K