source: git/Singular/ipprint.cc @ 737a68

spielwiese
Last change on this file since 737a68 was 737a68, checked in by Oleksandr Motsak <motsak@…>, 13 years ago
CHG: moved libpolys/polys/polys.h to kernel/polys.h & updated includes ADD: moved (definition of) currRing/rChangeCurrRing to kernel/polys.cc!?
  • Property mode set to 100644
File size: 9.1 KB
Line 
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 <polys/matpol.h>
16#include <Singular/subexpr.h>
17#include <misc/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*/
29static 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*/
40static 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*/
58static 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*/
181static 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*/
191static 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
217BOOLEAN jjPRINT(leftv res, leftv u)
218{
219  SPrintStart();
220  BOOLEAN bo=FALSE;
221  switch(u->Typ())
222  {
223      case INTVEC_CMD:
224        bo=ipPrint_INTVEC(u);
225        break;
226
227      case INTMAT_CMD:
228        bo=ipPrint_INTMAT(u);
229        break;
230
231      case MATRIX_CMD:
232        bo=ipPrint_MA(u);
233        break;
234
235      case IDEAL_CMD:
236      {
237        char* s = u->String(NULL, FALSE, 2);
238        PrintS(s);
239        PrintLn();
240        omFree(s);
241        break;
242      }
243
244      case MODUL_CMD:
245      {
246        matrix m = idModule2Matrix(idCopy((ideal) u->Data()));
247        ipPrint_MA0(m, u->Name());
248        idDelete((ideal *) &m);
249        break;
250      }
251
252      case VECTOR_CMD:
253        bo=ipPrint_V(u);
254        break;
255
256      default:
257        u->Print();
258        break;
259  }
260  char *s=SPrintEnd();
261  if (u->next==NULL)
262  {
263    int l=strlen(s);
264    if (s[l-1]=='\n') s[l-1]='\0';
265  }
266  res->data=(void*)s;
267  return bo;
268}
269
270
271/*2
272* dbprint
273*/
274BOOLEAN jjDBPRINT(leftv res, leftv u)
275{
276  BOOLEAN print=(printlevel>myynest);
277  if ((u->next!=NULL)&&(u->Typ()==INT_CMD))
278  {
279    print=  (((int)((long)(u->Data()))) > 0);
280    u=u->next;
281  }
282  if (print)
283  {
284    BOOLEAN r=FALSE;
285    leftv h=u;
286    leftv hh;
287    while (h!=NULL)
288    {
289      hh=h->next;
290      h->next=NULL;
291      if (jjPRINT(res, h)) return TRUE;
292      PrintS((char*)res->data);
293      omFree(res->data);
294      PrintLn();
295      h->next=hh;
296      h=hh;
297    }
298  }
299  return FALSE;
300}
301
302static void ipPrintBetti(leftv u)
303{
304  int i,j;
305  int row_shift=(int)((long)(atGet(u,"rowShift",INT_CMD)));
306  intvec * betti=(intvec *)u->Data();
307  // head line --------------------------------------------------------
308  PrintS("      "); // 6 spaces for no. and :
309  for(j=0;j<betti->cols();j++) Print(" %5d",j); // 6 spaces pro column
310  PrintS("\n------"); // 6 spaces for no. and :
311  for(j=0;j<betti->cols();j++) PrintS("------"); // 6 spaces pro column
312  PrintLn();
313  // the table --------------------------------------------------------
314  for(i=0;i<betti->rows();i++)
315  {
316    Print("%5d:",i+row_shift);
317    for(j=1;j<=betti->cols();j++)
318    {
319      int m=IMATELEM(*betti,i+1,j);
320      if (m==0)
321        PrintS("     -");
322      else
323        Print(" %5d",m);
324    }
325    PrintLn();
326  }
327  // sum --------------------------------------------------------------
328  PrintS("------"); // 6 spaces for no. and :
329  for(j=0;j<betti->cols();j++) PrintS("------"); // 6 spaces pro column
330  PrintS("\ntotal:");
331  for(j=0;j<betti->cols();j++)
332  {
333    int s=0;
334    for(i=0;i<betti->rows();i++)
335    {
336      s+=IMATELEM(*betti,i+1,j+1);
337    }
338    Print(" %5d",s); // 6 spaces pro column
339  }
340  PrintLn();
341}
342
343
344/*2
345* print(...,"format")
346*/
347BOOLEAN jjPRINT_FORMAT(leftv res, leftv u, leftv v)
348{
349/* ==================== betti ======================================== */
350  if ((u->Typ()==INTMAT_CMD)&&(strcmp((char *)v->Data(),"betti")==0))
351  {
352    SPrintStart();
353    ipPrintBetti(u);
354    char *s = SPrintEnd();
355    s[strlen(s)]='\0';
356    res->data=s;
357  }
358  else
359/* ======================== end betti ================================= */
360  {
361    char* ns = omStrDup((char*) v->Data());
362    int dim = 1;
363    if (strlen(ns) == 3 && ns[1] == '2')
364    {
365      dim = 2;
366      ns[1] = ns[2];
367      ns[2] = '\0';
368    }
369    if (strcmp(ns,"%l") == 0)
370    {
371      res->data = (char*) u->String(NULL, TRUE, dim);
372      if (dim == 2)
373      {
374        char* ns = (char*) omAlloc(strlen((char*) res->data) + 2);
375        strcpy(ns, (char*) res->data);
376        omFree(res->data);
377        strcat(ns, "\n");
378        res->data = ns;
379      }
380    }
381    else if (strcmp(ns,"%t") == 0)
382    {
383      SPrintStart();
384      type_cmd(u);
385      res->data = SPrintEnd();
386      if (dim != 2)
387        ((char*)res->data)[strlen((char*)res->data) -1] = '\0';
388    }
389    else if (strcmp(ns,"%;") == 0)
390    {
391      SPrintStart();
392      u->Print();
393      if (dim == 2) PrintLn();
394      res->data = SPrintEnd();
395    }
396    else if  (strcmp(ns,"%p") == 0)
397    {
398      iiExprArith1(res, u, PRINT_CMD);
399    }
400    else if (strcmp(ns,"%b") == 0 && (u->Typ()==INTMAT_CMD))
401    {
402      SPrintStart();
403      ipPrintBetti(u);
404      if (dim == 2) PrintLn();
405      res->data = SPrintEnd();
406    }
407    else
408    {
409      res->data = u->String(NULL, FALSE, dim);
410      if (dim == 2)
411      {
412        char* ns = (char*) omAlloc(strlen((char*) res->data) + 2);
413        strcpy(ns, (char*) res->data);
414        omFree(res->data);
415        strcat(ns, "\n");
416        res->data = ns;
417      }
418    }
419    omFree(ns);
420  }
421  return FALSE;
422}
Note: See TracBrowser for help on using the repository browser.