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 )
41  n_WriteLong(pGetCoeff(p),C);
42  else
43  n_WriteShort(pGetCoeff(p),C);
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 )
55  n_WriteLong(pGetCoeff(p),C);
56  else
57  n_WriteShort(pGetCoeff(p),C);
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 #ifdef HAVE_SHIFTBBA
70  if (rIsLPRing(r)) // this implies bNotShortOut
71  {
72  int lV = r->isLPring;
73  int lastVar = p_mLastVblock(p, r) * lV;
74  BOOLEAN wroteBlock = FALSE;
75  for (i=0; i<rVar(r); i++)
76  {
77  {
78  long ee = p_GetExp(p,i+1,r);
79  BOOLEAN endOfBlock = ((i+1) % lV) == 0;
80  BOOLEAN writeEmptyBlock = ee==0L && endOfBlock && !wroteBlock && i < lastVar;
81  if (ee!=0L || writeEmptyBlock)
82  {
83  if (wroteBlock)
84  StringAppendS("&");
85  else if (wroteCoef)
86  StringAppendS("*");
87  //else
88  wroteCoef=TRUE; //(bNotShortOut);
89  writeGen=TRUE;
90  if (writeEmptyBlock)
91  StringAppendS("_");
92  else
93  {
94  StringAppendS(rRingVar(i, r));
95  if (ee != 1L)
96  {
97  StringAppend("^%ld", ee);
98  }
99  wroteBlock = TRUE;
100  }
101  }
102  if (endOfBlock)
103  wroteBlock = FALSE;
104  }
105  }
106  }
107  else
108 #endif
109  {
110  for (i=0; i<rVar(r); i++)
111  {
112  {
113  long ee = p_GetExp(p,i+1,r);
114  if (ee!=0L)
115  {
116  if (wroteCoef)
117  StringAppendS("*");
118  //else
119  wroteCoef=(bNotShortOut);
120  writeGen=TRUE;
121  StringAppendS(rRingVar(i, r));
122  if (ee != 1L)
123  {
124  if (bNotShortOut) StringAppendS("^");
125  StringAppend("%ld", ee);
126  }
127  }
128  }
129  }
130  }
131  //StringAppend("{%d}",p->Order);
132  if (p_GetComp(p, r) != (long)ko)
133  {
134  if (writeGen) StringAppendS("*");
135  StringAppend("gen(%d)", p_GetComp(p, r));
136  }
137 }
138 
139 /// if possible print p in a short way...
140 void p_String0Short(const poly p, ring lmRing, ring tailRing)
141 {
142  // NOTE: the following (non-thread-safe!) UGLYNESS
143  // (changing naRing->ShortOut for a while) is due to Hans!
144  // Just think of other ring using the VERY SAME naRing and possible
145  // side-effects...
146  const BOOLEAN bLMShortOut = rShortOut(lmRing);
147  const BOOLEAN bTAILShortOut = rShortOut(tailRing);
148 
149  lmRing->ShortOut = rCanShortOut(lmRing);
150  tailRing->ShortOut = rCanShortOut(tailRing);
151 
152  p_String0(p, lmRing, tailRing);
153 
154  lmRing->ShortOut = bLMShortOut;
155  tailRing->ShortOut = bTAILShortOut;
156 }
157 
158 /// print p in a long way...
159 void p_String0Long(const poly p, ring lmRing, ring tailRing)
160 {
161  // NOTE: the following (non-thread-safe!) UGLYNESS
162  // (changing naRing->ShortOut for a while) is due to Hans!
163  // Just think of other ring using the VERY SAME naRing and possible
164  // side-effects...
165  // but this is not a problem: i/o is not thread-safe anyway.
166  const BOOLEAN bLMShortOut = rShortOut(lmRing);
167  const BOOLEAN bTAILShortOut = rShortOut(tailRing);
168 
169  lmRing->ShortOut = FALSE;
170  tailRing->ShortOut = FALSE;
171 
172  p_String0(p, lmRing, tailRing);
173 
174  lmRing->ShortOut = bLMShortOut;
175  tailRing->ShortOut = bTAILShortOut;
176 }
177 
178 
179 void p_String0(poly p, ring lmRing, ring tailRing)
180 {
181  if (p == NULL)
182  {
183  StringAppendS("0");
184  return;
185  }
186  p_Normalize(p,lmRing);
187  if ((n_GetChar(lmRing->cf) == 0)
188  && (nCoeff_is_transExt(lmRing->cf)))
189  p_Normalize(p,lmRing); /* Manual/absfact.tst */
190  if ((p_GetComp(p, lmRing) == 0) || (!lmRing->VectorOut))
191  {
192  writemon(p,0, lmRing);
193  p = pNext(p);
194  while (p!=NULL)
195  {
196  assume((p->coef==NULL)||(!n_IsZero(p->coef,tailRing->cf)));
197  if ((p->coef==NULL)||n_GreaterZero(p->coef,tailRing->cf))
198  StringAppendS("+");
199  writemon(p,0, tailRing);
200  p = pNext(p);
201  }
202  return;
203  }
204 
205  long k = 1;
206  StringAppendS("[");
207  loop
208  {
209  while (k < p_GetComp(p,lmRing))
210  {
211  StringAppendS("0,");
212  k++;
213  }
214  writemon(p,k,lmRing);
215  pIter(p);
216  while ((p!=NULL) && (k == p_GetComp(p, tailRing)))
217  {
218  if (n_GreaterZero(p->coef,tailRing->cf)) StringAppendS("+");
219  writemon(p,k,tailRing);
220  pIter(p);
221  }
222  if (p == NULL) break;
223  StringAppendS(",");
224  k++;
225  }
226  StringAppendS("]");
227 }
228 
229 char* p_String(poly p, ring lmRing, ring tailRing)
230 {
231  StringSetS("");
232  p_String0(p, lmRing, tailRing);
233  return StringEndS();
234 }
235 
236 /*2
237 * writes a polynomial p to stdout
238 */
239 void p_Write0(poly p, ring lmRing, ring tailRing)
240 {
241  char *s=p_String(p, lmRing, tailRing);
242  PrintS(s);
243  omFree(s);
244 }
245 
246 /*2
247 * writes a polynomial p to stdout followed by \n
248 */
249 void p_Write(poly p, ring lmRing, ring tailRing)
250 {
251  p_Write0(p, lmRing, tailRing);
252  PrintLn();
253 }
254 
255 #if !defined(__OPTIMIZE__) || defined(KDEBUG)
256 /*2
257 *the standard debugging output:
258 *print the first two monomials of the poly (wrp) or only the lead term (wrp0),
259 *possibly followed by the string "+..."
260 */
261 void p_wrp0(poly p, ring ri)
262 {
263  poly r;
264 
265  if (p==NULL) PrintS("NULL");
266  else if (pNext(p)==NULL) p_Write0(p, ri);
267  else
268  {
269  r = pNext(p);
270  pNext(p) = NULL;
271  p_Write0(p, ri);
272  if (r!=NULL)
273  {
274  PrintS("+...");
275  pNext(p) = r;
276  }
277  }
278 }
279 #endif
280 void p_wrp(poly p, ring lmRing, ring tailRing)
281 {
282  poly r;
283 
284  if (p==NULL) PrintS("NULL");
285  else if (pNext(p)==NULL) p_Write0(p, lmRing);
286  else
287  {
288  r = pNext(pNext(p));
289  pNext(pNext(p)) = NULL;
290  p_Write0(p, tailRing);
291  if (r!=NULL)
292  {
293  PrintS("+...");
294  pNext(pNext(p)) = r;
295  }
296  }
297 }
const CanonicalForm int s
Definition: facAbsFact.cc:55
char * p_String(poly p, ring lmRing, ring tailRing)
Definition: polys0.cc:229
int p_mLastVblock(poly p, const ring ri)
Definition: shiftop.cc:412
static BOOLEAN p_LmIsConstantComp(const poly p, const ring r)
Definition: p_polys.h:961
void PrintLn()
Definition: reporter.cc:310
#define FALSE
Definition: auxiliary.h:94
static FORCE_INLINE BOOLEAN n_IsOne(number n, const coeffs r)
TRUE iff &#39;n&#39; represents the one element.
Definition: coeffs.h:468
static BOOLEAN rField_is_R(const ring r)
Definition: ring.h:513
#define p_GetComp(p, r)
Definition: monomials.h:64
static BOOLEAN rShortOut(const ring r)
Definition: ring.h:575
static short rVar(const ring r)
#define rVar(r) (r->N)
Definition: ring.h:586
void p_String0Short(const poly p, ring lmRing, ring tailRing)
if possible print p in a short way...
Definition: polys0.cc:140
static FORCE_INLINE int n_GetChar(const coeffs r)
Return the characteristic of the coeff. domain.
Definition: coeffs.h:444
#define TRUE
Definition: auxiliary.h:98
int k
Definition: cfEzgcd.cc:92
static char const ** rParameter(const ring r)
(r->cf->parameter)
Definition: ring.h:619
char * StringEndS()
Definition: reporter.cc:151
#define loop
Definition: structs.h:80
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
static BOOLEAN rIsLPRing(const ring r)
Definition: ring.h:408
static BOOLEAN rCanShortOut(const ring r)
Definition: ring.h:580
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
#define pIter(p)
Definition: monomials.h:37
void p_Write(poly p, ring lmRing, ring tailRing)
Definition: polys0.cc:249
void p_wrp(poly p, ring lmRing, ring tailRing)
Definition: polys0.cc:280
void p_Write0(poly p, ring lmRing, ring tailRing)
Definition: polys0.cc:239
static long p_GetExp(const poly p, const unsigned long iBitmask, const int VarOffset)
get a single variable exponent : the integer VarOffset encodes:
Definition: p_polys.h:468
#define omFree(addr)
Definition: omAllocDecl.h:261
#define assume(x)
Definition: mod2.h:390
The main handler for Singular numbers which are suitable for Singular polynomials.
void StringSetS(const char *st)
Definition: reporter.cc:128
void StringAppendS(const char *st)
Definition: reporter.cc:107
All the auxiliary stuff.
void p_String0Long(const poly p, ring lmRing, ring tailRing)
print p in a long way...
Definition: polys0.cc:159
static FORCE_INLINE BOOLEAN nCoeff_is_transExt(const coeffs r)
TRUE iff r represents a transcendental extension field.
Definition: coeffs.h:940
#define StringAppend
Definition: emacs.cc:79
int i
Definition: cfEzgcd.cc:125
void PrintS(const char *s)
Definition: reporter.cc:284
static char * rRingVar(short i, const ring r)
Definition: ring.h:571
static FORCE_INLINE BOOLEAN n_IsZero(number n, const coeffs r)
TRUE iff &#39;n&#39; represents the zero element.
Definition: coeffs.h:464
void p_wrp0(poly p, ring ri)
Definition: polys0.cc:261
static BOOLEAN rField_is_long_C(const ring r)
Definition: ring.h:540
void p_Normalize(poly p, const ring r)
Definition: p_polys.cc:3732
#define NULL
Definition: omList.c:12
static BOOLEAN rField_is_long_R(const ring r)
Definition: ring.h:537
#define pNext(p)
Definition: monomials.h:36
void p_String0(poly p, ring lmRing, ring tailRing)
print p according to ShortOut in lmRing & tailRing
Definition: polys0.cc:179
static FORCE_INLINE BOOLEAN n_IsMOne(number n, const coeffs r)
TRUE iff &#39;n&#39; represents the additive inverse of the one element, i.e. -1.
Definition: coeffs.h:472
static FORCE_INLINE BOOLEAN n_GreaterZero(number n, const coeffs r)
ordered fields: TRUE iff &#39;n&#39; is positive; in Z/pZ: TRUE iff 0 < m <= roundedBelow(p/2), where m is the long representing n in C: TRUE iff (Im(n) != 0 and Im(n) >= 0) or (Im(n) == 0 and Re(n) >= 0) in K(a)/<p(a)>: TRUE iff (n != 0 and (LC(n) > 0 or deg(n) > 0)) in K(t_1, ..., t_n): TRUE iff (LC(numerator(n) is a constant and > 0) or (LC(numerator(n) is not a constant) in Z/2^kZ: TRUE iff 0 < n <= 2^(k-1) in Z/mZ: TRUE iff the internal mpz is greater than zero in Z: TRUE iff n > 0
Definition: coeffs.h:494
int p
Definition: cfModGcd.cc:4019
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, e.g. in K(a): a2 instead of a^2
Definition: coeffs.h:588
int BOOLEAN
Definition: auxiliary.h:85
static void writemon(poly p, int ko, const ring r)
Definition: polys0.cc:24