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

spielwiese
Last change on this file since c45b8f0 was 66d68c, checked in by Hans Schoenemann <hannes@…>, 14 years ago
format git-svn-id: file:///usr/local/Singular/svn/trunk@13499 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 7.7 KB
Line 
1///////////////////////////////////////////////////////////////////////////////
2version="$Id$";
3category="Visualization";
4info="
5LIBRARY: surf.lib    Procedures for Graphics with Surf
6AUTHOR: Hans Schoenemann
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;
142
143  if (isWindows())
144  {
145    surf_call = "((xwin -multiwindow -clipboard -silent-dup-error";
146    surf_call = surf_call + " >/dev/null 2>&1 &) && sleep 5 && (surf";
147    if (defined(extra_surf_opts))
148    {
149      surf_call = surf_call + " " + extra_surf_opts;
150    }
151    surf_call = surf_call + l + ">/dev/null 2>&1))";
152    surf_call = surf_call + "&& /bin/rm " + l;
153    "Press q to exit from 'surf'.";
154    "(You may leave the XServer running for further invocations"
155    + " of 'plot'.)";
156    i = system("sh", surf_call);
157  }
158  else
159  {
160    surf_call = "surf ";
161    if (defined(extra_surf_opts))
162    {
163      surf_call = surf_call + " " + extra_surf_opts;
164    }
165    surf_call = surf_call + l + " >/dev/null 2>&1";
166
167    if ("ppcMac-darwin" == system("uname"))
168    {
169       surf_call = surf_call + " || " + "singularsurf "
170                   + extra_surf_opts + " " + l + " >/dev/null 2>&1";
171    }
172
173    "Press q to exit from 'surf'";
174    i = system("sh", surf_call);
175    system("sh", "/bin/rm " + l);
176  }
177
178  if (i != 0)
179  {
180    err_mes = "calling `surf` failed" + newline
181              + " (The shell returned the error code "
182              + string(i) + "." + newline
183              + "Probably, the executable `surf` was not found.)";
184    ERROR(err_mes);
185  }
186}
187example
188{ "EXAMPLE:"; echo = 2;
189  // ---------  plane curves ------------
190  ring rr0 = 0,(x1,x2),dp;
191
192  ideal I = x1^3 - x2^2;
193  plot(I);
194
195  ring rr1 = 0,(x,y,z),dp;
196  ideal I(1) = 2x2-1/2x3 +1-y+1;
197  plot(I(1));
198
199  //  ---- Singular Logo --------------
200  poly logo = ((x+3)^3 + 2*(x+3)^2 - y^2)*(x^3 - y^2)*((x-3)^3-2*(x-3)^2-y^2);
201  plot(logo);
202
203  // Steiner surface
204  ideal J(2) = x^2*y^2+x^2*z^2+y^2*z^2-17*x*y*z;
205  plot(J(2));
206
207  // --------------------
208  plot(x*(x2-y2)+z2);
209
210  // E7
211  plot(x^3-x*y^3+z^2);
212
213  // Whitney umbrella
214  plot(z^2-x^2*y);
215
216}
217proc surfer(ideal I)
218"USAGE:   surfer(f);  f poly
219ASSUME: f defines a surface given by one equation
220RETURN: nothing
221NOTE: requires the external program 'surfer' to be installed,
222      to close the graphical interface just close the window of surfer
223EXAMPLE: example surfer; shows an example
224"
225{
226  string l = "./surfer" + string(system("pid"));
227  string err_mes; // string containing error messages
228  def base=basering;
229  intvec v=num_of_vars(I);
230  int i,j,n;
231  for(i=size(v);i>0;i--)
232  {
233    if (v[i]!=0) { n++; }
234  }
235  if (n==0 or n>3)
236  {
237    err_mes="Cannot plot equations with "+string(n)+" variables";
238    ERROR(err_mes);
239  }
240  ring r=0,(x,y,z),dp;
241  short=0;
242  map phi=base,0;
243  j=1;
244  for(i=1;i<=size(v);i++)
245  {
246    if (v[i]!=0)
247    {
248      phi[i]=var(j);
249      j++;
250      if(j==4) break;
251    }
252  }
253  ideal I=simplify(phi(I),2);
254  if (leadcoef(I[1]) <0) { I[1]=-I[1]; }
255  if (ncols(I)==1 and (n==3 or nvars(base)==3)) // surface
256  {
257    write(":w " + l, "surface=" + string(I[1]) + ";");
258  }
259  else
260  {
261    err_mes = "cannot plot " + string(ncols(I)) + " equations in "
262            + string(n) + " variables";
263    ERROR(err_mes);
264  }
265  string surf_call;
266
267  surf_call = "surfer";
268  surf_call = surf_call + " " + l + " >/dev/null 2>&1";
269  "Close window to exit from 'surfer'";
270  i = system("sh", surf_call);
271  system("sh", "/bin/rm " + l);
272
273  if (i != 0)
274  {
275    err_mes = "calling `surfer` failed" + newline
276              + " (The shell returned the error code "
277              + string(i) + "." + newline
278              + "Probably, the executable `surfer` was not found.)";
279    ERROR(err_mes);
280  }
281}
282example
283{ "EXAMPLE:"; echo = 2;
284  ring rr1 = 0,(x,y,z),dp;
285  // Steiner surface
286  ideal J(2) = x^2*y^2+x^2*z^2+y^2*z^2-17*x*y*z;
287  surfer(J(2));
288
289  // --------------------
290  surfer(x*(x2-y2)+z2);
291
292  // E7
293  surfer(x^3-x*y^3+z^2);
294
295  // Whitney umbrella
296  surfer(z^2-x^2*y);
297
298}
299proc isWindows()
300"returns 1 if this SINGULAR instance runs under (some) Windows OS;
3010 otherwise"
302{
303  string s = system("uname");
304  for (int i = 1; i <= size(s)-2; i = i + 1)
305  {
306    if (s[i] == "W")
307    {
308      if (s[i+1] == "i")
309      {
310        if (s[i+2] == "n")
311        {
312          return (1);
313        }
314      }
315    }
316  }
317  return (0);
318}
319///////////////////////////////////////////////////////////////////////////////
Note: See TracBrowser for help on using the repository browser.