source: git/Singular/LIB/surf.lib @ dea3d2

fieker-DuValspielwiese
Last change on this file since dea3d2 was 1e1ec4, checked in by Oleksandr Motsak <motsak@…>, 11 years ago
Updated LIBs according to master add: new LIBs from master fix: updated LIBs due to minpoly/(de)numerator changes fix: -> $Id$ fix: Fixing wrong rebase of SW on master (LIBs)
  • Property mode set to 100755
File size: 10.4 KB
RevLine 
[7e1114d]1///////////////////////////////////////////////////////////////////////////////
[f7d745]2version="$Id$";
[fd3fb7]3category="Visualization";
[7e1114d]4info="
[8bb77b]5LIBRARY: surf.lib    Procedures for Graphics with Surf
[6f212c]6AUTHOR: Hans Schoenemann, Frank Seelisch
[131a579]7
[62bb5e]8NOTE:
[8bb77b]9 @texinfo
[1a3911]10 Using this library requires the program @code{surf} to be installed.
[8bb77b]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/}.
[94b16b]14 The procedure surfer requires the program @code{surfer} to be installed.
15 You can download @code{surfer} from
[1a3911]16  @uref{http://www.imaginary2008.de/surfer.imaginary2008.de}
[48573c]17 @*Under Windows, version 159 or newer of @code{surfer} is required.
[8bb77b]18 @end texinfo
[7e1114d]19
[043cba]20SEE ALSO: surfex_lib
21
[f34c37c]22PROCEDURES:
[49d99c]23 plot(I);    plots plane curves and surfaces
24 surfer(I);  plots surfaces interactivly
[7e1114d]25";
26
27///////////////////////////////////////////////////////////////////////////////
[72382c2]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.
[3754ca]34An entry is zero if and only if the corresponding variable does not occur in the ideal.
[72382c2]35"
[7e1114d]36{
37  intvec v;
38  int i;
39  poly p;
[72382c2]40  for(i=ncols(I);i>0;i--)
[7e1114d]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}
[72382c2]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}
[7e1114d]62
[d85126]63proc plot(ideal I,list #)
[50cbdc]64"USAGE:   plot(I);  I ideal or poly
65ASSUME: I defines a plane curve or a surface given by one equation
[aef7ccb]66RETURN: nothing
[6f212c]67NOTE: requires the external program `surf` to be installed,
68      to close the graphical interface just press `Q`
[7e1114d]69EXAMPLE: example plot; shows an example
70"
71{
[d85126]72  string extra_surf_opts=" -x --auto-resize "; // remove this line for surf 0.9
[d0869f]73  string l = "/tmp/surf" + string(system("pid"));
[afe420]74  string err_mes; // string containing error messages
[7e1114d]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  {
[afe420]84    err_mes="Cannot plot equations with "+string(n)+" variables";
[9173792]85    ERROR(err_mes);
[7e1114d]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);
[50cbdc]101  if (leadcoef(I[1]) <0) { I[1]=-I[1]; }
[307ffc]102  if (ncols(I)==1 and n<=2 and nvars(base)!=3) // curve
[7e1114d]103  {
104    write(":w "+l,"clip=none;");
[b9b906]105      write(l, "width=500; height=500; set_size; do_background=yes;
[d0869f]106               background_red=255; background_green=255;
107               background_blue=255;");
[7e1114d]108    write(l,
109    "root_finder=d_chain_bisection;epsilon=0.0000000001;iterations=20000;");
[aef7ccb]110    write(l, "curve_green=0; curve_blue=0; curve_width=1.5;");
[b9b906]111    if (size(#)>0)
[05d2e8]112    {
113      write(l,#[1]);
114    }
[7e1114d]115    write(l,"curve=",I[1],";");
116    write(l,"draw_curve;");
117  }
118  else
119  {
[307ffc]120    if (ncols(I)==1 and (n==3 or nvars(base)==3)) // surface
[7e1114d]121    {
[d85126]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;");
[7e1114d]132    }
133    else
134    {
[d85126]135      err_mes = "cannot plot " + string(ncols(I)) + " equations in "
136              + string(n) + " variables";
[afe420]137      ERROR(err_mes);
[7e1114d]138    }
139  }
[d0869f]140
[6f212c]141  string surf_call; i = 0;
[d0869f]142  if (isWindows())
[94b16b]143  {
[6f212c]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
[d0869f]154    {
[6f212c]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      }
[d0869f]178    }
[94b16b]179  }
180  else
181  {
[d0869f]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";
[0610f0e]188
[6f212c]189    if (system("uname") == "ppcMac-darwin")
[d0869f]190    {
191       surf_call = surf_call + " || " + "singularsurf "
192                   + extra_surf_opts + " " + l + " >/dev/null 2>&1";
193    }
194
[6f212c]195    "Press q to exit from `surf`.";
[d0869f]196    i = system("sh", surf_call);
[6f212c]197    if (i != 0)
198    {
199      err_mes = "calling `surf` failed" + newline
[d0869f]200              + " (The shell returned the error code "
201              + string(i) + "." + newline
[6f212c]202              + "  Make sure that the program Surfer is installed.)";
203      ERROR(err_mes);
204    }
[62bb5e]205  }
[6f212c]206  system("sh", "/bin/rm " + l);
[7e1114d]207}
208example
[d85126]209{ "EXAMPLE:"; echo = 2;
[50cbdc]210  // ---------  plane curves ------------
211  ring rr0 = 0,(x1,x2),dp;
[7e1114d]212
[50cbdc]213  ideal I = x1^3 - x2^2;
214  plot(I);
[7e1114d]215
[50cbdc]216  ring rr1 = 0,(x,y,z),dp;
217  ideal I(1) = 2x2-1/2x3 +1-y+1;
218  plot(I(1));
[7e1114d]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
[50cbdc]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));
[917fb5]227
[50cbdc]228  // --------------------
[7e1114d]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
[94b16b]237}
[6f212c]238
[ddc52f]239proc surfer(ideal I)
[94b16b]240"USAGE:   surfer(f);  f poly
241ASSUME: f defines a surface given by one equation
242RETURN: nothing
[6f212c]243NOTE: requires the external program `surfer` to be installed,
[94b16b]244      to close the graphical interface just close the window of surfer
245EXAMPLE: example surfer; shows an example
246"
247{
[6f212c]248  string lForWindows = "surfer" + string(system("pid"));
249  string l = "./" + lForWindows;
[94b16b]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  {
[d0869f]280    write(":w " + l, "surface=" + string(I[1]) + ";");
[94b16b]281  }
282  else
283  {
284    err_mes = "cannot plot " + string(ncols(I)) + " equations in "
285            + string(n) + " variables";
286    ERROR(err_mes);
287  }
[7030440]288
[6f212c]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);
[1e1ec4]307      link ll = "|: cygpath -w " + singularPath;
308      singularPath = "'\""+read(ll)+"\"'"; close(ll);
[6f212c]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  }
[d0869f]322  system("sh", "/bin/rm " + l);
[94b16b]323
324  if (i != 0)
325  {
[d0869f]326    err_mes = "calling `surfer` failed" + newline
327              + " (The shell returned the error code "
[6f212c]328              + string(i) + ".";
[94b16b]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);
[7e1114d]347}
[6f212c]348
349static proc isWindows()
[d0869f]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}
[6f212c]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}
[7e1114d]398///////////////////////////////////////////////////////////////////////////////
Note: See TracBrowser for help on using the repository browser.