我正在尝试使用计算物理书中的函数求解带状联立方程:http://www-personal.umich.edu/~mejn/cp/programs/banded.py 但是,运行该函数会导致溢出错误:
OverflowError: cannot convert float infinity to integer in banded line 39: v[m] /= div
矩阵 A 和 w 为:
A =
[[ 3 -1 -1 ... 0 0 0]
[-1 4 -1 ... 0 0 0]
[-1 -1 4 ... 0 0 0]
...
[ 0 0 0 ... 4 -1 -1]
[ 0 0 0 ... -1 4 -1]
[ 0 0 0 ... -1 -1 3
w = [5 5 0 ... 0 0 0]
x = banded(A,w,2,2)
有人可以帮我弄清楚为什么在使用这个函数时会出现溢出错误吗? 谢谢。
最佳答案
我认为你应该将 A 制作成带状矩阵,如 banded.py 所示
( - - A02 A13 A24 ... ( - A01 A12 A23 A34 ... ( A00 A11 A22 A33 A44 ... ( A10 A21 A32 A43 A54 ... ( A20 A31 A42 A53 A64 ...
此外,我发现如果矩阵 A 或向量 w 中的矩阵元素是整数,则带状函数将无法正常工作,您可以考虑将其初始化为 float 。
关于python - 求解带状联立方程时出现溢出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55185161/