使用Python组合九次尖点多分支和九重图

2024-09-30 06:30:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写Python和LaTeX(最好是矢量图形TikZ/渐近线/PGF/Metapost/GeoGebra,按顺序排列)代码,通过在终端上运行简单代码生成此动画

这里是一个thread on Tex.SE,其中讨论了几种绘制Mandelbrot集的方法,但我无法像Python那样轻松地修改LaTeX中的数学方程。因此,我切换到Python并生成尽可能接近矢量图形的高分辨率输出(~2000 dpi)。我想尽可能地画出一般定义的多布罗特。例如,当d为负时

恩尼亚布罗特

Enneabrot-animation

以下代码取自GitHub上的this Mandelbrot (dimension=2) project,我需要通过更改定义Mandelbrot集的递归方程中的指数来创建此分形项目的高维版本,如Triabrot(维=4)、Pentabrot(维=6)、Heptabrot(维=8)和Enneabrot(维=10)。换句话说,对于Mandelbrot分形,我们定义了一个维数变量d,而不是z_{n+1}=z_n*z_n+z_0,然后d维多重Brot的方程将是z_{n+1}=z_n^d+z_0,并且每个这样的方程将在其稳定区的图形中产生d-1尖点。要了解有关此主题的更多信息,请观看以下两个YouTube视频:Times Tables by MathologerMandelbrot Set by Numberphile

每一块输出都需要改变轴;这是因为分形在复杂平面中移动,我们需要沿着镜头移动,通过Python观察它们。我们还可以使用哪些其他公式?也许axisFix=((d^2-4)/d)/10?在这个建议的公式中,d^2-4是存在的,因为我希望Mandelbrot分形(d=2)在没有任何位移的情况下打印在中心。因此,轴平移的值为零。这很有趣,因为对于d=-2它也是零,这意味着我们也必须试着看类似z_{n+1}=z_n^{-2.0}+z_0的方程。目标是找到最平滑的函数f(d),使f(2)=0,并通过以较小的大小(0.01-0.1)增量增加d的值并打印此处定义为动画的mandelbrot(阈值、密度、尺寸)函数的输出来制作动画。函数越平滑,演示文稿中幻灯片动画中幻灯片的过渡就越平滑

import numpy as np
import matplotlib.pyplot as plt
# counts the number of iterations until the function diverges or
# returns the iteration threshold that we check until
def countIterationsUntilDivergent(c, threshold, d):
    z = complex(0, 0)
    for iteration in range(threshold):
# here is the recurrence relation z_{n+1} = z_n^d + z_0, used for 
# drawing d-1-dimensional Mandelbrot creatures (growing fractals)
        z = z**d + c
        if abs(z) > 4:
            break
            pass
        pass
    return iteration

# takes the iteration limit before declaring function as convergent and
# takes the density of the atlas
# create atlas, plot mandelbrot set, display set
def mandelbrot(threshold, density, d):
# it is necessary to change the axis for every patch of outputs;
# this happens because the fractals move in the complex plane
# and we need to move along our lens to watch them through Python
## what other formulas could we use? Maybe axisFix = ((d^2-4)/d)/10?
## d^2-4 is there because I want the Mandelbrot fractal (d=2)
## to be right there were it is printed without any replacement
## so the value of the axis translation would be zero. This is
## funny because it is also zero for d=-2 which means that
## we must also be trying to look at equations like
### z_{n+1}=z_n^{-2.0} + z_0
### the goal is to find the smoothest function
### f(d) such that f(2)=0 and make an animation 
### by increasing the value of d by increments of small size (0.01-0.1)
### and printing the output of the mandelbrot function defined here
### as an animation. The smoother the function, the smoother
### the transition of slides in the animation
    axisFix = d/10 
    # location and size of the atlas rectangle
    realAxis = np.linspace(-2.25+axisFix, 0.75+axisFix, density)
    imaginaryAxis = np.linspace(-1.5, 1.5, density)
    # realAxis = np.linspace(-0.22, -0.219, 1000)
    # imaginaryAxis = np.linspace(-0.70, -0.699, 1000)
    realAxisLen = len(realAxis)
    imaginaryAxisLen = len(imaginaryAxis)

    # 2-D array to represent mandelbrot atlas
    atlas = np.empty((realAxisLen, imaginaryAxisLen))

    # color each point in the atlas depending on the iteration count
    for ix in range(realAxisLen):
        for iy in range(imaginaryAxisLen):
            cx = realAxis[ix]
            cy = imaginaryAxis[iy]
            c = complex(cx, cy)
            atlas[ix, iy] = countIterationsUntilDivergent(c, threshold, d)
            pass
        pass

    # plot and display mandelbrot set
    fig1 = plt.gcf()
    plt.axis('off')
    # plt.savefig('mandel.eps', format='eps')
    plt.imshow(atlas.T, interpolation="nearest")
    # plt.show()
    output_name = str(d)+'.pdf'
    fig1.savefig(output_name, format='pdf', bbox_inches='tight', dpi=2000)

# time to party!!
dimensions = np.arange(10, 100) / 10
# for d in dimensions:
#     mandelbrot(120, 1000, d)

# Enneabrot
mandelbrot(120, 1000, 10)
# Heptabrot
mandelbrot(120, 1000, 8)
# Pentabrot
mandelbrot(120, 1000, 6)
# Triabrot
mandelbrot(120, 1000, 4)
# Mandelbrot
mandelbrot(120, 1000, 2)

Tags: andofthetoinforisnp
1条回答
网友
1楼 · 发布于 2024-09-30 06:30:40

以下是结果,感谢Aryan Hemmati编辑了最后一张照片,将灵魂的九面形与十维Mandelbrot分形相结合

Enneabrot animation

我需要一个Python和LaTeX(最好是矢量图形TikZ/渐近线/PGF/Metapost/GeoGebra,按顺序排列)通过在终端上运行简单代码生成此动画。我们可以很容易地改变参数来制作七叶草(d=8)、五叶草(d=6)甚至三叶草(d=4)。我附上了使用Python绘制Enneabrot(d=10)的代码,我修改了Danyaal Rangwala的this code,并在递归方程中定义了一个新的变量d(维度),用于计算解的精确平衡区,这最终显示Enneabrot是我最后一次尝试生成这种分形的数字(从d=1.0开始,增量为0.1,直到d=10.0)

在这里,我将发布Mandelbrot分形(d=2)以及我在上面定义的分形的输出:Enneabrot(d=10)、Heptabrot(d=8)、Pentabrot(d=6),甚至Triabrot(d=4)。其他图表将在本answer的下一版本中遵循

曼德布罗特(d=2) Mandelbrot


Triabrot(d=4) Triabrot


潘塔布罗特(d=6) Pentabrot


七叶草(d=8) Heptabrot


埃尼亚布罗特(d=10) Enneabrot

相关问题 更多 >

    热门问题