home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / e / e032 / 3.ddi / FILES / PROGRAMM.PAK / COMPLEXM.M < prev    next >
Encoding:
Text File  |  1992-07-29  |  5.4 KB  |  175 lines

  1.  
  2. (*********************************************************************
  3.  
  4.     Copyright 1991 by Roman E. Maeder
  5.     
  6.     Adapted from
  7.     Roman E. Maeder: Programming in Mathematica,
  8.     Second Edition, Addison-Wesley, 1991.
  9.  
  10.     Permission is hereby granted to make copies of this file for
  11.     any purpose other than direct profit, or as part of a
  12.     commercial product, provided this copyright notice is left
  13.     intact.  Sale, other than for the cost of media, is prohibited.
  14.     
  15.     Permission is hereby granted to reproduce part or all of
  16.     this file, provided that the source is acknowledged.
  17.  
  18.  *********************************************************************)
  19.  
  20.     (* Enclosed with Mathematica by permission *)
  21.  
  22.  
  23. (*:Version: Mathematica 2.0 *)
  24.  
  25. (*:Name: Graphics`ComplexMap.m` *)
  26.  
  27. (*:Title: CartesianMap and PolarMap *)
  28.  
  29. (*:Author:
  30.     Roman E. Maeder
  31. *)
  32.  
  33. (*:Keywords:
  34.     Cartesian coordinates, polar coordinates, lines, mapping.
  35. *)
  36.  
  37. (*:Requirements: none. *)
  38.  
  39. (*:Warnings: none. *)
  40.  
  41. (*:Sources:
  42.     Roman E. Maeder: Programming in Mathematica,
  43.     Second Edition, Addison-Wesley, 1991.
  44. *)
  45.  
  46. (*:Summary:
  47. This package plots the images of cartesian coordinate lines and
  48. polar coordinate lines under a user supplied function f.
  49. *)
  50.  
  51. BeginPackage["Graphics`ComplexMap`"]
  52.  
  53. CartesianMap::usage = "CartesianMap[f, {x0, x1, (dx)}, {y0, y1, (dy)}, options...] plots
  54.     the image of the cartesian coordinate lines under the function f.
  55.     The default values of dx and dy are chosen so that the number of lines
  56.     is equal to the value of the option PlotPoints of Plot3D[]"
  57.  
  58. PolarMap::usage = "PolarMap[f, {r0:0, r1, (dr)}, {phi0, phi1, (dphi)}, options...] plots
  59.     the image of the polar coordinate lines under the function f.
  60.     The default values of dr and dphi are chosen so that the number of lines
  61.     is equal to the value of the option PlotPoints of Plot3D[].
  62.     The default for the phi range is {0, 2Pi}."
  63.  
  64. Begin["`Private`"]
  65.  
  66. huge = 10.0^6
  67.  
  68. SplitLine[vl_] :=
  69.     Module[{vll, pos, linelist = {}, low, high},
  70.         vll = If[NumberQ[#], #, Indeterminate]& /@ vl;
  71.         pos = Flatten[ Position[vll, Indeterminate] ];
  72.         pos = Union[ pos, {0, Length[vll]+1} ];
  73.         Do[ low = pos[[i]]+1;
  74.             high = pos[[i+1]]-1;
  75.             If[ low < high, AppendTo[linelist, Take[vll, {low, high}]] ],
  76.            {i, 1, Length[pos]-1}];
  77.         linelist
  78.     ]
  79.  
  80. MakeLines[points_] :=
  81.     Module[{lines, newpoints},
  82.         newpoints = points /.
  83.             { z_?NumberQ :> huge z/Abs[z] /; Abs[z] > huge,
  84.               z_?NumberQ :> 0.0 /; Abs[z] < 1/huge,
  85.               DirectedInfinity[z_] :> huge z/Abs[z] };
  86.         lines = Join[ newpoints, Transpose[newpoints] ];
  87.         lines = Flatten[ SplitLine /@ lines, 1 ];
  88.         lines = Map[ {Re[#], Im[#]}&, lines, {2} ];
  89.         lines = Map[ Line, lines ];
  90.         Graphics[lines]
  91.     ]
  92.  
  93. FilterOptions[ command_Symbol, opts___ ] :=
  94.     Module[{keywords = First /@ Options[command]},
  95.         Sequence @@ Select[ {opts}, MemberQ[keywords, First[#]]& ]
  96.     ]
  97.  
  98. CartesianMap[ f_, {x0_, x1_, dx_:Automatic}, {y0_, y1_, dy_:Automatic}, opts___ ] :=
  99.     Module[ {x, y, points, plotpoints, ndx=N[dx], ndy=N[dy]},
  100.         plotpoints = PlotPoints /. {opts} /. Options[Plot3D];
  101.         If[ dx === Automatic, ndx = N[(x1-x0)/(plotpoints-1)] ];
  102.         If[ dy === Automatic, ndy = N[(y1-y0)/(plotpoints-1)] ];
  103.         points = Table[ N[f[x + I y]],
  104.             {x, x0, x1, ndx}, {y, y0, y1, ndy} ];
  105.         Show[ MakeLines[points], FilterOptions[Graphics, opts],
  106.             AspectRatio->Automatic, Axes->Automatic ]
  107.     ] /; NumberQ[N[x0]] && NumberQ[N[x1]] && NumberQ[N[y0]] && NumberQ[N[y1]]
  108.          (NumberQ[N[dx]] || dx === Automatic) &&
  109.          (NumberQ[N[dy]] || dy === Automatic)
  110.  
  111. PolarMap[ f_, {r0_:0, r1_, dr_:Automatic}, {phi0_, phi1_, dphi_:Automatic}, opts___ ] :=
  112.     Module[ {r, phi, points, plotpoints, ndr=dr, ndphi=dphi},
  113.         plotpoints = PlotPoints /. {opts} /. Options[Plot3D];
  114.         If[ dr === Automatic, ndr = N[(r1-r0)/(plotpoints-1)] ];
  115.         If[ dphi === Automatic, ndphi = N[(phi1-phi0)/(plotpoints-1)] ];
  116.         points = Table[ N[f[r Exp[I phi]]],
  117.             {r, r0, r1, ndr}, {phi, phi0, phi1, ndphi} ];
  118.         Show[ MakeLines[points], FilterOptions[Graphics, opts],
  119.             AspectRatio->Automatic, Axes->Automatic ]
  120.     ] /; NumberQ[N[r0]] && NumberQ[N[r1]] && NumberQ[N[phi0]] && NumberQ[N[phi1]]
  121.          (NumberQ[N[dr]] || dr === Automatic) &&
  122.          (NumberQ[N[dphi]] || dphi === Automatic)
  123.  
  124. PolarMap[ f_, rRange_List, opts___Rule ] :=
  125.     PolarMap[ f, rRange, {0, 2Pi}, opts ]
  126.  
  127. End[ ]   (* Graphics`ComplexMap`Private` *)
  128.  
  129. Protect[CartesianMap, PolarMap]
  130.  
  131. EndPackage[ ]   (* Graphics`ComplexMap` *)
  132.  
  133. (*:Limitations: none known. *)
  134.  
  135. (*:Tests:
  136.  
  137. *)
  138.  
  139. (*:Examples:
  140.  
  141. CartesianMap[ Cos,{0.2, Pi-0.2 },{-2,2}]
  142.  
  143. CartesianMap[ Cos, {0.2, Pi-0.2, (Pi-0.4)/19}, {-2,2,4/16}]
  144.  
  145. CartesianMap[ Exp, {-1,1,0.2},{-2,2,0.2}]
  146.  
  147. PolarMap[ Log, {0.1,10,0.5},{-3,3,0.15}]
  148.  
  149. PolarMap[ 1/Conjugate[#]&, {0.1,5.1,0.5},{-Pi,Pi,Pi/12}]
  150.  
  151. CartesianMap[ Zeta,{0.1,0.9},{0,20}]
  152.  
  153. SetOptions[ Plot3D, PlotPoints->25]; CartesianMap[ Zeta,{0.1,0.9},{0,20}]
  154.  
  155. PolarMap[ Sqrt,{1},{-Pi-0.0001,Pi}]
  156.  
  157. PolarMap[ Sqrt,{1},{-Pi+0.0001,Pi}]
  158.  
  159. PolarMap[ Identity,{1,2},{-Pi,Pi,Pi/12}]
  160.  
  161. PolarMap[ Identity,{1,2},{-Pi,Pi,Pi/12},
  162.           AspectRatio -> 0.5, Axes -> None, Framed -> True ]
  163.  
  164. CartesianMap[ 1/Conjugate[#]&, {-2,2,4/19},{-2,2,4/19},PlotRange->All]
  165.  
  166. f[z_] := Indeterminate /; Abs[z] < 10.^-3
  167. f[z_] := 10^100 /; Abs[z-2] < 10.^-3
  168. f[z_] := DirectedInfinity[-2-I] /; Abs[z-(-2+I)] < 10.^-3
  169. f[z_] := DirectedInfinity[] /; Abs[z-(2-2I)] < 10.^-3
  170. f[z_] := z
  171. CartesianMap[f,{-2,2,1/3},{-2,2,1/3},Axes->None]
  172. CartesianMap[1/Conjugate[#]&,{-2,2,0.2},{-2,2,0.2}]
  173.  
  174. *)
  175.