# determinant of a very large matrix in MATLAB

I have a very large random matrix which its elements are either $0$ or $1$ randomly. The size of the matrix is $5000$, however when I want to calculate the determinant of the matrix, it is either $Inf$ or $-Inf$. Why it is the case (as I know thw determinant is a real number and for a finite size matrix with finite elements, it cannot be $Inf$) and how can I remedy any possible mistake?

• However, the determinant can easily overflow the double precision range.. – Carl Christian Mar 12 '16 at 2:29
• @CarlChristian So why do I have overflow for every scenario. As I said the matrix is generated randomly, and for each generation, the determinant is $Inf$ or $-Inf$ – Carl_Gauß Mar 12 '16 at 2:31
• Try dividing the matrix by $5000$ before taking the determinant; that should give you results that won't overflow. – Omnomnomnom Mar 12 '16 at 2:48
• See this answer on SciComp.SE for why numerically calculating the determinant of a large matrix is not a good idea. Inf in Matlab means larger than realmax ($\approx10^{308}$). – horchler Mar 12 '16 at 3:18

## 2 Answers

I did a search for "determinant of random matrix" and found this article:

https://people.math.osu.edu/nguyen.1261/cikk/loglaw.pdf

It shows that the log of the determinant of a n by n random matrix is usually about $n\log(n)/2$.

Therefore, for large $n$, any computation of the determinant will almost certainly overflow.

• $5000\ln(5000)$ is not a large number. The reason why it overflows is because of roundoffs in the LU decomposition of A, which matlab uses to compute the determinant. – bartgol Mar 12 '16 at 4:27
• This is the log of the determinant. The determinant is about $5000^{2500}$. – marty cohen Mar 12 '16 at 4:37
• My bad, I missed the first "log" in your answer. – bartgol Mar 12 '16 at 4:48
• For clarity it might help to put the formula for the determinant itself in the post ($\sqrt{n^n}$), or italicize the "log", or some such thing. I think this is an easy reading mistake to make. – David Z Mar 12 '16 at 10:13

If the determinant is needed, then a numerically reliable strategy is to compute the $QR$ decomposition of $A$ with column pivoting, i.e. $AP = QR$, where $P$ is a permutation matrix, $Q$ is an orthogonal matrix and $R$ is an upper triangular matrix. In MATLAB the relevant subroutine is 'qr'. Then the determinant of $A$ equals the product of the diagonal entries of $R$ (up to a sign change which is determined by the determinant of $P$). The question of computing the determinant then reduces to handling the product of $n$ terms. This product can easily overflow or underflow, but it is likely that you will be able to determine the logarithm of the absolute value of this product, using the relation $\log(ab) = \log(a) + \log(b)$.

There are exceptions, but normally the condition number of a matrix is more important than the determinant.