Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I worked from Lauritz V. Thaulow's code and was able to get a pretty significant speed-up with the following code:</p> <pre><code>import numpy as np import matplotlib.pyplot as plt from itertools import count def newton_fractal(xmin, xmax, ymin, ymax, xres, yres): yarr, xarr = np.meshgrid(np.linspace(xmin, xmax, xres), \ np.linspace(ymin, ymax, yres) * 1j) arr = yarr + xarr ydim, xdim = arr.shape arr = arr.flatten() f = np.poly1d([1,0,0,-1]) # x^3 - 1 fp = np.polyder(f) counts = np.zeros(shape=arr.shape) unconverged = np.ones(shape=arr.shape, dtype=bool) indices = np.arange(len(arr)) for i in count(): f_g = f(arr[unconverged]) new_unconverged = np.abs(f_g) &gt; 0.00001 counts[indices[unconverged][~new_unconverged]] = i if not np.any(new_unconverged): return counts.reshape((ydim, xdim)) unconverged[unconverged] = new_unconverged arr[unconverged] -= f_g[new_unconverged] / fp(arr[unconverged]) N = 1000 pic = newton_fractal(-10, 10, -10, 10, N, N) plt.imshow(pic) plt.show() </code></pre> <p>For N=1000, I get a time of 11.1 seconds using Lauritz's code and a time of 1.7 seconds using this code. </p> <p>There are two main speed-ups here. First, I used meshgrid to speed-up the creation of the numpy array of input values. This is actually a pretty significant part of the speed-up when N=1000.</p> <p>The second speed-up comes from only doing calculations on the unconverged portions. Lauritz was using masked arrays for this before realizing that they were slowing things down. I haven't looked at them in quite some time, but I do remember masked arrays being a source of slowness in the past. I believe it is because they are largely implemented in pure Python over a numpy array rather than being written almost completely in C like numpy arrays.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload