My Project
polys0.cc
Go to the documentation of this file.
1 /****************************************
2 * Computer Algebra System SINGULAR *
3 ****************************************/
4 
5 /*
6 * ABSTRACT - all basic methods to convert polynomials to strings
7 */
8 
9 /* includes */
10 
11 #include "misc/auxiliary.h"
12 
13 #include "coeffs/numbers.h"
14 #include "polys/monomials/ring.h"
16 #ifdef HAVE_SHIFTBBA
17 #include "polys/shiftop.h"
18 #endif
19 
20 /*2
21 * writes a monomial (p),
22 * uses form x*gen(.) if ko != coloumn number of p
23 */
24 static void writemon(poly p, int ko, const ring r)
25 {
26  assume(r != NULL);
27  const coeffs C = r->cf;
28  assume(C != NULL);
29 
30  BOOLEAN wroteCoef=FALSE,writeGen=FALSE;
31  const BOOLEAN bNotShortOut = (rShortOut(r) == FALSE);
32 
33  if (((p_GetComp(p,r) == ko)
34  &&(p_LmIsConstantComp(p, r)))
35  || ((!n_IsOne(pGetCoeff(p),C))
36  && (!n_IsMOne(pGetCoeff(p),C))
37  )
38  )
39  {
40  if( bNotShortOut )
42  else
44 
45  wroteCoef=(bNotShortOut)
46  || (rParameter(r)!=NULL)
47  || rField_is_R(r) || (rField_is_long_R(r)) || (rField_is_long_C(r));
48  writeGen=TRUE;
49  }
50  else if (n_IsMOne(pGetCoeff(p),C))
51  {
52  if (n_GreaterZero(pGetCoeff(p),C))
53  {
54  if( bNotShortOut )
56  else
58 
59  wroteCoef=(bNotShortOut)
60  || (rParameter(r)!=NULL)
61  || rField_is_R(r) || (rField_is_long_R(r)) || (rField_is_long_C(r));
62  writeGen=TRUE;
63  }
64  else
65  StringAppendS("-");
66  }
67 
68  int i;
69  {
70  for (i=0; i<rVar(r); i++)
71  {
72  {
73  long ee = p_GetExp(p,i+1,r);
74  if (ee!=0L)
75  {
76  if (wroteCoef)
77  StringAppendS("*");
78  //else
79  wroteCoef=(bNotShortOut);
80  writeGen=TRUE;
82  if (ee != 1L)
83  {
84  if (bNotShortOut) StringAppendS("^");
85  StringAppend("%ld", ee);
86  }
87  }
88  }
89  }
90  }
91  //StringAppend("{%d}",p->Order);
92  if (p_GetComp(p, r) != (long)ko)
93  {
94  if (writeGen) StringAppendS("*");
95  StringAppend("gen(%d)", p_GetComp(p, r));
96  }
97 }
98 
99 /*2
100 * writes a monomial (p),
101 * uses form x*gen(.) if ko != coloumn number of p
102 */
103 #ifdef HAVE_SHIFTBBA
104 static void writemonLP(poly p, int ko, const ring r)
105 {
106  assume(r != NULL);
107  const coeffs C = r->cf;
108  assume(C != NULL);
109 
110  BOOLEAN wroteCoef=FALSE,writeGen=FALSE;
111 
112  if (((p_GetComp(p,r) == ko)
113  &&(p_LmIsConstantComp(p, r)))
114  || ((!n_IsOne(pGetCoeff(p),C))
115  && (!n_IsMOne(pGetCoeff(p),C))
116  )
117  )
118  {
119  n_WriteLong(pGetCoeff(p),C);
120 
121  wroteCoef=TRUE;
122  writeGen=TRUE;
123  }
124  else if (n_IsMOne(pGetCoeff(p),C))
125  {
126  if (n_GreaterZero(pGetCoeff(p),C))
127  {
128  n_WriteLong(pGetCoeff(p),C);
129 
130  wroteCoef=TRUE;
131  writeGen=TRUE;
132  }
133  else
134  StringAppendS("-");
135  }
136 
137  int i;
138  {
139  int lV = r->isLPring;
140  int lastVar = p_mLastVblock(p, r) * lV;
141  BOOLEAN wroteBlock = FALSE;
142  for (i=0; i<rVar(r); i++)
143  {
144  {
145  long ee = p_GetExp(p,i+1,r);
146  BOOLEAN endOfBlock = ((i+1) % lV) == 0;
147  BOOLEAN writeEmptyBlock = ee==0L && endOfBlock && !wroteBlock && i < lastVar;
148  if (ee!=0L || writeEmptyBlock)
149  {
150  if (wroteBlock)
151  StringAppendS("&");
152  else if (wroteCoef)
153  StringAppendS("*");
154  //else
155  wroteCoef=TRUE; //(bNotShortOut);
156  writeGen=TRUE;
157  if (writeEmptyBlock)
158  StringAppendS("_");
159  else
160  {
161  StringAppendS(rRingVar(i, r));
162  if (ee != 1L)
163  {
164  StringAppend("^%ld", ee);
165  }
166  wroteBlock = TRUE;
167  }
168  }
169  if (endOfBlock)
170  wroteBlock = FALSE;
171  }
172  }
173  }
174  //StringAppend("{%d}",p->Order);
175  if (p_GetComp(p, r) != (long)ko)
176  {
177  if (writeGen) StringAppendS("*");
178  StringAppend("gen(%d)", p_GetComp(p, r));
179  }
180 }
181 #endif
182 
183 /// if possible print p in a short way...
184 void p_String0Short(const poly p, ring lmRing, ring tailRing)
185 {
186  // NOTE: the following (non-thread-safe!) UGLYNESS
187  // (changing naRing->ShortOut for a while) is due to Hans!
188  // Just think of other ring using the VERY SAME naRing and possible
189  // side-effects...
190  const BOOLEAN bLMShortOut = rShortOut(lmRing);
191  const BOOLEAN bTAILShortOut = rShortOut(tailRing);
192 
193  lmRing->ShortOut = rCanShortOut(lmRing);
194  tailRing->ShortOut = rCanShortOut(tailRing);
195 
196  p_String0(p, lmRing, tailRing);
197 
198  lmRing->ShortOut = bLMShortOut;
199  tailRing->ShortOut = bTAILShortOut;
200 }
201 
202 /// print p in a long way...
203 void p_String0Long(const poly p, ring lmRing, ring tailRing)
204 {
205  // NOTE: the following (non-thread-safe!) UGLYNESS
206  // (changing naRing->ShortOut for a while) is due to Hans!
207  // Just think of other ring using the VERY SAME naRing and possible
208  // side-effects...
209  // but this is not a problem: i/o is not thread-safe anyway.
210  const BOOLEAN bLMShortOut = rShortOut(lmRing);
211  const BOOLEAN bTAILShortOut = rShortOut(tailRing);
212 
213  lmRing->ShortOut = FALSE;
214  tailRing->ShortOut = FALSE;
215 
216  p_String0(p, lmRing, tailRing);
217 
218  lmRing->ShortOut = bLMShortOut;
219  tailRing->ShortOut = bTAILShortOut;
220 }
221 
222 
223 void p_String0(poly p, ring lmRing, ring tailRing)
224 {
225  if (p == NULL)
226  {
227  StringAppendS("0");
228  return;
229  }
230  p_Normalize(p,lmRing);
231  if ((n_GetChar(lmRing->cf) == 0)
232  && (nCoeff_is_transExt(lmRing->cf)))
233  p_Normalize(p,lmRing); /* Manual/absfact.tst */
234 #ifdef HAVE_SHIFTBBA
235  if(lmRing->isLPring)
236  {
237  if ((p_GetComp(p, lmRing) == 0) || (!lmRing->VectorOut))
238  {
239  writemonLP(p,0, lmRing);
240  p = pNext(p);
241  while (p!=NULL)
242  {
243  assume((p->coef==NULL)||(!n_IsZero(p->coef,tailRing->cf)));
244  if ((p->coef==NULL)||n_GreaterZero(p->coef,tailRing->cf))
245  StringAppendS("+");
246  writemonLP(p,0, tailRing);
247  p = pNext(p);
248  }
249  return;
250  }
251  }
252  else
253 #endif
254  {
255  if ((p_GetComp(p, lmRing) == 0) || (!lmRing->VectorOut))
256  {
257  writemon(p,0, lmRing);
258  p = pNext(p);
259  while (p!=NULL)
260  {
261  assume((p->coef==NULL)||(!n_IsZero(p->coef,tailRing->cf)));
262  if ((p->coef==NULL)||n_GreaterZero(p->coef,tailRing->cf))
263  StringAppendS("+");
264  writemon(p,0, tailRing);
265  p = pNext(p);
266  }
267  return;
268  }
269  }
270 
271  long k = 1;
272  StringAppendS("[");
273 #ifdef HAVE_SHIFTBBA
274  if(lmRing->isLPring)
275  {
276  loop
277  {
278  while (k < p_GetComp(p,lmRing))
279  {
280  StringAppendS("0,");
281  k++;
282  }
283  writemonLP(p,k,lmRing);
284  pIter(p);
285  while ((p!=NULL) && (k == p_GetComp(p, tailRing)))
286  {
287  if (n_GreaterZero(p->coef,tailRing->cf)) StringAppendS("+");
288  writemonLP(p,k,tailRing);
289  pIter(p);
290  }
291  if (p == NULL) break;
292  StringAppendS(",");
293  k++;
294  }
295  }
296  else
297 #endif
298  {
299  loop
300  {
301  while (k < p_GetComp(p,lmRing))
302  {
303  StringAppendS("0,");
304  k++;
305  }
306  writemon(p,k,lmRing);
307  pIter(p);
308  while ((p!=NULL) && (k == p_GetComp(p, tailRing)))
309  {
310  if (n_GreaterZero(p->coef,tailRing->cf)) StringAppendS("+");
311  writemon(p,k,tailRing);
312  pIter(p);
313  }
314  if (p == NULL) break;
315  StringAppendS(",");
316  k++;
317  }
318  }
319  StringAppendS("]");
320 }
321 
322 char* p_String(poly p, ring lmRing, ring tailRing)
323 {
324  StringSetS("");
325  p_String0(p, lmRing, tailRing);
326  return StringEndS();
327 }
328 
329 /*2
330 * writes a polynomial p to stdout
331 */
332 void p_Write0(poly p, ring lmRing, ring tailRing)
333 {
334  char *s=p_String(p, lmRing, tailRing);
335  PrintS(s);
336  omFree(s);
337 }
338 
339 /*2
340 * writes a polynomial p to stdout followed by \n
341 */
342 void p_Write(poly p, ring lmRing, ring tailRing)
343 {
344  p_Write0(p, lmRing, tailRing);
345  PrintLn();
346 }
347 
348 #if !defined(__OPTIMIZE__) || defined(KDEBUG)
349 /*2
350 *the standard debugging output:
351 *print the first two monomials of the poly (wrp) or only the lead term (wrp0),
352 *possibly followed by the string "+..."
353 */
354 void p_wrp0(poly p, ring ri)
355 {
356  poly r;
357 
358  if (p==NULL) PrintS("NULL");
359  else if (pNext(p)==NULL) p_Write0(p, ri);
360  else
361  {
362  r = pNext(p);
363  pNext(p) = NULL;
364  p_Write0(p, ri);
365  if (r!=NULL)
366  {
367  PrintS("+...");
368  pNext(p) = r;
369  }
370  }
371 }
372 #endif
373 void p_wrp(poly p, ring lmRing, ring tailRing)
374 {
375  poly r;
376 
377  if (p==NULL) PrintS("NULL");
378  else if (pNext(p)==NULL) p_Write0(p, lmRing);
379  else
380  {
381  r = pNext(pNext(p));
382  pNext(pNext(p)) = NULL;
383  p_Write0(p, tailRing);
384  if (r!=NULL)
385  {
386  PrintS("+...");
387  pNext(pNext(p)) = r;
388  }
389  }
390 }
All the auxiliary stuff.
int BOOLEAN
Definition: auxiliary.h:87
#define TRUE
Definition: auxiliary.h:100
#define FALSE
Definition: auxiliary.h:96
int i
Definition: cfEzgcd.cc:132
int k
Definition: cfEzgcd.cc:99
int p
Definition: cfModGcd.cc:4078
static FORCE_INLINE BOOLEAN n_GreaterZero(number n, const coeffs r)
ordered fields: TRUE iff 'n' is positive; in Z/pZ: TRUE iff 0 < m <= roundedBelow(p/2),...
Definition: coeffs.h:494
static FORCE_INLINE BOOLEAN n_IsMOne(number n, const coeffs r)
TRUE iff 'n' represents the additive inverse of the one element, i.e. -1.
Definition: coeffs.h:472
static FORCE_INLINE void n_WriteLong(number n, const coeffs r)
write to the output buffer of the currently used reporter
Definition: coeffs.h:583
static FORCE_INLINE BOOLEAN n_IsZero(number n, const coeffs r)
TRUE iff 'n' represents the zero element.
Definition: coeffs.h:464
static FORCE_INLINE int n_GetChar(const coeffs r)
Return the characteristic of the coeff. domain.
Definition: coeffs.h:444
static FORCE_INLINE void n_WriteShort(number n, const coeffs r)
write to the output buffer of the currently used reporter in a shortest possible way,...
Definition: coeffs.h:588
static FORCE_INLINE BOOLEAN n_IsOne(number n, const coeffs r)
TRUE iff 'n' represents the one element.
Definition: coeffs.h:468
static FORCE_INLINE BOOLEAN nCoeff_is_transExt(const coeffs r)
TRUE iff r represents a transcendental extension field.
Definition: coeffs.h:918
#define StringAppend
Definition: emacs.cc:79
const CanonicalForm int s
Definition: facAbsFact.cc:51
#define assume(x)
Definition: mod2.h:387
#define p_GetComp(p, r)
Definition: monomials.h:64
#define pIter(p)
Definition: monomials.h:37
#define pNext(p)
Definition: monomials.h:36
static number & pGetCoeff(poly p)
return an alias to the leading coefficient of p assumes that p != NULL NOTE: not copy
Definition: monomials.h:44
The main handler for Singular numbers which are suitable for Singular polynomials.
#define omFree(addr)
Definition: omAllocDecl.h:261
#define NULL
Definition: omList.c:12
void p_Normalize(poly p, const ring r)
Definition: p_polys.cc:3847
static BOOLEAN p_LmIsConstantComp(const poly p, const ring r)
Definition: p_polys.h:978
static long p_GetExp(const poly p, const unsigned long iBitmask, const int VarOffset)
get a single variable exponent @Note: the integer VarOffset encodes:
Definition: p_polys.h:469
void p_String0(poly p, ring lmRing, ring tailRing)
print p according to ShortOut in lmRing & tailRing
Definition: polys0.cc:223
void p_Write(poly p, ring lmRing, ring tailRing)
Definition: polys0.cc:342
static void writemon(poly p, int ko, const ring r)
Definition: polys0.cc:24
void p_String0Long(const poly p, ring lmRing, ring tailRing)
print p in a long way...
Definition: polys0.cc:203
void p_String0Short(const poly p, ring lmRing, ring tailRing)
if possible print p in a short way...
Definition: polys0.cc:184
void p_wrp0(poly p, ring ri)
Definition: polys0.cc:354
char * p_String(poly p, ring lmRing, ring tailRing)
Definition: polys0.cc:322
static void writemonLP(poly p, int ko, const ring r)
Definition: polys0.cc:104
void p_Write0(poly p, ring lmRing, ring tailRing)
Definition: polys0.cc:332
void p_wrp(poly p, ring lmRing, ring tailRing)
Definition: polys0.cc:373
void StringSetS(const char *st)
Definition: reporter.cc:128
void StringAppendS(const char *st)
Definition: reporter.cc:107
void PrintS(const char *s)
Definition: reporter.cc:284
char * StringEndS()
Definition: reporter.cc:151
void PrintLn()
Definition: reporter.cc:310
static BOOLEAN rField_is_R(const ring r)
Definition: ring.h:519
static char * rRingVar(short i, const ring r)
Definition: ring.h:578
static BOOLEAN rField_is_long_C(const ring r)
Definition: ring.h:546
static char const ** rParameter(const ring r)
(r->cf->parameter)
Definition: ring.h:626
static BOOLEAN rShortOut(const ring r)
Definition: ring.h:582
static BOOLEAN rField_is_long_R(const ring r)
Definition: ring.h:543
static BOOLEAN rCanShortOut(const ring r)
Definition: ring.h:587
static short rVar(const ring r)
#define rVar(r) (r->N)
Definition: ring.h:593
int p_mLastVblock(poly p, const ring ri)
Definition: shiftop.cc:420
#define loop
Definition: structs.h:79