受 3blue1brown 的启发,我正在尝试用 Python绘制 Tetration函数的逃逸(散度)图——类似于维基百科上这张漂亮的图形。
def tetration_com(base, tol=10**-15, max_step=10**6):
# returns t, the infinite tetration of base.
# if t does not converge, the function returns an escape value, aka how fast it diverges..
t = 1.0
step = 0
escape = None
ln_base = cmath.log(base)
t_last = 0
try:
while(abs(t - t_last) > tol):
if(step > max_step):
raise OverflowError
t_last = t
t = cmath.exp(ln_base*t) # [ base^t == e^(ln(base)*t) ]
step += 1
except(OverflowError):
t = None
escape = 1000/step
# the escape value is is inversely related to the number of steps it took
# us to diverge to infinity
return t, escape
我试图让它与 meshgrid 一起工作,以便绘制 xy 平面上的逃逸图。Python 不喜欢输出是 2 个解压缩的变量(限制或转义)——我绝对可以通过拆分成两个函数来解决这个问题。
但另一个问题是复杂的数学运算(cmath.log,cmath.exp)只适用于标量......
我试图向量化函数:
nx, ny = 700, 500
x, y = np.linspace(-3.5, 3.5, nx), np.linspace(-2.5, 2.5, ny)
xv, yv = np.meshgrid(x, y)
tetration_vec = np.vectorize(tetration_com)
t, escape = tetration_vec(xv + yv*1j, max_step=500)
但它永远在运行。
关于如何处理复杂数学运算和矢量化的任何建议?
www说
相关分类