source: git/Singular/LIB/graphics.lib @ 8942a5

fieker-DuValspielwiese
Last change on this file since 8942a5 was 803c5a1, checked in by Gert-Martin Greuel <greuel@…>, 23 years ago
* GMG: Kosmetik git-svn-id: file:///usr/local/Singular/svn/trunk@4981 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 10.6 KB
Line 
1///////////////////////////////////////////////////////////////////////////////
2version="$Id: graphics.lib,v 1.8 2000-12-22 13:53:49 greuel Exp $";
3category="Visualization";
4info="
5LIBRARY: graphics.lib    Procedures to use Graphics with Mathematica
6AUTHOR:   Christian Gorzel, gorzelc@math.uni-muenster.de
7
8PROCEDURES:
9 staircase(fname,I);  Mathematica text for displaying staircase of I
10 mathinit();          string for loading Mathematica's ImplicitPlot
11 mplot(fname,I[# s]); Mathematica text for various plots
12";
13
14///////////////////////////////////////////////////////////////////////////////
15
16proc staircase(string fname,ideal I)
17"USAGE:   staircase(s,I); s a string, I ideal in two variables
18RETURN:  string with Mathematica input for displaying staircase (exponent
19         vectors of initial ideal) of I
20NOTE:    ideal I should be given by a standard basis
21         Copy and paste works only in a Mathematica notebook
22EXAMPLE: example staircase; shows an example
23"
24{
25  intvec v;
26  int maxx, maxy;
27  list l;
28  string s;
29  string el;
30
31 if(nvars(basering)!=2)
32 { "-- Error: need two variables ";
33   return();
34 }
35 if (not(attrib(I,"isSB")))
36 { " -- Warning: Ideal should be a standardbasis "; newline; }
37
38 for(int i=1; i<=ncols(I); i++)
39 {
40  if (i>1) { el = el + ",";}
41  v = leadexp(I[i]);
42  if (v[1] > maxx) { maxx = v[1];}
43  if (v[2] > maxy) { maxy = v[2];}
44  el = el + "{" + string(v) + "}";
45 }
46 el = "{" + el + "}";
47 maxx = maxx + 3;
48 maxy = maxy + 3;
49
50 s = newline +
51     "Show[Graphics[{" + newline +
52     "{GrayLevel[0.5],Map[Rectangle[#,{" +
53          string(maxx) + "," + string(maxy) + "}] &, " + el + "]}," + newline +
54     "{PointSize[0.03], Map[Point," + el + "]}," + newline +
55       "Table[Circle[{i,j},0.1],{i,0," +
56           string(maxx) + "},{j,0," + string(maxy) + "}]}," + newline +
57     "  Axes->True,AspectRatio->Automatic]]";
58 s = s + endstr(fname);
59 return(s);
60}
61example
62{ "EXAMPLE:"; echo =2;
63  ring r0 = 0,(x,y),ls;
64  ideal I = -1x2y6-1x4y2, 7x6y5+1/2x7y4+6x4y6;
65  staircase("",std(I));
66
67  ring r1 = 0,(x,y),dp;
68  ideal I = fetch(r0,I);
69  staircase("",std(I));
70
71  ring r2 = 0,(x,y),wp(2,3);
72  ideal I = fetch(r0,I);
73  staircase("",std(I));
74
75  // Paste the output into a Mathematica notebook
76  // active evalutation of the cell with SHIFT RETURN
77}
78///////////////////////////////////////////////////////////////////////////////
79
80proc mathinit()
81"USAGE:   mathinit();
82RETURN:  initializing string for loading Mathematica's ImplicitPlot
83EXAMPLE: example mathinit; shows an example
84"
85{
86  // write("init.m","<< Graphics`ImplicitPlot`");
87  return("<< Graphics`ImplicitPlot`");
88}
89example
90{ "EXAMPLE:"; echo =2;
91  mathinit();
92
93  // Paste the output into a Mathematica notebook
94  // active evalutation of the cell with SHIFT RETURN
95}
96///////////////////////////////////////////////////////////////////////////////
97
98static proc checkshort()
99{
100  ring @r;
101}
102static proc determvars(ideal I)
103// determine the variables which are in the ideal I
104{
105  intvec v;
106  int i,j,k;
107
108  k=1;
109  for(j=1;j<=size(I);j++)
110  { for(i=1;i<=nvars(basering);i++)
111    { if(I[j]!=subst(I[j],var(i),0)) {v[k] = i; k++;}
112    }
113  }
114  ring @r=0,x,ls;
115  poly f;
116  for(i=1;i<=size(v);i++)     // sort VARS
117  { f = f + x^v[i]; }
118  v=0;
119  for (i=1;i<=size(f);i++)
120  {v[i]=leadexp(f[i])[1];}
121 return(v);
122}
123///////////////////////////////////////////////////////////////////////////////
124
125static proc endstr(string filename)
126{ int i;
127  string suffix,cmd,name;
128
129 if(size(filename))
130 {
131  for (i=size(filename);i;i--)
132  { if (filename[i] == ".") {break;}
133  }
134
135 if (i>0)
136 { suffix = filename[i,size(filename)-i+1];
137   name = ">" + filename[1,i-1]+ ".m";
138 }
139 else { print("--Error: Suffix of filename incorrect"); return("");}
140// if (suffix ==".m") { cmd = "Display[\" " + filename + "\",% ]";}
141 if (suffix ==".mps") { cmd = "Display[\" " + filename + "\",%] ";}
142 if (suffix ==".ps") { cmd = "Display[\" ! psfix > " + filename + "\", %]";}
143 if (suffix ==".eps")
144                { cmd = "Display[\" ! psfix -epsf > " + filename + "\", %]";}
145
146 }
147 return(newline + cmd);
148}
149
150///////////////////////////////////////////////////////////////////////////////
151proc  mplot(string fname,ideal I,list #)
152"USAGE:   mplot(fname, I[,s]); fname string; I ideal, s strings representing
153         the plot region or further ideals
154RETURN:  string, text with Mathematica commands to display a plot
155NOTE:    The plotregion is defaulted to -1,1 around zero
156         (mostly interested in local situation in singularity theory)
157         For implicit given curves enter first the string returned by
158         proc mathinit into Mathematica in order to load ImplicitPlot
159
160         ideal with 2 entries in one variable means a parametrised plane curve
161         ideal with 3 entries in one variable means a parametrised space curve
162         ideal with 3 entries in two variables means a parametrised surface
163
164         ideal I with 2 generators in two variables means an implicit curve
165          given as I[1]==I[2]
166         ideal with 1 generator (or one polynomial) in two variables means
167          an implicit curve given as  f == 0
168EXAMPLE: example mplot; shows an example
169"
170{
171  int i,j,k,mapping;
172  int planecurve,spacecurve,implcrv,surface;
173  intvec VARS,v;
174  string xpart,ypart,zpart = "-1,1","-1,1","All";
175  string pstring,actstring,xname,yname,str,closing;
176  string basr = nameof(basering);
177  ideal J;
178
179  if (ncols(I)>3)
180  { "-- Error: can only draw upto dimension 3";
181    return("");
182  }
183  ring @r = 0,(s,t),lp;
184  ideal @J,@I;
185
186  setring(`basr`);
187  // def d = basering;
188  // d;
189  // listvar(d);
190
191  str = newline;
192
193  VARS = determvars(I);
194   // "VARS: ";VARS;
195
196  if (size(VARS)>2 or VARS==0)
197  { "-- Error: Need some variables, but can only draw in 2 or 3 dimensions";
198    return("");
199  }
200  if (size(matrix(I))==1 and size(VARS)==2)
201  { i =size(I[1]);
202   //I[2]=I[1][(i/ 2 + 1)..i]; I[2];
203   // I[1]=I[1][1..(i/ 2)]; I[1];
204   I[2]=0;
205  }
206  if (size(matrix(I))==2)
207  { if (size(VARS)==1) {planecurve=1; str = str + "ParametricPlot[";}
208    if (size(VARS)==2) {implcrv=1; str = str + "ImplicitPlot[";}
209  }
210  if (size(matrix(I))==3)
211  { if (size(VARS)==1) {spacecurve=1;}
212    if (size(VARS)==2) {surface=1;}
213    str =  str + "ParametricPlot3D[";
214  }
215
216  short = 0;
217
218  pstring = string(I);
219
220 // switch to another ring if necessary
221
222  checkshort();
223//  "short: "; short;
224
225  if (short!=1)      // construct a map
226  {
227    mapping = 1;
228    setring @r;
229    @J = 0;
230    for(i=1;i<=size(VARS);i++)
231    { @J[VARS[i]]=var(i);}
232    map phi = (`basr`,@J);
233    @I = phi(I);
234    short =0;
235    pstring = string(@I);
236    setring `basr`;
237  }
238
239  i = find(pstring,newline);
240  while(i)
241  {pstring[i]=" ";
242   i = find(pstring,newline,i);
243  }
244  if (implcrv)
245  { i = find(pstring,",");
246    pstring = pstring[1,i-1] + "==" + pstring[i+1,size(pstring)-i];
247  }
248  else
249  { pstring = "{" + pstring + "}";}
250//  "mapping "; mapping;
251  if (mapping)
252  { xname = "s";
253    if (size(VARS)==2) {yname="t";}
254  }
255  else
256  { xname = varstr(VARS[1]);
257    if (size(VARS)==2) {yname=varstr(VARS[2]);}
258  }
259
260  j =1;
261
262  for(k=1;k<=size(#);k++)
263  { if (typeof(#[k])=="ideal" or typeof(#[k])=="poly")
264    { //#[k] = ideal(#[k]);
265      v = determvars(#[k]);
266      J = #[k];
267      short =0;
268      if (size(matrix(J))==1 and size(VARS)==2 and implcrv)
269      { i =size(J[1]);
270      //  J[2]=J[1][(i/ 2 + 1)..i];
271      //  J[1]=J[1][1..(i/ 2)];
272        J[2] =0;
273      }
274      if ((v!= VARS) or (size(J)!=size(I)))
275      { print("--Error: ---- ");
276        return();
277      }
278      else
279      { if (mapping)
280        { setring @r;
281          short =0;
282          actstring = string(phi(J));
283          setring(`basr`);
284        }
285        else {actstring = string(J);}
286        i = find(actstring,newline);
287        while(i)
288        { actstring[i]=" ";
289         i = find(actstring,newline,i);
290        }
291        if (implcrv)
292        {i = find(actstring,",");
293         actstring = actstring[1,i-1]+ "==" + actstring[i+1,size(actstring)-i];
294         pstring = pstring + "," + actstring;
295        }
296        else
297        { pstring = pstring + ",{" + actstring +"}";
298        }
299
300      }
301    }
302    if (typeof(#[k])=="string")
303    {  if (j==3) {zpart = #[k];j++;}
304       if (j==2) {ypart = #[k];j++;}
305       if (j==1) {xpart = #[k];j++;}
306   }
307  }
308
309 if (spacecurve or planecurve or implcrv)
310 { str = str + "{" + pstring + "},{" + xname + "," + xpart + "}";}
311 if (implcrv and (j==3)) {str = str + ",{" + yname + "," + ypart + "}";}
312 if (surface)
313 { str = str + "{" + pstring + "},{" + xname + "," + xpart + "},{"
314                                     + yname + "," + ypart + "}";}
315
316  if (planecurve) {closing = "," + newline +" AspectRatio->Automatic";}
317  if (implcrv) {closing = "," + newline +
318   " AxesLabel->{\"" + varstr(VARS[1]) + "\",\"" + varstr(VARS[2]) + "\"}";}
319  if (spacecurve) { closing = "," + newline + " ViewPoint->{1.3,-2.4,2}";}
320  if (surface)
321  {closing = "," +newline +
322              " Boxed->True, Axes->True, ViewPoint->{1.3,-2.4,2}";}
323
324  str = str + closing + "];" + endstr(fname);
325
326  return(str);
327}
328example
329{ "EXAMPLE:"; echo =2;
330   // ---------  plane curves ------------
331   ring rr0 = 0,x1,dp; export rr0;
332
333   ideal I = x1^3 + x1, x1^2;
334   ideal J = x1^2,-x1+x1^3; "";
335   mplot("",J); "";
336   mplot("",I,"-2,2",J);
337
338   ring rr1 = 0,x,dp;  export rr1;
339   ideal I(1) = 2x2-1/2x3 +1, x-1;
340   ideal I(2) = x3-x ,x; "";
341   mplot("",I(1),I(2),"-2,4");
342
343 // Paste the output into a Mathematica notebook
344 // active evalutation of the cell with SHIFT RETURN
345
346 pause("Hit RETURN to continue");
347   // --------- space curves --------------
348   I(1) = x3,-1/10x3+x2,x2;
349   I(2) = x2,-x2+2,-2x2-x+1; "";
350   mplot("",I(1),I(2));
351
352 // Paste the output into a Mathematica notebook
353 // active evalutation of the cell with SHIFT RETURN
354
355 pause("Hit RETURN to continue");
356   // --------- implicit curves ------------
357   ring rr = 0,(x,y),ds;  export rr;
358   // implicit curves
359   ideal I(1) = y,-x2;
360   ideal I(2) = x2,-y2 +4; "";
361
362   //the lemniscate
363
364   ideal I(3) = x4+2x2y2 + y4, x2-y2;  "";
365
366   // a critical part
367   // adjust the plotregion properly to get a good picture
368
369   poly f = (x-y)*(x2+y);
370   mplot("",f); "";
371   ideal J = jacob(f);
372   J; "";
373   mplot("",J[1],J[2]);     // bad resolution
374   "";
375   mplot("",J[1],J[2],"-10,10","-10,10");  // get better picture in this way
376
377   mplot("",I(1),I(2),"-2.5,2.5"); "";
378
379   mplot("",I(3),"-2,2");
380
381 // Paste the output into a Mathematica notebook
382 // active evalutation of the cell with SHIFT RETURN
383
384 pause("Hit RETURN to continue");
385   // ----------- surfaces -------------------
386   ideal J(1) = 3xy4 + 2xy2, x5y3 + x + y6,10x2;
387
388   // the Whitney-Umbrella
389
390   ideal J(2) = xy,y,x2; "";
391   mplot("",J(1),"-2,1","1,2");  "";
392   mplot("",J(2),"-1.5,1.5","-2,2");
393
394 // Paste the output into a Mathematica notebook
395 // active evalutation of the cell with SHIFT RETURN
396}
397///////////////////////////////////////////////////////////////////////////////
398
399
Note: See TracBrowser for help on using the repository browser.