Code for determinant.

I would like to built a code to find the determinant of a 24x24 matrix.I dont want to use the det(X) function,but a fuction that would be quick.
The one i created,whick is too slow for that.

2 Commenti

Stephen23
Stephen23 il 15 Nov 2018
"I dont want to use the det(X) function,but a function that would be quick."
Ah, the sweet, blind optimism of youth.
Have you actually looked at the definition of the determinant, and figured out how many calculations it would require for a 24x24 matrix? When is your estimated completion?
eden hazard
eden hazard il 15 Nov 2018
Haha it is the optimism of my teacher. The fuction should calculate the determinant in 5-30 minutes

Accedi per commentare.

 Risposta accettata

John D'Errico
John D'Errico il 15 Nov 2018
NO NO NO!!!!!
First, you should not be using the determinant here. I don't know why you think you need to use it. But if you don't understand how to efficiently compute a determinant, the probability is 1 that you should not be using the determinant in the first place. There are far better tools to determine is amatrix issingular, which is why you are surely wanting to compute the determinant of a 24x24 matrix.
Ok, why is the code you wrote a an obscenely bad one? Do you understand that the code you wrote will require O(factorial(24)) computations to execute?
factorial(24)
ans =
6.20448401733239e+23
Even the largest supercomputer the NSA owns would grind to a miserable halt.
So, CAN you compute the determinant more efficiently? Well, yes. USE DET! (In fact there are ways to compute the determinant efficiently. But since you should not be using it in the first place, I won't go into details.)
So better yet, don't compute the determianant at all. So instead, you need to learn how to use tools like rank, cond or svd to do what you are trying to do. Of course, we don't really yet know what you are doing.

6 Commenti

eden hazard
eden hazard il 15 Nov 2018
For an assignment i have for the university i have to calculate the det of a 24x24 matrix by using a fuction that was built by me, not the det fuction MATLAB already have.So, i would like to know if any of you will give me a function that works better than mine :P
Walter Roberson
Walter Roberson il 15 Nov 2018
So a 6 core i7-8700 can handle about 200 gigaflops, so 200e9 = 2e11 multiplication per second . The determinant of a 24 x 24 matrix involves adding 24 factorial terms each involving 23 multiplications. 24 factorial times 23 is about 1.4e25 operations .
14e24 divided by 2e11 is about 7e13. Thus such a system would take about 70 trillion seconds . Roughly 2 1/4 million years .
Now suppose you had aa computational breakthrough and managed to compute it a million times faster . You still would not finish for over 2 years .
eden hazard
eden hazard il 15 Nov 2018
Yes i understand that. But my teacher want us to use our function to calculate the determinant...so i just asked if there is a way!
Steven Lord
Steven Lord il 15 Nov 2018
There are ways to compute the determinant more quickly, but the approach using minors / cofactors is not going to be quick. Other posters (including John and Walter) have stated just how not-quick that approach will be.
Look at the properties of the determinant and see if one of those properties suggests an alternate approach that will be faster. Alternately see the calculation section of the Wikipedia page for yet another approach and a caution about when to use them and when not to use them.
Calculating the determinant because your professor has assigned you to do so as part of your homework is one time to use the determinant. Determining whether a matrix is singular or solving a system of linear equations are examples of when not to use the determinant as there are better tools out there.
James Tursa
James Tursa il 15 Nov 2018
Modificato: James Tursa il 15 Nov 2018
My teacher in college once described the minors/cofactor method to me this way:
"Well, it may be slow, but it's innacurate!"
Steven Lord
Steven Lord il 16 Nov 2018
"innaccurate" as in "start it computing, go to bed, and maybe you'll have something close to right when you wake up?" grin

Accedi per commentare.

Più risposte (1)

Reza Lashani
Reza Lashani il 3 Feb 2021
Modificato: Reza Lashani il 3 Feb 2021

0 voti

One of the fastest ways to determine the determinant of a matrix is doing row operation. For an invertible matrix we know that row operations finally reach to identity matrix which has determinant equal to 1. For calculating determinant we can write an efficient and of course fast code to do row operation and it is not necessary to make the first non-zero entry 1 by dividing it by itself. It cause we finally reach to a diagonal matrix (stead of identity matrix) and the determinant is equal to the product of pivots of the final matrix. Doing this algorithm on my ordinary system for a 24 × 24 matrix lates less than 1 second.

4 Commenti

John D'Errico
John D'Errico il 3 Feb 2021
Modificato: John D'Errico il 3 Feb 2021
No. The best way to compute the determinant, If your matrix is non-symbolic is to use det. (det does use LU, so it is close to what you have suggested.) But even there, computing the determinant of a 24x24 matrix is just a silly thing to do. It is highly unstable.
Anyway, while you may THINK that your time of "less than a second" is a good thing. So it might be a smart idea to actually see how fast det is.
A = rand(24);
timeit(@() det(A))
ans =
4.8397e-06
So less than a second is correct. But in this case, MATLAB actually takes only a few MILLIONTHS of a second.
Writing your own code to do matrix operations is a BAD thing to do, especially if you don't really understand what is happening. And even if you do, it is STILL a bad thing. Never write your own code to perform computations when you already have high quality code available for the same purpose, written by people who are seriously good at what they do.
Consider that random matrix.
>> det(A)
ans =
0.030614
>> det(10*A)
ans =
3.0614e+22
>> det(A/10)
ans =
3.0614e-26
Is computing the determinant useful for ANYTHING? Not really, if you consider that simply scaling the matrix produces either a result that is huge, or incredibly tiny for the determinant. And that is almost always the reason people have to want to compute a determinant. Is the determinant zero?
Is that matrix singular? NO. Of course not. But if you use the determinant to decide if A/10 is singular, you might easily decide it is.
rank(A)
ans =
24
So while the best way to compute the determinant of a matrix is to use det, an even better way is to not compute the determinant at all. There is literally no good reason to compute the determinant of a 24x24 matrix.
Reza Lashani
Reza Lashani il 3 Feb 2021
Modificato: Reza Lashani il 3 Feb 2021
Yes you are right. Using det function or UL matrices is the best, most efficient and fastest way to compute determinant. I just wanted to show another way for computing matrix determinant and as you know the question is actually a practice and cause understand better MATLAB source code and time optimization for codes, the purpose is not using suggested algorithms.
mahdi moshtoghi
mahdi moshtoghi il 26 Feb 2021
hey, how to write the determinan 4*4 and 3*3 on matlab?
syms A [3 3]
syms B [4 4]
simplify(det(A))
ans = 
simplify(det(B))
ans = 

Accedi per commentare.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by