source: git/Singular/LIB/graphics.lib @ 2f776b

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