source: git/Singular/ipprint.cc @ a04a05

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