Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 调用qr\u solve时无法从复数创建mpf_Python_Sympy_Mpmath - Fatal编程技术网

Python 调用qr\u solve时无法从复数创建mpf

Python 调用qr\u solve时无法从复数创建mpf,python,sympy,mpmath,Python,Sympy,Mpmath,我做了以下进口: 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) 但

我做了以下进口:

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

这似乎发生在Symphy在输入之前设置矩阵时,这对我来说似乎很奇怪,因为I表示Symphy复杂类型。我想我可能在某处使用了错误的矩阵或输入类型。

问题是mpmath不知道如何将Sympy对象
I
(虚单位)转换为其表示复数的对象。但是,正如您所看到的,它可以与Python的
j
一起工作

应该注意的是,用于高精度浮点计算的库独立于Sympy(用于符号计算的库)。不需要定义Symphy对象,然后让mpmath处理它们。直接使用mpmath

import mpmath
mpmath.mp.dps = 50    # 50 digit arithmetic, for demonstration
A = mpmath.matrix([[2+3j, 1], [1, 1]])
b = mpmath.matrix([[1], [1]])
mpmath.qr_solve(A, b)
输出:

(matrix(
[[mpc(real='3.436455137311543569947658013827436034380299778494123e-55', imag='-1.2867118438861911327915233933248665673255859691704302e-55')],
 [mpc(real='1.0', imag='-1.1953121943036561823547019562093175737721941131313411e-54')]]), mpf('1.7055229691470039210052256620915903242996742650953353e-54'))

令人惊叹的。再次感谢你,梅塔!我有点想通了(也就是说),但很难将pythonjs引入到函数中。这就很好地解释了。在最新版本的SymPy中,mpmath不再作为
SymPy.mpmath
提供。您应该直接导入mpmath。