Make Your Own Julia Fractals
Total Page:16
File Type:pdf, Size:1020Kb
CODING MANDELBROT & PYTHON PYTHON: MAKE YOUR TUTORIAL OWN JULIA FRACTALS With a smattering of Python and some clever maths you can create TARIQ RASHID even more beautiful chaos inside your Linux box. ulia sets are closely related to the Mandelbrot Set, which we explored last issue with some Jbasic Python. All of these are produced by the code we’ll develop here from the same simple iterated function z2+c as the Mandelbrot fractals. You can see that they are different to the Mandelbrot set, and yet there’s something about them that is similar. You can also see that some Julia sets are a single object like the Mandelbrot, but PRO TIP some are many pieces, with some even becoming so Explore the Mandelbrot fragmented they are almost like dust. and Julia fractals using the interactive XaoS open It might not be obvious, but while there is only one source software at http:// Mandelbrot, there are infinitely many Julia sets. The Just one look tells you that the Julia set is very closely bit.ly/1vLdz52 or through recipe, or algorithm, for creating Julia sets is the same related, but subtly different to the Mandelbrot set. a web browser http://bit. ly/1lbXYL1 as that for the Mandelbrot set except for one key difference. pass Let’s remind ourselves of the recipe for the pass Mandelbrot set. We choose a rectangle on the return iteration complex plane and test many evenly spaced points You can see that z doesn’t start at (0+0i) anymore to see whether they’re part of the Mandelbrot set. We but is a parameter passed to the function. The do this by seeing if repeated iteration of a function parameter c is also passed but is a fixed complex z2+c, where c is the point being tested, results in the number and not a number representing the point orbit escaping and diverging, or not. And if it does, being tested, because z does that now. we colour the point according to how fast the point The Python program to plot Julia sets is presented diverges. here for easy reference: This recipe is essentially the same for making Julia # loads the numerical and plotting extensions to Python Instead of c representing sets. The difference is that the roles of the z and c # needed if you’re using a locally installed IPython, not for some the point on the complex values are reversed (see diagram below). online IPython providers being tested (as in the The following shows the calculating function julia(z, %pylab inline Mandlebrot set), it is kept c, maxiter), based on the mandel(c, maxiter) function constant for all the points we developed in the last issue: # set the location and size of the atlas rectangle being tested. Similarly def julia(z, c, maxiter): xvalues = linspace(-2, 2, 1000) instead of z starting from zero, it starts as for iteration in xrange(maxiter): yvalues = linspace(-2, 2, 1000) the complex number z = (z*z) + c representing the point if abs(z) > 4: # size of these lists of x and y values being tested. break xlen = len(xvalues) ylen = len(yvalues) Mandelbrot set Julia set # value of c (unique for each Julia set) c = complex(-0.35, 0.65) Iterate z2 + c # julia function, takes the fixed parameters z and c and the maximum number of iterations maxiter, as inputs def julia(z, c, maxiter): Different c Different z # start iterating and stop when it’s done maxiter times C is point on rectangle being Z is point on rectangle being for iteration in xrange(maxiter): tested, varies across rectangle tested, varies across rectangle Z starts at zero (0+01) C is fixed for this Julia set # the main function which generates the output value of z from the input values using the formula (z^2) + c 98 www.linuxvoice.com MANDELBROT & PYTHON CODING z = (z*z) + c # check if the (pythagorean) magnitude of the output complex number z is bigger than 4, and if so stop iterating as we’ve diverged already if abs(z) > 4: break pass pass # return the number of iterations we actually did, not the final value of z, as this tells us how quickly the values diverged past the magnitude threshold of 4 return iteration # create an array of the right size to represent the atlas, we use the number of items in xvalues and yvalues atlas = empty((xlen,ylen)) Some Julia fractals # go through each point in this atlas array and test to see how are more broken up, while others are more many iterations are needed to diverge (or reach the maximum pass contiguous. iterations when not diverging) pass for ix in xrange(xlen): for iy in xrange(ylen): # set the figure size figsize(18,18) # at this point in the array, work out what the actual real and imaginary parts of x are by looking it up in the xvalue and yvalue # plot the array atlas as an image, with its values represented as lists colours, peculiarity of python that we have to transpose the zx = xvalues[ix] array zy = yvalues[iy] z = complex(zx, zy) imshow(atlas.T, interpolation=”nearest”) Try plotting different Julia sets by changing the c # now we know what c is for this place in the atlas, apply the parameter. Remember it is the single parameter c mandel() function to return the number of iterations it took to which uniquely defines the Julia set. diverge # we use 80 maximum iterations to stop and accept the 3D fractal landscapes function didn’t diverge This section will require you to use a locally installed atlas[ix,iy] = julia(z,c,80) version of IPython, because the online IPython services can’t access your OpenGL graphics subsystem to PRO TIP render the three-dimensional plots. Pretty as a picture The code for the full Let’s try to turn the two-dimensional flat images of programs to calculate and Here’s an image produced by one of the filters provided by the Mandelbrot and Julia sets into three dimensions. plot the Mandelbrot and Julia fractals is online at the same extension that we got the Gaussian filter from. It’s One way to do this is to use the colour information in http://bit.ly/1qpnlsE. beautiful – and is the cover of the author’s ebook (http:// each image array to represent altitude; the height of a www.amazon.co.uk/Make-Your-Mandelbrot-Tariq-Rashid- landscape above sea level as it were. We should then ebook/dp/B00JFIEC2A). See if you can recreate it yourself. see mountainous landscapes shaped by the values of The image is the result of a filter that highlights edges (a Sobel filter) applied to the same Julia images we created arrays filled by the Mandelbrot or Julia calculations. previously. The code is at http://bit.ly/1qpnlsE. Surface plot Let’s try it on a very simple array first. The following code prepares a 3x2 array: a = zeros( [3,2] ) a[0,0] = 1 a[0,1] = 2 a[1,0] = 4 a[2,1] = 3 print a Plotting a simple flat image based on these values, using imshow(a, interpolation=”nearest”) results in the simple 2D plot as expected. www.linuxvoice.com 99 CODING MANDELBROT & PYTHON Now let’s plot this same array in three dimensions, three-dimensional plots, remembering to import the with the third dimension given by the value of each mayavi extension too. It’s worth keeping the previous cell. To do this we need to use a new extension to imshow() instruction to see the flat view too. The Python called mayavi. instructions are as follows. The warp_scale was Telling IPython that we want to use mayavi uses adjusted to 1.0 to give a reasonable height, the default the import instruction, just as before. The special setting created hills that were a little too steep: paylab instruction is to ensure that the common set mayavi.mlab.surf(atlas.T, warp_scale=1.0) PRO TIP of numerical extensions are automatically loaded in mayavi.mlab.show() Don’t forget that in the local IPython: These three-dimensional height fields as some Python, like many %pylab inline people call them, give an interesting perspective of the programming langauges, counting elements of import mayavi.mlab Mandelbrot set. arrays and lists starts at The instructions to plot a surface are simple: Let’s try that again but with the original rectangle 0 not 1. mayavi.mlab.surf(a, warp_scale=”auto”) zooming into details of the Mandelbrot set, and also mayavi.mlab.show() change the colour palette by using the interactive The first instruction prepares the surface plot and menus. Some of these landscapes are quite haunting. allows IPython to automatically scale the heights. The Let’s try the same idea with Julia sets. second instruction is needed to actually show the plot. This time the result will appear in a separate window Gentler landscapes outside your browser. You can do quite a lot to this Some of the landscapes we created were a little spiky 3D plot, including rotating it around and changing the and noisy. Let’s see if we can calm them down, lighting that illuminates it. The images at the bottom smooth off the sharp edges, and reduce the of the page show the above small array plotted in cragginess a little, all in the hope of creating a perhaps this way, and again rotated using the mouse. Try it more gentle, more appealing, landscape. yourself. One way to do this is to calculate a new image For easy reference, the entire code for creating a array, based on the original one, but with each new simple 3x2 array and plotting a three-dimensional plot value somehow smoothed based on its neighbour’s of it as as follows: values.