source: git/Singular/ipprint.cc @ 000098

spielwiese
Last change on this file since 000098 was 000098, checked in by Hans Schoenemann <hannes@…>, 13 years ago
print returns a string git-svn-id: file:///usr/local/Singular/svn/trunk@14313 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 9.1 KB
RevLine 
[0e1846]1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
[341696]4/* $Id$ */
[0e1846]5/*
[e2f1c7]6* ABSTRACT: interpreter: printing
[0e1846]7*/
8
[b1dfaf]9#include <kernel/mod2.h>
[599326]10#include <Singular/tok.h>
11#include <Singular/ipid.h>
[b1dfaf]12#include <omalloc/omalloc.h>
[599326]13#include <kernel/febase.h>
14#include <kernel/polys.h>
15#include <kernel/matpol.h>
16#include <Singular/subexpr.h>
17#include <kernel/intvec.h>
18#include <Singular/ipshell.h>
19#include <Singular/ipprint.h>
20#include <kernel/ideals.h>
21#include <Singular/attrib.h>
[0e1846]22
23/*2
24* print for: int, string, poly, vector, ideal
25*/
26/*2
27* print for: intvec
28*/
[a79a128]29static BOOLEAN ipPrint_INTVEC(leftv u)
[0e1846]30{
31  intvec *v=(intvec *)u->Data();
32  v->show();
33  PrintLn();
34  return FALSE;
35}
36
37/*2
38* print for: intmat
39*/
[a79a128]40static BOOLEAN ipPrint_INTMAT(leftv u)
[0e1846]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*/
[a79a128]58static void ipPrint_MA0(matrix m, const char *name)
[0e1846]59{
60  if (MATCOLS(m)>0)
61  {
[c232af]62    char **s=(char **)omAlloc(MATCOLS(m)*MATROWS(m)*sizeof(char*));
[0e1846]63    char *ss;
[c232af]64    int *l=(int *)omAlloc0(MATCOLS(m)*sizeof(int));
[0e1846]65    int i,j,k;
[f43a74]66    int vl=si_max(colmax/MATCOLS(m),8);
[0e1846]67
68    /* make enough space for the "largest" name*/
[c232af]69    ss=(char *)omAlloc(14+strlen(name));
[0e1846]70    sprintf(ss,"%s[%d,%d]",name,MATCOLS(m),MATROWS(m));
[32c80f]71    vl=si_max(vl,(int)strlen(ss));
[c232af]72    omFree(ss);
[0e1846]73
74    /* convert all polys to string */
75    i=MATCOLS(m)*MATROWS(m)-1;
76    ss=pString(m->m[i]);
[ef304d]77    if ((int)strlen(ss)>colmax) s[i]=NULL;
[c232af]78    else                        s[i]=omStrDup(ss);
[0e1846]79    for(i--;i>=0;i--)
80    {
81      pString(m->m[i]);
[b45d97]82      ss=StringAppendS(",");
[ef304d]83      if ((int)strlen(ss)>colmax) s[i]=NULL;
[c232af]84      else                        s[i]=omStrDup(ss);
[0e1846]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        {
[c232af]94          ss=(char *)omAlloc(14+strlen(name));
[0e1846]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,",");
[32c80f]101            vl=si_max(vl,(int)strlen(ss));
[0e1846]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          {
[c232af]127            omFree((ADDRESS)s[i*MATCOLS(m)+j]);
128            ss=(char *)omAlloc(14+strlen(name));
[0e1846]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            {
[1fb897]139//#ifdef TEST
140//              PrintS("pagewidth too small in print(matrix)\n");
141//#endif
[0e1846]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)]);
[c232af]158      omFree(s[i*MATCOLS(m)]);
[0e1846]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]);
[c232af]168        omFree(s[i*MATCOLS(m)+j]);
[0e1846]169      }
170      PrintLn();
171    }
172    /* clean up */
[c232af]173    omFreeSize((ADDRESS)s,MATCOLS(m)*MATROWS(m)*sizeof(char*));
174    omFreeSize((ADDRESS)l,MATCOLS(m)*sizeof(int));
[0e1846]175  }
176}
177
178/*2
179* print for: matrix
180*/
[a79a128]181static BOOLEAN ipPrint_MA(leftv u)
[0e1846]182{
183  matrix m=(matrix)u->Data();
[43dfb6]184  ipPrint_MA0(m,u->Name());
[0e1846]185  return FALSE;
186}
187
188/*2
189* print for: vector
190*/
[a79a128]191static BOOLEAN ipPrint_V(leftv u)
[0e1846]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]);
[c232af]213  omFreeSize((ADDRESS)m,l*sizeof(poly));
[0e1846]214  return FALSE;
215}
216
[a79a128]217BOOLEAN jjPRINT(leftv res, leftv u)
218{
[000098]219  SPrintStart();
220  BOOLEAN bo=FALSE;
[a79a128]221  switch(u->Typ())
222  {
223      case INTVEC_CMD:
[000098]224        bo=ipPrint_INTVEC(u);
225        break;
[d2ac76]226
[a79a128]227      case INTMAT_CMD:
[000098]228        bo=ipPrint_INTMAT(u);
229        break;
[d2ac76]230
[a79a128]231      case MATRIX_CMD:
[000098]232        bo=ipPrint_MA(u);
233        break;
[a79a128]234
235      case IDEAL_CMD:
236      {
[7604db]237        char* s = u->String(NULL, FALSE, 2);
[a79a128]238        PrintS(s);
239        PrintLn();
[c232af]240        omFree(s);
[000098]241        break;
[a79a128]242      }
[d2ac76]243
[a79a128]244      case MODUL_CMD:
245      {
246        matrix m = idModule2Matrix(idCopy((ideal) u->Data()));
[43dfb6]247        ipPrint_MA0(m, u->Name());
[a79a128]248        idDelete((ideal *) &m);
[000098]249        break;
[a79a128]250      }
[d2ac76]251
[a79a128]252      case VECTOR_CMD:
[000098]253        bo=ipPrint_V(u);
254        break;
[d2ac76]255
[a79a128]256      default:
257        u->Print();
[000098]258        break;
[a79a128]259  }
[000098]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;
[a79a128]268}
269
270
[0e1846]271/*2
272* dbprint
273*/
274BOOLEAN jjDBPRINT(leftv res, leftv u)
275{
[04310f8]276  BOOLEAN print=(printlevel>myynest);
[0e1846]277  if ((u->next!=NULL)&&(u->Typ()==INT_CMD))
278  {
[04310f8]279    print=  (((int)((long)(u->Data()))) > 0);
[0e1846]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;
[a79a128]291      if (jjPRINT(res, h)) return TRUE;
[000098]292      PrintS((char*)res->data);
293      omFree(res->data);
294      PrintLn();
[0e1846]295      h->next=hh;
296      h=hh;
297    }
298  }
299  return FALSE;
300}
301
[a79a128]302static void ipPrintBetti(leftv u)
303{
304  int i,j;
[7447d8]305  int row_shift=(int)((long)(atGet(u,"rowShift",INT_CMD)));
[a79a128]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  {
[ba774b]316    Print("%5d:",i+row_shift);
[a79a128]317    for(j=1;j<=betti->cols();j++)
318    {
[d2ac76]319      int m=IMATELEM(*betti,i+1,j);
320      if (m==0)
321        PrintS("     -");
322      else
323        Print(" %5d",m);
[a79a128]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
[0e1846]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  {
[000098]352    SPrintStart();
[a79a128]353    ipPrintBetti(u);
[000098]354    char *s = SPrintEnd();
355    s[strlen(s)]='\0';
356    res->data=s;
[0e1846]357  }
[000098]358  else
[0e1846]359/* ======================== end betti ================================= */
[7604db]360  {
[000098]361    char* ns = omStrDup((char*) v->Data());
362    int dim = 1;
363    if (strlen(ns) == 3 && ns[1] == '2')
[8c8bea]364    {
[000098]365      dim = 2;
366      ns[1] = ns[2];
367      ns[2] = '\0';
[8c8bea]368    }
[000098]369    if (strcmp(ns,"%l") == 0)
[8c8bea]370    {
[000098]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();
[8c8bea]395    }
[000098]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);
[a79a128]420  }
421  return FALSE;
[0e1846]422}
Note: See TracBrowser for help on using the repository browser.