arn*_*ney 1 python sympy complex-numbers
在Python中使用符号计算我有
import sympy
from cmath import *
from mpmath import arg, cplot
z = sympy.symbols('z')
fhandle='z**2'
g = lambda w: sympy.sympify(fhandle).evalf(subs={z: w})
g(1+2j)
# Returns: -3.0 + 4.0*I
# hence the next command fails, because I is expected to be 1j
cplot(g, [-3,3], [-3,3])
Run Code Online (Sandbox Code Playgroud)
抓取网页我只发现了这将解决print命令问题,但无法使用cplot.
有什么建议?
一种选择是通过调用包装结果complex:
>>> def g(w):
... return complex(sympy.sympify(fhandle).evalf(subs={z: w}))
...
>>> g(1+2j)
(-3+4j)
Run Code Online (Sandbox Code Playgroud)
之后mpmath.cplot(g, [-3, 3], [-3, 3])产生

请注意,我在这里使用了一个命名函数.lambda如果你要立刻给它一个名字,那么使用a并没有多大意义.