【问题标题】:How do calculate a determinant of an large matrix without getting an overflow?如何在不溢出的情况下计算大矩阵的行列式?
【发布时间】:2019-11-09 22:34:49
【问题描述】:

早上好!

我有一个 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

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

【问题讨论】:

  • 也许看看这篇关于如何处理大量数字的帖子 => softwareengineering.stackexchange.com/questions/128589/…
  • @VictorDeleau 你好维克多。我在创建这个问题之前看到了这个问题,因为我无法将那里的解决方案与我的问题联系起来。这里我们有一个 float ** large int。

标签: python python-3.x numpy matrix determinants


【解决方案1】:

您可以通过调用“numpy.linalg.slogdet”而不是“numpy.linalg.det”来解决此问题。正如已经指出的here

slogdet 可以在 det 导致上溢/下溢时使用。

希望它有效。

【讨论】:

    猜你喜欢
    • 2017-09-03
    • 1970-01-01
    • 2011-08-09
    • 2022-11-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-12
    • 2013-02-07
    • 2018-11-09
    相关资源
    最近更新 更多