[380a17b] | 1 | //////////////////////////////////////////////////////////////////////////// |
---|
[3686937] | 2 | version="version surf.lib 4.0.0.0 Jun_2013 "; // $Id$ |
---|
[fd3fb7] | 3 | category="Visualization"; |
---|
[7e1114d] | 4 | info=" |
---|
[8bb77b] | 5 | LIBRARY: surf.lib Procedures for Graphics with Surf |
---|
[6f212c] | 6 | AUTHOR: Hans Schoenemann, Frank Seelisch |
---|
[131a579] | 7 | |
---|
[62bb5e] | 8 | NOTE: |
---|
[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] | 20 | SEE ALSO: surfex_lib |
---|
| 21 | |
---|
[f34c37c] | 22 | PROCEDURES: |
---|
[49d99c] | 23 | plot(I); plots plane curves and surfaces |
---|
| 24 | surfer(I); plots surfaces interactivly |
---|
[7e1114d] | 25 | "; |
---|
| 26 | |
---|
| 27 | /////////////////////////////////////////////////////////////////////////////// |
---|
[72382c2] | 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. |
---|
[3754ca] | 34 | An 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] | 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 | } |
---|
[7e1114d] | 62 | |
---|
[d85126] | 63 | proc plot(ideal I,list #) |
---|
[50cbdc] | 64 | "USAGE: plot(I); I ideal or poly |
---|
| 65 | ASSUME: I defines a plane curve or a surface given by one equation |
---|
[aef7ccb] | 66 | RETURN: nothing |
---|
[6f212c] | 67 | NOTE: requires the external program `surf` to be installed, |
---|
| 68 | to close the graphical interface just press `Q` |
---|
[7e1114d] | 69 | EXAMPLE: example plot; shows an example |
---|
| 70 | " |
---|
| 71 | { |
---|
[d0869f] | 72 | string l = "/tmp/surf" + string(system("pid")); |
---|
[afe420] | 73 | string err_mes; // string containing error messages |
---|
[7e1114d] | 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 | { |
---|
[afe420] | 83 | err_mes="Cannot plot equations with "+string(n)+" variables"; |
---|
[9173792] | 84 | ERROR(err_mes); |
---|
[7e1114d] | 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); |
---|
[50cbdc] | 100 | if (leadcoef(I[1]) <0) { I[1]=-I[1]; } |
---|
[307ffc] | 101 | if (ncols(I)==1 and n<=2 and nvars(base)!=3) // curve |
---|
[7e1114d] | 102 | { |
---|
| 103 | write(":w "+l,"clip=none;"); |
---|
[b9b906] | 104 | write(l, "width=500; height=500; set_size; do_background=yes; |
---|
[d0869f] | 105 | background_red=255; background_green=255; |
---|
| 106 | background_blue=255;"); |
---|
[7e1114d] | 107 | write(l, |
---|
| 108 | "root_finder=d_chain_bisection;epsilon=0.0000000001;iterations=20000;"); |
---|
[aef7ccb] | 109 | write(l, "curve_green=0; curve_blue=0; curve_width=1.5;"); |
---|
[b9b906] | 110 | if (size(#)>0) |
---|
[05d2e8] | 111 | { |
---|
| 112 | write(l,#[1]); |
---|
| 113 | } |
---|
[7e1114d] | 114 | write(l,"curve=",I[1],";"); |
---|
| 115 | write(l,"draw_curve;"); |
---|
| 116 | } |
---|
| 117 | else |
---|
| 118 | { |
---|
[307ffc] | 119 | if (ncols(I)==1 and (n==3 or nvars(base)==3)) // surface |
---|
[7e1114d] | 120 | { |
---|
[d85126] | 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;"); |
---|
[7e1114d] | 131 | } |
---|
| 132 | else |
---|
| 133 | { |
---|
[d85126] | 134 | err_mes = "cannot plot " + string(ncols(I)) + " equations in " |
---|
| 135 | + string(n) + " variables"; |
---|
[afe420] | 136 | ERROR(err_mes); |
---|
[7e1114d] | 137 | } |
---|
| 138 | } |
---|
[d0869f] | 139 | |
---|
[6f212c] | 140 | string surf_call; i = 0; |
---|
[d0869f] | 141 | if (isWindows()) |
---|
[94b16b] | 142 | { |
---|
[6f212c] | 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 |
---|
[d0869f] | 153 | { |
---|
[6f212c] | 154 | surf_call = "((xwin -multiwindow -clipboard -silent-dup-error"; |
---|
[a529d3d] | 155 | surf_call = surf_call + " >/dev/null 2>&1 &) && sleep 5 && (sinngularsurf "; |
---|
[6f212c] | 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 | } |
---|
[d0869f] | 173 | } |
---|
[94b16b] | 174 | } |
---|
| 175 | else |
---|
| 176 | { |
---|
[a529d3d] | 177 | surf_call = "singularsurf "; |
---|
[d0869f] | 178 | surf_call = surf_call + l + " >/dev/null 2>&1"; |
---|
[a529d3d] | 179 | "Close window to exit from `singularurf`."; |
---|
[0610f0e] | 180 | |
---|
[d0869f] | 181 | i = system("sh", surf_call); |
---|
[6f212c] | 182 | if (i != 0) |
---|
| 183 | { |
---|
| 184 | err_mes = "calling `surf` failed" + newline |
---|
[d0869f] | 185 | + " (The shell returned the error code " |
---|
[a529d3d] | 186 | + string(i) + "."; |
---|
[6f212c] | 187 | ERROR(err_mes); |
---|
| 188 | } |
---|
[62bb5e] | 189 | } |
---|
[6f212c] | 190 | system("sh", "/bin/rm " + l); |
---|
[7e1114d] | 191 | } |
---|
| 192 | example |
---|
[d85126] | 193 | { "EXAMPLE:"; echo = 2; |
---|
[50cbdc] | 194 | // --------- plane curves ------------ |
---|
| 195 | ring rr0 = 0,(x1,x2),dp; |
---|
[7e1114d] | 196 | |
---|
[50cbdc] | 197 | ideal I = x1^3 - x2^2; |
---|
| 198 | plot(I); |
---|
[7e1114d] | 199 | |
---|
[50cbdc] | 200 | ring rr1 = 0,(x,y,z),dp; |
---|
| 201 | ideal I(1) = 2x2-1/2x3 +1-y+1; |
---|
| 202 | plot(I(1)); |
---|
[7e1114d] | 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 | |
---|
[50cbdc] | 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)); |
---|
[917fb5] | 211 | |
---|
[50cbdc] | 212 | // -------------------- |
---|
[7e1114d] | 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 | |
---|
[94b16b] | 221 | } |
---|
[6f212c] | 222 | |
---|
[ddc52f] | 223 | proc surfer(ideal I) |
---|
[94b16b] | 224 | "USAGE: surfer(f); f poly |
---|
| 225 | ASSUME: f defines a surface given by one equation |
---|
| 226 | RETURN: nothing |
---|
[6f212c] | 227 | NOTE: requires the external program `surfer` to be installed, |
---|
[94b16b] | 228 | to close the graphical interface just close the window of surfer |
---|
| 229 | EXAMPLE: example surfer; shows an example |
---|
| 230 | " |
---|
| 231 | { |
---|
[6f212c] | 232 | string lForWindows = "surfer" + string(system("pid")); |
---|
| 233 | string l = "./" + lForWindows; |
---|
[94b16b] | 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 | { |
---|
[d0869f] | 264 | write(":w " + l, "surface=" + string(I[1]) + ";"); |
---|
[94b16b] | 265 | } |
---|
| 266 | else |
---|
| 267 | { |
---|
| 268 | err_mes = "cannot plot " + string(ncols(I)) + " equations in " |
---|
| 269 | + string(n) + " variables"; |
---|
| 270 | ERROR(err_mes); |
---|
| 271 | } |
---|
[7030440] | 272 | |
---|
[6f212c] | 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); |
---|
[1e1ec4] | 291 | link ll = "|: cygpath -w " + singularPath; |
---|
| 292 | singularPath = "'\""+read(ll)+"\"'"; close(ll); |
---|
[6f212c] | 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 | } |
---|
[d0869f] | 306 | system("sh", "/bin/rm " + l); |
---|
[94b16b] | 307 | |
---|
| 308 | if (i != 0) |
---|
| 309 | { |
---|
[d0869f] | 310 | err_mes = "calling `surfer` failed" + newline |
---|
| 311 | + " (The shell returned the error code " |
---|
[6f212c] | 312 | + string(i) + "."; |
---|
[94b16b] | 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); |
---|
[7e1114d] | 331 | } |
---|
[6f212c] | 332 | |
---|
| 333 | static proc isWindows() |
---|
[d0869f] | 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 | } |
---|
[6f212c] | 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 | } |
---|
[7e1114d] | 382 | /////////////////////////////////////////////////////////////////////////////// |
---|