4

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.

Stealing
  • 187
  • 1
  • 12
JulianeB
  • 91
  • 5
  • Maybe look at this post on how to handle large numbers => https://softwareengineering.stackexchange.com/questions/128589/how-to-handle-large-numbers – Victor Deleau Nov 09 '19 at 22:42
  • @VictorDeleau Hello Victor. I saw this question before I created this one, because I could not relate the solution there to my problem. Here we have a float ** large int. – JulianeB Nov 09 '19 at 22:55

1 Answers1

7

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.

Milad Sikaroudi
  • 677
  • 6
  • 16