There are 2 main options that come to my mind:
The product of eigenvalues of square matrix is the determinant of this matrix, therefore a sum of logarithms of each eigenvalue is a logarithm of the determinant of this matrix. Assume
det(A) = a
anddet(B) = b
for compact notation. After applying aforementioned for 2 matricesA
andB
, we end up withlog(a)
andlog(b)
, then actually the following is true:log(a + b) = log(a) + log(1 + e ^ (log(b) - log(a)))
Yes, we get a logarithm of the sum. What would you do with it next? I don't know, depends on what you have to. If you have to remove logarithm by
e ^ log(a + b) = a + b
, then you might be lucky that the value ofa + b
does not underflow now, but in some cases it can still underflow as well.Perform clever preconditioning; there might be tons of options here, and you better read about them from some trusted sources as this is a serious topic. The simplest (and probably the cheapest ever) example of preconditioning for this particular problem could be to recall that
det(c * A) = (c ^ n) * det(A)
, whereA
isn
byn
matrix, and to premultiply your matrix with somec
, compute the determinant, and then to divide it byc ^ n
to get the actual one.
Update
I thought about one more option. If on the last stages of #1 or #2 you still experience underflow too frequently, then it might be a good idea to increase precision specifically for these last operations, for example, by utilizing GNU MPFR.