# source:git/libpolys/polys/polys0.cc@8968f10

spielwiese
Last change on this file since 8968f10 was 8968f10, checked in by Hans Schoenemann <hannes@…>, 8 years ago
writemon/p_Write0/p_String opt. (p. 1)
• Property mode set to `100644`
File size: 5.4 KB
Line
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 <polys/structs.h>
14#include <coeffs/numbers.h>
15#include <polys/monomials/ring.h>
16#include <polys/monomials/p_polys.h>
17// #include <???/febase.h>
18
19/*2
20* writes a monomial (p),
21* uses form x*gen(.) if ko != coloumn number of p
22*/
23static void writemon(poly p, int ko, const ring r)
24{
25  assume(r != NULL);
26  const coeffs C = r->cf;
27  assume(C != NULL);
28
29  BOOLEAN wroteCoef=FALSE,writeGen=FALSE;
30  const BOOLEAN bNotShortOut = (rShortOut(r) == FALSE);
31
32  if (((p_GetComp(p,r) == ko)
33    &&(p_LmIsConstantComp(p, r)))
34  || ((!n_IsOne(pGetCoeff(p),C))
35    && (!n_IsMOne(pGetCoeff(p),C))
36  )
37  )
38  {
39    if( bNotShortOut )
40      n_WriteLong(pGetCoeff(p),C);
41    else
42      n_WriteShort(pGetCoeff(p),C);
43
44    wroteCoef=(bNotShortOut)
45    || (rParameter(r)!=NULL)
46    || rField_is_R(r) || (rField_is_long_R(r)) || (rField_is_long_C(r));
47    writeGen=TRUE;
48  }
49  else if (n_IsMOne(pGetCoeff(p),C))
50  {
51    if (n_GreaterZero(pGetCoeff(p),C))
52    {
53      if( bNotShortOut )
54        n_WriteLong(pGetCoeff(p),C);
55      else
56        n_WriteShort(pGetCoeff(p),C);
57
58      wroteCoef=(bNotShortOut)
59      || (rParameter(r)!=NULL)
60      || rField_is_R(r) || (rField_is_long_R(r)) || (rField_is_long_C(r));
61      writeGen=TRUE;
62    }
63    else
64      StringAppendS("-");
65  }
66
67  int i;
68  for (i=0; i<rVar(r); i++)
69  {
70    {
71      long ee = p_GetExp(p,i+1,r);
72      if (ee!=0L)
73      {
74        if (wroteCoef)
75          StringAppendS("*");
76        //else
77          wroteCoef=(bNotShortOut);
78        writeGen=TRUE;
79        StringAppendS(rRingVar(i, r));
80        if (ee != 1L)
81        {
82          if (bNotShortOut) StringAppendS("^");
83          StringAppend("%ld", ee);
84        }
85      }
86    }
87  }
88  //StringAppend("{%d}",p->Order);
89  if (p_GetComp(p, r) != (long)ko)
90  {
91    if (writeGen) StringAppendS("*");
92    StringAppend("gen(%d)", p_GetComp(p, r));
93  }
94}
95
96/// if possible print p in a short way...
97void p_String0Short(const poly p, ring lmRing, ring tailRing)
98{
99  // NOTE: the following (non-thread-safe!) UGLYNESS
100  // (changing naRing->ShortOut for a while) is due to Hans!
101  // Just think of other ring using the VERY SAME naRing and possible
102  // side-effects...
103  const BOOLEAN bLMShortOut = rShortOut(lmRing);
104  const BOOLEAN bTAILShortOut = rShortOut(tailRing);
105
106  lmRing->ShortOut = rCanShortOut(lmRing);
107  tailRing->ShortOut = rCanShortOut(tailRing);
108
109  p_String0(p, lmRing, tailRing);
110
111  lmRing->ShortOut = bLMShortOut;
112  tailRing->ShortOut = bTAILShortOut;
113}
114
115/// print p in a long way...
116void p_String0Long(const poly p, ring lmRing, ring tailRing)
117{
118  // NOTE: the following (non-thread-safe!) UGLYNESS
119  // (changing naRing->ShortOut for a while) is due to Hans!
120  // Just think of other ring using the VERY SAME naRing and possible
121  // side-effects...
122  // but this is not a problem: i/o is not thread-safe anyway.
123  const BOOLEAN bLMShortOut = rShortOut(lmRing);
124  const BOOLEAN bTAILShortOut = rShortOut(tailRing);
125
126  lmRing->ShortOut = FALSE;
127  tailRing->ShortOut = FALSE;
128
129  p_String0(p, lmRing, tailRing);
130
131  lmRing->ShortOut = bLMShortOut;
132  tailRing->ShortOut = bTAILShortOut;
133}
134
135
136void p_String0(poly p, ring lmRing, ring tailRing)
137{
138  if (p == NULL)
139  {
140    StringAppendS("0");
141    return;
142  }
143  p_Normalize(p,lmRing);
144  p_Normalize(p,lmRing); /* Manual/absfact.tst */
145  if ((p_GetComp(p, lmRing) == 0) || (!lmRing->VectorOut))
146  {
147    writemon(p,0, lmRing);
148    p = pNext(p);
149    while (p!=NULL)
150    {
151      assume((p->coef==NULL)||(!n_IsZero(p->coef,tailRing->cf)));
152      if ((p->coef==NULL)||n_GreaterZero(p->coef,tailRing->cf))
153        StringAppendS("+");
154      writemon(p,0, tailRing);
155      p = pNext(p);
156    }
157    return;
158  }
159
160  long k = 1;
161  StringAppendS("[");
162  loop
163  {
164    while (k < p_GetComp(p,lmRing))
165    {
166      StringAppendS("0,");
167      k++;
168    }
169    writemon(p,k,lmRing);
170    pIter(p);
171    while ((p!=NULL) && (k == p_GetComp(p, tailRing)))
172    {
173      if (n_GreaterZero(p->coef,tailRing->cf)) StringAppendS("+");
174      writemon(p,k,tailRing);
175      pIter(p);
176    }
177    if (p == NULL) break;
178    StringAppendS(",");
179    k++;
180  }
181  StringAppendS("]");
182}
183
184char* p_String(poly p, ring lmRing, ring tailRing)
185{
186  StringSetS("");
187  p_String0(p, lmRing, tailRing);
188  return StringEndS();
189}
190
191/*2
192* writes a polynomial p to stdout
193*/
194void p_Write0(poly p, ring lmRing, ring tailRing)
195{
196  char *s=p_String(p, lmRing, tailRing);
197  PrintS(s);
198  omFree(s);
199}
200
201/*2
202* writes a polynomial p to stdout followed by \n
203*/
204void p_Write(poly p, ring lmRing, ring tailRing)
205{
206  p_Write0(p, lmRing, tailRing);
207  PrintLn();
208}
209
210#if !defined(__OPTIMIZE__) || defined(KDEBUG)
211/*2
212*the standard debugging output:
213*print the first two monomials of the poly (wrp) or only the lead term (wrp0),
214*possibly followed by the string "+..."
215*/
216void p_wrp0(poly p, ring ri)
217{
218  poly r;
219
220  if (p==NULL) PrintS("NULL");
221  else if (pNext(p)==NULL) p_Write0(p, ri);
222  else
223  {
224    r = pNext(p);
225    pNext(p) = NULL;
226    p_Write0(p, ri);
227    if (r!=NULL)
228    {
229      PrintS("+...");
230      pNext(p) = r;
231    }
232  }
233}
234#endif
235void p_wrp(poly p, ring lmRing, ring tailRing)
236{
237  poly r;
238
239  if (p==NULL) PrintS("NULL");
240  else if (pNext(p)==NULL) p_Write0(p, lmRing);
241  else
242  {
243    r = pNext(pNext(p));
244    pNext(pNext(p)) = NULL;
245    p_Write0(p, tailRing);
246    if (r!=NULL)
247    {
248      PrintS("+...");
249      pNext(pNext(p)) = r;
250    }
251  }
252}
Note: See TracBrowser for help on using the repository browser.