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 | @end texinfo |
---|
19 | |
---|
20 | SEE ALSO: surfex_lib |
---|
21 | |
---|
22 | PROCEDURES: |
---|
23 | plot(I); plots plane curves and surfaces |
---|
24 | surfer(I); plots surfaces interactivly |
---|
25 | "; |
---|
26 | |
---|
27 | /////////////////////////////////////////////////////////////////////////////// |
---|
28 | proc num_of_vars(ideal I) |
---|
29 | "USAGE: num_of_vars(ideal I) |
---|
30 | |
---|
31 | RETURN: an intvec containing one entry for each ring variable. |
---|
32 | each contains the sums of all degrees in this variable of all monomials |
---|
33 | occuring in the ideal. |
---|
34 | An 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 | } |
---|
51 | example |
---|
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 | |
---|
63 | proc plot(ideal I,list #) |
---|
64 | "USAGE: plot(I); I ideal or poly |
---|
65 | ASSUME: I defines a plane curve or a surface given by one equation |
---|
66 | RETURN: nothing |
---|
67 | NOTE: requires the external program `surf` to be installed, |
---|
68 | to close the graphical interface just press `Q` |
---|
69 | EXAMPLE: example plot; shows an example |
---|
70 | " |
---|
71 | { |
---|
72 | string l = "/tmp/surf" + string(system("pid")); |
---|
73 | string err_mes; // string containing error messages |
---|
74 | def base=basering; |
---|
75 | intvec v=num_of_vars(I); |
---|
76 | int i,j,n; |
---|
77 | for(i=size(v);i>0;i--) |
---|
78 | { |
---|
79 | if (v[i]!=0) { n++; } |
---|
80 | } |
---|
81 | if (n==0 or n>3) |
---|
82 | { |
---|
83 | err_mes="Cannot plot equations with "+string(n)+" variables"; |
---|
84 | ERROR(err_mes); |
---|
85 | } |
---|
86 | ring r=0,(x,y,z),dp; |
---|
87 | short=0; |
---|
88 | map phi=base,0; |
---|
89 | j=1; |
---|
90 | for(i=1;i<=size(v);i++) |
---|
91 | { |
---|
92 | if (v[i]!=0) |
---|
93 | { |
---|
94 | phi[i]=var(j); |
---|
95 | j++; |
---|
96 | if(j==4) break; |
---|
97 | } |
---|
98 | } |
---|
99 | ideal I=simplify(phi(I),2); |
---|
100 | if (leadcoef(I[1]) <0) { I[1]=-I[1]; } |
---|
101 | if (ncols(I)==1 and n<=2 and nvars(base)!=3) // curve |
---|
102 | { |
---|
103 | write(":w "+l,"clip=none;"); |
---|
104 | write(l, "width=500; height=500; set_size; do_background=yes; |
---|
105 | background_red=255; background_green=255; |
---|
106 | background_blue=255;"); |
---|
107 | write(l, |
---|
108 | "root_finder=d_chain_bisection;epsilon=0.0000000001;iterations=20000;"); |
---|
109 | write(l, "curve_green=0; curve_blue=0; curve_width=1.5;"); |
---|
110 | if (size(#)>0) |
---|
111 | { |
---|
112 | write(l,#[1]); |
---|
113 | } |
---|
114 | write(l,"curve=",I[1],";"); |
---|
115 | write(l,"draw_curve;"); |
---|
116 | } |
---|
117 | else |
---|
118 | { |
---|
119 | if (ncols(I)==1 and (n==3 or nvars(base)==3)) // surface |
---|
120 | { |
---|
121 | write(":w " + l, |
---|
122 | "root_finder=d_chain_bisection;epsilon=0.0000000001;iterations=20000;"); |
---|
123 | write(l, "width=500; height=500; set_size; do_background=yes; background_red=255; background_green=255; background_blue=255;"); |
---|
124 | write(l, "rot_x=0.14; rot_y=-0.3;"); |
---|
125 | if (size(#) > 0) |
---|
126 | { |
---|
127 | write(l, #[1]); |
---|
128 | } |
---|
129 | write(l, "surface=",I[1],";"); |
---|
130 | write(l, "draw_surface;"); |
---|
131 | } |
---|
132 | else |
---|
133 | { |
---|
134 | err_mes = "cannot plot " + string(ncols(I)) + " equations in " |
---|
135 | + string(n) + " variables"; |
---|
136 | ERROR(err_mes); |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | string surf_call; i = 0; |
---|
141 | if (isWindows()) |
---|
142 | { |
---|
143 | string surferPath = getShellOutput("which surfer"); |
---|
144 | if (find(surferPath, "no surfer in") != 0) |
---|
145 | { /* did not find surfer: either not installed or |
---|
146 | not yet included in $PATH variable */ |
---|
147 | err_mes = "calling `surfer` failed" + newline |
---|
148 | + " (Either the program Surfer is not installed," + newline |
---|
149 | + " or it has not yet been included in $PATH.)"; |
---|
150 | ERROR(err_mes); |
---|
151 | } |
---|
152 | else |
---|
153 | { |
---|
154 | surf_call = "((xwin -multiwindow -clipboard -silent-dup-error"; |
---|
155 | surf_call = surf_call + " >/dev/null 2>&1 &) && sleep 5 && (sinngularsurf "; |
---|
156 | surf_call = surf_call + l + ">/dev/null 2>&1))"; |
---|
157 | surf_call = surf_call + "&& /bin/rm " + l; |
---|
158 | "Press q to exit from `surf`."; |
---|
159 | " (You may leave the XServer running for further" + newline |
---|
160 | + " invocations of `plot`.)"; |
---|
161 | i = system("sh", surf_call); |
---|
162 | if (i != 0) |
---|
163 | { |
---|
164 | err_mes = "calling `surf` failed" + newline |
---|
165 | + " (The shell returned the error code " |
---|
166 | + string(i) + "." + newline |
---|
167 | + " Perhaps the XServer was not properly set up, so" + newline |
---|
168 | + " try your plot command again. If `plot` fails" + newline |
---|
169 | + " again, then make sure that the program Surfer" + newline |
---|
170 | + " is installed and included in your $PATH variable.)"; |
---|
171 | ERROR(err_mes); |
---|
172 | } |
---|
173 | } |
---|
174 | } |
---|
175 | else |
---|
176 | { |
---|
177 | surf_call = "singularsurf "; |
---|
178 | surf_call = surf_call + l + " >/dev/null 2>&1"; |
---|
179 | "Close window to exit from `singularsurf`."; |
---|
180 | |
---|
181 | i = system("sh", surf_call); |
---|
182 | if (i != 0) |
---|
183 | { |
---|
184 | err_mes = "calling `surf` failed" + newline |
---|
185 | + " (The shell returned the error code " |
---|
186 | + string(i) + "."; |
---|
187 | ERROR(err_mes); |
---|
188 | } |
---|
189 | } |
---|
190 | system("sh", "/bin/rm " + l); |
---|
191 | } |
---|
192 | example |
---|
193 | { "EXAMPLE:"; echo = 2; |
---|
194 | // --------- plane curves ------------ |
---|
195 | ring rr0 = 0,(x1,x2),dp; |
---|
196 | |
---|
197 | ideal I = x1^3 - x2^2; |
---|
198 | plot(I); |
---|
199 | |
---|
200 | ring rr1 = 0,(x,y,z),dp; |
---|
201 | ideal I(1) = 2x2-1/2x3 +1-y+1; |
---|
202 | plot(I(1)); |
---|
203 | |
---|
204 | // ---- Singular Logo -------------- |
---|
205 | poly logo = ((x+3)^3 + 2*(x+3)^2 - y^2)*(x^3 - y^2)*((x-3)^3-2*(x-3)^2-y^2); |
---|
206 | plot(logo); |
---|
207 | |
---|
208 | // Steiner surface |
---|
209 | ideal J(2) = x^2*y^2+x^2*z^2+y^2*z^2-17*x*y*z; |
---|
210 | plot(J(2)); |
---|
211 | |
---|
212 | // -------------------- |
---|
213 | plot(x*(x2-y2)+z2); |
---|
214 | |
---|
215 | // E7 |
---|
216 | plot(x^3-x*y^3+z^2); |
---|
217 | |
---|
218 | // Whitney umbrella |
---|
219 | plot(z^2-x^2*y); |
---|
220 | |
---|
221 | } |
---|
222 | |
---|
223 | proc surfer(ideal I) |
---|
224 | "USAGE: surfer(f); f poly |
---|
225 | ASSUME: f defines a surface given by one equation |
---|
226 | RETURN: nothing |
---|
227 | NOTE: requires the external program `surfer` to be installed, |
---|
228 | to close the graphical interface just close the window of surfer |
---|
229 | EXAMPLE: example surfer; shows an example |
---|
230 | " |
---|
231 | { |
---|
232 | string lForWindows = "surfer" + string(system("pid")); |
---|
233 | string l = "./" + lForWindows; |
---|
234 | string err_mes; // string containing error messages |
---|
235 | def base=basering; |
---|
236 | intvec v=num_of_vars(I); |
---|
237 | int i,j,n; |
---|
238 | for(i=size(v);i>0;i--) |
---|
239 | { |
---|
240 | if (v[i]!=0) { n++; } |
---|
241 | } |
---|
242 | if (n==0 or n>3) |
---|
243 | { |
---|
244 | err_mes="Cannot plot equations with "+string(n)+" variables"; |
---|
245 | ERROR(err_mes); |
---|
246 | } |
---|
247 | ring r=0,(x,y,z),dp; |
---|
248 | short=0; |
---|
249 | map phi=base,0; |
---|
250 | j=1; |
---|
251 | for(i=1;i<=size(v);i++) |
---|
252 | { |
---|
253 | if (v[i]!=0) |
---|
254 | { |
---|
255 | phi[i]=var(j); |
---|
256 | j++; |
---|
257 | if(j==4) break; |
---|
258 | } |
---|
259 | } |
---|
260 | ideal I=simplify(phi(I),2); |
---|
261 | if (leadcoef(I[1]) <0) { I[1]=-I[1]; } |
---|
262 | if (ncols(I)==1 and (n==3 or nvars(base)==3)) // surface |
---|
263 | { |
---|
264 | write(":w " + l, "surface=" + string(I[1]) + ";"); |
---|
265 | } |
---|
266 | else |
---|
267 | { |
---|
268 | err_mes = "cannot plot " + string(ncols(I)) + " equations in " |
---|
269 | + string(n) + " variables"; |
---|
270 | ERROR(err_mes); |
---|
271 | } |
---|
272 | |
---|
273 | string surf_call; i = 0; |
---|
274 | if (isWindows()) |
---|
275 | { |
---|
276 | string surferPath = getShellOutput("which surfer"); |
---|
277 | if (find(surferPath, "no surfer in") != 0) |
---|
278 | { /* did not find surfer: either not installed or |
---|
279 | not yet included in $PATH variable */ |
---|
280 | err_mes = "calling `surfer` failed" + newline |
---|
281 | + " (Either the program Surfer is not installed," + newline |
---|
282 | + " or it has not yet been included in $PATH.)"; |
---|
283 | ERROR(err_mes); |
---|
284 | } |
---|
285 | else |
---|
286 | { |
---|
287 | string singularPath = getShellOutput("pwd"); |
---|
288 | surferPath = windowsCorrection(surferPath); |
---|
289 | surferPath = surferPath[1..size(surferPath)-size("/surfer")]; |
---|
290 | singularPath = windowsCorrection(singularPath); |
---|
291 | link ll = "|: cygpath -w " + singularPath; |
---|
292 | singularPath = "'\""+read(ll)+"\"'"; close(ll); |
---|
293 | surf_call = "cygstart -w -d " + surferPath + " "; |
---|
294 | surf_call = surf_call + surferPath + "/surfer "; |
---|
295 | surf_call = surf_call + singularPath + "/" + lForWindows; |
---|
296 | "Close window to exit from `surfer`."; |
---|
297 | i = system("sh", surf_call); |
---|
298 | } |
---|
299 | } |
---|
300 | else |
---|
301 | { |
---|
302 | surf_call = "surfer " + l + " >/dev/null 2>&1"; |
---|
303 | "Close window to exit from `surfer`."; |
---|
304 | i = system("sh", surf_call); |
---|
305 | } |
---|
306 | system("sh", "/bin/rm " + l); |
---|
307 | |
---|
308 | if (i != 0) |
---|
309 | { |
---|
310 | err_mes = "calling `surfer` failed" + newline |
---|
311 | + " (The shell returned the error code " |
---|
312 | + string(i) + "."; |
---|
313 | ERROR(err_mes); |
---|
314 | } |
---|
315 | } |
---|
316 | example |
---|
317 | { "EXAMPLE:"; echo = 2; |
---|
318 | ring rr1 = 0,(x,y,z),dp; |
---|
319 | // Steiner surface |
---|
320 | ideal J(2) = x^2*y^2+x^2*z^2+y^2*z^2-17*x*y*z; |
---|
321 | surfer(J(2)); |
---|
322 | |
---|
323 | // -------------------- |
---|
324 | surfer(x*(x2-y2)+z2); |
---|
325 | |
---|
326 | // E7 |
---|
327 | surfer(x^3-x*y^3+z^2); |
---|
328 | |
---|
329 | // Whitney umbrella |
---|
330 | surfer(z^2-x^2*y); |
---|
331 | } |
---|
332 | |
---|
333 | static proc isWindows() |
---|
334 | "returns 1 if this SINGULAR instance runs under (some) Windows OS; |
---|
335 | 0 otherwise" |
---|
336 | { |
---|
337 | string s = system("uname"); |
---|
338 | for (int i = 1; i <= size(s)-2; i = i + 1) |
---|
339 | { |
---|
340 | if (s[i] == "W") |
---|
341 | { |
---|
342 | if (s[i+1] == "i") |
---|
343 | { |
---|
344 | if (s[i+2] == "n") |
---|
345 | { |
---|
346 | return (1); |
---|
347 | } |
---|
348 | } |
---|
349 | } |
---|
350 | } |
---|
351 | return (0); |
---|
352 | } |
---|
353 | |
---|
354 | static proc getShellOutput(string shellCommand) |
---|
355 | "returns the console output when executing the given shellCommand" |
---|
356 | { |
---|
357 | int s; |
---|
358 | string tempFilename = "tmp" + string(system("pid")); |
---|
359 | s = system("sh", shellCommand + " > " + tempFilename + " 2>&1"); |
---|
360 | string r1 = read(tempFilename); |
---|
361 | s = size(r1) - 1; |
---|
362 | string r2 = r1[1..s]; |
---|
363 | s = system("sh", "/bin/rm " + tempFilename); |
---|
364 | return (r2); |
---|
365 | } |
---|
366 | |
---|
367 | static proc windowsCorrection(string windowsPath) |
---|
368 | "puts a backslash in front of each space and each special character |
---|
369 | and returns the resulting string" |
---|
370 | { |
---|
371 | string s = ""; int i; |
---|
372 | for (i = 1; i <= size(windowsPath); i++) |
---|
373 | { |
---|
374 | if (find(" ()", windowsPath[i]) != 0) |
---|
375 | { |
---|
376 | s = s + "\\"; |
---|
377 | } |
---|
378 | s = s + windowsPath[i]; |
---|
379 | } |
---|
380 | return (s); |
---|
381 | } |
---|
382 | /////////////////////////////////////////////////////////////////////////////// |
---|