我进行以下导入:
from sympy.matrices import Matrix as sy_matrix
import sympy.mpmath as sy_mp
像这样创建我的矩阵:
sysMat = sy_matrix([[0.0]*sz1]*sz2)
resVec = sy_matrix([[0.0]]*sz2)
. 然后用 python 复数(例如 1.0+1.0j)填充,然后调用:
coeffVec = sy_mp.qr_solve(sysMat, resVec)
但是我得到以下异常:
File "..\RatSMat.py", line 69, in solve
self.coeffVec = sy_mp.qr_solve(self.sysMat, self.resVec)
File "C:\Python27\lib\site-packages\sympy\mpmath\matrices\linalg.py", line 406, in qr_solve
A, b = ctx.matrix(A, **kwargs).copy(), ctx.matrix(b, **kwargs).copy()
File "C:\Python27\lib\site-packages\sympy\mpmath\matrices\matrices.py", line 328, in __init__
A = self.ctx.matrix(args[0].tolist())
File "C:\Python27\lib\site-packages\sympy\mpmath\matrices\matrices.py", line 301, in __init__
self[i, j] = convert(a)
File "C:\Python27\lib\site-packages\sympy\mpmath\ctx_mp_python.py", line 662, in convert
return ctx._convert_fallback(x, strings)
File "C:\Python27\lib\site-packages\sympy\mpmath\ctx_mp.py", line 614, in _convert_fallback
raise TypeError("cannot create mpf from " + repr(x))
TypeError: cannot create mpf from -2.27448396079674e-13*I
当 sympy 在输入之前设置矩阵时似乎会发生这种情况,这对我来说似乎很奇怪,因为 I 表示 sympy 复杂类型。认为我可能在某处使用了错误的矩阵或输入类型。