source: git/Singular/LIB/surf.lib @ 4173c7

spielwiese
Last change on this file since 4173c7 was 6f212c, checked in by Frank Seelisch <seelisch@…>, 13 years ago
commands plot and surfer finally work under Windows (fixes track ticket #208) git-svn-id: file:///usr/local/Singular/svn/trunk@14162 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100755
File size: 10.4 KB
Line 
1///////////////////////////////////////////////////////////////////////////////
2version="$Id: surf.lib 13499 2010-10-15 09:55:26Z hannes $";
3category="Visualization";
4info="
5LIBRARY: surf.lib    Procedures for Graphics with Surf
6AUTHOR: Hans Schoenemann, Frank Seelisch
7
8NOTE:
9 @texinfo
10 Using this library requires the program @code{surf} to be installed.
11 You can download @code{surf} either from
12  @uref{http://sourceforge.net/projects/surf}
13  or from @uref{ftp://www.mathematik.uni-kl.de/pub/Math/Singular/utils/}.
14 The procedure surfer requires the program @code{surfer} to be installed.
15 You can download @code{surfer} from
16  @uref{http://www.imaginary2008.de/surfer.imaginary2008.de}
17 @*Under Windows, version 159 or newer of @code{surfer} is required.
18 @end texinfo
19
20SEE ALSO: surfex_lib
21
22PROCEDURES:
23 plot(I);    plots plane curves and surfaces
24 surfer(I);  plots surfaces interactivly
25";
26
27///////////////////////////////////////////////////////////////////////////////
28proc num_of_vars(ideal I)
29"USAGE: num_of_vars(ideal I)
30
31RETURN: an intvec containing one entry for each ring variable.
32each contains the sums of all degrees in this variable of all monomials
33occuring in the ideal.
34An entry is zero if and only if the corresponding variable does not occur in the ideal.
35"
36{
37  intvec v;
38  int i;
39  poly p;
40  for(i=ncols(I);i>0;i--)
41  {
42    p=I[i];
43    while(p!=0)
44    {
45      v=v+leadexp(p);
46      p=p-lead(p);
47    }
48  }
49  return(v);
50}
51example
52{
53  "EXAMPLE:"; echo = 2;
54  ring r = 0, (x,y,z),dp;
55  ideal j0 = x^2-x*y;
56  num_of_vars(j0);
57  ideal j1 = x^2-x*y-y;
58  num_of_vars(j1);
59  ideal j2 = x^2-x*y-y, x^3-2*y;
60  num_of_vars(j2);
61}
62
63proc plot(ideal I,list #)
64"USAGE:   plot(I);  I ideal or poly
65ASSUME: I defines a plane curve or a surface given by one equation
66RETURN: nothing
67NOTE: requires the external program `surf` to be installed,
68      to close the graphical interface just press `Q`
69EXAMPLE: example plot; shows an example
70"
71{
72  string extra_surf_opts=" -x --auto-resize "; // remove this line for surf 0.9
73  string l = "/tmp/surf" + string(system("pid"));
74  string err_mes; // string containing error messages
75  def base=basering;
76  intvec v=num_of_vars(I);
77  int i,j,n;
78  for(i=size(v);i>0;i--)
79  {
80    if (v[i]!=0) { n++; }
81  }
82  if (n==0 or n>3)
83  {
84    err_mes="Cannot plot equations with "+string(n)+" variables";
85    ERROR(err_mes);
86  }
87  ring r=0,(x,y,z),dp;
88  short=0;
89  map phi=base,0;
90  j=1;
91  for(i=1;i<=size(v);i++)
92  {
93    if (v[i]!=0)
94    {
95      phi[i]=var(j);
96      j++;
97      if(j==4) break;
98    }
99  }
100  ideal I=simplify(phi(I),2);
101  if (leadcoef(I[1]) <0) { I[1]=-I[1]; }
102  if (ncols(I)==1 and n<=2 and nvars(base)!=3) // curve
103  {
104    write(":w "+l,"clip=none;");
105      write(l, "width=500; height=500; set_size; do_background=yes;
106               background_red=255; background_green=255;
107               background_blue=255;");
108    write(l,
109    "root_finder=d_chain_bisection;epsilon=0.0000000001;iterations=20000;");
110    write(l, "curve_green=0; curve_blue=0; curve_width=1.5;");
111    if (size(#)>0)
112    {
113      write(l,#[1]);
114    }
115    write(l,"curve=",I[1],";");
116    write(l,"draw_curve;");
117  }
118  else
119  {
120    if (ncols(I)==1 and (n==3 or nvars(base)==3)) // surface
121    {
122      write(":w " + l,
123            "root_finder=d_chain_bisection;epsilon=0.0000000001;iterations=20000;");
124      write(l, "width=500; height=500; set_size; do_background=yes; background_red=255; background_green=255; background_blue=255;");
125      write(l, "rot_x=0.14; rot_y=-0.3;");
126      if (size(#) > 0)
127      {
128          write(l, #[1]);
129      }
130      write(l, "surface=",I[1],";");
131      write(l, "draw_surface;");
132    }
133    else
134    {
135      err_mes = "cannot plot " + string(ncols(I)) + " equations in "
136              + string(n) + " variables";
137      ERROR(err_mes);
138    }
139  }
140
141  string surf_call; i = 0;
142  if (isWindows())
143  {
144    string surferPath = getShellOutput("which surfer");
145    if (find(surferPath, "no surfer in") != 0)
146    { /* did not find surfer: either not installed or
147         not yet included in $PATH variable */
148      err_mes = "calling `surfer` failed" + newline
149      + " (Either the program Surfer is not installed," + newline
150      + "  or it has not yet been included in $PATH.)";
151      ERROR(err_mes);
152    }
153    else
154    {
155      surf_call = "((xwin -multiwindow -clipboard -silent-dup-error";
156      surf_call = surf_call + " >/dev/null 2>&1 &) && sleep 5 && (surf";
157      if (defined(extra_surf_opts))
158      {
159        surf_call = surf_call + " " + extra_surf_opts;
160      }
161      surf_call = surf_call + l + ">/dev/null 2>&1))";
162      surf_call = surf_call + "&& /bin/rm " + l;
163      "Press q to exit from `surf`.";
164        " (You may leave the XServer running for further" + newline
165      + "  invocations of `plot`.)";
166      i = system("sh", surf_call);
167      if (i != 0)
168      {
169        err_mes = "calling `surf` failed" + newline
170                + " (The shell returned the error code "
171                + string(i) + "." + newline
172                + "  Perhaps the XServer was not properly set up, so" + newline
173                + "  try your plot command again. If `plot` fails" + newline
174                + "  again, then make sure that the program Surfer" + newline
175                + "  is installed and included in your $PATH variable.)";
176        ERROR(err_mes);
177      }
178    }
179  }
180  else
181  {
182    surf_call = "surf ";
183    if (defined(extra_surf_opts))
184    {
185      surf_call = surf_call + " " + extra_surf_opts;
186    }
187    surf_call = surf_call + l + " >/dev/null 2>&1";
188
189    if (system("uname") == "ppcMac-darwin")
190    {
191       surf_call = surf_call + " || " + "singularsurf "
192                   + extra_surf_opts + " " + l + " >/dev/null 2>&1";
193    }
194
195    "Press q to exit from `surf`.";
196    i = system("sh", surf_call);
197    if (i != 0)
198    {
199      err_mes = "calling `surf` failed" + newline
200              + " (The shell returned the error code "
201              + string(i) + "." + newline
202              + "  Make sure that the program Surfer is installed.)";
203      ERROR(err_mes);
204    }
205  }
206  system("sh", "/bin/rm " + l);
207}
208example
209{ "EXAMPLE:"; echo = 2;
210  // ---------  plane curves ------------
211  ring rr0 = 0,(x1,x2),dp;
212
213  ideal I = x1^3 - x2^2;
214  plot(I);
215
216  ring rr1 = 0,(x,y,z),dp;
217  ideal I(1) = 2x2-1/2x3 +1-y+1;
218  plot(I(1));
219
220  //  ---- Singular Logo --------------
221  poly logo = ((x+3)^3 + 2*(x+3)^2 - y^2)*(x^3 - y^2)*((x-3)^3-2*(x-3)^2-y^2);
222  plot(logo);
223
224  // Steiner surface
225  ideal J(2) = x^2*y^2+x^2*z^2+y^2*z^2-17*x*y*z;
226  plot(J(2));
227
228  // --------------------
229  plot(x*(x2-y2)+z2);
230
231  // E7
232  plot(x^3-x*y^3+z^2);
233
234  // Whitney umbrella
235  plot(z^2-x^2*y);
236
237}
238
239proc surfer(ideal I)
240"USAGE:   surfer(f);  f poly
241ASSUME: f defines a surface given by one equation
242RETURN: nothing
243NOTE: requires the external program `surfer` to be installed,
244      to close the graphical interface just close the window of surfer
245EXAMPLE: example surfer; shows an example
246"
247{
248  string lForWindows = "surfer" + string(system("pid"));
249  string l = "./" + lForWindows;
250  string err_mes; // string containing error messages
251  def base=basering;
252  intvec v=num_of_vars(I);
253  int i,j,n;
254  for(i=size(v);i>0;i--)
255  {
256    if (v[i]!=0) { n++; }
257  }
258  if (n==0 or n>3)
259  {
260    err_mes="Cannot plot equations with "+string(n)+" variables";
261    ERROR(err_mes);
262  }
263  ring r=0,(x,y,z),dp;
264  short=0;
265  map phi=base,0;
266  j=1;
267  for(i=1;i<=size(v);i++)
268  {
269    if (v[i]!=0)
270    {
271      phi[i]=var(j);
272      j++;
273      if(j==4) break;
274    }
275  }
276  ideal I=simplify(phi(I),2);
277  if (leadcoef(I[1]) <0) { I[1]=-I[1]; }
278  if (ncols(I)==1 and (n==3 or nvars(base)==3)) // surface
279  {
280    write(":w " + l, "surface=" + string(I[1]) + ";");
281  }
282  else
283  {
284    err_mes = "cannot plot " + string(ncols(I)) + " equations in "
285            + string(n) + " variables";
286    ERROR(err_mes);
287  }
288
289  string surf_call; i = 0;
290  if (isWindows())
291  {
292    string surferPath = getShellOutput("which surfer");
293    if (find(surferPath, "no surfer in") != 0)
294    { /* did not find surfer: either not installed or
295         not yet included in $PATH variable */
296      err_mes = "calling `surfer` failed" + newline
297      + " (Either the program Surfer is not installed," + newline
298      + "  or it has not yet been included in $PATH.)";
299      ERROR(err_mes);
300    }
301    else
302    {
303      string singularPath = getShellOutput("pwd");
304      surferPath = windowsCorrection(surferPath);
305      surferPath = surferPath[1..size(surferPath)-size("/surfer")];
306      singularPath = windowsCorrection(singularPath);
307      singularPath = "c:"
308         + singularPath[size("/cygdrive/c/")..size(singularPath)];
309      surf_call = "cygstart -w -d " + surferPath + " ";
310      surf_call = surf_call + surferPath + "/surfer ";
311      surf_call = surf_call + singularPath + "/" + lForWindows;
312      "Close window to exit from `surfer`.";
313      i = system("sh", surf_call);
314    }
315  }
316  else
317  {
318    surf_call = "surfer " + l + " >/dev/null 2>&1";
319    "Close window to exit from `surfer`.";
320    i = system("sh", surf_call);
321  }
322  system("sh", "/bin/rm " + l);
323
324  if (i != 0)
325  {
326    err_mes = "calling `surfer` failed" + newline
327              + " (The shell returned the error code "
328              + string(i) + ".";
329    ERROR(err_mes);
330  }
331}
332example
333{ "EXAMPLE:"; echo = 2;
334  ring rr1 = 0,(x,y,z),dp;
335  // Steiner surface
336  ideal J(2) = x^2*y^2+x^2*z^2+y^2*z^2-17*x*y*z;
337  surfer(J(2));
338
339  // --------------------
340  surfer(x*(x2-y2)+z2);
341
342  // E7
343  surfer(x^3-x*y^3+z^2);
344
345  // Whitney umbrella
346  surfer(z^2-x^2*y);
347}
348
349static proc isWindows()
350"returns 1 if this SINGULAR instance runs under (some) Windows OS;
3510 otherwise"
352{
353  string s = system("uname");
354  for (int i = 1; i <= size(s)-2; i = i + 1)
355  {
356    if (s[i] == "W")
357    {
358      if (s[i+1] == "i")
359      {
360        if (s[i+2] == "n")
361        {
362          return (1);
363        }
364      }
365    }
366  }
367  return (0);
368}
369
370static proc getShellOutput(string shellCommand)
371"returns the console output when executing the given shellCommand"
372{
373   int s;
374   string tempFilename = "tmp" + string(system("pid"));
375   s = system("sh", shellCommand + " > " + tempFilename + " 2>&1");
376   string r1 = read(tempFilename);
377   s = size(r1) - 1;
378   string r2 = r1[1..s];
379   s = system("sh", "/bin/rm " + tempFilename);
380   return (r2);
381}
382
383static proc windowsCorrection(string windowsPath)
384"puts a backslash in front of each space and each special character
385and returns the resulting string"
386{
387  string s = ""; int i;
388  for (i = 1; i <= size(windowsPath); i++)
389  {
390    if (find(" ()", windowsPath[i]) != 0)
391    {
392      s = s + "\\";
393    }
394    s = s + windowsPath[i];
395  }
396  return (s);
397}
398///////////////////////////////////////////////////////////////////////////////
Note: See TracBrowser for help on using the repository browser.