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