source: git/Singular/ipprint.cc @ 89f472a

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