Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The following code block is in Trott's Guidebook for Programming. The associated notebook says that this and a few other routines ran in a matter of seconds. I killed it after half an hour.
I am curious to know what might be the issue. My guess is that $MaxMachineNumber is much bigger on my new laptop.

Module[{cp, cls, L = 0.02},
  (* an initial contour plot *)
  cp = ContourPlot[Im[Exp[1/(x + I y)]], {x, -L, L}, {y, -L, L}, 
                   PlotPoints -> 400, DisplayFunction -> Identity] /.
  (* replace large high-precision numbers by biiig machine numbers *)
     z_?(Abs[#] > $MaxMachineNumber&) :> Sign[z] $MaxMachineNumber/2;
  (* homogeneously distributed contour lines *)                 
  cls = #[[100]]& /@ Partition[Sort[Flatten[cp[[1]]]], 800];   
  (* the final contour plot *)     
  ListContourPlot[cp[[1]], MeshRange -> {{-L, L}, {-L, L}},
                  Contours -> cls, ContourLines -> False,
                  ColorFunction -> (Hue[Random[]]&), 
                  AspectRatio -> Automatic, FrameTicks -> None]]
share|improve this question
Perhaps ContourPlot has changed. in V9.0.1, cp[[1]] is a GraphicsComplex, and MeshRange is no longer a valid option. I don't see how the code could work as it is. The initial ContourPlot takes forever with PlotPoints -> 400. It takes 10 sec. with PlotPoints -> 25 and several GB of ram. – Michael E2 Nov 16 '13 at 18:10
It's a pity - these are really impressive books, but it's difficult (particularly for us newcomers who don't remember Mathematica 7 or earlier) to get the code to run. I got the initial plot by reducing PlotPoints, but the code for the final plot is harder to adapt. (The notebooks were originally developed "in the 1991–1992 academic year on an Apple Macintosh IIfx with 20 MB RAM"... - but he must have updated them to run on a computer from the future...). – cormullion Nov 16 '13 at 18:17
@cormullion I have all Trott books. I do not actually run any code, I just use them to read the code from the pages and try to figure what it does, but it is too advanced for me. 90% of the time I can't understand the code, but I like to look at it still because it looks amazing. – Nasser Nov 17 '13 at 1:26
up vote 7 down vote accepted

For this very case there is a very simple approach to make the code run. Between Mathematica version 5 and version 6 there were some major changes and one of them was how graphics is handled. To make it easier to run version 5 (or earlier) code with version 6 it brought the possibility to load the older/legacy (version 5) graphics handling -- and that is still possible. On my Mathematica 9 on a Windows 7 laptop the given code works without any changes when before executing I evaluate:

 << Version5`Graphics`

Note that the MeshRange option still is marked as invalid (red) but I think will be treated correctly by the now active version 5 graphics routines. Concerning the improvement in hardware the runtime is relatively slow (probably a minute or so) but certainly not half an hour. Memory usage doesn't look problematic as well. You should also be aware that this will switch to the older (post script based) graphics for all the following code. To switch back either restart the Kernel or do a

<< Version6`Graphics`

It most probably doesn't solve all the issues that todays Mathematica versions might have with Michael's code from the early ninties, but it certainly is something that's worth knowing when exploring his books...

share|improve this answer
Awesome. I'd forgotten about the legacy packages. It takes less than 3 seconds on my new-ish laptop. – Michael E2 Nov 17 '13 at 13:13

A similar, but not identical, result using more up-to-date graphics might look like this:

Module[{data, cls, cf, L = 0.02},
 data = Clip[With[{r = Range[-L, L, 2 L/799]}, Im[Exp[1/Outer[Plus, -I r, r]]]], 
    {-$MaxMachineNumber, $MaxMachineNumber}];
 cls = Sort[Flatten[data]][[1 ;; -1 ;; 2000]];
 cf = With[{c = {#, Random[] // Hue} & /@ cls}, Blend[c, #] &];
 ArrayPlot[data, ColorFunction -> cf, ColorFunctionScaling -> False]]

enter image description here

It's worth noting that the ContourPlot in the version 5 code is being used just to get an array of z values. In my code I just compute the data array directly. I've used Span instead of Partition and Part to extract the list of contour levels. Instead of ListContourPlot I have used Blend to construct a color function and ArrayPlot for display.

share|improve this answer
+1 can you do the rest of the books as well :) – cormullion Nov 17 '13 at 20:52
Your code runs quite fast and produces a very similar result. I appreciate the rewrite. Very skilled. – NotAZombie Nov 17 '13 at 21:54

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.