Mathematica Version 6 Compatibility Information

Updated July 2007 with Version 6.0.1 (MacOS)

Introduction

Most of the code in the book works right away with version 6. When you open a notebook from the CD you are given the option of running a compatibility check. Feel free to do this - the author is doing the same and will comment below on things that are flagged. Note that embedded C code listings will get flagged and this is irrelevant! Many chapters get straight through the compatibility check with no problems.

There are a few places where things break. Most of these are to do with the fact that the simple looping command Do, when used to make a movie, needs to be replaced by the new command Manipulate. ParametricPlot3D has a new colouring structure that requires some minor editing to get it to work as in the book, as does the ordinary Plot3D. Lighting also needs to be kept on in order to see anything at all! 
	There are many neat new features that the author is still discovering at the time of final editing. If you click on a 3D graphic you gain access to 3D manipulation tools. You can also now give transparency for surfaces, which makes a wonderful enhacement to all the 3D graphics in the book. Some places where you might turn this feature on are given below and in the other notebooks in this directory of the CD. 
	The Sections below give chapter by chapter information. Do e-mail the author if you find problems, or new neat features to exploit. It is intended to maintain and update this notebook.

Now a warning: kill all semi-colons after graphics!!

Mathematica 6 contains a new way of managing output. You might find that code you have routinely used to produce graphics stops producung any output at all. This will happen, if like me, you have got used to routinely appending semi-colons (;) at the end of graphics commands in order to suppress the (often-superfluous) Out statement. You need to delete all such semi-colons. I have tried my best to eliminate all the semi-colons from the book but at the time of producing this update I was still finding them here and there embedded in other code (see the Chapter 7 update). If you find somewhere that I have not covered with this update try looking for a semi-colon!

2 Complex algebra and geometry

2.11 \[MathematicaIcon] Mathematica visualization of complex roots and logs

This is a good place to try out the real time rotation and other tools.  Just click on the graphic below and move the mouse. Holding down the shift key moves the grpahic in the plane of the notebook page, and (on my Mac) holding down the Alt or Command keys zooms in and out. Compared to the printed text, the definition of ViewRootSurface has been very slightly adjusted to account for some odd behaviour at r=0 but is essentially unchanged.

ViewRootSurface[n_Integer, resolution_Integer] := 
ParametricPlot3D[{r*Cos[theta], r*Sin[theta], r^(1/n)*Cos[theta/n]}, 
{r, 0.000001, 2}, {theta, 0, 2*n*Pi},
PlotPoints -> {resolution, resolution*n}, 
Boxed -> False, Axes -> False,
AspectRatio -> 1,ViewPoint -> {-3, -3, 0}]

ViewRootSurface[2, 20]

The above code is Version 6 compatible. If you want to make a V6-savvy plot then the most interesting thing to do is to exploit the introduction of transparency controls via the Opacity function. Here is a simple example:

ViewRootSurfaceSix[n_Integer, resolution_Integer] := 
ParametricPlot3D[{r*Cos[theta], r*Sin[theta], r^(1/n)*Cos[theta/n]}, 
{r, 0.000001, 2}, {theta, 0, 2*n*Pi},
PlotPoints -> {resolution, resolution*n}, 
Boxed -> False, Axes -> False,
AspectRatio -> 1,ViewPoint -> {-3, -3, 0}, PlotStyle -> Opacity[0.5]]

ViewRootSurfaceSix[2, 20]

You might like to explore some other functions this way, and make similar adaptations to ViewLogSurface.

Exercises

2.41 \[MathematicaIcon] Use the function ViewRootSurface to investigate the behaviour of the cube and fourth-root functions. If you have Mathematica 6 use the Opacity option to get the best view.

3 Cubics, quartics and visualization of complex roots

3.4 \[MathematicaIcon] Root movies and root locus plots

This is a good first opportunity to use the new Manipulate function instead of the boring old Do operation!

