source: git/Singular/LIB/graphics.lib @ f5d2647

spielwiese
Last change on this file since f5d2647 was 3686937, checked in by Oleksandr Motsak <motsak@…>, 11 years ago
Added '$Id$' as a comment to all libs (LIB/*.lib)
  • Property mode set to 100644
File size: 9.8 KB
Line 
1//////////////////////////////////////////////////////////////////////////////
2version="version graphics.lib 4.0.0.0 Jun_2013 "; // $Id$
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 diagrams of an
19         ideal I, i.e. exponent vectors of the initial ideal of I
20NOTE:    ideal I should be given by a standard basis. Let s=\"\" and copy and
21         paste the result into 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 [,I1,I2,..,s] ); fname=string; I,I1,I2,..=ideals,
153         s=string representing the plot region.@*
154         Use the ideals I1,I2,.. in order to produce multiple plots (they need
155         to have the same number of entries as I!).
156RETURN:  string, text with Mathematica commands to display a plot
157NOTE:    The plotregion is defaulted to -1,1 around zero.
158         For implicit given curves enter first the string returned by
159         procedure mathinit into Mathematica in order to load ImplicitPlot.
160         The following conventions for I are used:
161  @format
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  - ideal with 2 entries in two variables means an implicit curve
166    given as I[1]==I[2],
167  - ideal with 1 entry (or one polynomial) in two variables means
168    an implicit curve given as  f == 0,
169  @end format
170EXAMPLE: example mplot; 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,x,dp; export rr0;
334
335   ideal I = x3 + x, x2;
336   ideal J = x2, -x+x3;
337   mplot("",I,J,"-2,2");
338
339 // Paste the output into a Mathematica notebook
340 // active evalutation of the cell with SHIFT RETURN
341
342 pause("Hit RETURN to continue");
343   // --------- space curves --------------
344   I = x3,-1/10x3+x2,x2;
345   mplot("",I);
346
347 // Paste the output into a Mathematica notebook
348 // active evalutation of the cell with SHIFT RETURN
349
350 pause("Hit RETURN to continue");
351   // ----------- surfaces -------------------
352   ring rr1 = 0,(x,y),dp; export rr1;
353   ideal J = xy,y,x2;
354   mplot("",J,"-2,1","1,2");
355
356 // Paste the output into a Mathematica notebook
357 // active evalutation of the cell with SHIFT RETURN
358 kill rr0,rr1;
359}
360///////////////////////////////////////////////////////////////////////////////
Note: See TracBrowser for help on using the repository browser.