下面的代码使用Tkinter的照片图像格式绘制Mandelbrot集,使用256次迭代和256色自定义调色板.

如果运行它,它会显示一个美丽的图像,其中有长长的烟雾状细丝,迭代界限之间没有同心边界.

如果我try 以相同的极限和精度重现c中的代码,我会得到迭代转义值之间的短丝和同心边界.

问题是,我错过了什么?为了获得更高的精确度,在我背后做了什么?

左边的附图是Python的

    # by Antoni Gual Via 4/2015

from tkinter import Tk, Canvas, PhotoImage,NW,mainloop 
from time import time

def mandel_pixel(c):   #c is a complex
  """ calculates the color index of the mandelbrot plane point passed in the arguments """
  maxIt = 256
  z =  c   
  for i in range(maxIt):
      a = z * z
      z=a + c  
      if a.real  > 4.:
         return i
      
  return maxIt

def mandelbrot(xa,xb,ya,yb,x,y):
    """ returns a mandelbrot in a string for Tk PhotoImage"""
    #color string table in Photoimage format #RRGGBB 
    clr=[ ' #%02x%02x%02x' % (int(255*((i/255)**.25)),0,0) for i in range(256)]
    clr.append(' #000000')  #append the color of the centre as index 256
    #calculate mandelbrot x,y coordinates for each screen pixel
    xm=list([xa + (xb - xa) * kx /x  for kx in range(x)])
    ym=list([ya + (yb - ya) * ky /y  for ky in range(y)])
    #build the Photoimage string by calling mandel_pixel to index in the color table
    return " ".join((("{"+"".join(clr[mandel_pixel(complex(i,j))] for i in xm))+"}" for j in ym))



#window size
x=640
y=480
#corners of  the complex plane to display  
xa = -2.0; xb = 1.0
ya = -1.27; yb = 1.27

#Tkinter window
window = Tk()
canvas = Canvas(window, width = x, height = y, bg = "#000000");canvas.pack()
img = PhotoImage(width = x, height = y)
canvas.create_image((0, 0), image = img, state = "normal", anchor = NW)

#do the mandelbrot
print('Starting Mandelbrot')
t1=time()
img.put(mandelbrot(xa,xb,ya,yb,x,y))
print(time()-t1, ' seconds')

mainloop()

还有C代码的相关部分

int mandel(double x0,double y0, int maxit){
  double x=x0,  xt=0.;
  double y=y0; 
  int i;
  for (i=1;((x*x+y*y)<4.) && (i<=maxit);i++){
   xt=x*x-y*y+x0;
   y=2.*x*y+y0;
   x=xt ;   
  } 
  return(i<maxit ? rgb(255.*(pow(((double)i/maxit),.25)),0,0):0);
}

enter image description here

推荐答案

我最近在扁桃树中看到了这样的细丝,所以我打赌问题应该是Python 代码出了什么问题……

即使你加上fractional escape which adds much more details,也没有这样的细丝,approximations of interior distance / SDF里也没有这样的东西……顺便说一句.我最近在这里看到了一个问题标题,它抱怨来自真实输入的PythonPower **运算符和错误的复数域结果,它可能是相关的,但我从来没有读过它,因为我不喜欢它的核心,所以我立即忽略了那个Q.

不过,我敢打赌,它的准确率是the problem lies in the complex power%.因为复合域pow是这样计算的:

vec2 cpow(vec2 a,vec2 b)    // a^b
    {
    return cexp(cmul(cln(a),b));
    }

How to express tetration function, for complex numbers开始是这个分形族的另一个分形图...

这比简单的整数指数乘加减法增加了更多的舍入...

因此,一旦你迭代多次,舍入误差将变得更加重要,所以你考虑的附加细节反而是精度误差.

我敢打赌,如果您重写了不使用POW**运算符的Python代码,那么结果将是相同的(这将证实我的结论).

Python相关问答推荐

将轨迹优化问题描述为NLP.如何用Gekko解决这个问题?当前面临异常:@错误:最大方程长度错误

try 与gemini-pro进行多轮聊天时出错

连接两个具有不同标题的收件箱

试图找到Python方法来部分填充numpy数组

如何让程序打印新段落上的每一行?

NP.round解算数据后NP.unique

Python中的变量每次增加超过1

与命令行相比,相同的Python代码在Companyter Notebook中运行速度慢20倍

如何在BeautifulSoup/CSS Select 器中处理regex?

在输入行运行时停止代码

Polars Group by描述扩展

如何使用正则表达式修改toml文件中指定字段中的参数值

Python日志(log)模块如何在将消息发送到父日志(log)记录器之前向消息添加类实例变量

需要帮助使用Python中的Google的People API更新联系人的多个字段'

应用指定的规则构建数组

当HTTP 201响应包含 Big Data 的POST请求时,应该是什么?  

为什么我的scipy.optimize.minimize(method=";newton-cg";)函数停留在局部最大值上?

遍历列表列表,然后创建数据帧

对于数组中的所有元素,Pandas SELECT行都具有值

如何在Polars中将列表中的新列添加到现有的数据帧中?