1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id: ipprint.cc,v 1.8 1998-11-16 08:41:17 Singular 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 | |
---|
21 | /*2 |
---|
22 | * print for: int, string, poly, vector, ideal |
---|
23 | */ |
---|
24 | BOOLEAN jjPRINT_GEN(leftv res, leftv u) |
---|
25 | { |
---|
26 | char *s=u->String(); |
---|
27 | if (s==NULL) return TRUE; |
---|
28 | PrintS(s); |
---|
29 | PrintLn(); |
---|
30 | FreeL((ADDRESS)s); |
---|
31 | return FALSE; |
---|
32 | } |
---|
33 | |
---|
34 | /*2 |
---|
35 | * print for: list |
---|
36 | */ |
---|
37 | BOOLEAN jjPRINT_LIST(leftv res, leftv u) |
---|
38 | { |
---|
39 | u->Print(); |
---|
40 | return FALSE; |
---|
41 | } |
---|
42 | |
---|
43 | /*2 |
---|
44 | * print for: intvec |
---|
45 | */ |
---|
46 | BOOLEAN jjPRINT_INTVEC(leftv res, leftv u) |
---|
47 | { |
---|
48 | intvec *v=(intvec *)u->Data(); |
---|
49 | v->show(); |
---|
50 | PrintLn(); |
---|
51 | return FALSE; |
---|
52 | } |
---|
53 | |
---|
54 | /*2 |
---|
55 | * print for: intmat |
---|
56 | */ |
---|
57 | BOOLEAN jjPRINT_INTMAT(leftv res, leftv u) |
---|
58 | { |
---|
59 | intvec *v=(intvec *)u->Data(); |
---|
60 | int i,j; |
---|
61 | for(i=0;i<v->rows();i++) |
---|
62 | { |
---|
63 | for(j=0;j<v->cols();j++) |
---|
64 | { |
---|
65 | Print(" %5d",IMATELEM(*v,i+1,j+1)); |
---|
66 | } |
---|
67 | PrintLn(); |
---|
68 | } |
---|
69 | return FALSE; |
---|
70 | } |
---|
71 | |
---|
72 | /*2 |
---|
73 | * internal print for: matrix |
---|
74 | */ |
---|
75 | void jjPRINT_MA0(matrix m, const char *name) |
---|
76 | { |
---|
77 | if (MATCOLS(m)>0) |
---|
78 | { |
---|
79 | char **s=(char **)Alloc(MATCOLS(m)*MATROWS(m)*sizeof(char*)); |
---|
80 | char *ss; |
---|
81 | int *l=(int *)Alloc0(MATCOLS(m)*sizeof(int)); |
---|
82 | int i,j,k; |
---|
83 | int vl=max(colmax/MATCOLS(m),8); |
---|
84 | |
---|
85 | /* make enough space for the "largest" name*/ |
---|
86 | ss=(char *)AllocL(14+strlen(name)); |
---|
87 | sprintf(ss,"%s[%d,%d]",name,MATCOLS(m),MATROWS(m)); |
---|
88 | vl=max(vl,strlen(ss)); |
---|
89 | FreeL(ss); |
---|
90 | |
---|
91 | /* convert all polys to string */ |
---|
92 | i=MATCOLS(m)*MATROWS(m)-1; |
---|
93 | ss=pString(m->m[i]); |
---|
94 | if ((int)strlen(ss)>colmax) s[i]=NULL; |
---|
95 | else s[i]=mstrdup(ss); |
---|
96 | for(i--;i>=0;i--) |
---|
97 | { |
---|
98 | pString(m->m[i]); |
---|
99 | ss=StringAppend(","); |
---|
100 | if ((int)strlen(ss)>colmax) s[i]=NULL; |
---|
101 | else s[i]=mstrdup(ss); |
---|
102 | } |
---|
103 | /* look up the width of all columns, put it in l[col_nr] */ |
---|
104 | /* insert names for very long entries */ |
---|
105 | for(i=MATROWS(m)-1;i>=0;i--) |
---|
106 | { |
---|
107 | for(j=MATCOLS(m)-1;j>=0;j--) |
---|
108 | { |
---|
109 | if (s[i*MATCOLS(m)+j]==NULL) |
---|
110 | { |
---|
111 | ss=(char *)AllocL(14+strlen(name)); |
---|
112 | s[i*MATCOLS(m)+j]=ss; |
---|
113 | ss[0]='\0'; |
---|
114 | sprintf(ss,"%s[%d,%d]",name,i+1,j+1); |
---|
115 | if ((i!=MATROWS(m)-1) || (j!=MATCOLS(m)-1)) |
---|
116 | { |
---|
117 | strcat(ss,","); |
---|
118 | vl=max(vl,strlen(ss)); |
---|
119 | } |
---|
120 | } |
---|
121 | k=strlen(s[i*MATCOLS(m)+j]); |
---|
122 | if (k>l[j]) l[j]=k; |
---|
123 | } |
---|
124 | } |
---|
125 | /* does it fit on a line ? */ |
---|
126 | int maxlen=0; |
---|
127 | for(j=MATCOLS(m)-1;j>=0;j--) |
---|
128 | { |
---|
129 | maxlen+=l[j]; |
---|
130 | } |
---|
131 | if (maxlen>colmax) |
---|
132 | { |
---|
133 | /* NO, it does not fit, so retry: */ |
---|
134 | /* look up the width of all columns, clear very long entriess */ |
---|
135 | /* put length in l[col_nr] */ |
---|
136 | /* insert names for cleared entries */ |
---|
137 | for(j=MATCOLS(m)-1;j>=0;j--) |
---|
138 | { |
---|
139 | for(i=MATROWS(m)-1;i>=0;i--) |
---|
140 | { |
---|
141 | k=strlen(s[i*MATCOLS(m)+j]); |
---|
142 | if (/*strlen(s[i*MATCOLS(m)+j])*/ k > vl) |
---|
143 | { |
---|
144 | FreeL((ADDRESS)s[i*MATCOLS(m)+j]); |
---|
145 | ss=(char *)AllocL(14+strlen(name)); |
---|
146 | s[i*MATCOLS(m)+j]=ss; |
---|
147 | ss[0]='\0'; |
---|
148 | sprintf(ss,"%s[%d,%d]",name,i+1,j+1); |
---|
149 | if ((i!=MATROWS(m)-1) || (j!=MATCOLS(m)-1)) |
---|
150 | { |
---|
151 | strcat(ss,","); |
---|
152 | } |
---|
153 | l[j]=strlen(s[i*MATCOLS(m)+j]); |
---|
154 | if (l[j]>vl) |
---|
155 | { |
---|
156 | #ifdef TEST |
---|
157 | PrintS("pagewidth too small in print(matrix)\n"); |
---|
158 | #endif |
---|
159 | vl=l[j]; /* make large names fit*/ |
---|
160 | } |
---|
161 | i=MATROWS(m); |
---|
162 | } |
---|
163 | else |
---|
164 | { |
---|
165 | if (k>l[j]) l[j]=k; |
---|
166 | } |
---|
167 | } |
---|
168 | } |
---|
169 | } |
---|
170 | /*output of the matrix*/ |
---|
171 | for(i=0;i<MATROWS(m);i++) |
---|
172 | { |
---|
173 | k=l[0]; |
---|
174 | Print("%-*.*s",l[0],l[0],s[i*MATCOLS(m)]); |
---|
175 | FreeL(s[i*MATCOLS(m)]); |
---|
176 | for(j=1;j<MATCOLS(m);j++) |
---|
177 | { |
---|
178 | if (k+l[j]>colmax) |
---|
179 | { |
---|
180 | PrintS("\n "); |
---|
181 | k=2; |
---|
182 | } |
---|
183 | k+=l[j]; |
---|
184 | Print("%-*.*s",l[j],l[j],s[i*MATCOLS(m)+j]); |
---|
185 | FreeL(s[i*MATCOLS(m)+j]); |
---|
186 | } |
---|
187 | PrintLn(); |
---|
188 | } |
---|
189 | /* clean up */ |
---|
190 | Free((ADDRESS)s,MATCOLS(m)*MATROWS(m)*sizeof(char*)); |
---|
191 | Free((ADDRESS)l,MATCOLS(m)*sizeof(int)); |
---|
192 | } |
---|
193 | } |
---|
194 | |
---|
195 | /*2 |
---|
196 | * print for: matrix |
---|
197 | */ |
---|
198 | BOOLEAN jjPRINT_MA(leftv res, leftv u) |
---|
199 | { |
---|
200 | matrix m=(matrix)u->Data(); |
---|
201 | jjPRINT_MA0(m,u->Fullname()); |
---|
202 | return FALSE; |
---|
203 | } |
---|
204 | |
---|
205 | /*2 |
---|
206 | * print for: vector |
---|
207 | */ |
---|
208 | BOOLEAN jjPRINT_V(leftv res, leftv u) |
---|
209 | { |
---|
210 | polyset m=NULL; |
---|
211 | int l,j; |
---|
212 | /*convert into an array of the components*/ |
---|
213 | pVec2Polys((poly)u->Data(), &m, &l); |
---|
214 | /*output*/ |
---|
215 | PrintS("["); |
---|
216 | j=0; |
---|
217 | loop |
---|
218 | { |
---|
219 | PrintS(pString(m[j])); |
---|
220 | j++; |
---|
221 | if (j<l) PrintS(","); |
---|
222 | else |
---|
223 | { |
---|
224 | PrintS("]\n"); |
---|
225 | break; |
---|
226 | } |
---|
227 | } |
---|
228 | /* clean up */ |
---|
229 | for(j=l-1;j>=0;j--) pDelete(&m[j]); |
---|
230 | Free((ADDRESS)m,l*sizeof(poly)); |
---|
231 | return FALSE; |
---|
232 | } |
---|
233 | |
---|
234 | /*2 |
---|
235 | * dbprint |
---|
236 | */ |
---|
237 | BOOLEAN jjDBPRINT(leftv res, leftv u) |
---|
238 | { |
---|
239 | BOOLEAN print=(printlevel>myynest); |
---|
240 | if ((u->next!=NULL)&&(u->Typ()==INT_CMD)) |
---|
241 | { |
---|
242 | print=((int)u->Data()>0); |
---|
243 | u=u->next; |
---|
244 | } |
---|
245 | if (print) |
---|
246 | { |
---|
247 | BOOLEAN r=FALSE; |
---|
248 | leftv h=u; |
---|
249 | leftv hh; |
---|
250 | while (h!=NULL) |
---|
251 | { |
---|
252 | hh=h->next; |
---|
253 | h->next=NULL; |
---|
254 | if (iiExprArith1(res,h,PRINT_CMD)) return TRUE; |
---|
255 | h->next=hh; |
---|
256 | h=hh; |
---|
257 | } |
---|
258 | } |
---|
259 | return FALSE; |
---|
260 | } |
---|
261 | |
---|
262 | /*2 |
---|
263 | * print(...,"format") |
---|
264 | */ |
---|
265 | BOOLEAN jjPRINT_FORMAT(leftv res, leftv u, leftv v) |
---|
266 | { |
---|
267 | /* ==================== betti ======================================== */ |
---|
268 | if ((u->Typ()==INTMAT_CMD)&&(strcmp((char *)v->Data(),"betti")==0)) |
---|
269 | { |
---|
270 | int i,j; |
---|
271 | intvec * betti=(intvec *)u->Data(); |
---|
272 | // head line -------------------------------------------------------- |
---|
273 | PrintS(" "); // 6 spaces for no. and : |
---|
274 | for(j=0;j<betti->cols();j++) Print(" %5d",j); // 6 spaces pro column |
---|
275 | PrintS("\n------"); // 6 spaces for no. and : |
---|
276 | for(j=0;j<betti->cols();j++) PrintS("------"); // 6 spaces pro column |
---|
277 | PrintLn(); |
---|
278 | // the table -------------------------------------------------------- |
---|
279 | for(i=0;i<betti->rows();i++) |
---|
280 | { |
---|
281 | Print("%5d:",i); |
---|
282 | for(j=1;j<=betti->cols();j++) |
---|
283 | { |
---|
284 | Print(" %5d",IMATELEM(*betti,i+1,j)); |
---|
285 | } |
---|
286 | PrintLn(); |
---|
287 | } |
---|
288 | // sum -------------------------------------------------------------- |
---|
289 | PrintS("------"); // 6 spaces for no. and : |
---|
290 | for(j=0;j<betti->cols();j++) PrintS("------"); // 6 spaces pro column |
---|
291 | PrintS("\ntotal:"); |
---|
292 | for(j=0;j<betti->cols();j++) |
---|
293 | { |
---|
294 | int s=0; |
---|
295 | for(i=0;i<betti->rows();i++) |
---|
296 | { |
---|
297 | s+=IMATELEM(*betti,i+1,j+1); |
---|
298 | } |
---|
299 | Print(" %5d",s); // 6 spaces pro column |
---|
300 | } |
---|
301 | PrintLn(); |
---|
302 | return FALSE; |
---|
303 | } |
---|
304 | /* ======================== end betti ================================= */ |
---|
305 | |
---|
306 | return jjPRINT_GEN(res,u); |
---|
307 | } |
---|