source: git/Singular/ipprint.cc @ 667ba1

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