如何在 Python 中加速矩阵求逆?

问题描述 投票:0回答:1

在我的代码中,每次运行大约需要 40 秒,大部分时间(38 秒)都花在矩阵求逆上,即 numpy.linalg.inv。这里,矩阵维度一般为100-1000。

因此,我想减少运行时间。有什么有效的方法可以解决吗

python numpy matrix-inverse
1个回答
0
投票

出于好奇,我尝试了以下方法:

import  numpy as np
from numpy.linalg import inv
from timeit import timeit


def compute_inverse(a):
    return  inv(a)

a = np.random.rand(1000,1000)

loop = 100

result = timeit('compute_inverse(a)', globals=globals(), number=loop)
print(result / loop)

我有 ~0.2 秒。

我的电脑配置是

RAM - 32GiB

Win10 固态硬盘

英特尔酷睿 i7

© www.soinside.com 2019 - 2024. All rights reserved.