source: git/Singular/ipprint.cc @ 9235af

fieker-DuValspielwiese
Last change on this file since 9235af was d2ac76, checked in by Hans Schönemann <hannes@…>, 25 years ago
*hannes: print(r,betti): 0 -> - git-svn-id: file:///usr/local/Singular/svn/trunk@3499 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 8.7 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: ipprint.cc,v 1.14 1999-08-19 11:56:28 Singular Exp $ */
5/*
6* ABSTRACT: interpreter: printing
7*/
8
9#include "mod2.h"
10#include "tok.h"
11#include "ipid.h"
12#include "mmemory.h"
13#include "febase.h"
14#include "polys.h"
15#include "matpol.h"
16#include "subexpr.h"
17#include "intvec.h"
18#include "ipshell.h"
19#include "ipprint.h"
20#include "ideals.h"
21
22/*2
23* print for: int, string, poly, vector, ideal
24*/
25/*2
26* print for: intvec
27*/
28static BOOLEAN ipPrint_INTVEC(leftv u)
29{
30  intvec *v=(intvec *)u->Data();
31  v->show();
32  PrintLn();
33  return FALSE;
34}
35
36/*2
37* print for: intmat
38*/
39static BOOLEAN ipPrint_INTMAT(leftv u)
40{
41  intvec *v=(intvec *)u->Data();
42  int i,j;
43  for(i=0;i<v->rows();i++)
44  {
45    for(j=0;j<v->cols();j++)
46    {
47      Print(" %5d",IMATELEM(*v,i+1,j+1));
48    }
49    PrintLn();
50  }
51  return FALSE;
52}
53
54/*2
55* internal print for: matrix
56*/
57static void ipPrint_MA0(matrix m, const char *name)
58{
59  if (MATCOLS(m)>0)
60  {
61    char **s=(char **)Alloc(MATCOLS(m)*MATROWS(m)*sizeof(char*));
62    char *ss;
63    int *l=(int *)Alloc0(MATCOLS(m)*sizeof(int));
64    int i,j,k;
65    int vl=max(colmax/MATCOLS(m),8);
66
67    /* make enough space for the "largest" name*/
68    ss=(char *)AllocL(14+strlen(name));
69    sprintf(ss,"%s[%d,%d]",name,MATCOLS(m),MATROWS(m));
70    vl=max(vl,strlen(ss));
71    FreeL(ss);
72
73    /* convert all polys to string */
74    i=MATCOLS(m)*MATROWS(m)-1;
75    ss=pString(m->m[i]);
76    if ((int)strlen(ss)>colmax) s[i]=NULL;
77    else                        s[i]=mstrdup(ss);
78    for(i--;i>=0;i--)
79    {
80      pString(m->m[i]);
81      ss=StringAppendS(",");
82      if ((int)strlen(ss)>colmax) s[i]=NULL;
83      else                        s[i]=mstrdup(ss);
84    }
85    /* look up the width of all columns, put it in l[col_nr] */
86    /* insert names for very long entries */
87    for(i=MATROWS(m)-1;i>=0;i--)
88    {
89      for(j=MATCOLS(m)-1;j>=0;j--)
90      {
91        if (s[i*MATCOLS(m)+j]==NULL)
92        {
93          ss=(char *)AllocL(14+strlen(name));
94          s[i*MATCOLS(m)+j]=ss;
95          ss[0]='\0';
96          sprintf(ss,"%s[%d,%d]",name,i+1,j+1);
97          if ((i!=MATROWS(m)-1) || (j!=MATCOLS(m)-1))
98          {
99            strcat(ss,",");
100            vl=max(vl,strlen(ss));
101          }
102        }
103        k=strlen(s[i*MATCOLS(m)+j]);
104        if (k>l[j]) l[j]=k;
105      }
106    }
107    /* does it fit on a line ? */
108    int maxlen=0;
109    for(j=MATCOLS(m)-1;j>=0;j--)
110    {
111      maxlen+=l[j];
112    }
113    if (maxlen>colmax)
114    {
115      /* NO, it does not fit, so retry: */
116      /* look up the width of all columns, clear very long entriess */
117      /* put length in l[col_nr] */
118      /* insert names for cleared entries */
119      for(j=MATCOLS(m)-1;j>=0;j--)
120      {
121        for(i=MATROWS(m)-1;i>=0;i--)
122        {
123          k=strlen(s[i*MATCOLS(m)+j]);
124          if (/*strlen(s[i*MATCOLS(m)+j])*/ k > vl)
125          {
126            FreeL((ADDRESS)s[i*MATCOLS(m)+j]);
127            ss=(char *)AllocL(14+strlen(name));
128            s[i*MATCOLS(m)+j]=ss;
129            ss[0]='\0';
130            sprintf(ss,"%s[%d,%d]",name,i+1,j+1);
131            if ((i!=MATROWS(m)-1) || (j!=MATCOLS(m)-1))
132            {
133              strcat(ss,",");
134            }
135            l[j]=strlen(s[i*MATCOLS(m)+j]);
136            if (l[j]>vl)
137            {
138#ifdef TEST
139              PrintS("pagewidth too small in print(matrix)\n");
140#endif
141              vl=l[j]; /* make large names fit*/
142            }
143            i=MATROWS(m);
144          }
145          else
146          {
147            if (k>l[j]) l[j]=k;
148          }
149        }
150      }
151    }
152    /*output of the matrix*/
153    for(i=0;i<MATROWS(m);i++)
154    {
155      k=l[0];
156      Print("%-*.*s",l[0],l[0],s[i*MATCOLS(m)]);
157      FreeL(s[i*MATCOLS(m)]);
158      for(j=1;j<MATCOLS(m);j++)
159      {
160        if (k+l[j]>colmax)
161        {
162          PrintS("\n  ");
163          k=2;
164        }
165        k+=l[j];
166        Print("%-*.*s",l[j],l[j],s[i*MATCOLS(m)+j]);
167        FreeL(s[i*MATCOLS(m)+j]);
168      }
169      PrintLn();
170    }
171    /* clean up */
172    Free((ADDRESS)s,MATCOLS(m)*MATROWS(m)*sizeof(char*));
173    Free((ADDRESS)l,MATCOLS(m)*sizeof(int));
174  }
175}
176
177/*2
178* print for: matrix
179*/
180static BOOLEAN ipPrint_MA(leftv u)
181{
182  matrix m=(matrix)u->Data();
183  ipPrint_MA0(m,u->Fullname());
184  return FALSE;
185}
186
187/*2
188* print for: vector
189*/
190static BOOLEAN ipPrint_V(leftv u)
191{
192  polyset m=NULL;
193  int l,j;
194  /*convert into an array of the components*/
195  pVec2Polys((poly)u->Data(), &m, &l);
196  /*output*/
197  PrintS("[");
198  j=0;
199  loop
200  {
201    PrintS(pString(m[j]));
202    j++;
203    if (j<l) PrintS(",");
204    else
205    {
206      PrintS("]\n");
207      break;
208    }
209  }
210  /* clean up */
211  for(j=l-1;j>=0;j--) pDelete(&m[j]);
212  Free((ADDRESS)m,l*sizeof(poly));
213  return FALSE;
214}
215
216BOOLEAN jjPRINT(leftv res, leftv u)
217{
218  switch(u->Typ())
219  {
220      case INTVEC_CMD:
221        return ipPrint_INTVEC(u);
222
223      case INTMAT_CMD:
224        return ipPrint_INTMAT(u);
225
226      case MATRIX_CMD:
227        return ipPrint_MA(u);
228
229      case IDEAL_CMD:
230      {
231        char* s = u->String(NULL, FALSE, 2);
232        PrintS(s);
233        PrintLn();
234        FreeL(s);
235        return FALSE;
236      }
237
238      case MODUL_CMD:
239      {
240        matrix m = idModule2Matrix(idCopy((ideal) u->Data()));
241        ipPrint_MA0(m, u->Fullname());
242        idDelete((ideal *) &m);
243        return FALSE;
244      }
245
246      case VECTOR_CMD:
247        return ipPrint_V(u);
248
249      default:
250        u->Print();
251        return FALSE;
252  }
253}
254
255
256/*2
257* dbprint
258*/
259BOOLEAN jjDBPRINT(leftv res, leftv u)
260{
261  BOOLEAN print=(printlevel>myynest);
262  if ((u->next!=NULL)&&(u->Typ()==INT_CMD))
263  {
264    print=((int)u->Data()>0);
265    u=u->next;
266  }
267  if (print)
268  {
269    BOOLEAN r=FALSE;
270    leftv h=u;
271    leftv hh;
272    while (h!=NULL)
273    {
274      hh=h->next;
275      h->next=NULL;
276      if (jjPRINT(res, h)) return TRUE;
277      h->next=hh;
278      h=hh;
279    }
280  }
281  return FALSE;
282}
283
284static void ipPrintBetti(leftv u)
285{
286  int i,j;
287  intvec * betti=(intvec *)u->Data();
288  // head line --------------------------------------------------------
289  PrintS("      "); // 6 spaces for no. and :
290  for(j=0;j<betti->cols();j++) Print(" %5d",j); // 6 spaces pro column
291  PrintS("\n------"); // 6 spaces for no. and :
292  for(j=0;j<betti->cols();j++) PrintS("------"); // 6 spaces pro column
293  PrintLn();
294  // the table --------------------------------------------------------
295  for(i=0;i<betti->rows();i++)
296  {
297    Print("%5d:",i);
298    for(j=1;j<=betti->cols();j++)
299    {
300      int m=IMATELEM(*betti,i+1,j);
301      if (m==0)
302        PrintS("     -");
303      else
304        Print(" %5d",m);
305    }
306    PrintLn();
307  }
308  // sum --------------------------------------------------------------
309  PrintS("------"); // 6 spaces for no. and :
310  for(j=0;j<betti->cols();j++) PrintS("------"); // 6 spaces pro column
311  PrintS("\ntotal:");
312  for(j=0;j<betti->cols();j++)
313  {
314    int s=0;
315    for(i=0;i<betti->rows();i++)
316    {
317      s+=IMATELEM(*betti,i+1,j+1);
318    }
319    Print(" %5d",s); // 6 spaces pro column
320  }
321  PrintLn();
322}
323
324
325/*2
326* print(...,"format")
327*/
328BOOLEAN jjPRINT_FORMAT(leftv res, leftv u, leftv v)
329{
330/* ==================== betti ======================================== */
331  if ((u->Typ()==INTMAT_CMD)&&(strcmp((char *)v->Data(),"betti")==0))
332  {
333    ipPrintBetti(u);
334    res->data = NULL;
335    res->rtyp = NONE;
336    return FALSE;
337  }
338/* ======================== end betti ================================= */
339
340  char* ns = mstrdup((char*) v->Data());
341  int dim = 1;
342  if (strlen(ns) == 3 && ns[1] == '2')
343  {
344    dim = 2;
345    ns[1] = ns[2];
346    ns[2] = '\0';
347  }
348  if (strcmp(ns,"%l") == 0)
349  {
350    res->data = (char*) u->String(NULL, TRUE, dim);
351    if (dim == 2)
352    {
353      char* ns = (char*) AllocL(strlen((char*) res->data) + 2);
354      strcpy(ns, (char*) res->data);
355      FreeL(res->data);
356      strcat(ns, "\n");
357      res->data = ns;
358    }
359  }
360  else if (strcmp(ns,"%t") == 0)
361  {
362    SPrintStart();
363    if (u->rtyp==IDHDL)
364      type_cmd((idhdl) (u->data));
365    else
366      type_cmd((idhdl) u);
367    res->data = SPrintEnd();
368    if (dim != 2)
369      ((char*)res->data)[strlen((char*)res->data) -1] = '\0';
370  }
371  else if (strcmp(ns,"%;") == 0)
372  {
373    SPrintStart();
374    u->Print();
375    if (dim == 2) PrintLn();
376    res->data = SPrintEnd();
377  }
378  else if  (strcmp(ns,"%p") == 0)
379  {
380    SPrintStart();
381    iiExprArith1(res, u, PRINT_CMD);
382    if (dim == 2) PrintLn();
383    res->data = SPrintEnd();
384  }
385  else if (strcmp(ns,"%b") == 0 && (u->Typ()==INTMAT_CMD))
386  {
387    SPrintStart();
388    ipPrintBetti(u);
389    if (dim == 2) PrintLn();
390    res->data = SPrintEnd();
391  }
392  else
393  {
394    res->data = u->String(NULL, FALSE, dim);
395    if (dim == 2)
396    {
397      char* ns = (char*) AllocL(strlen((char*) res->data) + 2);
398      strcpy(ns, (char*) res->data);
399      FreeL(res->data);
400      strcat(ns, "\n");
401      res->data = ns;
402    }
403  }
404
405  FreeL(ns);
406  res->rtyp = STRING_CMD;
407  return FALSE;
408}
Note: See TracBrowser for help on using the repository browser.