[0e1846] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
| 4 | /* |
---|
[e2f1c7] | 5 | * ABSTRACT: interpreter: printing |
---|
[0e1846] | 6 | */ |
---|
| 7 | |
---|
[16f511] | 8 | #ifdef HAVE_CONFIG_H |
---|
[ba5e9e] | 9 | #include "singularconfig.h" |
---|
[16f511] | 10 | #endif /* HAVE_CONFIG_H */ |
---|
[b1dfaf] | 11 | #include <kernel/mod2.h> |
---|
| 12 | #include <omalloc/omalloc.h> |
---|
[fb85f97] | 13 | |
---|
| 14 | #include <misc/intvec.h> |
---|
| 15 | |
---|
| 16 | #include <polys/matpol.h> |
---|
| 17 | |
---|
[599326] | 18 | #include <kernel/febase.h> |
---|
[737a68] | 19 | #include <kernel/polys.h> |
---|
[599326] | 20 | #include <kernel/ideals.h> |
---|
[fb85f97] | 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" |
---|
[0e1846] | 28 | |
---|
| 29 | /*2 |
---|
| 30 | * print for: int, string, poly, vector, ideal |
---|
| 31 | */ |
---|
| 32 | /*2 |
---|
| 33 | * print for: intvec |
---|
| 34 | */ |
---|
[a79a128] | 35 | static BOOLEAN ipPrint_INTVEC(leftv u) |
---|
[0e1846] | 36 | { |
---|
| 37 | intvec *v=(intvec *)u->Data(); |
---|
| 38 | v->show(); |
---|
| 39 | PrintLn(); |
---|
| 40 | return FALSE; |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | /*2 |
---|
| 44 | * print for: intmat |
---|
| 45 | */ |
---|
[a79a128] | 46 | static BOOLEAN ipPrint_INTMAT(leftv u) |
---|
[0e1846] | 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 | */ |
---|
[a79a128] | 64 | static void ipPrint_MA0(matrix m, const char *name) |
---|
[0e1846] | 65 | { |
---|
| 66 | if (MATCOLS(m)>0) |
---|
| 67 | { |
---|
[c232af] | 68 | char **s=(char **)omAlloc(MATCOLS(m)*MATROWS(m)*sizeof(char*)); |
---|
[0e1846] | 69 | char *ss; |
---|
[c232af] | 70 | int *l=(int *)omAlloc0(MATCOLS(m)*sizeof(int)); |
---|
[0e1846] | 71 | int i,j,k; |
---|
[f43a74] | 72 | int vl=si_max(colmax/MATCOLS(m),8); |
---|
[0e1846] | 73 | |
---|
| 74 | /* make enough space for the "largest" name*/ |
---|
[c232af] | 75 | ss=(char *)omAlloc(14+strlen(name)); |
---|
[0e1846] | 76 | sprintf(ss,"%s[%d,%d]",name,MATCOLS(m),MATROWS(m)); |
---|
[32c80f] | 77 | vl=si_max(vl,(int)strlen(ss)); |
---|
[c232af] | 78 | omFree(ss); |
---|
[0e1846] | 79 | |
---|
| 80 | /* convert all polys to string */ |
---|
| 81 | i=MATCOLS(m)*MATROWS(m)-1; |
---|
| 82 | ss=pString(m->m[i]); |
---|
[538512] | 83 | if ((int)strlen(ss)>colmax) { s[i]=NULL; omFree(ss); } |
---|
| 84 | else s[i]=ss; |
---|
[0e1846] | 85 | for(i--;i>=0;i--) |
---|
| 86 | { |
---|
[538512] | 87 | StringSetS(""); |
---|
| 88 | pString0(m->m[i]); |
---|
| 89 | StringAppendS(","); |
---|
| 90 | ss=StringEndS(); |
---|
[ef304d] | 91 | if ((int)strlen(ss)>colmax) s[i]=NULL; |
---|
[538512] | 92 | else s[i]=ss; |
---|
[0e1846] | 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 | { |
---|
[c232af] | 102 | ss=(char *)omAlloc(14+strlen(name)); |
---|
[0e1846] | 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,","); |
---|
[32c80f] | 109 | vl=si_max(vl,(int)strlen(ss)); |
---|
[0e1846] | 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 | { |
---|
[c232af] | 135 | omFree((ADDRESS)s[i*MATCOLS(m)+j]); |
---|
| 136 | ss=(char *)omAlloc(14+strlen(name)); |
---|
[0e1846] | 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 | { |
---|
[1fb897] | 147 | //#ifdef TEST |
---|
| 148 | // PrintS("pagewidth too small in print(matrix)\n"); |
---|
| 149 | //#endif |
---|
[0e1846] | 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)]); |
---|
[c232af] | 166 | omFree(s[i*MATCOLS(m)]); |
---|
[0e1846] | 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]); |
---|
[c232af] | 176 | omFree(s[i*MATCOLS(m)+j]); |
---|
[0e1846] | 177 | } |
---|
| 178 | PrintLn(); |
---|
| 179 | } |
---|
| 180 | /* clean up */ |
---|
[c232af] | 181 | omFreeSize((ADDRESS)s,MATCOLS(m)*MATROWS(m)*sizeof(char*)); |
---|
| 182 | omFreeSize((ADDRESS)l,MATCOLS(m)*sizeof(int)); |
---|
[0e1846] | 183 | } |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | /*2 |
---|
| 187 | * print for: matrix |
---|
| 188 | */ |
---|
[a79a128] | 189 | static BOOLEAN ipPrint_MA(leftv u) |
---|
[0e1846] | 190 | { |
---|
| 191 | matrix m=(matrix)u->Data(); |
---|
[43dfb6] | 192 | ipPrint_MA0(m,u->Name()); |
---|
[0e1846] | 193 | return FALSE; |
---|
| 194 | } |
---|
| 195 | |
---|
[fb85f97] | 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 | |
---|
[0e1846] | 206 | /*2 |
---|
| 207 | * print for: vector |
---|
| 208 | */ |
---|
[a79a128] | 209 | static BOOLEAN ipPrint_V(leftv u) |
---|
[0e1846] | 210 | { |
---|
| 211 | polyset m=NULL; |
---|
| 212 | int l,j; |
---|
| 213 | /*convert into an array of the components*/ |
---|
[d02fff] | 214 | p_Vec2Polys((poly)u->Data(), &m, &l, currRing); |
---|
[0e1846] | 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]); |
---|
[c232af] | 231 | omFreeSize((ADDRESS)m,l*sizeof(poly)); |
---|
[0e1846] | 232 | return FALSE; |
---|
| 233 | } |
---|
| 234 | |
---|
[a79a128] | 235 | BOOLEAN jjPRINT(leftv res, leftv u) |
---|
| 236 | { |
---|
[000098] | 237 | SPrintStart(); |
---|
| 238 | BOOLEAN bo=FALSE; |
---|
[a79a128] | 239 | switch(u->Typ()) |
---|
| 240 | { |
---|
| 241 | case INTVEC_CMD: |
---|
[000098] | 242 | bo=ipPrint_INTVEC(u); |
---|
[891122] | 243 | break; |
---|
[d2ac76] | 244 | |
---|
[a79a128] | 245 | case INTMAT_CMD: |
---|
[000098] | 246 | bo=ipPrint_INTMAT(u); |
---|
[891122] | 247 | break; |
---|
[d2ac76] | 248 | |
---|
[a79a128] | 249 | case MATRIX_CMD: |
---|
[000098] | 250 | bo=ipPrint_MA(u); |
---|
[891122] | 251 | break; |
---|
[a79a128] | 252 | |
---|
| 253 | case IDEAL_CMD: |
---|
| 254 | { |
---|
[7604db] | 255 | char* s = u->String(NULL, FALSE, 2); |
---|
[a79a128] | 256 | PrintS(s); |
---|
| 257 | PrintLn(); |
---|
[c232af] | 258 | omFree(s); |
---|
[000098] | 259 | break; |
---|
[a79a128] | 260 | } |
---|
[d2ac76] | 261 | |
---|
[a79a128] | 262 | case MODUL_CMD: |
---|
| 263 | { |
---|
[39457c] | 264 | matrix m = id_Module2Matrix(id_Copy((ideal) u->Data(),currRing),currRing); |
---|
[43dfb6] | 265 | ipPrint_MA0(m, u->Name()); |
---|
[39457c] | 266 | id_Delete((ideal *) &m,currRing); |
---|
[000098] | 267 | break; |
---|
[a79a128] | 268 | } |
---|
[d2ac76] | 269 | |
---|
[a79a128] | 270 | case VECTOR_CMD: |
---|
[000098] | 271 | bo=ipPrint_V(u); |
---|
[891122] | 272 | break; |
---|
[d2ac76] | 273 | |
---|
[fb85f97] | 274 | case RING_CMD: |
---|
| 275 | case QRING_CMD: |
---|
| 276 | bo=ipPrint_RING(u); |
---|
[891122] | 277 | break; |
---|
[fb85f97] | 278 | |
---|
[a79a128] | 279 | default: |
---|
| 280 | u->Print(); |
---|
[000098] | 281 | break; |
---|
[a79a128] | 282 | } |
---|
[000098] | 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; |
---|
[a79a128] | 291 | } |
---|
| 292 | |
---|
| 293 | |
---|
[0e1846] | 294 | /*2 |
---|
| 295 | * dbprint |
---|
| 296 | */ |
---|
| 297 | BOOLEAN jjDBPRINT(leftv res, leftv u) |
---|
| 298 | { |
---|
[04310f8] | 299 | BOOLEAN print=(printlevel>myynest); |
---|
[0e1846] | 300 | if ((u->next!=NULL)&&(u->Typ()==INT_CMD)) |
---|
| 301 | { |
---|
[04310f8] | 302 | print= (((int)((long)(u->Data()))) > 0); |
---|
[0e1846] | 303 | u=u->next; |
---|
| 304 | } |
---|
| 305 | if (print) |
---|
| 306 | { |
---|
[6909cfb] | 307 | // BOOLEAN r=FALSE; |
---|
[0e1846] | 308 | leftv h=u; |
---|
| 309 | leftv hh; |
---|
| 310 | while (h!=NULL) |
---|
| 311 | { |
---|
| 312 | hh=h->next; |
---|
| 313 | h->next=NULL; |
---|
[a79a128] | 314 | if (jjPRINT(res, h)) return TRUE; |
---|
[000098] | 315 | PrintS((char*)res->data); |
---|
| 316 | omFree(res->data); |
---|
| 317 | PrintLn(); |
---|
[0e1846] | 318 | h->next=hh; |
---|
| 319 | h=hh; |
---|
| 320 | } |
---|
| 321 | } |
---|
| 322 | return FALSE; |
---|
| 323 | } |
---|
| 324 | |
---|
[a79a128] | 325 | static void ipPrintBetti(leftv u) |
---|
| 326 | { |
---|
| 327 | int i,j; |
---|
[7447d8] | 328 | int row_shift=(int)((long)(atGet(u,"rowShift",INT_CMD))); |
---|
[a79a128] | 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 | { |
---|
[ba774b] | 339 | Print("%5d:",i+row_shift); |
---|
[a79a128] | 340 | for(j=1;j<=betti->cols();j++) |
---|
| 341 | { |
---|
[d2ac76] | 342 | int m=IMATELEM(*betti,i+1,j); |
---|
| 343 | if (m==0) |
---|
| 344 | PrintS(" -"); |
---|
| 345 | else |
---|
| 346 | Print(" %5d",m); |
---|
[a79a128] | 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 | |
---|
[0e1846] | 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 | { |
---|
[000098] | 375 | SPrintStart(); |
---|
[a79a128] | 376 | ipPrintBetti(u); |
---|
[000098] | 377 | char *s = SPrintEnd(); |
---|
| 378 | s[strlen(s)]='\0'; |
---|
| 379 | res->data=s; |
---|
[0e1846] | 380 | } |
---|
[000098] | 381 | else |
---|
[0e1846] | 382 | /* ======================== end betti ================================= */ |
---|
[7604db] | 383 | { |
---|
[000098] | 384 | char* ns = omStrDup((char*) v->Data()); |
---|
| 385 | int dim = 1; |
---|
| 386 | if (strlen(ns) == 3 && ns[1] == '2') |
---|
[8c8bea] | 387 | { |
---|
[000098] | 388 | dim = 2; |
---|
| 389 | ns[1] = ns[2]; |
---|
| 390 | ns[2] = '\0'; |
---|
[8c8bea] | 391 | } |
---|
[000098] | 392 | if (strcmp(ns,"%l") == 0) |
---|
[8c8bea] | 393 | { |
---|
[000098] | 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(); |
---|
[8c8bea] | 418 | } |
---|
[000098] | 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); |
---|
[a79a128] | 443 | } |
---|
| 444 | return FALSE; |
---|
[0e1846] | 445 | } |
---|