source: git/Singular/ipprint.cc @ d17626

fieker-DuValspielwiese
Last change on this file since d17626 was d17626, checked in by Hans Schoenemann <hannes@…>, 9 years ago
fix: 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}
184
185/*2
186* print for: matrix
187*/
188static BOOLEAN ipPrint_MA(leftv u)
189{
190  matrix m=(matrix)u->Data();
191  ipPrint_MA0(m,u->Name());
192  return FALSE;
193}
194
195/*2
196* print for: ring
197*/
198static BOOLEAN ipPrint_RING(leftv u)
199{
200  ring r=(ring)u->Data();
201  PrintS("polynomial ring, over a ");
202  if (rField_is_Ring(r))
203  {
204    if (rField_is_Domain(r)) PrintS("domain");
205    else                     PrintS("ring (with zero-divisors)");
206  }
207  else PrintS("field");
208  if (r->OrdSgn==1) PrintS(", global"); else PrintS(", local/mixed");
209  PrintS(" ordering\n");
210  rWrite(r, TRUE);
211  return FALSE;
212}
213
214#ifdef SINGULAR_4_1
215static BOOLEAN ipPrint_CRING(leftv u)
216{
217  coeffs r=(coeffs)u->Data();
218  if (nCoeff_is_Ring(r))
219  {
220    if (nCoeff_is_Domain(r)) PrintS("domain: ");
221    else                     PrintS("ring (with zero-divisors): ");
222  }
223  else PrintS("field: ");
224  PrintS(nCoeffName(r));
225  return FALSE;
226}
227#endif
228/*2
229* print for: vector
230*/
231static BOOLEAN ipPrint_V(leftv u)
232{
233  polyset m=NULL;
234  int l,j;
235  /*convert into an array of the components*/
236  p_Vec2Polys((poly)u->Data(), &m, &l, currRing);
237  /*output*/
238  PrintS("[");
239  j=0;
240  loop
241  {
242    PrintS(pString(m[j]));
243    j++;
244    if (j<l) PrintS(",");
245    else
246    {
247      PrintS("]\n");
248      break;
249    }
250  }
251  /* clean up */
252  for(j=l-1;j>=0;j--) pDelete(&m[j]);
253  omFreeSize((ADDRESS)m,l*sizeof(poly));
254  return FALSE;
255}
256
257BOOLEAN jjPRINT(leftv res, leftv u)
258{
259  SPrintStart();
260  BOOLEAN bo=FALSE;
261  switch(u->Typ())
262  {
263      case INTVEC_CMD:
264        bo=ipPrint_INTVEC(u);
265        break;
266
267      case INTMAT_CMD:
268        bo=ipPrint_INTMAT(u);
269        break;
270
271      case MATRIX_CMD:
272        bo=ipPrint_MA(u);
273        break;
274
275      case IDEAL_CMD:
276      {
277        char* s = u->String(NULL, FALSE, 2);
278        PrintS(s);
279        PrintLn();
280        omFree(s);
281        break;
282      }
283
284      case MODUL_CMD:
285      {
286        matrix m = id_Module2Matrix(id_Copy((ideal) u->Data(),currRing),currRing);
287        ipPrint_MA0(m, u->Name());
288        id_Delete((ideal *) &m,currRing);
289        break;
290      }
291
292      case VECTOR_CMD:
293        bo=ipPrint_V(u);
294        break;
295
296      case RING_CMD:
297      case QRING_CMD:
298        bo=ipPrint_RING(u);
299        break;
300
301      #ifdef SINGULAR_4_1
302      case CRING_CMD:
303        bo=ipPrint_CRING(u);
304        break;
305      #endif
306
307      default:
308        u->Print();
309        break;
310  }
311  char *s=SPrintEnd();
312  if (u->next==NULL)
313  {
314    int l=strlen(s);
315    if (s[l-1]=='\n') s[l-1]='\0';
316  }
317  res->data=(void*)s;
318  return bo;
319}
320
321
322/*2
323* dbprint
324*/
325BOOLEAN jjDBPRINT(leftv res, leftv u)
326{
327  BOOLEAN print=(printlevel>myynest);
328  if ((u->next!=NULL)&&(u->Typ()==INT_CMD))
329  {
330    print=  (((int)((long)(u->Data()))) > 0);
331    u=u->next;
332  }
333  if (print)
334  {
335    // BOOLEAN r=FALSE;
336    leftv h=u;
337    leftv hh;
338    while (h!=NULL)
339    {
340      hh=h->next;
341      h->next=NULL;
342      if (jjPRINT(res, h)) return TRUE;
343      PrintS((char*)res->data);
344      omFree(res->data);
345      PrintLn();
346      h->next=hh;
347      h=hh;
348    }
349  }
350  return FALSE;
351}
352
353static void ipPrintBetti(leftv u)
354{
355  int i,j;
356  int row_shift=(int)((long)(atGet(u,"rowShift",INT_CMD)));
357  intvec * betti=(intvec *)u->Data();
358  // head line --------------------------------------------------------
359  PrintS("      "); // 6 spaces for no. and :
360  for(j=0;j<betti->cols();j++) Print(" %5d",j); // 6 spaces pro column
361  PrintS("\n------"); // 6 spaces for no. and :
362  for(j=0;j<betti->cols();j++) PrintS("------"); // 6 spaces pro column
363  PrintLn();
364  // the table --------------------------------------------------------
365  for(i=0;i<betti->rows();i++)
366  {
367    Print("%5d:",i+row_shift);
368    for(j=1;j<=betti->cols();j++)
369    {
370      int m=IMATELEM(*betti,i+1,j);
371      if (m==0)
372        PrintS("     -");
373      else
374        Print(" %5d",m);
375    }
376    PrintLn();
377  }
378  // sum --------------------------------------------------------------
379  PrintS("------"); // 6 spaces for no. and :
380  for(j=0;j<betti->cols();j++) PrintS("------"); // 6 spaces pro column
381  PrintS("\ntotal:");
382  for(j=0;j<betti->cols();j++)
383  {
384    int s=0;
385    for(i=0;i<betti->rows();i++)
386    {
387      s+=IMATELEM(*betti,i+1,j+1);
388    }
389    Print(" %5d",s); // 6 spaces pro column
390  }
391  PrintLn();
392}
393
394
395/*2
396* print(...,"format")
397*/
398BOOLEAN jjPRINT_FORMAT(leftv res, leftv u, leftv v)
399{
400/* ==================== betti ======================================== */
401  if ((u->Typ()==INTMAT_CMD)&&(strcmp((char *)v->Data(),"betti")==0))
402  {
403    SPrintStart();
404    ipPrintBetti(u);
405    char *s = SPrintEnd();
406    s[strlen(s)]='\0';
407    res->data=s;
408  }
409  else
410/* ======================== end betti ================================= */
411  {
412    char* ns = omStrDup((char*) v->Data());
413    int dim = 1;
414    if (strlen(ns) == 3 && ns[1] == '2')
415    {
416      dim = 2;
417      ns[1] = ns[2];
418      ns[2] = '\0';
419    }
420    if (strcmp(ns,"%l") == 0)
421    {
422      res->data = (char*) u->String(NULL, TRUE, dim);
423      if (dim == 2)
424      {
425        char* ns = (char*) omAlloc(strlen((char*) res->data) + 2);
426        strcpy(ns, (char*) res->data);
427        omFree(res->data);
428        strcat(ns, "\n");
429        res->data = ns;
430      }
431    }
432    else if (strcmp(ns,"%t") == 0)
433    {
434      SPrintStart();
435      type_cmd(u);
436      res->data = SPrintEnd();
437      if (dim != 2)
438        ((char*)res->data)[strlen((char*)res->data) -1] = '\0';
439    }
440    else if (strcmp(ns,"%;") == 0)
441    {
442      SPrintStart();
443      u->Print();
444      if (dim == 2) PrintLn();
445      res->data = SPrintEnd();
446    }
447    else if  (strcmp(ns,"%p") == 0)
448    {
449      iiExprArith1(res, u, PRINT_CMD);
450    }
451    else if (strcmp(ns,"%b") == 0 && (u->Typ()==INTMAT_CMD))
452    {
453      SPrintStart();
454      ipPrintBetti(u);
455      if (dim == 2) PrintLn();
456      res->data = SPrintEnd();
457    }
458    else
459    {
460      res->data = u->String(NULL, FALSE, dim);
461      if (dim == 2)
462      {
463        char* ns = (char*) omAlloc(strlen((char*) res->data) + 2);
464        strcpy(ns, (char*) res->data);
465        omFree(res->data);
466        strcat(ns, "\n");
467        res->data = ns;
468      }
469    }
470    omFree(ns);
471  }
472  return FALSE;
473}
Note: See TracBrowser for help on using the repository browser.