JulianeB
JulianeB

Reputation: 81

How do calculate a determinant of an large matrix without getting an overflow?

Good morning!

I have an nxn-Matrix with n large enough to give me an overflow.

I tried to fix this problem the mathematical way and it worked for small n but now I get an overflow from the Exponent.

To explain it better here the code:

    # create a quadratic Matrix matrix with shape 500x500
    matrix = [[ 1.03796037 -0.00898546 -0.00410423 ... -0.0453022   0.02608995
      -0.01146299]
     ...

     [-0.01146299 -0.04572196  0.07370042 ...  0.03203931  0.07298667
       0.98693473]]

    # calculate the mean of matrix
    mean = matrix.mean()
    # calculate n
    n = matrix.shape[0]
    # divide the mean from the matrix and calculate the determinant 
    determinant = np.linalg.det(matrix/mean)
    # use now det(c*M) = c^n*det(M)
    solution = mean**n*determinant

    >>>> inf

This method works to prevent the overflow from the np.linalg.det() function but the calculation of the power mean**n gives an overflow and I have no idea how to solve this. The mean is btw a float and n a int Can you help here? Please only in Python or numpy.

Upvotes: 3

Views: 2493

Answers (1)

Milad Sikaroudi
Milad Sikaroudi

Reputation: 717

You would address it by calling "numpy.linalg.slogdet" instead of "numpy.linalg.det". As it has been pointed out here

slogdet can be used whenever the det has led to overflowing/underflowing.

Hope it works.

Upvotes: 8

Related Questions