source: git/libpolys/polys/polys0.cc @ 0635d51

spielwiese
Last change on this file since 0635d51 was 16f511, checked in by Oleksandr Motsak <motsak@…>, 11 years ago
Fixed the usage of "config.h" (if defined HAVE_CONFIG_H)
  • 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#ifdef HAVE_CONFIG_H
11#include "config.h"
12#endif /* HAVE_CONFIG_H */
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 (pGetCoeff(p)!=NULL)
33    n_Normalize(pGetCoeff(p),C);
34
35  if (((p_GetComp(p,r) == (short)ko)
36    &&(p_LmIsConstantComp(p, r)))
37  || ((!n_IsOne(pGetCoeff(p),C))
38    && (!n_IsMOne(pGetCoeff(p),C))
39  )
40  )
41  {
42    if( bNotShortOut ) 
43      n_WriteLong(pGetCoeff(p),C);
44    else
45      n_WriteShort(pGetCoeff(p),C);
46   
47    wroteCoef=(bNotShortOut) 
48    || (rParameter(r)!=NULL)
49    || rField_is_R(r) || (rField_is_long_R(r)) || (rField_is_long_C(r));
50    writeGen=TRUE;
51  }
52  else if (n_IsMOne(pGetCoeff(p),C))
53  {
54    if (n_GreaterZero(pGetCoeff(p),C))
55    {
56      if( bNotShortOut ) 
57        n_WriteLong(pGetCoeff(p),C);
58      else
59        n_WriteShort(pGetCoeff(p),C);
60     
61      wroteCoef=(bNotShortOut)
62      || (rParameter(r)!=NULL)
63      || rField_is_R(r) || (rField_is_long_R(r)) || (rField_is_long_C(r));
64      writeGen=TRUE;
65    }
66    else
67      StringAppendS("-");
68  }
69
70  int i;
71  for (i=0; i<rVar(r); i++)
72  {
73    {
74      long ee = p_GetExp(p,i+1,r);
75      if (ee!=0L)
76      {
77        if (wroteCoef)
78          StringAppendS("*");
79        //else
80          wroteCoef=(bNotShortOut);
81        writeGen=TRUE;
82        StringAppendS(rRingVar(i, r));
83        if (ee != 1L)
84        {
85          if (bNotShortOut) StringAppendS("^");
86          StringAppend("%ld", ee);
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/// if possible print p in a short way...
100void p_String0Short(const poly p, ring lmRing, ring tailRing)
101{
102  // NOTE: the following (non-thread-safe!) UGLYNESS
103  // (changing naRing->ShortOut for a while) is due to Hans!
104  // Just think of other ring using the VERY SAME naRing and possible
105  // side-effects...
106  const BOOLEAN bLMShortOut = rShortOut(lmRing); 
107  const BOOLEAN bTAILShortOut = rShortOut(tailRing); 
108
109  lmRing->ShortOut = rCanShortOut(lmRing);
110  tailRing->ShortOut = rCanShortOut(tailRing);
111 
112  p_String0(p, lmRing, tailRing);
113
114  lmRing->ShortOut = bLMShortOut;
115  tailRing->ShortOut = bTAILShortOut;
116}
117
118/// print p in a long way...
119void p_String0Long(const poly p, ring lmRing, ring tailRing)
120{
121  // NOTE: the following (non-thread-safe!) UGLYNESS
122  // (changing naRing->ShortOut for a while) is due to Hans!
123  // Just think of other ring using the VERY SAME naRing and possible
124  // side-effects...
125  const BOOLEAN bLMShortOut = rShortOut(lmRing); 
126  const BOOLEAN bTAILShortOut = rShortOut(tailRing); 
127
128  lmRing->ShortOut = FALSE;
129  tailRing->ShortOut = FALSE;
130
131  p_String0(p, lmRing, tailRing);
132
133  lmRing->ShortOut = bLMShortOut;
134  tailRing->ShortOut = bTAILShortOut;
135}
136
137
138void p_String0(poly p, ring lmRing, ring tailRing)
139{
140  if (p == NULL)
141  {
142    StringAppendS("0");
143    return;
144  }
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, lmRing, 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.