source: git/Singular/ipprint.cc @ 00d2a4

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