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