1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id: ipprint.cc,v 1.11 1999-04-19 11:02:39 obachman Exp $ */ |
---|
5 | /* |
---|
6 | * ABSTRACT: interpreter: printing |
---|
7 | */ |
---|
8 | |
---|
9 | #include "mod2.h" |
---|
10 | #include "tok.h" |
---|
11 | #include "ipid.h" |
---|
12 | #include "mmemory.h" |
---|
13 | #include "febase.h" |
---|
14 | #include "polys.h" |
---|
15 | #include "matpol.h" |
---|
16 | #include "subexpr.h" |
---|
17 | #include "intvec.h" |
---|
18 | #include "ipshell.h" |
---|
19 | #include "ipprint.h" |
---|
20 | #include "ideals.h" |
---|
21 | |
---|
22 | /*2 |
---|
23 | * print for: int, string, poly, vector, ideal |
---|
24 | */ |
---|
25 | /*2 |
---|
26 | * print for: intvec |
---|
27 | */ |
---|
28 | static BOOLEAN ipPrint_INTVEC(leftv u) |
---|
29 | { |
---|
30 | intvec *v=(intvec *)u->Data(); |
---|
31 | v->show(); |
---|
32 | PrintLn(); |
---|
33 | return FALSE; |
---|
34 | } |
---|
35 | |
---|
36 | /*2 |
---|
37 | * print for: intmat |
---|
38 | */ |
---|
39 | static BOOLEAN ipPrint_INTMAT(leftv u) |
---|
40 | { |
---|
41 | intvec *v=(intvec *)u->Data(); |
---|
42 | int i,j; |
---|
43 | for(i=0;i<v->rows();i++) |
---|
44 | { |
---|
45 | for(j=0;j<v->cols();j++) |
---|
46 | { |
---|
47 | Print(" %5d",IMATELEM(*v,i+1,j+1)); |
---|
48 | } |
---|
49 | PrintLn(); |
---|
50 | } |
---|
51 | return FALSE; |
---|
52 | } |
---|
53 | |
---|
54 | /*2 |
---|
55 | * internal print for: matrix |
---|
56 | */ |
---|
57 | static void ipPrint_MA0(matrix m, const char *name) |
---|
58 | { |
---|
59 | if (MATCOLS(m)>0) |
---|
60 | { |
---|
61 | char **s=(char **)Alloc(MATCOLS(m)*MATROWS(m)*sizeof(char*)); |
---|
62 | char *ss; |
---|
63 | int *l=(int *)Alloc0(MATCOLS(m)*sizeof(int)); |
---|
64 | int i,j,k; |
---|
65 | int vl=max(colmax/MATCOLS(m),8); |
---|
66 | |
---|
67 | /* make enough space for the "largest" name*/ |
---|
68 | ss=(char *)AllocL(14+strlen(name)); |
---|
69 | sprintf(ss,"%s[%d,%d]",name,MATCOLS(m),MATROWS(m)); |
---|
70 | vl=max(vl,strlen(ss)); |
---|
71 | FreeL(ss); |
---|
72 | |
---|
73 | /* convert all polys to string */ |
---|
74 | i=MATCOLS(m)*MATROWS(m)-1; |
---|
75 | ss=pString(m->m[i]); |
---|
76 | if ((int)strlen(ss)>colmax) s[i]=NULL; |
---|
77 | else s[i]=mstrdup(ss); |
---|
78 | for(i--;i>=0;i--) |
---|
79 | { |
---|
80 | pString(m->m[i]); |
---|
81 | ss=StringAppendS(","); |
---|
82 | if ((int)strlen(ss)>colmax) s[i]=NULL; |
---|
83 | else s[i]=mstrdup(ss); |
---|
84 | } |
---|
85 | /* look up the width of all columns, put it in l[col_nr] */ |
---|
86 | /* insert names for very long entries */ |
---|
87 | for(i=MATROWS(m)-1;i>=0;i--) |
---|
88 | { |
---|
89 | for(j=MATCOLS(m)-1;j>=0;j--) |
---|
90 | { |
---|
91 | if (s[i*MATCOLS(m)+j]==NULL) |
---|
92 | { |
---|
93 | ss=(char *)AllocL(14+strlen(name)); |
---|
94 | s[i*MATCOLS(m)+j]=ss; |
---|
95 | ss[0]='\0'; |
---|
96 | sprintf(ss,"%s[%d,%d]",name,i+1,j+1); |
---|
97 | if ((i!=MATROWS(m)-1) || (j!=MATCOLS(m)-1)) |
---|
98 | { |
---|
99 | strcat(ss,","); |
---|
100 | vl=max(vl,strlen(ss)); |
---|
101 | } |
---|
102 | } |
---|
103 | k=strlen(s[i*MATCOLS(m)+j]); |
---|
104 | if (k>l[j]) l[j]=k; |
---|
105 | } |
---|
106 | } |
---|
107 | /* does it fit on a line ? */ |
---|
108 | int maxlen=0; |
---|
109 | for(j=MATCOLS(m)-1;j>=0;j--) |
---|
110 | { |
---|
111 | maxlen+=l[j]; |
---|
112 | } |
---|
113 | if (maxlen>colmax) |
---|
114 | { |
---|
115 | /* NO, it does not fit, so retry: */ |
---|
116 | /* look up the width of all columns, clear very long entriess */ |
---|
117 | /* put length in l[col_nr] */ |
---|
118 | /* insert names for cleared entries */ |
---|
119 | for(j=MATCOLS(m)-1;j>=0;j--) |
---|
120 | { |
---|
121 | for(i=MATROWS(m)-1;i>=0;i--) |
---|
122 | { |
---|
123 | k=strlen(s[i*MATCOLS(m)+j]); |
---|
124 | if (/*strlen(s[i*MATCOLS(m)+j])*/ k > vl) |
---|
125 | { |
---|
126 | FreeL((ADDRESS)s[i*MATCOLS(m)+j]); |
---|
127 | ss=(char *)AllocL(14+strlen(name)); |
---|
128 | s[i*MATCOLS(m)+j]=ss; |
---|
129 | ss[0]='\0'; |
---|
130 | sprintf(ss,"%s[%d,%d]",name,i+1,j+1); |
---|
131 | if ((i!=MATROWS(m)-1) || (j!=MATCOLS(m)-1)) |
---|
132 | { |
---|
133 | strcat(ss,","); |
---|
134 | } |
---|
135 | l[j]=strlen(s[i*MATCOLS(m)+j]); |
---|
136 | if (l[j]>vl) |
---|
137 | { |
---|
138 | #ifdef TEST |
---|
139 | PrintS("pagewidth too small in print(matrix)\n"); |
---|
140 | #endif |
---|
141 | vl=l[j]; /* make large names fit*/ |
---|
142 | } |
---|
143 | i=MATROWS(m); |
---|
144 | } |
---|
145 | else |
---|
146 | { |
---|
147 | if (k>l[j]) l[j]=k; |
---|
148 | } |
---|
149 | } |
---|
150 | } |
---|
151 | } |
---|
152 | /*output of the matrix*/ |
---|
153 | for(i=0;i<MATROWS(m);i++) |
---|
154 | { |
---|
155 | k=l[0]; |
---|
156 | Print("%-*.*s",l[0],l[0],s[i*MATCOLS(m)]); |
---|
157 | FreeL(s[i*MATCOLS(m)]); |
---|
158 | for(j=1;j<MATCOLS(m);j++) |
---|
159 | { |
---|
160 | if (k+l[j]>colmax) |
---|
161 | { |
---|
162 | PrintS("\n "); |
---|
163 | k=2; |
---|
164 | } |
---|
165 | k+=l[j]; |
---|
166 | Print("%-*.*s",l[j],l[j],s[i*MATCOLS(m)+j]); |
---|
167 | FreeL(s[i*MATCOLS(m)+j]); |
---|
168 | } |
---|
169 | PrintLn(); |
---|
170 | } |
---|
171 | /* clean up */ |
---|
172 | Free((ADDRESS)s,MATCOLS(m)*MATROWS(m)*sizeof(char*)); |
---|
173 | Free((ADDRESS)l,MATCOLS(m)*sizeof(int)); |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | /*2 |
---|
178 | * print for: matrix |
---|
179 | */ |
---|
180 | static BOOLEAN ipPrint_MA(leftv u) |
---|
181 | { |
---|
182 | matrix m=(matrix)u->Data(); |
---|
183 | ipPrint_MA0(m,u->Fullname()); |
---|
184 | return FALSE; |
---|
185 | } |
---|
186 | |
---|
187 | /*2 |
---|
188 | * print for: vector |
---|
189 | */ |
---|
190 | static BOOLEAN ipPrint_V(leftv u) |
---|
191 | { |
---|
192 | polyset m=NULL; |
---|
193 | int l,j; |
---|
194 | /*convert into an array of the components*/ |
---|
195 | pVec2Polys((poly)u->Data(), &m, &l); |
---|
196 | /*output*/ |
---|
197 | PrintS("["); |
---|
198 | j=0; |
---|
199 | loop |
---|
200 | { |
---|
201 | PrintS(pString(m[j])); |
---|
202 | j++; |
---|
203 | if (j<l) PrintS(","); |
---|
204 | else |
---|
205 | { |
---|
206 | PrintS("]\n"); |
---|
207 | break; |
---|
208 | } |
---|
209 | } |
---|
210 | /* clean up */ |
---|
211 | for(j=l-1;j>=0;j--) pDelete(&m[j]); |
---|
212 | Free((ADDRESS)m,l*sizeof(poly)); |
---|
213 | return FALSE; |
---|
214 | } |
---|
215 | |
---|
216 | BOOLEAN jjPRINT(leftv res, leftv u) |
---|
217 | { |
---|
218 | switch(u->Typ()) |
---|
219 | { |
---|
220 | case INTVEC_CMD: |
---|
221 | return ipPrint_INTVEC(u); |
---|
222 | |
---|
223 | case INTMAT_CMD: |
---|
224 | return ipPrint_INTMAT(u); |
---|
225 | |
---|
226 | case MATRIX_CMD: |
---|
227 | return ipPrint_MA(u); |
---|
228 | |
---|
229 | case IDEAL_CMD: |
---|
230 | { |
---|
231 | char* s = u->String(NULL, FALSE, 2); |
---|
232 | PrintS(s); |
---|
233 | PrintLn(); |
---|
234 | FreeL(s); |
---|
235 | return FALSE; |
---|
236 | } |
---|
237 | |
---|
238 | case MODUL_CMD: |
---|
239 | { |
---|
240 | matrix m = idModule2Matrix(idCopy((ideal) u->Data())); |
---|
241 | ipPrint_MA0(m, u->Fullname()); |
---|
242 | idDelete((ideal *) &m); |
---|
243 | return FALSE; |
---|
244 | } |
---|
245 | |
---|
246 | case VECTOR_CMD: |
---|
247 | return ipPrint_V(u); |
---|
248 | |
---|
249 | default: |
---|
250 | u->Print(); |
---|
251 | return FALSE; |
---|
252 | } |
---|
253 | } |
---|
254 | |
---|
255 | |
---|
256 | /*2 |
---|
257 | * dbprint |
---|
258 | */ |
---|
259 | BOOLEAN jjDBPRINT(leftv res, leftv u) |
---|
260 | { |
---|
261 | BOOLEAN print=(printlevel>myynest); |
---|
262 | if ((u->next!=NULL)&&(u->Typ()==INT_CMD)) |
---|
263 | { |
---|
264 | print=((int)u->Data()>0); |
---|
265 | u=u->next; |
---|
266 | } |
---|
267 | if (print) |
---|
268 | { |
---|
269 | BOOLEAN r=FALSE; |
---|
270 | leftv h=u; |
---|
271 | leftv hh; |
---|
272 | while (h!=NULL) |
---|
273 | { |
---|
274 | hh=h->next; |
---|
275 | h->next=NULL; |
---|
276 | if (jjPRINT(res, h)) return TRUE; |
---|
277 | h->next=hh; |
---|
278 | h=hh; |
---|
279 | } |
---|
280 | } |
---|
281 | return FALSE; |
---|
282 | } |
---|
283 | |
---|
284 | static void ipPrintBetti(leftv u) |
---|
285 | { |
---|
286 | int i,j; |
---|
287 | intvec * betti=(intvec *)u->Data(); |
---|
288 | // head line -------------------------------------------------------- |
---|
289 | PrintS(" "); // 6 spaces for no. and : |
---|
290 | for(j=0;j<betti->cols();j++) Print(" %5d",j); // 6 spaces pro column |
---|
291 | PrintS("\n------"); // 6 spaces for no. and : |
---|
292 | for(j=0;j<betti->cols();j++) PrintS("------"); // 6 spaces pro column |
---|
293 | PrintLn(); |
---|
294 | // the table -------------------------------------------------------- |
---|
295 | for(i=0;i<betti->rows();i++) |
---|
296 | { |
---|
297 | Print("%5d:",i); |
---|
298 | for(j=1;j<=betti->cols();j++) |
---|
299 | { |
---|
300 | Print(" %5d",IMATELEM(*betti,i+1,j)); |
---|
301 | } |
---|
302 | PrintLn(); |
---|
303 | } |
---|
304 | // sum -------------------------------------------------------------- |
---|
305 | PrintS("------"); // 6 spaces for no. and : |
---|
306 | for(j=0;j<betti->cols();j++) PrintS("------"); // 6 spaces pro column |
---|
307 | PrintS("\ntotal:"); |
---|
308 | for(j=0;j<betti->cols();j++) |
---|
309 | { |
---|
310 | int s=0; |
---|
311 | for(i=0;i<betti->rows();i++) |
---|
312 | { |
---|
313 | s+=IMATELEM(*betti,i+1,j+1); |
---|
314 | } |
---|
315 | Print(" %5d",s); // 6 spaces pro column |
---|
316 | } |
---|
317 | PrintLn(); |
---|
318 | } |
---|
319 | |
---|
320 | |
---|
321 | /*2 |
---|
322 | * print(...,"format") |
---|
323 | */ |
---|
324 | BOOLEAN jjPRINT_FORMAT(leftv res, leftv u, leftv v) |
---|
325 | { |
---|
326 | /* ==================== betti ======================================== */ |
---|
327 | if ((u->Typ()==INTMAT_CMD)&&(strcmp((char *)v->Data(),"betti")==0)) |
---|
328 | { |
---|
329 | ipPrintBetti(u); |
---|
330 | res->data = NULL; |
---|
331 | res->rtyp = NONE; |
---|
332 | return FALSE; |
---|
333 | } |
---|
334 | /* ======================== end betti ================================= */ |
---|
335 | |
---|
336 | char* ns = mstrdup((char*) v->Data()); |
---|
337 | int dim = 1; |
---|
338 | if (strlen(ns) == 3 && ns[1] == '2') |
---|
339 | { |
---|
340 | dim = 2; |
---|
341 | ns[1] = ns[2]; |
---|
342 | ns[2] = '\0'; |
---|
343 | } |
---|
344 | if (strcmp(ns,"%l") == 0) |
---|
345 | { |
---|
346 | res->data = (char*) u->String(NULL, TRUE, dim); |
---|
347 | } |
---|
348 | else if (strcmp(ns,"%t") == 0) |
---|
349 | { |
---|
350 | SPrintStart(); |
---|
351 | type_cmd((idhdl) u); |
---|
352 | res->data = SPrintEnd(); |
---|
353 | } |
---|
354 | else if (strcmp(ns,"%;") == 0) |
---|
355 | { |
---|
356 | SPrintStart(); |
---|
357 | u->Print(); |
---|
358 | res->data = SPrintEnd(); |
---|
359 | } |
---|
360 | else if (strcmp(ns,"%p") == 0) |
---|
361 | { |
---|
362 | SPrintStart(); |
---|
363 | iiExprArith1(res, u, PRINT_CMD); |
---|
364 | res->data = SPrintEnd(); |
---|
365 | } |
---|
366 | else if (strcmp(ns,"%b") == 0 && (u->Typ()==INTMAT_CMD)) |
---|
367 | { |
---|
368 | SPrintStart(); |
---|
369 | ipPrintBetti(u); |
---|
370 | res->data = SPrintEnd(); |
---|
371 | } |
---|
372 | else |
---|
373 | { |
---|
374 | res->data = u->String(NULL, FALSE, dim); |
---|
375 | } |
---|
376 | |
---|
377 | FreeL(ns); |
---|
378 | res->rtyp = STRING_CMD; |
---|
379 | return FALSE; |
---|
380 | } |
---|
381 | |
---|
382 | |
---|
383 | |
---|
384 | |
---|
385 | |
---|
386 | |
---|
387 | |
---|
388 | |
---|
389 | |
---|
390 | |
---|
391 | |
---|
392 | |
---|
393 | |
---|
394 | |
---|