Creating your own function Determinant in Mathematica

In summary, the student is trying to find a way to code a recursive function to calculate the determinant of any n x n matrix.
  • #1
santais
18
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): [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.
 
Physics news on Phys.org
  • #2
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): [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.

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:
  • #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]
 
  • #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 :)
 
  • #5


Hi there,

Creating your own function to calculate the determinant of a matrix in Mathematica can be a challenging task, but it's definitely doable. The recursive formula you've been given is a great starting point, but as you mentioned, it can be difficult to implement without getting errors.

One approach you could take is to break down the problem into smaller steps. First, you would need to create a function that can calculate the determinant of a 2 x 2 matrix, as this will be the base case for your recursive function. Once you have this function, you can then use it as a building block for your larger determinant function.

Next, you can create a loop that will iterate through the matrix and calculate the determinant of each submatrix using your 2 x 2 determinant function. Then, using the recursive formula, you can sum up all these determinants to get the final result.

It's important to also consider edge cases, such as when the matrix is empty or when it is not a square matrix. You can add conditional statements in your function to handle these cases.

I hope this helps to give you some ideas and get you started on your assignment. Good luck!
 

1. What is a function determinant in Mathematica?

A function determinant in Mathematica is a special type of mathematical function that calculates the determinant of a given matrix or array. It is a useful tool for solving systems of linear equations and is often used in various mathematical and scientific applications.

2. How do I create my own function determinant in Mathematica?

To create your own function determinant in Mathematica, you can use the built-in function Det, which calculates the determinant of a matrix or array. You can also define your own custom function using the Det function as a template and inputting your desired matrix or array.

3. Can I customize my function determinant to work with specific types of matrices?

Yes, you can customize your function determinant to work with specific types of matrices by using conditional statements or pattern matching in your function definition. This allows you to create a more specialized function that is tailored to your specific needs.

4. Are there any limitations to creating a function determinant in Mathematica?

While there are no inherent limitations to creating a function determinant in Mathematica, it is important to keep in mind the limitations of the Det function itself. For example, the Det function can only calculate the determinant of square matrices, so your custom function should also be limited to square matrices.

5. Can I use my function determinant in other Mathematica functions or programs?

Yes, you can use your function determinant in other Mathematica functions or programs by calling it as you would any other function. This allows you to incorporate your custom function into larger programs or calculations, making it a versatile tool for your scientific work.

Similar threads

Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • Linear and Abstract Algebra
Replies
15
Views
4K
  • Programming and Computer Science
Replies
5
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • Advanced Physics Homework Help
Replies
1
Views
835
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Back
Top