source: git/Singular/ipprint.cc @ 72a01e

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