1 | // last modified 21.07.2005, Oliver Wienand |
---|
2 | /////////////////////////////////////////////////////////////////////////////// |
---|
3 | version="$Id: surf.lib,v 1.29 2008-12-12 17:27:01 Singular Exp $"; |
---|
4 | category="Visualization"; |
---|
5 | info=" |
---|
6 | LIBRARY: surf.lib Procedures for Graphics with Surf |
---|
7 | AUTHOR: Hans Schoenemann, |
---|
8 | the program surf is written by Stefan Endrass |
---|
9 | |
---|
10 | NOTE: |
---|
11 | @texinfo |
---|
12 | To use this library requires the program @code{surf} to be installed. |
---|
13 | @code{surf} is only available for Linux PCs and Sun workstations. |
---|
14 | You can download @code{surf} either from |
---|
15 | @uref{http://sourceforge.net/projects/surf} |
---|
16 | or from @uref{ftp://www.mathematik.uni-kl.de/pub/Math/Singular/utils/}. |
---|
17 | @end texinfo |
---|
18 | |
---|
19 | SEE ALSO: surfex_lib |
---|
20 | |
---|
21 | PROCEDURES: |
---|
22 | plot(I); plots plane curves and surfaces |
---|
23 | "; |
---|
24 | |
---|
25 | /////////////////////////////////////////////////////////////////////////////// |
---|
26 | proc num_of_vars(ideal I) |
---|
27 | "USAGE: num_of_vars(ideal I) |
---|
28 | |
---|
29 | RETURN: an intvec containing one entry for each ring variable. |
---|
30 | each contains the sums of all degrees in this variable of all monomials |
---|
31 | occuring in the ideal. |
---|
32 | An entry is zero iff the corresponding variable does not occur in the ideal. |
---|
33 | " |
---|
34 | { |
---|
35 | intvec v; |
---|
36 | int i; |
---|
37 | poly p; |
---|
38 | for(i=ncols(I);i>0;i--) |
---|
39 | { |
---|
40 | p=I[i]; |
---|
41 | while(p!=0) |
---|
42 | { |
---|
43 | v=v+leadexp(p); |
---|
44 | p=p-lead(p); |
---|
45 | } |
---|
46 | } |
---|
47 | return(v); |
---|
48 | } |
---|
49 | example |
---|
50 | { |
---|
51 | "EXAMPLE:"; echo = 2; |
---|
52 | ring r = 0, (x,y,z),dp; |
---|
53 | ideal j0 = x^2-x*y; |
---|
54 | num_of_vars(j0); |
---|
55 | ideal j1 = x^2-x*y-y; |
---|
56 | num_of_vars(j1); |
---|
57 | ideal j2 = x^2-x*y-y, x^3-2*y; |
---|
58 | num_of_vars(j2); |
---|
59 | } |
---|
60 | |
---|
61 | proc plot(ideal I,list #) |
---|
62 | "USAGE: plot(I); I ideal or poly |
---|
63 | ASSUME: I defines a plane curve or a surface given by one equation |
---|
64 | RETURN: nothing |
---|
65 | NOTE: requires the external program 'surf' to be installed, |
---|
66 | to close the graphical interface just press 'Q' |
---|
67 | EXAMPLE: example plot; shows an example |
---|
68 | " |
---|
69 | { |
---|
70 | string extra_surf_opts=" -x --auto-resize "; // remove this line for surf 0.9 |
---|
71 | string l = "/tmp/surf"+string(system("pid")); |
---|
72 | string err_mes; // string containing error messages |
---|
73 | def base=basering; |
---|
74 | intvec v=num_of_vars(I); |
---|
75 | int i,j,n; |
---|
76 | for(i=size(v);i>0;i--) |
---|
77 | { |
---|
78 | if (v[i]!=0) { n++; } |
---|
79 | } |
---|
80 | if (n==0 or n>3) |
---|
81 | { |
---|
82 | err_mes="Cannot plot equations with "+string(n)+" variables"; |
---|
83 | ERROR(err_mes); |
---|
84 | } |
---|
85 | ring r=0,(x,y,z),dp; |
---|
86 | short=0; |
---|
87 | map phi=base,0; |
---|
88 | j=1; |
---|
89 | for(i=1;i<=size(v);i++) |
---|
90 | { |
---|
91 | if (v[i]!=0) |
---|
92 | { |
---|
93 | phi[i]=var(j); |
---|
94 | j++; |
---|
95 | if(j==4) break; |
---|
96 | } |
---|
97 | } |
---|
98 | ideal I=simplify(phi(I),2); |
---|
99 | if (leadcoef(I[1]) <0) { I[1]=-I[1]; } |
---|
100 | if (ncols(I)==1 and n<=2 and nvars(base)!=3) // curve |
---|
101 | { |
---|
102 | write(":w "+l,"clip=none;"); |
---|
103 | write(l, "width=500; height=500; set_size; do_background=yes; |
---|
104 | background_red=255; background_green=255; background_blue=255;"); |
---|
105 | write(l, |
---|
106 | "root_finder=d_chain_bisection;epsilon=0.0000000001;iterations=20000;"); |
---|
107 | write(l, "curve_green=0; curve_blue=0; curve_width=1.5;"); |
---|
108 | if (size(#)>0) |
---|
109 | { |
---|
110 | write(l,#[1]); |
---|
111 | } |
---|
112 | write(l,"curve=",I[1],";"); |
---|
113 | write(l,"draw_curve;"); |
---|
114 | } |
---|
115 | else |
---|
116 | { |
---|
117 | if (ncols(I)==1 and (n==3 or nvars(base)==3)) // surface |
---|
118 | { |
---|
119 | write(":w " + l, |
---|
120 | "root_finder=d_chain_bisection;epsilon=0.0000000001;iterations=20000;"); |
---|
121 | write(l, "width=500; height=500; set_size; do_background=yes; background_red=255; background_green=255; background_blue=255;"); |
---|
122 | write(l, "rot_x=0.14; rot_y=-0.3;"); |
---|
123 | if (size(#) > 0) |
---|
124 | { |
---|
125 | write(l, #[1]); |
---|
126 | } |
---|
127 | write(l, "surface=",I[1],";"); |
---|
128 | write(l, "draw_surface;"); |
---|
129 | } |
---|
130 | else |
---|
131 | { |
---|
132 | err_mes = "cannot plot " + string(ncols(I)) + " equations in " |
---|
133 | + string(n) + " variables"; |
---|
134 | ERROR(err_mes); |
---|
135 | } |
---|
136 | } |
---|
137 | string surf_call; |
---|
138 | surf_call = "surf "; |
---|
139 | if (defined(extra_surf_opts)) |
---|
140 | { |
---|
141 | surf_call = surf_call + " " + extra_surf_opts; |
---|
142 | } |
---|
143 | surf_call =surf_call + l + " >/dev/null 2>&1"; |
---|
144 | |
---|
145 | "Press q to exit from 'surf'"; |
---|
146 | if ("ppcMac-darwin" != system("uname")) { |
---|
147 | i=system("sh", surf_call); |
---|
148 | } else { |
---|
149 | surf_call = surf_call + " || " + "singularsurf " |
---|
150 | + extra_surf_opts + " " + l +" >/dev/null 2>&1"; |
---|
151 | i = system("sh", surf_call); |
---|
152 | } |
---|
153 | |
---|
154 | if (i != 0) |
---|
155 | { |
---|
156 | err_mes = "calling `surf` failed. (the shell return the error code " |
---|
157 | + string(i) + ")." + newline + |
---|
158 | "probably the executable `surf` is not found."; |
---|
159 | ERROR(err_mes); |
---|
160 | } |
---|
161 | i = system("sh", "/bin/rm "+l); |
---|
162 | } |
---|
163 | example |
---|
164 | { "EXAMPLE:"; echo = 2; |
---|
165 | // --------- plane curves ------------ |
---|
166 | ring rr0 = 0,(x1,x2),dp; |
---|
167 | |
---|
168 | ideal I = x1^3 - x2^2; |
---|
169 | plot(I); |
---|
170 | |
---|
171 | ring rr1 = 0,(x,y,z),dp; |
---|
172 | ideal I(1) = 2x2-1/2x3 +1-y+1; |
---|
173 | plot(I(1)); |
---|
174 | |
---|
175 | // ---- Singular Logo -------------- |
---|
176 | poly logo = ((x+3)^3 + 2*(x+3)^2 - y^2)*(x^3 - y^2)*((x-3)^3-2*(x-3)^2-y^2); |
---|
177 | plot(logo); |
---|
178 | |
---|
179 | // Steiner surface |
---|
180 | ideal J(2) = x^2*y^2+x^2*z^2+y^2*z^2-17*x*y*z; |
---|
181 | plot(J(2)); |
---|
182 | |
---|
183 | // -------------------- |
---|
184 | plot(x*(x2-y2)+z2); |
---|
185 | |
---|
186 | // E7 |
---|
187 | plot(x^3-x*y^3+z^2); |
---|
188 | |
---|
189 | // Whitney umbrella |
---|
190 | plot(z^2-x^2*y); |
---|
191 | |
---|
192 | } |
---|
193 | /////////////////////////////////////////////////////////////////////////////// |
---|