source: git/libpolys/polys/polys0.cc @ a44bcf

spielwiese
Last change on this file since a44bcf was b39b313, checked in by Janko Boehm <boehm@…>, 11 years ago
Fix for integer std
  • 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...
98void 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  p_String0(p, lmRing, tailRing);
111
112  lmRing->ShortOut = bLMShortOut;
113  tailRing->ShortOut = bTAILShortOut;
114}
115
116/// print p in a long way...
117void p_String0Long(const poly p, ring lmRing, ring tailRing)
118{
119  // NOTE: the following (non-thread-safe!) UGLYNESS
120  // (changing naRing->ShortOut for a while) is due to Hans!
121  // Just think of other ring using the VERY SAME naRing and possible
122  // side-effects...
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  if ((p_GetComp(p, lmRing) == 0) || (!lmRing->VectorOut))
144  {
145    writemon(p,0, lmRing);
146    p = pNext(p);
147    while (p!=NULL)
148    {
149      assume((p->coef==NULL)||(!n_IsZero(p->coef,tailRing->cf)));
150      if ((p->coef==NULL)||n_GreaterZero(p->coef,tailRing->cf))
151        StringAppendS("+");
152      writemon(p,0, tailRing);
153      p = pNext(p);
154    }
155    return;
156  }
157
158  long k = 1;
159  StringAppendS("[");
160  loop
161  {
162    while (k < p_GetComp(p,lmRing))
163    {
164      StringAppendS("0,");
165      k++;
166    }
167    writemon(p,k,lmRing);
168    pIter(p);
169    while ((p!=NULL) && (k == p_GetComp(p, tailRing)))
170    {
171      if (n_GreaterZero(p->coef,tailRing->cf)) StringAppendS("+");
172      writemon(p,k,tailRing);
173      pIter(p);
174    }
175    if (p == NULL) break;
176    StringAppendS(",");
177    k++;
178  }
179  StringAppendS("]");
180}
181
182char* p_String(poly p, ring lmRing, ring tailRing)
183{
184  StringSetS("");
185  p_String0(p, lmRing, tailRing);
186  return StringEndS();
187}
188
189/*2
190* writes a polynomial p to stdout
191*/
192void p_Write0(poly p, ring lmRing, ring tailRing)
193{
194  char *s=p_String(p, lmRing, tailRing);
195  PrintS(s);
196  omFree(s);
197}
198
199/*2
200* writes a polynomial p to stdout followed by \n
201*/
202void p_Write(poly p, ring lmRing, ring tailRing)
203{
204  p_Write0(p, lmRing, tailRing);
205  PrintLn();
206}
207
208#if !defined(__OPTIMIZE__) || defined(KDEBUG)
209/*2
210*the standard debugging output:
211*print the first two monomials of the poly (wrp) or only the lead term (wrp0),
212*possibly followed by the string "+..."
213*/
214void p_wrp0(poly p, ring ri)
215{
216  poly r;
217
218  if (p==NULL) PrintS("NULL");
219  else if (pNext(p)==NULL) p_Write0(p, ri);
220  else
221  {
222    r = pNext(p);
223    pNext(p) = NULL;
224    p_Write0(p, ri);
225    if (r!=NULL)
226    {
227      PrintS("+...");
228      pNext(p) = r;
229    }
230  }
231}
232#endif
233void p_wrp(poly p, ring lmRing, ring tailRing)
234{
235  poly r;
236
237  if (p==NULL) PrintS("NULL");
238  else if (pNext(p)==NULL) p_Write0(p, lmRing);
239  else
240  {
241    r = pNext(pNext(p));
242    pNext(pNext(p)) = NULL;
243    p_Write0(p, lmRing, tailRing);
244    if (r!=NULL)
245    {
246      PrintS("+...");
247      pNext(pNext(p)) = r;
248    }
249  }
250}
Note: See TracBrowser for help on using the repository browser.