determinant of singular matrix is non-zero

15 views (last 30 days)
Marco
Marco on 2 Oct 2011
Commented: John D'Errico on 6 Jul 2021
Hi. I have tried to insert this matrix into MATLAB. I have took it from the "Getting started" matlab guide. It's the Durer magic 4x4 matrix. It is defined as follow:
A = [32, 8, 11, 17; 8, 20, 17, 23; 11, 17, 14, 26; 17, 23, 26, 2]
This matrix should be singular, according to the Getting started guide and also to other math software. But when i call det(A), instead of returning 0, MATLAB returns
>> det(A)
ans =
-2.174260771425907e-011
I just want it to return 0. If i insert a matrix of integers, how the determinant can be non-integer? I understand that Matlab threat all the numbers and matrix as double, but how can i tell him to threat them as integer, and do integer calculation, and then return just 0? I have tried with the intxx function, but they are not working with the det function
>> det(int32(A)) ??? Undefined function or method 'det' for input arguments of type 'int32'.
  1 Comment
Teja Muppirala
Teja Muppirala on 3 Oct 2011
If you want to confirm that a matrix is singular, looking at the determinant is usually not a good way to do it.
[1e-100 0
0 1e100]
is almost singular, but the determinant is = 1.
1e-300*eye(10)
is not singular at all, but the determinant is 1e-3000.
The condition number is probably a better check.

Sign in to comment.

Answers (3)

bym
bym on 2 Oct 2011
A =
32 8 11 17
8 20 17 23
11 17 14 26
17 23 26 2
a = sym(A)
a =
[ 32, 8, 11, 17]
[ 8, 20, 17, 23]
[ 11, 17, 14, 26]
[ 17, 23, 26, 2]
det(a)
ans =
0

Greg Heath
Greg Heath on 2 Oct 2011
That is very interesting because obsolete Matlab 6.5 does yield 0.
Greg
  2 Comments
John D'Errico
John D'Errico on 6 Jul 2021
My guess is that at some point they were using exact computations for the determinant for smaller matrices. That is, we can write the determinaint of a 2x2 or 3x3 or even a 4x4 matrix out as just set of multiplies and adds. That will be exact for small matrices composed of only small integers.

Sign in to comment.


David Young
David Young on 3 Oct 2011
I get the same result as you, Marco, in version 2011a.
The doc page for the det function says that the calculation is now done using Gaussian elimination.
I'd guess that we're seeing the results of a trade-off decision. The algorithm used for recent versions of det probably has superior qualities overall - it may be faster, especially for large matrices, and it may be more accurate for large matrices or matrices with a large range of values. However, it clearly introduces rounding errors for small integer matrices. A look at the numerical algorithms literature, or ideally a conversation with the implementor, would shed more light on this.
It's probably relevant to note that the doc page also says:
"Testing singularity using abs(det(X)) <= tolerance is not recommended as it is difficult to choose the correct tolerance. The function cond(X) can check for singular and nearly singular matrices."
cond(A) returns a result of the order of 10^-16 for your example.
If you want a function that gives an exact result for small integers, you can carry out a naive computation like this:
function d = determinant(a)
%DETERMINANT computes determinant using naive algorithm
% D = DETERMINANT(A) computes the determinant of the square matrix A
% using the Laplace expansion. MATLAB's built-in DET is likely to be
% preferable for most applications, but this function gives an exact
% answer for small matrices of small integers.
if min(size(a)) == 1
if max(size(a)) == 1
d = a;
else
error('determinant:nonSquare', 'A must be square and 2D');
end
else
row1 = a(1, :);
rows2etc = a(2:end, :);
sign = 1;
d = 0;
for ii = 1:length(row1)
minor = [rows2etc(:, 1:ii-1) rows2etc(:, ii+1:end)];
d = d + sign * row1(ii) * determinant(minor);
sign = -sign;
end
end
end
This function gives an exact zero for your example.

Community Treasure Hunt

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

Start Hunting!