简体   繁体   English

如何在 python 中声明一个变量而不赋值?

[英]How to declare a variable in python without assigning a value?

I'm trying to create graphs of the Mandelbrot set.我正在尝试创建 Mandelbrot 集的图表。

I have managed to do this by iterating over a lot of points but this takes a lot of processing power, so I'm now trying to generate a polynomial by iterating f(z) = z**2 + c many times and then finding the roots for z = c , in order to generate a boundary of the set.我已经设法通过迭代很多点来做到这一点,但这需要大量的处理能力,所以我现在试图通过迭代f(z) = z**2 + c多次然后找到z = c的根,以生成集合的边界。

However I can't seem to get Python to generate the polynomial, any help would be much apprecaited.但是我似乎无法让 Python 生成多项式,任何帮助都会非常感激。

Edit:Implemented azro's fix but now I get the error - TypeError: unsupported operand type(s) for ** or pow(): 'NoneType' and 'int'编辑:实施了 azro 的修复,但现在我收到错误 - TypeError: unsupported operand type(s) for ** or pow(): 'NoneType' and 'int'

Code so far:到目前为止的代码:

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)

This is a very hard problem.这是一个非常困难的问题。 Searching through literature, I only found this (which doesn't seem very reputable).通过文献搜索,我只发现了这个(这似乎不太有名)。 However, it does seem to begin to create what you want.然而,它似乎开始创造你想要的东西。 This is only up to 8 iterations.这最多只有 8 次迭代。 So the polynomial gets very complicated very fast.所以多项式很快变得非常复杂。 See the following code:请参阅以下代码:

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, '.')

某事也许

I would suggest something more like the following (stolen and modified from here ).我会建议更像以下内容(从此处窃取和修改)。 This sounds like something you've already tried.这听起来像是你已经尝试过的东西。 But try changing the max iterations to get something that can run relatively fast (30 was fast and had relatively high resolution for me).但是尝试更改最大迭代次数以获得可以运行相对较快的东西(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')

输出图片

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM