【问题标题】:How to declare a variable in python without assigning a value?如何在python中声明一个变量而不赋值?
【发布时间】:2020-06-01 17:44:40
【问题描述】:

我正在尝试创建 Mandelbrot 集的图表。

我已经设法通过迭代很多点来做到这一点,但这需要大量的处理能力,所以我现在尝试通过多次迭代f(z) = z**2 + c 来生成多项式,然后找到@987654322 的根@,以生成集合的边界。

但是我似乎无法让 Python 生成多项式,任何帮助都会非常感激。

编辑:实施了 azro 的修复,但现在我收到错误 - TypeError: unsupported operand type(s) for ** or pow(): 'NoneType' and 'int'

到目前为止的代码:

import numpy as np

c = None

def f(z):
    return z**2 + c

eqn = c

for i in range(100):
    eqn = f(eqn)

np.roots(eqn)

【问题讨论】:

  • return z**2 + c
  • 你应该重命名你的问题,因为当我看到它时,我想建议c = None
  • 很好,但现在我得到了这个错误:TypeError: unsupported operand type(s) for ** or pow(): 'NoneType' and 'int'
  • Python 没有变量声明;只有作业。
  • 即使你在没有赋值的情况下声明c,你也会因为尝试使用没有值的变量而得到NameError。你为你的潜在问题选择了一个非解决方案。

标签: python iteration fractals mandelbrot


【解决方案1】:

这是一个非常困难的问题。通过文献搜索,我只找到了this(这似乎不太有名)。然而,它似乎开始创造你想要的东西。这最多只有 8 次迭代。所以多项式很快变得非常复杂。见以下代码:

import matplotlib.pyplot as plt

coeff = [0, 1, 1, 2, 5, 14, 26, 44, 69, 94, 114, 116, 94, 60, 28, 8, 1]
coeff = [0, 1, 1, 2, 5, 14, 42, 132, 429, 1302, 3774, 10652, 29538, 80812, 218324, 582408, 1534301, 3993030, 10269590, 26108844, 65626918, 163107044, 400844588, 974083128, 2340595778, 5560968284, 13062923500, 30336029592, 69640352964, 158015533208, 354347339496, 785248461712, 1719477330477, 3720187393990, 7952125694214, 16792863663700, 35031835376454, 72188854953372, 146932182777116, 295372837865192, 586400982013486, 1149605839249820, 2225301467579844, 4252710138415640, 8022825031835276, 14938862548001560, 27452211062573400, 49778848242964944, 89054473147697354, 157160523515654628, 273551721580800380, 469540646039042536, 794643418760272876, 1325752376790240280, 2180053774442766712, 3532711259225506384, 5640327912922026260, 8870996681171366696, 13741246529612440920, 20959276151880728336, 31472438318100876584, 46514944583399578896, 67649247253332557392, 96791719611591962592, 136210493669590627493, 188481251186354006062, 256386228250001079082, 342743629811082484420, 450159936955994386738, 580706779030058464252, 735537050036491961156, 914470757914434625800, 1115597581733327913554, 1334957092752100409132, 1566365198635995978988, 1801452751402955781592, 2029966595320794439668, 2240353897304462193848, 2420609646335251593480, 2559320275988283588176, 2646791812246207696810, 2676118542978972739644, 2644036970936308845148, 2551425591643957182856, 2403354418943890067404, 2208653487832260558008, 1979045408073272278264, 1727958521630464742736, 1469189341596552030212, 1215604411161527170376, 978057923319151340728, 764655844340519788496, 580430565842543266504, 427417353874088245520, 305060580205223726864, 210835921361505594848, 140960183546144741182, 91071943593142473900, 56796799826096529620, 34150590308701283528, 19772322481956974532, 11008161481780603512, 5884917700519129288, 3016191418506637264, 1479594496462756340, 693434955498545848, 309881648709683160, 131760770157606224, 53181959591958024, 20324543852025936, 7333879739219600, 2490875091238112, 793548088258508, 236221241425176, 65418624260840, 16771945556496, 3958458557608, 854515874096, 167453394320, 29524775520, 4634116312, 639097008, 76185104, 7685024, 637360, 41696, 2016, 64, 1]

r = np.roots(coeff)

plt.plot(r.real, r.imag, '.')

我会建议类似以下的内容(从here 窃取和修改)。这听起来像是你已经尝试过的东西。但是尝试更改最大迭代次数以获得可以相对快速运行的东西(30 是快速的并且对我来说具有相对较高的分辨率)。

MAX_ITER = 30

def mandelbrot(c):
    z = 0
    n = 0
    while abs(z) <= 2 and n < MAX_ITER:
        z = z*z + c
        n += 1
    return n
# Image size (pixels)
WIDTH = 600
HEIGHT = 400

# Plot window
RE_START = -2
RE_END = 1
IM_START = -1
IM_END = 1

img = np.zeros((WIDTH, HEIGHT))
for x in range(0, WIDTH):
    for y in range(0, HEIGHT):
        # Convert pixel coordinate to complex number
        c = complex(RE_START + (x / WIDTH) * (RE_END - RE_START),
                    IM_START + (y / HEIGHT) * (IM_END - IM_START))
        # Compute the number of iterations
        m = mandelbrot(c)

        if m > MAX_ITER-1:
          img[x, y] = 1

plt.imshow(img.T, cmap='bone')

【讨论】:

    猜你喜欢
    • 2015-12-12
    • 2022-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2016-06-17
    相关资源
    最近更新 更多