source: git/Singular/LIB/surf.lib @ 0610f0e

spielwiese
Last change on this file since 0610f0e was 0610f0e, checked in by Hans Schoenemann <hannes@…>, 14 years ago
format git-svn-id: file:///usr/local/Singular/svn/trunk@12790 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 7.7 KB
Line 
1// last modified 21.07.2005, Oliver Wienand
2///////////////////////////////////////////////////////////////////////////////
3version="$Id$";
4category="Visualization";
5info="
6LIBRARY: surf.lib    Procedures for Graphics with Surf
7AUTHOR: Hans Schoenemann
8
9NOTE:
10 @texinfo
11 Using this library requires the program @code{surf} to be installed.
12 You can download @code{surf} either from
13  @uref{http://sourceforge.net/projects/surf}
14  or from @uref{ftp://www.mathematik.uni-kl.de/pub/Math/Singular/utils/}.
15 The procedure surfer requires the program @code{surfer} to be installed.
16 You can download @code{surfer} from
17  @uref{http://www.imaginary2008.de/surfer.imaginary2008.de}
18 @*Under Windows, version 159 or newer of @code{surfer} is required.
19 @end texinfo
20
21SEE ALSO: surfex_lib
22
23PROCEDURES:
24 plot(I);    plots plane curves and surfaces
25 surfer(I);  plots surfaces interactivly
26";
27
28///////////////////////////////////////////////////////////////////////////////
29proc num_of_vars(ideal I)
30"USAGE: num_of_vars(ideal I)
31
32RETURN: an intvec containing one entry for each ring variable.
33each contains the sums of all degrees in this variable of all monomials
34occuring in the ideal.
35An entry is zero if and only if the corresponding variable does not occur in the ideal.
36"
37{
38  intvec v;
39  int i;
40  poly p;
41  for(i=ncols(I);i>0;i--)
42  {
43    p=I[i];
44    while(p!=0)
45    {
46      v=v+leadexp(p);
47      p=p-lead(p);
48    }
49  }
50  return(v);
51}
52example
53{
54  "EXAMPLE:"; echo = 2;
55  ring r = 0, (x,y,z),dp;
56  ideal j0 = x^2-x*y;
57  num_of_vars(j0);
58  ideal j1 = x^2-x*y-y;
59  num_of_vars(j1);
60  ideal j2 = x^2-x*y-y, x^3-2*y;
61  num_of_vars(j2);
62}
63
64proc plot(ideal I,list #)
65"USAGE:   plot(I);  I ideal or poly
66ASSUME: I defines a plane curve or a surface given by one equation
67RETURN: nothing
68NOTE: requires the external program 'surf' to be installed,
69      to close the graphical interface just press 'Q'
70EXAMPLE: example plot; shows an example
71"
72{
73  string extra_surf_opts=" -x --auto-resize "; // remove this line for surf 0.9
74  string l = "/tmp/surf" + string(system("pid"));
75  string err_mes; // string containing error messages
76  def base=basering;
77  intvec v=num_of_vars(I);
78  int i,j,n;
79  for(i=size(v);i>0;i--)
80  {
81    if (v[i]!=0) { n++; }
82  }
83  if (n==0 or n>3)
84  {
85    err_mes="Cannot plot equations with "+string(n)+" variables";
86    ERROR(err_mes);
87  }
88  ring r=0,(x,y,z),dp;
89  short=0;
90  map phi=base,0;
91  j=1;
92  for(i=1;i<=size(v);i++)
93  {
94    if (v[i]!=0)
95    {
96      phi[i]=var(j);
97      j++;
98      if(j==4) break;
99    }
100  }
101  ideal I=simplify(phi(I),2);
102  if (leadcoef(I[1]) <0) { I[1]=-I[1]; }
103  if (ncols(I)==1 and n<=2 and nvars(base)!=3) // curve
104  {
105    write(":w "+l,"clip=none;");
106      write(l, "width=500; height=500; set_size; do_background=yes;
107               background_red=255; background_green=255;
108               background_blue=255;");
109    write(l,
110    "root_finder=d_chain_bisection;epsilon=0.0000000001;iterations=20000;");
111    write(l, "curve_green=0; curve_blue=0; curve_width=1.5;");
112    if (size(#)>0)
113    {
114      write(l,#[1]);
115    }
116    write(l,"curve=",I[1],";");
117    write(l,"draw_curve;");
118  }
119  else
120  {
121    if (ncols(I)==1 and (n==3 or nvars(base)==3)) // surface
122    {
123      write(":w " + l,
124            "root_finder=d_chain_bisection;epsilon=0.0000000001;iterations=20000;");
125      write(l, "width=500; height=500; set_size; do_background=yes; background_red=255; background_green=255; background_blue=255;");
126      write(l, "rot_x=0.14; rot_y=-0.3;");
127      if (size(#) > 0)
128      {
129          write(l, #[1]);
130      }
131      write(l, "surface=",I[1],";");
132      write(l, "draw_surface;");
133    }
134    else
135    {
136      err_mes = "cannot plot " + string(ncols(I)) + " equations in "
137              + string(n) + " variables";
138      ERROR(err_mes);
139    }
140  }
141
142  string surf_call;
143
144  if (isWindows())
145  {
146    surf_call = "((xwin -multiwindow -clipboard -silent-dup-error";
147    surf_call = surf_call + " >/dev/null 2>&1 &) && sleep 5 && (surf";
148    if (defined(extra_surf_opts))
149    {
150      surf_call = surf_call + " " + extra_surf_opts;
151    }
152    surf_call = surf_call + l + ">/dev/null 2>&1))";
153    surf_call = surf_call + "&& /bin/rm " + l;
154    "Press q to exit from 'surf'.";
155    "(You may leave the XServer running for further invocations"
156    + " of 'plot'.)";
157    i = system("sh", surf_call);
158  }
159  else
160  {
161    surf_call = "surf ";
162    if (defined(extra_surf_opts))
163    {
164      surf_call = surf_call + " " + extra_surf_opts;
165    }
166    surf_call = surf_call + l + " >/dev/null 2>&1";
167
168    if ("ppcMac-darwin" == system("uname"))
169    {
170       surf_call = surf_call + " || " + "singularsurf "
171                   + extra_surf_opts + " " + l + " >/dev/null 2>&1";
172    }
173
174    "Press q to exit from 'surf'";
175    i = system("sh", surf_call);
176    system("sh", "/bin/rm " + l);
177  }
178
179  if (i != 0)
180  {
181    err_mes = "calling `surf` failed" + newline
182              + " (The shell returned the error code "
183              + string(i) + "." + newline
184              + "Probably, the executable `surf` was not found.)";
185    ERROR(err_mes);
186  }
187}
188example
189{ "EXAMPLE:"; echo = 2;
190  // ---------  plane curves ------------
191  ring rr0 = 0,(x1,x2),dp;
192
193  ideal I = x1^3 - x2^2;
194  plot(I);
195
196  ring rr1 = 0,(x,y,z),dp;
197  ideal I(1) = 2x2-1/2x3 +1-y+1;
198  plot(I(1));
199
200  //  ---- Singular Logo --------------
201  poly logo = ((x+3)^3 + 2*(x+3)^2 - y^2)*(x^3 - y^2)*((x-3)^3-2*(x-3)^2-y^2);
202  plot(logo);
203
204  // Steiner surface
205  ideal J(2) = x^2*y^2+x^2*z^2+y^2*z^2-17*x*y*z;
206  plot(J(2));
207
208  // --------------------
209  plot(x*(x2-y2)+z2);
210
211  // E7
212  plot(x^3-x*y^3+z^2);
213
214  // Whitney umbrella
215  plot(z^2-x^2*y);
216
217}
218proc surfer(ideal I)
219"USAGE:   surfer(f);  f poly
220ASSUME: f defines a surface given by one equation
221RETURN: nothing
222NOTE: requires the external program 'surfer' to be installed,
223      to close the graphical interface just close the window of surfer
224EXAMPLE: example surfer; shows an example
225"
226{
227  string l = "./surfer" + string(system("pid"));
228  string err_mes; // string containing error messages
229  def base=basering;
230  intvec v=num_of_vars(I);
231  int i,j,n;
232  for(i=size(v);i>0;i--)
233  {
234    if (v[i]!=0) { n++; }
235  }
236  if (n==0 or n>3)
237  {
238    err_mes="Cannot plot equations with "+string(n)+" variables";
239    ERROR(err_mes);
240  }
241  ring r=0,(x,y,z),dp;
242  short=0;
243  map phi=base,0;
244  j=1;
245  for(i=1;i<=size(v);i++)
246  {
247    if (v[i]!=0)
248    {
249      phi[i]=var(j);
250      j++;
251      if(j==4) break;
252    }
253  }
254  ideal I=simplify(phi(I),2);
255  if (leadcoef(I[1]) <0) { I[1]=-I[1]; }
256  if (ncols(I)==1 and (n==3 or nvars(base)==3)) // surface
257  {
258    write(":w " + l, "surface=" + string(I[1]) + ";");
259  }
260  else
261  {
262    err_mes = "cannot plot " + string(ncols(I)) + " equations in "
263            + string(n) + " variables";
264    ERROR(err_mes);
265  }
266  string surf_call;
267
268  surf_call = "surfer";
269  surf_call = surf_call + " " + l + " >/dev/null 2>&1";
270  "Close window to exit from 'surfer'";
271  i = system("sh", surf_call);
272  system("sh", "/bin/rm " + l);
273
274  if (i != 0)
275  {
276    err_mes = "calling `surfer` failed" + newline
277              + " (The shell returned the error code "
278              + string(i) + "." + newline
279              + "Probably, the executable `surfer` was not found.)";
280    ERROR(err_mes);
281  }
282}
283example
284{ "EXAMPLE:"; echo = 2;
285  ring rr1 = 0,(x,y,z),dp;
286  // Steiner surface
287  ideal J(2) = x^2*y^2+x^2*z^2+y^2*z^2-17*x*y*z;
288  surfer(J(2));
289
290  // --------------------
291  surfer(x*(x2-y2)+z2);
292
293  // E7
294  surfer(x^3-x*y^3+z^2);
295
296  // Whitney umbrella
297  surfer(z^2-x^2*y);
298
299}
300proc isWindows()
301"returns 1 if this SINGULAR instance runs under (some) Windows OS;
3020 otherwise"
303{
304  string s = system("uname");
305  for (int i = 1; i <= size(s)-2; i = i + 1)
306  {
307    if (s[i] == "W")
308    {
309      if (s[i+1] == "i")
310      {
311        if (s[i+2] == "n")
312        {
313          return (1);
314        }
315      }
316    }
317  }
318  return (0);
319}
320///////////////////////////////////////////////////////////////////////////////
Note: See TracBrowser for help on using the repository browser.