如何计算大矩阵的行列式而不会溢出?

Jul*_*neB 4 python numpy matrix determinants python-3.x

早上好!

我有一个 nxn 矩阵,其中 n 大到足以让我溢出。

我试图用数学方法解决这个问题,它适用于小 n 但现在我从指数中溢出。

为了更好地解释它,这里的代码:

    # 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
Run Code Online (Sandbox Code Playgroud)

这种方法可以防止np.linalg.det()函数溢出,但是计算幂mean**n会导致溢出,我不知道如何解决这个问题。意思是 btw a float 和 na int 你能在这里帮忙吗?请仅使用 Python 或 numpy。

Mil*_*rdy 6

您可以通过调用“ numpy.linalg.slogdet”而不是“ ”来解决它numpy.linalg.det。正如这里所指出的

只要det导致溢出/下溢,就可以使用slogdet

希望它有效。