我的四分之一(复数)函数必须更好地矢量化(Python)

我的四分之一(复数)函数必须更好地矢量化(Python),python,numpy,math,vectorization,complex-numbers,Python,Numpy,Math,Vectorization,Complex Numbers,我试图用Python来描绘函数的转义(分歧)——类似于这个漂亮的图形 我正试图使它与网格一起工作,以便在x-y平面上绘制逃逸图。Python不喜欢输出是两个未打包的变量(limit或escape)——我可以通过拆分成两个函数来解决这个问题 但另一个问题是,复杂的数学运算(cmath.log、cmath.exp)只适用于标量 我尝试将函数矢量化: nx, ny = 700, 500 x, y = np.linspace(-3.5, 3.5, nx), np.linspace(-2.5, 2.5,

我试图用Python来描绘函数的转义(分歧)——类似于这个漂亮的图形

我正试图使它与网格一起工作,以便在x-y平面上绘制逃逸图。Python不喜欢输出是两个未打包的变量(limit或escape)——我可以通过拆分成两个函数来解决这个问题

但另一个问题是,复杂的数学运算(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)
def tetra_graph_escape(real, imag, tol=10**-15, max_step=10**3, max_val=10**2):
  return np.array([np.array([tetration_com(r + im*1j, tol=tol, max_step=max_step, max_val=max_val)[1]
                             for im in imag]) for r in real])
但它永远在运行


关于如何处理复杂的数学运算和矢量化,有什么建议吗?

以下是我最后如何绘制逃生图的:

def tetration_com(base, tol=10**-15, max_step=10**6, max_val=10**2):
  # 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

  t_last = 0
  try:
    while(abs(t - t_last) > tol):
      if(step > max_step or abs(t) > max_val):
        raise OverflowError
      t_last = t
      t = pow(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
矢量化辅助函数:

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)
def tetra_graph_escape(real, imag, tol=10**-15, max_step=10**3, max_val=10**2):
  return np.array([np.array([tetration_com(r + im*1j, tol=tol, max_step=max_step, max_val=max_val)[1]
                             for im in imag]) for r in real])
绘图:

# graph our escape:
nx, ny = 700, 500
x, y = np.linspace(-3.5, 3.5, nx), np.linspace(-2.5, 2.5, ny)

val, escape = tetra_graph_conv(x, y), tetra_graph_escape(x, y)

import matplotlib.pyplot as plt

for r in range(len(escape)):
  for c in range(len(escape[0])):
    if escape[r][c] is None:
      escape[r][c] = -100

escape[460][250]

plt.contour(escape)
四分体逃逸等高线图:


为什么不使用numpy为exp和log提供的函数?这可能会让您感兴趣,您的目标是基于GLSL的分形渲染。。。里面的链接也值得一看,因为它们处理基于直方图的着色,以实现最佳的图像色彩可能…很高兴能提供帮助。。。这是非常有趣的衍生和代码。。。GLSL在这方面非常并行(我可以用足够高的fps平滑地缩放),但是会带来很多精度和调试问题以及限制,因此有时即使是简单的数学也无法按预期工作。