source: git/libpolys/polys/polys0.cc @ 975db18

spielwiese
Last change on this file since 975db18 was ce1f78, checked in by Oleksandr Motsak <motsak@…>, 12 years ago
fixed short/long output of coeffs depending on ShortOut/CanShortOut options of rings Note: the ugly ShortOut correction p_String0Short/p_String0Long is due to Hans
  • Property mode set to 100644
File size: 5.4 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id$ */
5
6/*
7* ABSTRACT - all basic methods to convert polynomials to strings
8*/
9
10/* includes */
11#include "config.h"
12// #include <polys/structs.h>
13#include <coeffs/numbers.h>
14#include <polys/monomials/ring.h>
15#include <polys/monomials/p_polys.h>
16// #include <???/febase.h>
17
18/*2
19* writes a monomial (p),
20* uses form x*gen(.) if ko != coloumn number of p
21*/
22static void writemon(poly p, int ko, const ring r)
23{
24  assume(r != NULL);
25  const coeffs C = r->cf;
26  assume(C != NULL);
27 
28  BOOLEAN wroteCoef=FALSE,writeGen=FALSE;
29  const BOOLEAN bNotShortOut = (rShortOut(r) == FALSE);
30
31  if (pGetCoeff(p)!=NULL)
32    n_Normalize(pGetCoeff(p),C);
33
34  if (((p_GetComp(p,r) == (short)ko)
35    &&(p_LmIsConstantComp(p, r)))
36  || ((!n_IsOne(pGetCoeff(p),C))
37    && (!n_IsMOne(pGetCoeff(p),C))
38  )
39  )
40  {
41    if( bNotShortOut ) 
42      n_WriteLong(pGetCoeff(p),C);
43    else
44      n_WriteShort(pGetCoeff(p),C);
45   
46    wroteCoef=(bNotShortOut) 
47    || (rParameter(r)!=NULL)
48    || rField_is_R(r) || (rField_is_long_R(r)) || (rField_is_long_C(r));
49    writeGen=TRUE;
50  }
51  else if (n_IsMOne(pGetCoeff(p),C))
52  {
53    if (n_GreaterZero(pGetCoeff(p),C))
54    {
55      if( bNotShortOut ) 
56        n_WriteLong(pGetCoeff(p),C);
57      else
58        n_WriteShort(pGetCoeff(p),C);
59     
60      wroteCoef=(bNotShortOut)
61      || (rParameter(r)!=NULL)
62      || rField_is_R(r) || (rField_is_long_R(r)) || (rField_is_long_C(r));
63      writeGen=TRUE;
64    }
65    else
66      StringAppendS("-");
67  }
68
69  int i;
70  for (i=0; i<rVar(r); i++)
71  {
72    {
73      long ee = p_GetExp(p,i+1,r);
74      if (ee!=0L)
75      {
76        if (wroteCoef)
77          StringAppendS("*");
78        //else
79          wroteCoef=(bNotShortOut);
80        writeGen=TRUE;
81        StringAppendS(rRingVar(i, r));
82        if (ee != 1L)
83        {
84          if (bNotShortOut) StringAppendS("^");
85          StringAppend("%ld", ee);
86        }
87      }
88    }
89  }
90  //StringAppend("{%d}",p->Order);
91  if (p_GetComp(p, r) != (long)ko)
92  {
93    if (writeGen) StringAppendS("*");
94    StringAppend("gen(%d)", p_GetComp(p, r));
95  }
96}
97
98/// if possible print p in a short way...
99char* p_String0Short(const poly p, ring lmRing, ring tailRing)
100{
101  // NOTE: the following (non-thread-safe!) UGLYNESS
102  // (changing naRing->ShortOut for a while) is due to Hans!
103  // Just think of other ring using the VERY SAME naRing and possible
104  // side-effects...
105  const BOOLEAN bLMShortOut = rShortOut(lmRing); 
106  const BOOLEAN bTAILShortOut = rShortOut(tailRing); 
107
108  lmRing->ShortOut = rCanShortOut(lmRing);
109  tailRing->ShortOut = rCanShortOut(tailRing);
110 
111  char* res = p_String0(p, lmRing, tailRing);
112
113  lmRing->ShortOut = bLMShortOut;
114  tailRing->ShortOut = bTAILShortOut;
115
116  return res;
117}
118
119/// print p in a long way...
120char* p_String0Long(const poly p, ring lmRing, ring tailRing)
121{
122  // NOTE: the following (non-thread-safe!) UGLYNESS
123  // (changing naRing->ShortOut for a while) is due to Hans!
124  // Just think of other ring using the VERY SAME naRing and possible
125  // side-effects...
126  const BOOLEAN bLMShortOut = rShortOut(lmRing); 
127  const BOOLEAN bTAILShortOut = rShortOut(tailRing); 
128
129  lmRing->ShortOut = FALSE;
130  tailRing->ShortOut = FALSE;
131
132  char* res = p_String0(p, lmRing, tailRing);
133
134  lmRing->ShortOut = bLMShortOut;
135  tailRing->ShortOut = bTAILShortOut;
136
137  return res;
138}
139
140
141char* p_String0(poly p, ring lmRing, ring tailRing)
142{
143  if (p == NULL)
144  {
145    return StringAppendS("0");
146  }
147  if ((p_GetComp(p, lmRing) == 0) || (!lmRing->VectorOut))
148  {
149    writemon(p,0, lmRing);
150    p = pNext(p);
151    while (p!=NULL)
152    {
153      if ((p->coef==NULL)||n_GreaterZero(p->coef,tailRing->cf))
154        StringAppendS("+");
155      writemon(p,0, tailRing);
156      p = pNext(p);
157    }
158    return StringAppendS("");
159  }
160
161  long k = 1;
162  StringAppendS("[");
163  loop
164  {
165    while (k < p_GetComp(p,lmRing))
166    {
167      StringAppendS("0,");
168      k++;
169    }
170    writemon(p,k,lmRing);
171    pIter(p);
172    while ((p!=NULL) && (k == p_GetComp(p, tailRing)))
173    {
174      if (n_GreaterZero(p->coef,tailRing->cf)) StringAppendS("+");
175      writemon(p,k,tailRing);
176      pIter(p);
177    }
178    if (p == NULL) break;
179    StringAppendS(",");
180    k++;
181  }
182  return StringAppendS("]");
183}
184
185char* p_String(poly p, ring lmRing, ring tailRing)
186{
187  StringSetS("");
188  return p_String0(p, lmRing, tailRing);
189}
190
191/*2
192* writes a polynomial p to stdout
193*/
194void p_Write0(poly p, ring lmRing, ring tailRing)
195{
196  PrintS(p_String(p, lmRing, tailRing));
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.