My Project  debian-1:4.1.1-p2+ds-4build1
ipprint.cc
Go to the documentation of this file.
1 /****************************************
2 * Computer Algebra System SINGULAR *
3 ****************************************/
4 /*
5 * ABSTRACT: interpreter: printing
6 */
7 
8 #include "kernel/mod2.h"
9 #include "omalloc/omalloc.h"
10 
11 #include "misc/intvec.h"
12 
13 #include "polys/matpol.h"
14 
15 #include "kernel/polys.h"
16 #include "kernel/ideals.h"
17 
18 #include "tok.h"
19 #include "ipid.h"
20 #include "subexpr.h"
21 #include "ipshell.h"
22 #include "ipprint.h"
23 #include "attrib.h"
24 
25 /*2
26 * print for: int, string, poly, vector, ideal
27 */
28 /*2
29 * print for: intvec
30 */
32 {
33  intvec *v=(intvec *)u->Data();
34  v->show();
35  PrintLn();
36  return FALSE;
37 }
38 
39 /*2
40 * print for: intmat
41 */
43 {
44  intvec *v=(intvec *)u->Data();
45  int i,j;
46  for(i=0;i<v->rows();i++)
47  {
48  for(j=0;j<v->cols();j++)
49  {
50  Print(" %5d",IMATELEM(*v,i+1,j+1));
51  }
52  PrintLn();
53  }
54  return FALSE;
55 }
56 
57 /*2
58 * internal print for: matrix
59 */
60 static void ipPrint_MA0(matrix m, const char *name)
61 {
62  if ((MATCOLS(m)>0)&&(MATROWS(m)>0))
63  {
64  char **s=(char **)omAlloc(MATCOLS(m)*MATROWS(m)*sizeof(char*));
65  char *ss;
66  int *l=(int *)omAlloc0(MATCOLS(m)*sizeof(int));
67  int i,j,k;
68  int vl=si_max(colmax/MATCOLS(m),8);
69 
70  /* make enough space for the "largest" name*/
71  ss=(char *)omAlloc(14+strlen(name));
72  sprintf(ss,"%s[%d,%d]",name,MATCOLS(m),MATROWS(m));
73  vl=si_max(vl,(int)strlen(ss));
74  omFree(ss);
75 
76  /* convert all polys to string */
77  i=MATCOLS(m)*MATROWS(m)-1;
78  ss=pString(m->m[i]);
79  if ((int)strlen(ss)>colmax) { s[i]=NULL; omFree(ss); }
80  else s[i]=ss;
81  for(i--;i>=0;i--)
82  {
83  StringSetS("");
84  pString0(m->m[i]);
85  StringAppendS(",");
86  ss=StringEndS();
87  if ((int)strlen(ss)>colmax) s[i]=NULL;
88  else s[i]=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  else Print("%d x %d zero matrix\n",MATROWS(m),MATCOLS(m));
181 }
182 
183 /*2
184 * print for: matrix
185 */
186 static 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 */
196 static BOOLEAN ipPrint_RING(leftv u)
197 {
198  ring r=(ring)u->Data();
199  PrintS("polynomial ring, over a ");
200  if (rField_is_Ring(r))
201  {
202  if (rField_is_Domain(r)) PrintS("domain");
203  else PrintS("ring (with zero-divisors)");
204  }
205  else PrintS("field");
206  if (r->OrdSgn==1) PrintS(", global");
207  else if (r->MixedOrder==1) PrintS(", mixed");
208  else PrintS(", local");
209  PrintS(" ordering\n");
210  rWrite(r, TRUE);
211  return FALSE;
212 }
213 
214 static BOOLEAN ipPrint_CRING(leftv u)
215 {
216  coeffs r=(coeffs)u->Data();
217  if (nCoeff_is_Ring(r))
218  {
219  if (nCoeff_is_Domain(r)) PrintS("domain: ");
220  else PrintS("ring (with zero-divisors): ");
221  }
222  else PrintS("field: ");
223  PrintS(nCoeffName(r));
224  return FALSE;
225 }
226 /*2
227 * print for: vector
228 */
229 static BOOLEAN ipPrint_V(leftv u)
230 {
231  polyset m=NULL;
232  int l,j;
233  /*convert into an array of the components*/
234  p_Vec2Polys((poly)u->Data(), &m, &l, currRing);
235  /*output*/
236  PrintS("[");
237  j=0;
238  loop
239  {
240  PrintS(pString(m[j]));
241  j++;
242  if (j<l) PrintS(",");
243  else
244  {
245  PrintS("]\n");
246  break;
247  }
248  }
249  /* clean up */
250  for(j=l-1;j>=0;j--) pDelete(&m[j]);
251  omFreeSize((ADDRESS)m,l*sizeof(poly));
252  return FALSE;
253 }
254 
256 {
257  SPrintStart();
258  BOOLEAN bo=FALSE;
259  switch(u->Typ())
260  {
261  case INTVEC_CMD:
262  bo=ipPrint_INTVEC(u);
263  break;
264 
265  case INTMAT_CMD:
266  bo=ipPrint_INTMAT(u);
267  break;
268 
269  case MATRIX_CMD:
270  bo=ipPrint_MA(u);
271  break;
272 
273  case IDEAL_CMD:
274  {
275  char* s = u->String(NULL, FALSE, 2);
276  PrintS(s);
277  PrintLn();
278  omFree(s);
279  break;
280  }
281 
282  case MODUL_CMD:
283  {
285  ipPrint_MA0(m, u->Name());
286  id_Delete((ideal *) &m,currRing);
287  break;
288  }
289 
290  case VECTOR_CMD:
291  bo=ipPrint_V(u);
292  break;
293 
294  case RING_CMD:
295  bo=ipPrint_RING(u);
296  break;
297 
298  case CRING_CMD:
299  bo=ipPrint_CRING(u);
300  break;
301 
302  default:
303  u->Print();
304  break;
305  }
306  char *s=SPrintEnd();
307  if (u->next==NULL)
308  {
309  int l=strlen(s);
310  if (s[l-1]=='\n') s[l-1]='\0';
311  }
312  res->data=(void*)s;
313  return bo;
314 }
315 
316 
317 /*2
318 * dbprint
319 */
321 {
322  BOOLEAN print=(printlevel>myynest);
323  if ((u->next!=NULL)&&(u->Typ()==INT_CMD))
324  {
325  print= (((int)((long)(u->Data()))) > 0);
326  u=u->next;
327  }
328  if (print)
329  {
330  // BOOLEAN r=FALSE;
331  leftv h=u;
332  leftv hh;
333  while (h!=NULL)
334  {
335  hh=h->next;
336  h->next=NULL;
337  if (jjPRINT(res, h)) return TRUE;
338  PrintS((char*)res->data);
339  omFree(res->data);
340  PrintLn();
341  h->next=hh;
342  h=hh;
343  }
344  }
345  return FALSE;
346 }
347 
348 static void ipPrintBetti(leftv u)
349 {
350  int i,j;
351  int row_shift=(int)((long)(atGet(u,"rowShift",INT_CMD)));
352  intvec * betti=(intvec *)u->Data();
353  // head line --------------------------------------------------------
354  PrintS(" "); // 6 spaces for no. and :
355  for(j=0;j<betti->cols();j++) Print(" %5d",j); // 6 spaces pro column
356  PrintS("\n------"); // 6 spaces for no. and :
357  for(j=0;j<betti->cols();j++) PrintS("------"); // 6 spaces pro column
358  PrintLn();
359  // the table --------------------------------------------------------
360  for(i=0;i<betti->rows();i++)
361  {
362  Print("%5d:",i+row_shift);
363  for(j=1;j<=betti->cols();j++)
364  {
365  int m=IMATELEM(*betti,i+1,j);
366  if (m==0)
367  PrintS(" -");
368  else
369  Print(" %5d",m);
370  }
371  PrintLn();
372  }
373  // sum --------------------------------------------------------------
374  PrintS("------"); // 6 spaces for no. and :
375  for(j=0;j<betti->cols();j++) PrintS("------"); // 6 spaces pro column
376  PrintS("\ntotal:");
377  for(j=0;j<betti->cols();j++)
378  {
379  int s=0;
380  for(i=0;i<betti->rows();i++)
381  {
382  s+=IMATELEM(*betti,i+1,j+1);
383  }
384  Print(" %5d",s); // 6 spaces pro column
385  }
386  PrintLn();
387 }
388 
389 
390 /*2
391 * print(...,"format")
392 */
394 {
395 /* ==================== betti ======================================== */
396  if ((u->Typ()==INTMAT_CMD)&&(strcmp((char *)v->Data(),"betti")==0))
397  {
398  SPrintStart();
399  ipPrintBetti(u);
400  char *s = SPrintEnd();
401  s[strlen(s)]='\0';
402  res->data=s;
403  }
404  else
405 /* ======================== end betti ================================= */
406  {
407  char* ns = omStrDup((char*) v->Data());
408  int dim = 1;
409  if (strlen(ns) == 3 && ns[1] == '2')
410  {
411  dim = 2;
412  ns[1] = ns[2];
413  ns[2] = '\0';
414  }
415  if (strcmp(ns,"%l") == 0)
416  {
417  res->data = (char*) u->String(NULL, TRUE, dim);
418  if (dim == 2)
419  {
420  char* ns = (char*) omAlloc(strlen((char*) res->data) + 2);
421  strcpy(ns, (char*) res->data);
422  omFree(res->data);
423  strcat(ns, "\n");
424  res->data = ns;
425  }
426  }
427  else if (strcmp(ns,"%t") == 0)
428  {
429  SPrintStart();
430  type_cmd(u);
431  res->data = SPrintEnd();
432  if (dim != 2)
433  ((char*)res->data)[strlen((char*)res->data) -1] = '\0';
434  }
435  else if (strcmp(ns,"%;") == 0)
436  {
437  SPrintStart();
438  u->Print();
439  if (dim == 2) PrintLn();
440  res->data = SPrintEnd();
441  }
442  else if (strcmp(ns,"%p") == 0)
443  {
445  }
446  else if (strcmp(ns,"%b") == 0 && (u->Typ()==INTMAT_CMD))
447  {
448  SPrintStart();
449  ipPrintBetti(u);
450  if (dim == 2) PrintLn();
451  res->data = SPrintEnd();
452  }
453  else
454  {
455  res->data = u->String(NULL, FALSE, dim);
456  if (dim == 2)
457  {
458  char* ns = (char*) omAlloc(strlen((char*) res->data) + 2);
459  strcpy(ns, (char*) res->data);
460  omFree(res->data);
461  strcat(ns, "\n");
462  res->data = ns;
463  }
464  }
465  omFree(ns);
466  }
467  return FALSE;
468 }
dim
int dim(ideal I, ring r)
Definition: tropicalStrategy.cc:22
FALSE
#define FALSE
Definition: auxiliary.h:94
omalloc.h
matrix
ip_smatrix * matrix
Definition: matpol.h:30
sleftv::Data
void * Data()
Definition: subexpr.cc:1133
ip_smatrix
Definition: matpol.h:13
StringAppendS
void StringAppendS(const char *st)
Definition: reporter.cc:106
j
int j
Definition: facHensel.cc:105
omFree
#define omFree(addr)
Definition: omAllocDecl.h:259
k
int k
Definition: cfEzgcd.cc:92
CRING_CMD
Definition: tok.h:55
rField_is_Domain
static BOOLEAN rField_is_Domain(const ring r)
Definition: ring.h:479
sleftv::Print
void Print(leftv store=NULL, int spaces=0)
Called by type_cmd (e.g. "r;") or as default in jPRINT.
Definition: subexpr.cc:67
ipPrint_RING
static BOOLEAN ipPrint_RING(leftv u)
Definition: ipprint.cc:195
iiExprArith1
BOOLEAN iiExprArith1(leftv res, leftv a, int op)
Definition: iparith.cc:8267
polyset
poly * polyset
Definition: polys.h:235
attrib.h
ADDRESS
void * ADDRESS
Definition: auxiliary.h:133
MODUL_CMD
Definition: grammar.cc:286
pString0
void pString0(poly p)
Definition: polys.h:281
ipPrint_MA
static BOOLEAN ipPrint_MA(leftv u)
Definition: ipprint.cc:185
SPrintEnd
char * SPrintEnd()
Definition: reporter.cc:272
jjPRINT_FORMAT
BOOLEAN jjPRINT_FORMAT(leftv res, leftv u, leftv v)
Definition: ipprint.cc:392
polys.h
type_cmd
void type_cmd(leftv v)
Definition: ipshell.cc:245
ipPrint_CRING
static BOOLEAN ipPrint_CRING(leftv u)
Definition: ipprint.cc:213
omStrDup
#define omStrDup(s)
Definition: omAllocDecl.h:261
ipPrint_V
static BOOLEAN ipPrint_V(leftv u)
Definition: ipprint.cc:228
pDelete
#define pDelete(p_ptr)
Definition: polys.h:166
PRINT_CMD
Definition: tok.h:154
StringEndS
char * StringEndS()
Definition: reporter.cc:150
loop
#define loop
Definition: structs.h:77
sleftv
Class used for (list of) interpreter objects.
Definition: subexpr.h:81
pString
char * pString(poly p)
Definition: polys.h:280
RING_CMD
Definition: grammar.cc:281
MATRIX_CMD
Definition: grammar.cc:285
currRing
ring currRing
Widely used global variable which specifies the current polynomial ring for Singular interpreter and ...
Definition: polys.cc:13
TRUE
#define TRUE
Definition: auxiliary.h:98
i
int i
Definition: cfEzgcd.cc:125
id_Delete
void id_Delete(ideal *h, ring r)
deletes an ideal/module/matrix
Definition: simpleideals.cc:113
res
CanonicalForm res
Definition: facAbsFact.cc:64
INT_CMD
Definition: tok.h:95
matpol.h
ipPrint_INTMAT
static BOOLEAN ipPrint_INTMAT(leftv u)
Definition: ipprint.cc:41
PrintS
void PrintS(const char *s)
Definition: reporter.cc:283
omFreeSize
#define omFreeSize(addr, size)
Definition: omAllocDecl.h:258
BOOLEAN
int BOOLEAN
Definition: auxiliary.h:85
rField_is_Ring
static BOOLEAN rField_is_Ring(const ring r)
Definition: ring.h:476
id_Module2Matrix
matrix id_Module2Matrix(ideal mod, const ring R)
Definition: simpleideals.cc:1200
nCoeff_is_Ring
static FORCE_INLINE BOOLEAN nCoeff_is_Ring(const coeffs r)
Definition: coeffs.h:758
IDEAL_CMD
Definition: grammar.cc:283
h
static Poly * h
Definition: janet.cc:972
mod2.h
ipPrint_MA0
static void ipPrint_MA0(matrix m, const char *name)
Definition: ipprint.cc:59
ipPrintBetti
static void ipPrintBetti(leftv u)
Definition: ipprint.cc:347
coeffs
intvec
Definition: intvec.h:16
omAlloc
#define omAlloc(size)
Definition: omAllocDecl.h:208
jjPRINT
BOOLEAN jjPRINT(leftv res, leftv u)
Definition: ipprint.cc:254
VECTOR_CMD
Definition: grammar.cc:290
myynest
int myynest
Definition: febase.cc:40
intvec.h
subexpr.h
INTVEC_CMD
Definition: tok.h:100
INTMAT_CMD
Definition: grammar.cc:279
IMATELEM
#define IMATELEM(M, I, J)
Definition: intvec.h:83
sleftv::String
char * String(void *d=NULL, BOOLEAN typed=FALSE, int dim=1)
Called for conversion to string (used by string(..), write(..),..)
Definition: subexpr.cc:746
jjDBPRINT
BOOLEAN jjDBPRINT(leftv res, leftv u)
Definition: ipprint.cc:319
rWrite
void rWrite(ring r, BOOLEAN details)
Definition: ring.cc:226
StringSetS
void StringSetS(const char *st)
Definition: reporter.cc:127
si_max
static int si_max(const int a, const int b)
Definition: auxiliary.h:138
ipprint.h
Print
#define Print
Definition: emacs.cc:79
intvec::cols
int cols() const
Definition: intvec.h:93
atGet
void * atGet(idhdl root, const char *name, int t, void *defaultReturnValue)
Definition: attrib.cc:130
p_Vec2Polys
void p_Vec2Polys(poly v, poly **p, int *len, const ring r)
Definition: p_polys.cc:3524
name
char name(const Variable &v)
Definition: factory.h:180
tok.h
sleftv::Typ
int Typ()
Definition: subexpr.cc:991
m
int m
Definition: cfEzgcd.cc:121
MATCOLS
#define MATCOLS(i)
Definition: matpol.h:27
NULL
#define NULL
Definition: omList.c:9
nCoeffName
static FORCE_INLINE char * nCoeffName(const coeffs cf)
Definition: coeffs.h:976
colmax
int colmax
Definition: febase.cc:36
ideals.h
l
int l
Definition: cfEzgcd.cc:93
intvec::rows
int rows() const
Definition: intvec.h:94
v
const Variable & v
< [in] a sqrfree bivariate poly
Definition: facBivar.h:37
sleftv::Name
const char * Name()
Definition: subexpr.h:119
s
const CanonicalForm int s
Definition: facAbsFact.cc:55
ipshell.h
id_Copy
ideal id_Copy(ideal h1, const ring r)
copy an ideal
Definition: simpleideals.cc:403
printlevel
int printlevel
Definition: febase.cc:35
PrintLn
void PrintLn()
Definition: reporter.cc:309
SPrintStart
void SPrintStart()
Definition: reporter.cc:245
MATROWS
#define MATROWS(i)
Definition: matpol.h:26
ipid.h
omAlloc0
#define omAlloc0(size)
Definition: omAllocDecl.h:209
sleftv::next
leftv next
Definition: subexpr.h:85
ipPrint_INTVEC
static BOOLEAN ipPrint_INTVEC(leftv u)
Definition: ipprint.cc:30
nCoeff_is_Domain
static FORCE_INLINE BOOLEAN nCoeff_is_Domain(const coeffs r)
returns TRUE, if r is a field or r has no zero divisors (i.e is a domain)
Definition: coeffs.h:769