source: git/libpolys/polys/polys0.cc @ 85bcd6

spielwiese
Last change on this file since 85bcd6 was 6ce030f, checked in by Oleksandr Motsak <motsak@…>, 12 years ago
removal of the $Id$ svn tag from everywhere NOTE: the git SHA1 may be used instead (only on special places) NOTE: the libraries Singular/LIB/*.lib still contain the marker due to our current use of svn
  • 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#include "config.h"
11// #include <polys/structs.h>
12#include <coeffs/numbers.h>
13#include <polys/monomials/ring.h>
14#include <polys/monomials/p_polys.h>
15// #include <???/febase.h>
16
17/*2
18* writes a monomial (p),
19* uses form x*gen(.) if ko != coloumn number of p
20*/
21static void writemon(poly p, int ko, const ring r)
22{
23  assume(r != NULL);
24  const coeffs C = r->cf;
25  assume(C != NULL);
26 
27  BOOLEAN wroteCoef=FALSE,writeGen=FALSE;
28  const BOOLEAN bNotShortOut = (rShortOut(r) == FALSE);
29
30  if (pGetCoeff(p)!=NULL)
31    n_Normalize(pGetCoeff(p),C);
32
33  if (((p_GetComp(p,r) == (short)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  for (i=0; i<rVar(r); i++)
70  {
71    {
72      long ee = p_GetExp(p,i+1,r);
73      if (ee!=0L)
74      {
75        if (wroteCoef)
76          StringAppendS("*");
77        //else
78          wroteCoef=(bNotShortOut);
79        writeGen=TRUE;
80        StringAppendS(rRingVar(i, r));
81        if (ee != 1L)
82        {
83          if (bNotShortOut) StringAppendS("^");
84          StringAppend("%ld", ee);
85        }
86      }
87    }
88  }
89  //StringAppend("{%d}",p->Order);
90  if (p_GetComp(p, r) != (long)ko)
91  {
92    if (writeGen) StringAppendS("*");
93    StringAppend("gen(%d)", p_GetComp(p, r));
94  }
95}
96
97/// if possible print p in a short way...
98char* p_String0Short(const poly p, ring lmRing, ring tailRing)
99{
100  // NOTE: the following (non-thread-safe!) UGLYNESS
101  // (changing naRing->ShortOut for a while) is due to Hans!
102  // Just think of other ring using the VERY SAME naRing and possible
103  // side-effects...
104  const BOOLEAN bLMShortOut = rShortOut(lmRing); 
105  const BOOLEAN bTAILShortOut = rShortOut(tailRing); 
106
107  lmRing->ShortOut = rCanShortOut(lmRing);
108  tailRing->ShortOut = rCanShortOut(tailRing);
109 
110  char* res = p_String0(p, lmRing, tailRing);
111
112  lmRing->ShortOut = bLMShortOut;
113  tailRing->ShortOut = bTAILShortOut;
114
115  return res;
116}
117
118/// print p in a long way...
119char* 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  char* res = p_String0(p, lmRing, tailRing);
132
133  lmRing->ShortOut = bLMShortOut;
134  tailRing->ShortOut = bTAILShortOut;
135
136  return res;
137}
138
139
140char* p_String0(poly p, ring lmRing, ring tailRing)
141{
142  if (p == NULL)
143  {
144    return StringAppendS("0");
145  }
146  if ((p_GetComp(p, lmRing) == 0) || (!lmRing->VectorOut))
147  {
148    writemon(p,0, lmRing);
149    p = pNext(p);
150    while (p!=NULL)
151    {
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 StringAppendS("");
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  return StringAppendS("]");
182}
183
184char* p_String(poly p, ring lmRing, ring tailRing)
185{
186  StringSetS("");
187  return p_String0(p, lmRing, tailRing);
188}
189
190/*2
191* writes a polynomial p to stdout
192*/
193void p_Write0(poly p, ring lmRing, ring tailRing)
194{
195  PrintS(p_String(p, lmRing, tailRing));
196}
197
198/*2
199* writes a polynomial p to stdout followed by \n
200*/
201void p_Write(poly p, ring lmRing, ring tailRing)
202{
203  p_Write0(p, lmRing, tailRing);
204  PrintLn();
205}
206
207#if !defined(__OPTIMIZE__) || defined(KDEBUG)
208/*2
209*the standard debugging output:
210*print the first two monomials of the poly (wrp) or only the lead term (wrp0),
211*possibly followed by the string "+..."
212*/
213void p_wrp0(poly p, ring ri)
214{
215  poly r;
216
217  if (p==NULL) PrintS("NULL");
218  else if (pNext(p)==NULL) p_Write0(p, ri);
219  else
220  {
221    r = pNext(p);
222    pNext(p) = NULL;
223    p_Write0(p, ri);
224    if (r!=NULL)
225    {
226      PrintS("+...");
227      pNext(p) = r;
228    }
229  }
230}
231#endif
232void p_wrp(poly p, ring lmRing, ring tailRing)
233{
234  poly r;
235
236  if (p==NULL) PrintS("NULL");
237  else if (pNext(p)==NULL) p_Write0(p, lmRing);
238  else
239  {
240    r = pNext(pNext(p));
241    pNext(pNext(p)) = NULL;
242    p_Write0(p, lmRing, tailRing);
243    if (r!=NULL)
244    {
245      PrintS("+...");
246      pNext(pNext(p)) = r;
247    }
248  }
249}
Note: See TracBrowser for help on using the repository browser.