source: git/Singular/ipprint.cc @ 93c2b4

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