Pretty Fractal Images

Download this: julia.zip

Instruction are provided in a Readme file, and in the comments within the source code. You can soom be making fractal images like this:

julia.png

julia.zip contains 4 versions of the same program. The Python versions are the slowest. The compiled versions, C and Fortran, are the fastest. The time for the program to run (user time) is from a Linux desktop.

source

type

user time

julia.c

C code

0.3 seconds

julia.f90

Fortran

0.3 seconds

julian.py

Python+numpy

6 seconds

julia.py

Python

41 seconds

The factor of ten slowness of Python, even with the number-crunching array extension of numpy, is similar to what is quoted in the table at the bottom of Performance Python Don't throw away your compilers...

The Julia Set Algorithm

The pretty pictures are generated using the algorithm for Julia sets. For the values of (x,y) that did not break out, we set a color based on the minimum value of x2+y2 achieved during the iteration. For values of (x,y) that did break out, we set a color based on the last value of (x,y) before the breakout. The choice of color scheme is purely for aesthetics, you are welcome to experiment with your own choices.

Compiling and Running

The following instructions are for Linux. Instructions for Windows can be inferred from Ready and PyIntro.

gcc -o juliac julia.c -lm

or some other C compiler on your system, usually invoked as:

cc -o juliac julia.c -lm

To get the above default image in a file, in PPM format, simple execute juliac with no command line arguments.

juliac accepts up to 6 arguments from the command line. The first three are: width, height and magnification. This is the same as using the defaults:

juliac 600 400 10

You may want to try these:

juliac 1200 800 10

juliac 1200 800 2

A Fortran 90 version is also available. Compile with

f90 -o juliaf julia.f90

juliaf does not accept arguments from the command line, rather they are input in the namelist file fractal.input. Run as

juliaf < fractal.input

To display the time required for execution, try this:

time juliac
time juliaf < fractal.input

The python versions julian.py and julia.py are much slower than the compiled versions. To keep you entertained while you wait, some information about the iteration of the algorithm is displayed on the monitor.

Don't know C? Here is a C tutorial, and a C++ tutorial.

Don't know Fortran? Here is a list of Fortran tutorials, if you are interested.

Some image conversion experiments

You may find it interesting to experiment with:

convert fromc.ppm -quality 50 a.jpg
convert fromc.ppm -quality 90 b.jpg

Then compare the file size and appearance of a.jpg and b.jpg.

If you read the online documentation for convert, you will find that .png also accepts the -quality parameter. But for .png, the parameter is for the quality of the compression, not the image. Next try

convert fromc.ppm -quality 50 a.png
convert fromc.ppm -quality 100 b.png
convert fromc.ppm  c.png

Then compare the file size and appearance of the files. They should look exactly the same.

convert a.png a.ppm
convert c.png c.ppm
diff a.ppm c.ppm

The will probably find that the diff command returns nothing; meaning the files are identical. PNG is a lossless compression.

If you post your graphics on a web page, you should post your graphics in either .png or .jpg format, rather than .ppm.

Scripting the C program

julia.zip contains a short program (a script) juliascript.py to sequentially call the C program with various parameters. The script also converts the image to PNG format with a sequential number in the name, so that the image can be used as a frame in an animation. The final animation is available to view at okazhun.com .

Mathematica

Mathematica is a commercial software package with enormously diverse capabilities, including a packed array syntax similar to numpy. Mathematica is free for students and employees at OU. Below are some test programs to compare the array capabilities of Mathematica with Python.

I find Mathematica can be twice as fast as numpy for matrix multiplication. But the packedarray syntax is not nearly as capable as numpy. Nevertheless, creative use of Mathematica's UnitStep does allow the Julia Set algorithm to be implemented. But Mathematica takes twice as long to produce the fractal image.

(Note: both the mathematica and python implementations can be expected to require about twice the number of operations as C or Fortran, because of the array syntax. C and Fortran can bail out of computing with certain elements in the array after a "breakout").