ImageTools for complex (argument ) plot

June 30 2011 acer 10546
Maple

14

The complexplot3d command can color by using (complex) argument for the hue, and compute height z by magnitude. So, when rotated to view the x-y plane straight on, it can provide a nice coloring of the argument of whatever complex-valued expression is being plotted.

Another way to obtain a similar plot is to use densityplot (with appropriate values for its scaletorange option) and apply argument to the expression or function being plotted. For some kinds of complex-valued expression, the hue values can be computed quickly and inserted into a float[8] Vector using a Compiled procedure.

The plotsetup command can be used to redirect the plot driver to produce gif or bmp (bitmap) image files. The underlying plot drivers in the Standard GUI seem to produce better images, with fewer artefacts due to discontinuities) than do those in the commandline or Classic interfaces. Redirecting the plot by setting the plotdevice in the Standard GUI also seems to produce a better quality plot than is obtained by doing right-click context-menu export to the same format.

But unfortunately, the GUI and/or its plot driver will use very large amounts of memory during the creation and export of such plots. A 1000x1000 grid densityplot or complexplot3d can cause the Standard GUI to use upwards of 1GB of memory for exporting a single 400KB bitmap. (One has to run Unix `top`, or the Windows TaskManager or equivalent, in order to see the details of this intense memory use.) The GUI may also become unresponsive during the process.

An alternative scheme for producing such images, when no axes or markings are otherwise wanted in the image, is to use the ImageTools package. For the examples below, the memory use by the Standard GUI stayed constant at about its original 148MB. The memory allocation by the kernel is displayed below. So this is a relatively fast and memory thrifty approach. The code below is printing only the measured memory allocation increase.

I ran this in Maple 14.01 in the Standard GUI, on 64bit Linux running on an Intel i5. (I deliberately do not Preview or View the image in the Worksheet, so as to avoid the mentioned GUI memory leaks.)

Several of these examples appeared in a followup question to a post about using Mathematica instead of Maple for generating the images in this article. I believe that this post shows that direct construction of the images (without using plot drivers) allows Maple to produce clear, high quality instances of these pictures, in some common image file format, quickly.

The code below uses only two parameters `a` and `b` to designate the complex box. This could easily be amended to allow options to designate and nonsymmetric and more general box as the domain in the complex plane.

The process below is quick, for functions which have fast numeric implementations for evaluation under evalhf. But it could be made even faster (for some expressions) by Compiling a procedure that populates the float[8] Arrays storing the image. And some of the Arrays could be re-used for multiple images of the same size, if the image files are exported between instances of use. I think that the memory use could also be at least halved, even in the case of producing just a single image.

The images produced are quite clear and mostly free of artefact. The generated files are only 45KB and 112KB in size.

[edited] Apologies: I inadvertantly pasted the wrong code when I first posted this. I've now replaced it with the intended and corrected version.

restart:

ArgPlot:=proc(F,a,b,m,n)
local img_h,img_s,img_v,img_hsv,T;
uses ImageTools;
   T:=subs(__FF=eval(F),proc(r,c,A,B,rowdim,coldim)
   local a,b;
      a:=A+(B-A)*(r-1)/rowdim;
      b:=A+(B-A)*(c-1)/coldim;
      (argument(__FF(a+b*I))/Pi+1)/2;
   end proc):
   try
      img_h:=Create(m,n,(r,c)->evalhf(T(c,r,a,b,m,n)),'order'='Fortran_order'):
   catch:
      img_h:=Create(m,n,(r,c)->evalf(T(c,r,a,b,m,n)),'order'='Fortran_order'):
   end try;
   rtable_options(img_h,'subtype'=Matrix);
   LinearAlgebra:-MatrixScalarMultiply(img_h,360.0,'inplace'=true);
   rtable_options(img_h,'subtype'=Array);
   img_s:=Create(m,n,(x,y)->1,'order'='Fortran_order'):
   img_v:=Create(m,n,(x,y)->1,'order'='Fortran_order'):
   img_hsv:=CombineLayers(img_h, img_s,img_v):
   HSVtoRGB(img_hsv):
end proc:

F:=z->`if`(z=0,Pi,exp(1/z)):

st,bu:=time(),kernelopts(bytesalloc):
  Q:=ArgPlot(F,-0.25,0.25,600,600):
time()-st,kernelopts(bytesalloc)-bu;

                        2.410, 114798048

st,bu:=time(),kernelopts(bytesalloc):
ImageTools:-Write(cat(kernelopts(homedir),"//arg_expinv.jpg"),Q,quality=85):
time()-st,kernelopts(bytesalloc)-bu;

                            0.020, 0

F:=unapply(convert(convert(taylor(1/(1-z),z=0,100),polynom),horner,z),z):

st,bu:=time(),kernelopts(bytesalloc):
  Q:=ArgPlot(F,-1.5,1.5,600,600):
time()-st,kernelopts(bytesalloc)-bu;

                        5.770, 51763960

st,bu:=time(),kernelopts(bytesalloc):
ImageTools:-Write(cat(kernelopts(homedir),"//arg_tayl.jpg"),Q,quality=85):
time()-st,kernelopts(bytesalloc)-bu;

                            0.020, 0

 

 

Here are the two images produced and exported as image files by the above code.

 

 

 

 

 

 

 

st,bu:=time(),kernelopts(bytesalloc):
Q:=ArgPlot(Zeta,-40.0,2.0,300,300):
time()-st,kernelopts(bytesalloc)-bu;

                       658.120, 283718920

 

 

 

F:=z->Beta(z,1.1-1.7*I): Digits:=7:
st,bu:=time(),kernelopts(bytesalloc):
Q:=ArgPlot(F,-10.0,10.0,300,300):
time()-st,kernelopts(bytesalloc)-bu;

                       690.196, 51043196

 

 

acer

Please Wait...