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