CRPlot[z_List, range_List, size_] := 
Module[{r},
r = Map[{Re[#], Im[#]}&, z];
ListPlot[r, PlotStyle -> PointSize[size],
AspectRatio -> 1, PlotRange -> {range, range},
PlotRegion -> {{0.05, 0.95}, {0.05, 0.95}}]]

PolySolver[poly_] :=z/.NSolve[poly==0, z]

mypoly[z_, λ_] := z^5+z^3 + z^2 + z + λ;

Movies

This is done in version 6 of Mathematica using Manipulate

Manipulate[CRPlot[PolySolver[mypoly[z,λ]], {-2, 2}, 0.05], {λ, 0, 4, 0.2}, SaveDefinitions -> True]

4 Newton–Raphson iteration and complex fractals

Most of this works as before. The key difference is at the end on fractal planets, due to a change in colouring methods. Some code from earlier sections is included as it is needed later, and you might wish to check the the basic plot is working in your current version before addressing the planetary issue!

4.7 \[MathematicaIcon] Fractal planets: Riemann sphere constructions

\[MathematicaIcon]6 Changes to colouring schemes

In version 6 of Mathematica the system for colouring surfaces by other functions has changed significantly and the code given in the book no longer works. One no longer supplies a second function containing the colour directive as the second part of a list. Instead one uses the new ColorFunction option, and to use the controlled scalings introduced anobe one must also override any automatic scaling of the arguments by setting
ColorFunctionScaling -> False. 

The ColorFunction is best supplied as a pure function and in examples such as the above five arguments can be supplied. 

Teh first three corresponde to (x,y,z) and the fourth and fifth to the parameters, here (θ, ϕ). Some relevant modifications to obtain examples similar to those in the book are easily obtained.

Note:the operation of ParametricPlot3D changed slightly prior to release of the final version, so that the routines on disc need minor modification. You delete the RenderAll command and set Lighting-> Automatic. Here is a self-contained routine.

NewtonCounter = Compile[{{z,_Complex}},
Module[{counter=0, zold=N[z]+1.0, znew=N[z]},
If[Abs[znew] < 10^(-9), znew = 10^(-9)+0.0*I, znew=znew];
For[counter = 0,
(Abs[zold-znew] > 10^(-6)) && (counter < 85), 
counter++,
(zold = znew;znew = 2*zold/3 + 1/(3*zold^2))];
Which[Abs[znew-1] < 10^(-4), counter,
Abs[znew+0.5-0.866025I] < 10^(-4), 85+counter,
Abs[znew+0.5+0.866025I] < 10^(-4), 170+counter,
True,255]]];

tr[x_] = Which[x<0,0,x>1,1,True,x];

NewtonColorRGB[x_] :=
Which[
x <0.333, RGBColor[tr[5*3x],0,0], 
0.334<x<0.666, RGBColor[0,tr[5*(3x-1)],0],
0.667<x<1, RGBColor[0,0,tr[5*(3x-2)]], 
True, RGBColor[0,0,0]]

ParametricPlot3D[{Sin[θ] Cos[ϕ], Sin[θ] Sin[ϕ],Cos[θ]},
{θ, 0.001, Pi-0.001}, {ϕ, 0, 2 Pi},
ColorFunction -> (NewtonColorRGB[NewtonCounter[Exp[I #5] Cot[#4/2]]/255]&), 
ColorFunctionScaling -> False,
Lighting -> Automatic, Boxed -> False, Mesh -> False,
Axes -> False, 
PlotPoints -> {50,100},
ViewPoint -> {0,0,-3}, Background -> RGBColor[0,0,0]]

GenNewtonCounter = Compile[{{z,_Complex}, {n,_Integer}}, Module[{counter=0, zold=N[z]+1, znew=N[z], k, m},
If[Abs[znew] < 10^(-9), znew = 10^(-9)+ 0.0*I, znew=znew]; For[counter = 0,
(Abs[zold-znew] > 10^(-6)) && (counter < 85), 
counter++,
(zold = znew;znew = zold*(1-1/n) + 1/(n*zold^(n-1)))];
For[k = 0, (Abs[znew - Exp[2*Pi*I*k/n]] > 0.1) && (k < n), k++, counter = counter + 85]; counter]];

GenNewtonColorSeven[n_, x_] :=
Which[x <0.14285, RGBColor[tr[(3)7x],tr[(3)7x],tr[(3)7x]], 
0.14286<x<0.28571, RGBColor[tr[(3)(7x-1)],tr[(3)(7x-1)],0],
0.28572<x<0.42857, RGBColor[0,tr[(3)(7x-2)],tr[(3)(7x-2)]], 
0.42858<x<0.57142, RGBColor[tr[(3)(7x-3)],0,tr[(3)(7x-3)]],
0.57143<x<0.71428, RGBColor[tr[(3)(7x-4)],0,0],
0.71429<x<0.85714, RGBColor[0,tr[(3)(7x-5)],0],
0.85715<x<1,        RGBColor[0,0,tr[(3)(7x-6)]],
True, RGBColor[0,0,0]]

ParametricPlot3D[
{Sin[θ] Cos[ϕ], Sin[θ] Sin[ϕ],Cos[θ]},
{θ, 0.001, Pi-0.001}, {ϕ, 0, 2 Pi},
ColorFunction -> (GenNewtonColorSeven[7, GenNewtonCounter[Exp[I #5] Cot[#4/2],7]/595]&),
ColorFunctionScaling -> False,
Lighting -> Automatic, Boxed -> False, Mesh -> False,
Axes -> False, NormalsFunction -> None,
PlotPoints -> {50,100},
ViewPoint -> {0,3,0},Background -> RGBColor[0,0,0]]

This code will not work in Mathematica versions 4, 5. In these examples a much lower value of PlotPoints has also been chosen initially in order the use the rotational graphics tools in version 6 on a slower machine. Also, the polar coordinates θ and ϕ run over their full ranges [0, π] and [0, 2 π] respectively, so that you have the entire fractal planet to fly around! The value of θ is truncated at the North and South pole to avoid kernel complaints about singular behaviour. You can fly around the planets using the rotate command within the Show Drawing Tools option under the Graphics menu.

5 A complex view of the real logistic map

A lot of experimentation goes into making pictures such as the bifurcation diagrams.

What is really happening here? It is a matter of 

a) real roots of a polynomial becoming unstable, 

simultaneously with

b) a pair of complex roots becoming both real and stable. 

If you have Mathematica 6 or later replace the Do command by Manipulate to get real-time control of the movie in the notebook in Section 5.3. You need to run several commands before trying this out.

Logistic[λ_, x_]:=λ x (1-(x^2))

(Subscript[Logistic, 2])[λ_, x_] := Nest[Logistic[λ,#]&,x,2]

(Subscript[logsol, 2]) =FullSimplify[ x/. Solve[x==(Subscript[Logistic, 2])[λ, x] ,x]];

(Subscript[Dlog, 2])[λ_, x_] =Simplify[D[(Subscript[Logistic, 2])[λ, x], x]];

(Subscript[der, 2])[λ_] =Simplify[(Subscript[Dlog, 2])[λ, (Subscript[logsol, 2])]];

values[r_] := {Abs[(Subscript[der, 2])[r]], (Subscript[logsol, 2 ])/. λ -> r}

cfunc[x_] := 
	If[x<=1, 
	{PointSize[0.06],RGBColor[0,0,1]},
	{PointSize[0.04], RGBColor[1,0,0]}]

pts[r_] := Map[Flatten[{cfunc[#[[1]]], Point[{Re[#[[2]]],Im[#[[2]]]}]}]&, Transpose[values[r]]]

Manipulate[frame[k] = Show[Graphics[pts[k/10]],
		PlotRange -> {{-2, 2}, {-2, 2}},
		Frame -> True,FrameTicks -> None,
		AspectRatio -> 1], {k, 1, 25}, SaveDefinitions -> True]

If you run the compatibility checker you will also get a suggestion to replace the call to GraphicsArray by GraphicsGrid. This is no big deal, and works as expected:

Show[GraphicsGrid[{{frame[1],frame[4],frame[9]},{frame[10],frame[11],frame[16]},{frame[19],frame[22],frame[25]}}]]

6 The Mandelbrot set

6.5 \[MathematicaIcon] MathLink versions of the escape-time algorithm

Note that on some platforms MathLink performance is greatly improved by upgrading to 6.0.1 from 6.0.0.

A Turbo-charged direct (PostScript^TM) MathLink version!

This particular implementation is for use with Mathematica 4 and 5 ONLY – the management of graphics in the Mathematica 6 front end is very different and the details to construct analogous code are not available at the time of writing.

Timings

Some of the comments on relative timings in the book seem out of date. First impressions are that the internal compiler does quite well!

6.6 \[MathematicaIcon] Diving in to the Mandelbrot set: fractal movies

The code here works in you make the (now familiar) substitution of Do with Manipulate

Directory[]

In order to access the mathlink files, you will need to set the directory appropriately. Here are two commands I use on my workstation and laptop - adapt these to your own environment and set the path to wherever you have put the MathLink files.

(* laptop *)
SetDirectory["Books/Complex/ComplexMath2005/AAACDMaster/MathLink/binMacOSX"];

(* workstation *)
SetDirectory["Books/ComplexMath2005/AAACDMaster/MathLink/binMacOSX"];

Directory[]

FileNames[]

Note that this path should be set to wherever you have put them – the above works only on the author's computer! See the CD notes for location and other information of the files on the enclosed CD.

Install["mandel"]

We can query the operation of the function as with any built-in function, The query returns information contained in the template file and references to the fact that it is a LinkObject:

?FractalML

We adapt our previous routine to call this function:

FracML[z_, {{remin_, remax_},{immin_, immax_}}, steps_] :=
{{{remin, remax},{immin, immax}},
FractalML[Re[z], Im[z],
{{remin, remax},{immin, immax}},
steps]}

Finally, for some variety, we define some new colour schemes:

FracColorA := (
If[#==1,
RGBColor[0,0,0],
If[#<=0.5, RGBColor[2#,2#,2#], RGBColor[2-2#,2-2#,1]]]&)

FracColorB := (
If[#==1,
RGBColor[0,0,0],
If[#<=0.5, RGBColor[1,1-2#,0], Hue[1.5-#]]]&)

FracPlot[{{{remin_, remax_}, {immin_, immax_}}, matrix_},
colorfn_] :=
ListDensityPlot[matrix,
Mesh -> False, Frame -> False,
ColorFunction -> colorfn,
PlotRange -> {0, 100},
AspectRatio -> (immax-immin)/(remax - remin)]

We can now try it out. We not not use a Timing command as this reports kernel time only, but this routine is about four times faster than the compiled Mathematica:

FracPlot[FracML[0, 
{{-2.1,0.5},{-1.2,1.2}},200],FracColorA]

It is now a simple matter to switch colour scheme:

FracPlot[FracML[0, 
{{-2.1,0.5},{-1.2,1.2}},200],FracColorB]

Mathematica 6 standard colour schemes

If you get fed up with my colour schemes you might like to try some of the new ones available in M6. These can be listed as follows"

ColorData["Gradients"]

You just have to substitiute the colour as follows:

ColorData["Gradients"][[39]]

FracPlot[FracML[0, 
{{-2.1,0.5},{-1.2,1.2}},288], ColorData["Gradients"][[39]]]

Note that with the mandel.c code, only the counting of iterations is farmed out to external code. This means that this code generates fractals that can be coloured as you choose, as above.

Zooming in with the MathLink code

This requires the zooming function precisely as in the book. It is possible to make this code work with the replacement of Do with Manipulate, but given that Manipulate is designed to work best with real-time computable graphics, it is rather clunky. It is better to use ListAnimate to animate a table, as in the example below.

g[pointx_, pointy_, n_, scale_] := 
Module[{width = scale^n},
{{pointx-width,pointx+width},{pointy-width,pointy+width}}]

?ListAnimate

Try this out with the setting of the array size to 20 as shown, first, to see it is working OK. Then increase this and wait a little while.

ListAnimate[Table[
	FracPlot[
	FracML[0, g[-1.40835915,0.13627737,n,0.8],20],
	FracColorB], 
{n, -3, 20}], DisplayAllSteps-> True]

Note also that there is some smoothing of the colours in the operation of ListDensityPlot. This can probably be removed, maybe by using the Raster function, but I have not sorted this out yet. Similar smoothing applies to the fractal planets.