【发布时间】:2019-05-28 05:57:33
【问题描述】:
我想将 Mathematica 中计算的分段函数输出转换为 Python。从 this 页面获取 Mathematica->Python 转换和 this 页面编写分段函数的灵感,我有
from numpy import linspace, vectorize, array
from numpy import arctan, log
import matplotlib.pyplot as plt
from sympy.parsing.mathematica import parse
def fun(x,a,b,c):
# string inside parse('') is my mathematica output
if a == 0:
out = parse('a (-I b + x) ArcTan[(b - a)/c]')
else:
out = parse('4 c a^2 Log[c^2 + (x - a)^2]')
return out
a = 0.17
b = 0.44
c = 0.29
x = linspace(0,50,int(1e3))
vfun = vectorize(fun)
y = vfun(x,a,b,c)
yp = 4*c*a**2*log(c**2 + (x - a)**2)
plt.figure()
plt.plot(x,y,'.-')
plt.title('mathematica -> python conversion')
plt.figure()
plt.plot(x,yp,'.-')
plt.title('expected')
plt.show()
剧情如下:
应该是这样的:
将 Mathematica 转换为 Python 时我做错了什么吗?或者在给a、b、c赋值时是否有问题? (请注意,这是一个 MWE,我要转换的 Mathematica 代码比上面显示的要长得多且复杂得多。)
【问题讨论】:
-
您能否也展示一下您希望看到的内容或 Mathematica 的初始输出是什么?例如,您也可以在 Mathematica 中绘图。
标签: python numpy wolfram-mathematica sympy