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