source: git/Singular/LIB/graphics.lib @ 7956ecc

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