Message Boards Message Boards

How can I optimize my code for Daniel Velleman plots?

Posted 10 years ago
Hi Everyone,

I am not experienced with the different programming paradigms offered by the current version of Mathematica.
I have written the following code to produce visualisations of complex functions, in the style of Daniel Velleman's very nice plots.
My code runs pretty slowly.
Please can anyone give me some ideas for how to optimise it ?

Thank you very much !

 Clear[complexcolourplot, f, saturationscalefactor,
   brightnessscalefactor, sidelength, increment, phi, t,
   halfsidelength, halfincrement, x, y];
 phi[t_] := t/(1 + t);
 complexcolourplot[f_, saturationscalefactor_, brightnessscalefactor_,
   sidelength_, increment_] :=
  Module[{halfsidelength, halfincrement, x, y},
  halfsidelength = sidelength/2;
  halfincrement = increment/2;
   Table[{Hue[Arg[f[x + I y]]/(2 Pi),
      1 - phi[saturationscalefactor Abs[f[x + I y]]],
      phi[brightnessscalefactor Abs[f[x + I y]]]],
     Rectangle[{x - halfincrement,
       y - halfincrement}, {x + halfincrement,
       y + halfincrement}]}, {x, -halfsidelength, halfsidelength,
     increment}, {y, -halfsidelength, halfsidelength, increment}]]]

For example, here is the identity function, which shows which colours are assigned to which complex numbers:
complexcolourplot[Identity, .2, 2, 20, .2]

and here is a simple monomial:
Clear[f, z];

f[z_] := z^2;

complexcolourplot[f, .2, 2, 20, .2]
POSTED BY: Simon Cowell
5 Replies
Posted 10 years ago
Oh wow, Sam, that is totally awesome !
Thank you very much for your help emoticon
I will enjoy studying that code more carefully !
POSTED BY: Simon Cowell
Simon, I would recommend looking at this post: How can I generate this “domain coloring” plot?

POSTED BY: Sam Carrettie
Posted 10 years ago
Wow !

Thank you very much for the helpful information, Shenghui - I appreciate you taking the time !
I will experiment with your raster method next time I am on campus.

Sam - I am referring to this paper:

The Fundamental Theorem of Algebra: A Visual Approach

which is I think where I first saw these kind of plots.

In fact I think the title of my original post has been edited, presumably by a mod. I don't think I used the term "Daniel Velleman Plots", and I am pretty sure I used the British English spelling of "optimise" ! ;)
POSTED BY: Simon Cowell
Simon, what are exactly Daniel Velleman plots? Do you have a link or a reference?
POSTED BY: Sam Carrettie
I guess that your way might be the easiest and most efficient way to handle this problem. Usually ColorFunction can handle such request pretty well. I notice that your color scheme depends upon both the arg value and radius of a complex number, which is quite painful for the build-in ColorFunciton option for DensityPlot or ContourPlot (these plot function may be what you need). Because it requires sort of a single-argument function. 

Another way of handling this is to use the Raster function along with ColorConvert. Here is an example: 
             Hue[Arg[f[x+I y]]/(2 Pi),
                 1-phi[saturationscalefactor Abs[f[x+I y]]],
                 phi[brightnessscalefactor Abs[f[x+I y]]]],
Briefly, ColorConvert function transfer your Hue[] objects to RGB[] list which is used in the Raster[] object. After you wrap the Graphics function around the Raster[], you get the plot. 
f[z_] := z^2;
Graphics@complexcolourRaster[f, .2, 2, 20, .1]

Performancewise these two methods are the same because the bottleneck is really from the Front End trying to render everything nicely, Yet the second one gives you a better option for storing and sharing the graphics. 
POSTED BY: Shenghui Yang
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract