PDA

View Full Version : how to find determinant of a nxn matrix



aktech
03-13-2004, 09:16 PM
I want to find out whether a given matrix is singular or not. It is fairly straightforward to do so for small (3x3 and 4x4) matrices but how do I find the determinant for an arbitrary (nxn) order matrix (I want to pass n as a function parameter).

atorres
03-14-2004, 12:48 PM
You can try to do some Gaussian Elimination. After that, once you have a triangular matrix, the determinant is found by multiplying the diagonal.

If all this is kind of obscure to you… you should do some search in the net. Some key words are: Linear Algebra, triangular matrix, Gauss Elimination.

- Alexis

xDigital
03-16-2004, 03:22 AM
Hi aktech,
here is a trial concerning gauss elemination. Its matlab code, but should be easyly transfered to C++ or a language of your choice. After calculating the gauss-Matrix multiply - as told by atorres - the diagonal entries. If the result is 0, matrix A might be singular. Beware, gauss-elemination is not suitable for any matrices.
Regards
xDigital

p.s. there might be much better sources for gauss, but it works.

function [gauss,b,x] = fcn_gauss(A,b)
gauss = A;
[r,c] = size(A);
nEquation = r-1;

for (i = 1:nEquation)
count = i;
for (j = nEquation:-1:i)
count = count+1;
if (gauss(i,i) == 0) break; end;
factor = gauss(count,i) / gauss(i,i);
gauss(count, :) = gauss(count, :) - factor*gauss(i, :);
b(count) = b(count) - factor*b(i);
end;
end;

x(r) = b(r)/gauss(r,r);
for (i = r-1:-1:1)
value = 0;
for (j = r:-1:1)
value = value+x(j)*gauss(i,j);
end;
x(i) = (b(i)-value)/gauss(i,i);
end;


[This message has been edited by xDigital (edited 03-16-2004).]

xDigital
03-16-2004, 03:23 AM
hmmm, the editor destroys the format...
hope you can read it anyway. Sorry.
xDigital

aktech
03-16-2004, 03:51 AM
Thanks, people! The Gauss-elimination technique came in quite useful once I got it into my head.
Thanks for the code xDigital. I have ported it to C++ and it works very well.