MATLAB: Is det a bad way to check matrix singularity

determinantlinear independencematrix singularity

Sorry, I can't define "bad" in the question. I've heard this several times and I've never liked determinants and I just finished the book Basic Matrices by C.G. Broyden and he didn't use determinants either. So could someone please clarify? Thank you.

Best Answer

The example I always like to give is this:
det(3*eye(1000))
ans =
Inf
det(.3*eye(1000))
ans =
0
det(eye(1000))
ans =
1
So is that very simply scaled identity matrix singular or not?
Or, try this:
A = rand(4,3)*rand(3,4);
det(A)
ans =
-3.79011315068409e-18
So the product of a 4x3 and a 3x4 matrix MUST be singular. Hey, det got that right. By the same logic, the product of a random 100x99 matrix and a 99x100 matrix must also be singular. Right?
A = rand(100,99)*rand(99,100);
det(A)
ans =
2.93629717617442e+37
Not according to det. So what went wrong?
Det uses a good scheme to do its work. It computed a pivoted LU factorization of the matrix. Then take the product of the diagonal elements of U.
[L,U,P] = lu(A);
prod(diag(U))
ans =
2.9363e+37
diag(U)
ans =
32.338
3.298
2.9682
-2.8183
2.8772
2.5713
2.7686
3.7067
-3.5259
4.1328
-6.0407
3.0507
2.7318
3.69
-3.8034
-3.3593
3.507
3.8023
-3.0113
-3.2846
5.2249
-3.0531
-5.1457
-2.868
-3.3524
2.3689
-3.3529
4.1264
7.6471
-3.7107
-4.1193
-3.6732
-4.1213
2.5827
5.6856
2.3059
4.7825
2.9447
5.1117
-3.6831
6.3472
-5.7061
3.2259
-3.5116
-2.7813
-3.812
-7.6134
4.2924
-3.3689
-2.8465
-3.3392
3.5206
3.5484
2.8341
4.7295
-4.1757
-4.8612
3.4009
-3.8609
4.5162
4.7767
6.3942
-3.7804
-3.707
4.0019
-2.2507
-4.4813
2.0913
3.813
4.8703
-3.3253
3.1835
-5.4834
4.0471
-6.381
1.3069
3.4877
-2.0475
3.0452
-2.6165
-2.1811
-4.9662
2.6201
-2.8188
4.2236
-1.5627
3.5806
-2.8705
1.8628
-2.2577
-1.9378
1.5462
-1.0559
-0.64591
-2.391
1.0017
-0.79486
0.43954
-0.069129
-1.2492e-12
But what happened here is that last diagonal element should in theory be mathematically 0. In fact, that last element was a small number, effectively down near eps. Then when you take the product of the elements, that one small number can become overwhelmed by the others.
So while rank and cond are not fooled, det got blown out of the water.
rank(A)
ans =
99
cond(A)
ans =
1.4581e+17
In the end, you can't trust det.
Sadly, this is what I wish those who teach students about using det would also teach them - one last lesson on why you should NOT trust it.
Related Question