复数:将SymPy转换为数字(I到1j) [英] Complex numbers: convert SymPy to numeric (I to 1j)

查看:505
本文介绍了复数:将SymPy转换为数字(I到1j)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中使用符号计算

using symbolic calculation in Python I have

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])

我只能在可以解决 print 命令的问题,但不适用于cplot。

Crawling the web I only found this which will fix the matter for the print command, but will not work with cplot.

有什么建议吗?

推荐答案

一种选择是通过调用 complex

>>> def g(w):
...     return complex(sympy.sympify(fhandle).evalf(subs={z: w}))
... 
>>> g(1+2j)
(-3+4j)

之后 mpmath.cplot(g,[-3,3],[-3,3])产生

请注意,我在这里使用了命名函数。如果要立即给它起一个名字,使用 lambda 没有多大意义。

Note that I've used a named function here. There's not much point to using a lambda if you're going to immediately give it a name anyhow.

这篇关于复数:将SymPy转换为数字(I到1j)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