source: git/Singular/LIB/surf_jupyter.lib @ 46174a

spielwiese
Last change on this file since 46174a was 46174a, checked in by Sebastian Gutsche <gutsche@…>, 7 years ago
Fixed wrong call of surf execute script which caused Singular to return to main loop too early and consequently not displaying the created image in jupyter notebooks occasionly.
  • Property mode set to 100644
File size: 5.6 KB
Line 
1////////////////////////////////////////////////////////////////////////////
2version="version surf_jupyter.lib 0.0.0.1 Jan_2016 "; // $Id$
3category="Visualization";
4info="
5LIBRARY: surf_jupyter.lib    Procedures for Graphics with Surf
6AUTHOR: Hans Schoenemann, Frank Seelisch, Sebastian Gutsche
7
8NOTE:
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
22SEE ALSO: surf_lib, surfex_lib
23
24PROCEDURES:
25 plot_jupyter(I);    plots plane curves and surfaces
26";
27
28///////////////////////////////////////////////////////////////////////////////
29static proc 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_jupyter(ideal I,list #)
65"USAGE:   plot_jupyter(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 current_pid = string(system("pid"));
74  string l = "/tmp/surf" + current_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
144    surf_call = "singularsurf_jupyter ";
145    surf_call = surf_call + l + " " + current_pid + " 2>&1";
146    i = system("sh", surf_call);
147    if (i != 0)
148    {
149      err_mes = "calling `surf` failed" + newline
150              + " (The shell returned the error code "
151              + string(i) + ".";
152      ERROR(err_mes);
153    }
154  i = system("sh", "command rm " + l);
155  return(l+".jpg");
156}
157example
158{ "EXAMPLE:"; echo = 2;
159  // ---------  plane curves ------------
160  ring rr0 = 0,(x1,x2),dp;
161
162  ideal I = x1^3 - x2^2;
163  plot(I);
164
165  ring rr1 = 0,(x,y,z),dp;
166  ideal I(1) = 2x2-1/2x3 +1-y+1;
167  plot(I(1));
168
169  //  ---- Singular Logo --------------
170  poly logo = ((x+3)^3 + 2*(x+3)^2 - y^2)*(x^3 - y^2)*((x-3)^3-2*(x-3)^2-y^2);
171  plot(logo);
172
173  // Steiner surface
174  ideal J(2) = x^2*y^2+x^2*z^2+y^2*z^2-17*x*y*z;
175  plot(J(2));
176
177  // --------------------
178  plot(x*(x2-y2)+z2);
179
180  // E7
181  plot(x^3-x*y^3+z^2);
182
183  // Whitney umbrella
184  plot(z^2-x^2*y);
185
186}
187
188static proc isMacOSX()
189"returns 1 if this SINGULAR instance runs under (some) Mac OS X;
1900 otherwise"
191{
192  string s = system("uname");
193
194  for (int i = 1; i <= size(s)-2; i = i + 1)
195  {
196    if (s[i] == "d" or s[i] == "D")
197    {
198      if (s[i+1] == "a" or s[i+1] == "A")
199      {
200        if (s[i+2] == "r" or s[i+2] == "R")
201        {
202          return (1);
203        }
204      }
205    }
206  }
207  return (0);
208}
209
210static proc getShellOutput(string shellCommand)
211"returns the console output when executing the given shellCommand"
212{
213   int s;
214   string tempFilename = "tmp" + string(system("pid"));
215   s = system("sh", shellCommand + " > " + tempFilename + " 2>&1");
216   string r1 = read(tempFilename);
217   s = size(r1) - 1;
218   string r2 = r1[1..s];
219   s = system("sh", "command rm " + tempFilename);
220   return (r2);
221}
222
223///////////////////////////////////////////////////////////////////////////////
Note: See TracBrowser for help on using the repository browser.