1 | //////////////////////////////////////////////////////////////////////////// |
---|
2 | version="version surf.lib 4.0.0.0 Jun_2013 "; // $Id$ |
---|
3 | category="Visualization"; |
---|
4 | info=" |
---|
5 | LIBRARY: surf.lib Procedures for Graphics with Surf |
---|
6 | AUTHOR: Hans Schoenemann, Frank Seelisch |
---|
7 | |
---|
8 | NOTE: |
---|
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 | Under Mac OS X please move SURFER.app from http://www.mathematik.uni-kl.de/~motsak/files/SURFER.dmg |
---|
19 | under your /Applications. |
---|
20 | @end texinfo |
---|
21 | |
---|
22 | SEE ALSO: surfex_lib |
---|
23 | |
---|
24 | PROCEDURES: |
---|
25 | plot(I); plots plane curves and surfaces |
---|
26 | surfer(I); plots surfaces interactivly |
---|
27 | "; |
---|
28 | |
---|
29 | /////////////////////////////////////////////////////////////////////////////// |
---|
30 | proc num_of_vars(ideal I) |
---|
31 | "USAGE: num_of_vars(ideal I) |
---|
32 | |
---|
33 | RETURN: an intvec containing one entry for each ring variable. |
---|
34 | each contains the sums of all degrees in this variable of all monomials |
---|
35 | occuring in the ideal. |
---|
36 | An entry is zero if and only if the corresponding variable does not occur in the ideal. |
---|
37 | " |
---|
38 | { |
---|
39 | intvec v; |
---|
40 | int i; |
---|
41 | poly p; |
---|
42 | for(i=ncols(I);i>0;i--) |
---|
43 | { |
---|
44 | p=I[i]; |
---|
45 | while(p!=0) |
---|
46 | { |
---|
47 | v=v+leadexp(p); |
---|
48 | p=p-lead(p); |
---|
49 | } |
---|
50 | } |
---|
51 | return(v); |
---|
52 | } |
---|
53 | example |
---|
54 | { |
---|
55 | "EXAMPLE:"; echo = 2; |
---|
56 | ring r = 0, (x,y,z),dp; |
---|
57 | ideal j0 = x^2-x*y; |
---|
58 | num_of_vars(j0); |
---|
59 | ideal j1 = x^2-x*y-y; |
---|
60 | num_of_vars(j1); |
---|
61 | ideal j2 = x^2-x*y-y, x^3-2*y; |
---|
62 | num_of_vars(j2); |
---|
63 | } |
---|
64 | |
---|
65 | proc plot(ideal I,list #) |
---|
66 | "USAGE: plot(I); I ideal or poly |
---|
67 | ASSUME: I defines a plane curve or a surface given by one equation |
---|
68 | RETURN: nothing |
---|
69 | NOTE: requires the external program `surf` to be installed, |
---|
70 | to close the graphical interface just press `Q` |
---|
71 | EXAMPLE: example plot; shows an example |
---|
72 | " |
---|
73 | { |
---|
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; i = 0; |
---|
143 | if (isWindows()) |
---|
144 | { |
---|
145 | string surferPath = getShellOutput("which surfer"); |
---|
146 | if (find(surferPath, "no surfer in") != 0) |
---|
147 | { /* did not find surfer: either not installed or |
---|
148 | not yet included in $PATH variable */ |
---|
149 | err_mes = "calling `surfer` failed" + newline |
---|
150 | + " (Either the program Surfer is not installed," + newline |
---|
151 | + " or it has not yet been included in $PATH.)"; |
---|
152 | ERROR(err_mes); |
---|
153 | } |
---|
154 | else |
---|
155 | { |
---|
156 | surf_call = "((xwin -multiwindow -clipboard -silent-dup-error"; |
---|
157 | surf_call = surf_call + " >/dev/null 2>&1 &) && sleep 5 && (sinngularsurf "; |
---|
158 | surf_call = surf_call + l + ">/dev/null 2>&1))"; |
---|
159 | surf_call = surf_call + "&& command rm " + l; |
---|
160 | "Press q to exit from `surf`."; |
---|
161 | " (You may leave the XServer running for further" + newline |
---|
162 | + " invocations of `plot`.)"; |
---|
163 | i = system("sh", surf_call); |
---|
164 | if (i != 0) |
---|
165 | { |
---|
166 | err_mes = "calling `surf` failed" + newline |
---|
167 | + " (The shell returned the error code " |
---|
168 | + string(i) + "." + newline |
---|
169 | + " Perhaps the XServer was not properly set up, so" + newline |
---|
170 | + " try your plot command again. If `plot` fails" + newline |
---|
171 | + " again, then make sure that the program Surfer" + newline |
---|
172 | + " is installed and included in your $PATH variable.)"; |
---|
173 | ERROR(err_mes); |
---|
174 | } |
---|
175 | } |
---|
176 | } |
---|
177 | else |
---|
178 | { |
---|
179 | surf_call = "singularsurf "; |
---|
180 | surf_call = surf_call + l + " >/dev/null 2>&1"; |
---|
181 | "Close window to exit from `singularsurf`."; |
---|
182 | |
---|
183 | i = system("sh", surf_call); |
---|
184 | if (i != 0) |
---|
185 | { |
---|
186 | err_mes = "calling `surf` failed" + newline |
---|
187 | + " (The shell returned the error code " |
---|
188 | + string(i) + "."; |
---|
189 | ERROR(err_mes); |
---|
190 | } |
---|
191 | } |
---|
192 | system("sh", "command rm " + l); |
---|
193 | } |
---|
194 | example |
---|
195 | { "EXAMPLE:"; echo = 2; |
---|
196 | // --------- plane curves ------------ |
---|
197 | ring rr0 = 0,(x1,x2),dp; |
---|
198 | |
---|
199 | ideal I = x1^3 - x2^2; |
---|
200 | plot(I); |
---|
201 | |
---|
202 | ring rr1 = 0,(x,y,z),dp; |
---|
203 | ideal I(1) = 2x2-1/2x3 +1-y+1; |
---|
204 | plot(I(1)); |
---|
205 | |
---|
206 | // ---- Singular Logo -------------- |
---|
207 | poly logo = ((x+3)^3 + 2*(x+3)^2 - y^2)*(x^3 - y^2)*((x-3)^3-2*(x-3)^2-y^2); |
---|
208 | plot(logo); |
---|
209 | |
---|
210 | // Steiner surface |
---|
211 | ideal J(2) = x^2*y^2+x^2*z^2+y^2*z^2-17*x*y*z; |
---|
212 | plot(J(2)); |
---|
213 | |
---|
214 | // -------------------- |
---|
215 | plot(x*(x2-y2)+z2); |
---|
216 | |
---|
217 | // E7 |
---|
218 | plot(x^3-x*y^3+z^2); |
---|
219 | |
---|
220 | // Whitney umbrella |
---|
221 | plot(z^2-x^2*y); |
---|
222 | |
---|
223 | } |
---|
224 | |
---|
225 | proc surfer(ideal I) |
---|
226 | "USAGE: surfer(f); f poly |
---|
227 | ASSUME: f defines a surface given by one equation |
---|
228 | RETURN: nothing |
---|
229 | NOTE: requires the external program `surfer` to be installed, |
---|
230 | to close the graphical interface just close the window of surfer |
---|
231 | EXAMPLE: example surfer; shows an example |
---|
232 | " |
---|
233 | { |
---|
234 | string lForWindows = "surfer" + string(system("pid")); |
---|
235 | string l = "./" + lForWindows; |
---|
236 | string err_mes; // string containing error messages |
---|
237 | def base=basering; |
---|
238 | intvec v=num_of_vars(I); |
---|
239 | int i,j,n; |
---|
240 | for(i=size(v);i>0;i--) |
---|
241 | { |
---|
242 | if (v[i]!=0) { n++; } |
---|
243 | } |
---|
244 | if (n==0 or n>3) |
---|
245 | { |
---|
246 | err_mes="Cannot plot equations with "+string(n)+" variables"; |
---|
247 | ERROR(err_mes); |
---|
248 | } |
---|
249 | ring r=0,(x,y,z),dp; |
---|
250 | short=0; |
---|
251 | map phi=base,0; |
---|
252 | j=1; |
---|
253 | for(i=1;i<=size(v);i++) |
---|
254 | { |
---|
255 | if (v[i]!=0) |
---|
256 | { |
---|
257 | phi[i]=var(j); |
---|
258 | j++; |
---|
259 | if(j==4) break; |
---|
260 | } |
---|
261 | } |
---|
262 | ideal I=simplify(phi(I),2); |
---|
263 | if (leadcoef(I[1]) <0) { I[1]=-I[1]; } |
---|
264 | if (ncols(I)==1 and (n==3 or nvars(base)==3)) // surface |
---|
265 | { |
---|
266 | write(":w " + l, "surface=" + string(I[1]) + ";"); |
---|
267 | } |
---|
268 | else |
---|
269 | { |
---|
270 | err_mes = "cannot plot " + string(ncols(I)) + " equations in " |
---|
271 | + string(n) + " variables"; |
---|
272 | ERROR(err_mes); |
---|
273 | } |
---|
274 | |
---|
275 | string surf_call; i = 0; |
---|
276 | |
---|
277 | if (isWindows()) |
---|
278 | { |
---|
279 | string surferPath = getShellOutput("which surfer"); |
---|
280 | if (find(surferPath, "no surfer in") != 0) |
---|
281 | { /* did not find surfer: either not installed or |
---|
282 | not yet included in $PATH variable */ |
---|
283 | err_mes = "calling `surfer` failed" + newline |
---|
284 | + " (Either the program Surfer is not installed," + newline |
---|
285 | + " or it has not yet been included in $PATH.)"; |
---|
286 | ERROR(err_mes); |
---|
287 | } |
---|
288 | else |
---|
289 | { |
---|
290 | string singularPath = getShellOutput("pwd"); |
---|
291 | surferPath = windowsCorrection(surferPath); |
---|
292 | surferPath = surferPath[1..size(surferPath)-size("/surfer")]; |
---|
293 | singularPath = windowsCorrection(singularPath); |
---|
294 | link ll = "|: cygpath -w " + singularPath; |
---|
295 | singularPath = "'\""+read(ll)+"\"'"; close(ll); |
---|
296 | surf_call = "cygstart -w -d " + surferPath + " "; |
---|
297 | surf_call = surf_call + surferPath + "/surfer "; |
---|
298 | surf_call = surf_call + singularPath + "/" + lForWindows; |
---|
299 | "Close window to exit from `surfer`."; |
---|
300 | i = system("sh", surf_call); |
---|
301 | } |
---|
302 | } |
---|
303 | else |
---|
304 | { |
---|
305 | surf_call = "surfer"; |
---|
306 | surf_call = surf_call + " " + l + " >/dev/null 2>&1"; |
---|
307 | "Close window to exit from `surfer`."; |
---|
308 | i = system("sh", surf_call); |
---|
309 | |
---|
310 | if ( (i != 0) && isMacOSX() ) |
---|
311 | { |
---|
312 | "*!* Sorry: calling `surfer` failed ['"+surf_call+"']" + newline |
---|
313 | + " (The shell returned the error code " + string(i) + "." + newline |
---|
314 | + "But since we are on Mac OS X, let us try to open SURFER.app instead..." + newline |
---|
315 | + "Appropriate SURFER.app is available for instance at http://www.mathematik.uni-kl.de/~motsak/files/SURFER.dmg"; |
---|
316 | |
---|
317 | // fallback, will only work if SURFER.app is available (e.g. in /Applications) |
---|
318 | // get SURFER.app e.g. from http://www.mathematik.uni-kl.de/~motsak/files/SURFER.dmg |
---|
319 | // note that the newer (Java-based) variant of Surfer may not support command line usage yet :( |
---|
320 | |
---|
321 | surf_call = "open -a SURFER -W --args -t -s"; |
---|
322 | surf_call = surf_call + " " + l + " >/dev/null 2>&1"; |
---|
323 | "Close window to exit from `surfer`."; |
---|
324 | i = system("sh", surf_call); |
---|
325 | } |
---|
326 | |
---|
327 | |
---|
328 | |
---|
329 | } |
---|
330 | system("sh", "command rm " + l); |
---|
331 | |
---|
332 | if (i != 0) |
---|
333 | { |
---|
334 | err_mes = "calling `surfer` failed" + newline |
---|
335 | + " (The shell returned the error code " |
---|
336 | + string(i) + "."; |
---|
337 | ERROR(err_mes); |
---|
338 | } |
---|
339 | } |
---|
340 | example |
---|
341 | { "EXAMPLE:"; echo = 2; |
---|
342 | ring rr1 = 0,(x,y,z),dp; |
---|
343 | // Steiner surface |
---|
344 | ideal J(2) = x^2*y^2+x^2*z^2+y^2*z^2-17*x*y*z; |
---|
345 | surfer(J(2)); |
---|
346 | |
---|
347 | // -------------------- |
---|
348 | surfer(x*(x2-y2)+z2); |
---|
349 | |
---|
350 | // E7 |
---|
351 | surfer(x^3-x*y^3+z^2); |
---|
352 | |
---|
353 | // Whitney umbrella |
---|
354 | surfer(z^2-x^2*y); |
---|
355 | } |
---|
356 | |
---|
357 | static proc isWindows() |
---|
358 | "returns 1 if this SINGULAR instance runs under (some) Windows OS; |
---|
359 | 0 otherwise" |
---|
360 | { |
---|
361 | string s = system("uname"); |
---|
362 | for (int i = 1; i <= size(s)-2; i = i + 1) |
---|
363 | { |
---|
364 | if (s[i] == "W") |
---|
365 | { |
---|
366 | if (s[i+1] == "i") |
---|
367 | { |
---|
368 | if (s[i+2] == "n") |
---|
369 | { |
---|
370 | return (1); |
---|
371 | } |
---|
372 | } |
---|
373 | } |
---|
374 | } |
---|
375 | return (0); |
---|
376 | } |
---|
377 | |
---|
378 | static proc isMacOSX() |
---|
379 | "returns 1 if this SINGULAR instance runs under (some) Mac OS X; |
---|
380 | 0 otherwise" |
---|
381 | { |
---|
382 | string s = system("uname"); |
---|
383 | |
---|
384 | for (int i = 1; i <= size(s)-2; i = i + 1) |
---|
385 | { |
---|
386 | if (s[i] == "d" or s[i] == "D") |
---|
387 | { |
---|
388 | if (s[i+1] == "a" or s[i+1] == "A") |
---|
389 | { |
---|
390 | if (s[i+2] == "r" or s[i+2] == "R") |
---|
391 | { |
---|
392 | return (1); |
---|
393 | } |
---|
394 | } |
---|
395 | } |
---|
396 | } |
---|
397 | return (0); |
---|
398 | } |
---|
399 | |
---|
400 | static proc getShellOutput(string shellCommand) |
---|
401 | "returns the console output when executing the given shellCommand" |
---|
402 | { |
---|
403 | int s; |
---|
404 | string tempFilename = "tmp" + string(system("pid")); |
---|
405 | s = system("sh", shellCommand + " > " + tempFilename + " 2>&1"); |
---|
406 | string r1 = read(tempFilename); |
---|
407 | s = size(r1) - 1; |
---|
408 | string r2 = r1[1..s]; |
---|
409 | s = system("sh", "command rm " + tempFilename); |
---|
410 | return (r2); |
---|
411 | } |
---|
412 | |
---|
413 | static proc windowsCorrection(string windowsPath) |
---|
414 | "puts a backslash in front of each space and each special character |
---|
415 | and returns the resulting string" |
---|
416 | { |
---|
417 | string s = ""; int i; |
---|
418 | for (i = 1; i <= size(windowsPath); i++) |
---|
419 | { |
---|
420 | if (find(" ()", windowsPath[i]) != 0) |
---|
421 | { |
---|
422 | s = s + "\\"; |
---|
423 | } |
---|
424 | s = s + windowsPath[i]; |
---|
425 | } |
---|
426 | return (s); |
---|
427 | } |
---|
428 | /////////////////////////////////////////////////////////////////////////////// |
---|