2

这是我到目前为止的代码:

import numpy as np
def mandelbrot(resol):

    R, I = np.meshgrid(np.linspace(-2,.5,resol), np.linspace(1.25j,-1.25j,resol))
    Z = R + I
    C = np.array(Z)

    iterations = np.zeros(Z.shape, dtype=np.uint8)
    escape_vals = np.zeros(Z.shape, dtype=np.complex128)

    for i in np.arange(MAXITERS):
        #only the complex values that haven't diverged get iterated
        _ = np.abs(Z) <= 2
        Z[_] *= Z[_]; Z[_] += C[_]

        #np.invert(_, _) just toggles all of the boolean values in _
        #So, wherever np.abs(Z) > 2...
        np.invert(_, _)
        iterations[_] = i
        escape_vals[_] = Z[_]

    #Take the last set of points np.abs(Z) > 2,
    #invert it; those are the points in the  Mandelbrot set.
    np.invert(_, _)
    iterations[_] = MAXITERS
    escape_vals[_] = Z[_]

    iterations += 2
    escape_vals *= escape_vals; escape_vals += C
    escape_vals *= escape_vals; escape_vals += C

    moduli = np.array(np.abs(escape_vals), np.float64)
    mus = np.array(iterations - np.log(np.log(moduli))/np.log(2))
    return mus

我的问题是我得到的模值介于 0 和 1 之间,当我记录这两次的对数时,我得到了 nan。我也不确定将这些值映射到我选择的调色板的最佳方法!有人看到我犯的错误吗?

4

0 回答 0