首页 > 解决方案 > 如何使用 Numba 加速 scipy.sparse.linalg 中提供的 Python 中的稀疏线性系统求解器?

问题描述

我希望使用 Numba 加速我的代码中的稀疏系统求解器部分。这是我到目前为止所拥有的:

# Both numba and numba-scipy packages are installed. I am using PyCharm IDE
import numba
import numba_scipy
# import other required stuff

@numba.jit(nopython=True)
def solve_using_numba(A, b):
    return sp.linalg.gmres(A, b)

# total = the number of points in the system

A = sp.lil_matrix((total, total), dtype=float)
# populate A with appropriate data
A = A.tocsc()

b = np.zeros((total, 1), dtype=float)
# populate b with appropriate data

y, exit_code = solve_using_numba(A, b)

# plot solution

这会引发错误

argument 0: cannot determine Numba type of <class 'scipy.sparse.csc.csc_matrix'>   

官方文档中,numba-scipy extends Numba to make it aware of SciPy.但似乎在这里,numba 无法使用 scipy 稀疏矩阵类。我哪里出错了,我能做些什么来解决这个问题?

我只需要加速代码的稀疏系统解决方案部分,因为其他东西非常轻量级,比如接受几个用户输入、构建 A 和 b 矩阵以及绘制最终结果。

标签: python-3.xscipysparse-matrixsolvernumba

解决方案


推荐阅读