1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id: polys0.cc,v 1.19 2000-12-06 12:38:30 Singular Exp $ */ |
---|
5 | |
---|
6 | /* |
---|
7 | * ABSTRACT - all basic methods to convert polynomials to strings |
---|
8 | */ |
---|
9 | |
---|
10 | /* includes */ |
---|
11 | #include "mod2.h" |
---|
12 | #include "tok.h" |
---|
13 | #include "structs.h" |
---|
14 | #include "numbers.h" |
---|
15 | #include "ring.h" |
---|
16 | #include "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 | */ |
---|
23 | static void writemon(poly p, int ko, ring r) |
---|
24 | { |
---|
25 | BOOLEAN wroteCoef=FALSE,writeGen=FALSE; |
---|
26 | |
---|
27 | if (pGetCoeff(p)!=NULL) |
---|
28 | n_Normalize(pGetCoeff(p),r); |
---|
29 | |
---|
30 | if (((p_GetComp(p,r) == (short)ko) |
---|
31 | &&(p_LmIsConstantComp(p, r))) |
---|
32 | || ((!n_IsOne(pGetCoeff(p),r)) |
---|
33 | && (!n_IsMOne(pGetCoeff(p),r)) |
---|
34 | ) |
---|
35 | ) |
---|
36 | { |
---|
37 | n_Write(p->coef,r); |
---|
38 | wroteCoef=(rShortOut(r) == FALSE ||(r->parameter!=NULL)); |
---|
39 | writeGen=TRUE; |
---|
40 | } |
---|
41 | else if (n_IsMOne(pGetCoeff(p),r)) |
---|
42 | { |
---|
43 | if (n_GreaterZero(pGetCoeff(p),r)) |
---|
44 | { |
---|
45 | n_Write(p->coef,r); |
---|
46 | wroteCoef=(rShortOut(r) == FALSE ||(r->parameter!=NULL)); |
---|
47 | writeGen=TRUE; |
---|
48 | } |
---|
49 | else |
---|
50 | StringAppendS("-"); |
---|
51 | } |
---|
52 | |
---|
53 | int i; |
---|
54 | for (i=0; i<r->N; i++) |
---|
55 | { |
---|
56 | { |
---|
57 | Exponent_t ee = p_GetExp(p,i+1,r); |
---|
58 | if (ee!=0) |
---|
59 | { |
---|
60 | if (wroteCoef) |
---|
61 | StringAppendS("*"); |
---|
62 | //else |
---|
63 | wroteCoef=(rShortOut(r) == FALSE); |
---|
64 | writeGen=TRUE; |
---|
65 | StringAppendS(rRingVar(i, r)); |
---|
66 | if (ee != 1) |
---|
67 | { |
---|
68 | if (rShortOut(r)==0) StringAppendS("^"); |
---|
69 | StringAppend("%d", ee); |
---|
70 | } |
---|
71 | } |
---|
72 | } |
---|
73 | } |
---|
74 | //StringAppend("{%d}",p->Order); |
---|
75 | if (p_GetComp(p, r) != (Exponent_t)ko) |
---|
76 | { |
---|
77 | if (writeGen) StringAppendS("*"); |
---|
78 | StringAppend("gen(%d)", p_GetComp(p, r)); |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | char* p_String0(poly p, ring lmRing, ring tailRing) |
---|
83 | { |
---|
84 | if (p == NULL) |
---|
85 | { |
---|
86 | return StringAppendS("0"); |
---|
87 | } |
---|
88 | if ((p_GetComp(p, lmRing) == 0) || (!lmRing->VectorOut)) |
---|
89 | { |
---|
90 | writemon(p,0, lmRing); |
---|
91 | p = pNext(p); |
---|
92 | while (p!=NULL) |
---|
93 | { |
---|
94 | if ((p->coef==NULL)||n_GreaterZero(p->coef,tailRing)) |
---|
95 | StringAppendS("+"); |
---|
96 | writemon(p,0, tailRing); |
---|
97 | p = pNext(p); |
---|
98 | } |
---|
99 | return StringAppendS(""); |
---|
100 | } |
---|
101 | |
---|
102 | Exponent_t k = 1; |
---|
103 | StringAppendS("["); |
---|
104 | loop |
---|
105 | { |
---|
106 | while (k < p_GetComp(p,lmRing)) |
---|
107 | { |
---|
108 | StringAppendS("0,"); |
---|
109 | k++; |
---|
110 | } |
---|
111 | writemon(p,k,lmRing); |
---|
112 | pIter(p); |
---|
113 | while ((p!=NULL) && (k == p_GetComp(p, tailRing))) |
---|
114 | { |
---|
115 | if (n_GreaterZero(p->coef,tailRing)) StringAppendS("+"); |
---|
116 | writemon(p,k,tailRing); |
---|
117 | pIter(p); |
---|
118 | } |
---|
119 | if (p == NULL) break; |
---|
120 | StringAppendS(","); |
---|
121 | k++; |
---|
122 | } |
---|
123 | return StringAppendS("]"); |
---|
124 | } |
---|
125 | |
---|
126 | char* p_String(poly p, ring lmRing, ring tailRing) |
---|
127 | { |
---|
128 | StringSetS(""); |
---|
129 | return p_String0(p, lmRing, tailRing); |
---|
130 | } |
---|
131 | |
---|
132 | /*2 |
---|
133 | * writes a polynomial p to stdout |
---|
134 | */ |
---|
135 | void p_Write0(poly p, ring lmRing, ring tailRing) |
---|
136 | { |
---|
137 | PrintS(p_String(p, lmRing, tailRing)); |
---|
138 | } |
---|
139 | |
---|
140 | /*2 |
---|
141 | * writes a polynomial p to stdout followed by \n |
---|
142 | */ |
---|
143 | void p_Write(poly p, ring lmRing, ring tailRing) |
---|
144 | { |
---|
145 | p_Write0(p, lmRing, tailRing); |
---|
146 | PrintLn(); |
---|
147 | } |
---|
148 | |
---|
149 | /*2 |
---|
150 | *the standard debugging output: |
---|
151 | *print the first two monomials of the poly (wrp) or only the lead term (wrp0), |
---|
152 | *possibly followed by the string "+..." |
---|
153 | */ |
---|
154 | void p_wrp0(poly p, ring ri) |
---|
155 | { |
---|
156 | poly r; |
---|
157 | |
---|
158 | if (p==NULL) PrintS("NULL"); |
---|
159 | else if (pNext(p)==NULL) p_Write0(p, ri); |
---|
160 | else |
---|
161 | { |
---|
162 | r = pNext(p); |
---|
163 | pNext(p) = NULL; |
---|
164 | p_Write0(p, ri); |
---|
165 | if (r!=NULL) |
---|
166 | { |
---|
167 | PrintS("+..."); |
---|
168 | pNext(p) = r; |
---|
169 | } |
---|
170 | } |
---|
171 | } |
---|
172 | #if 1 |
---|
173 | void p_wrp(poly p, ring lmRing, ring tailRing) |
---|
174 | { |
---|
175 | poly r; |
---|
176 | |
---|
177 | if (p==NULL) PrintS("NULL"); |
---|
178 | else if (pNext(p)==NULL) p_Write0(p, lmRing); |
---|
179 | else |
---|
180 | { |
---|
181 | r = pNext(pNext(p)); |
---|
182 | pNext(pNext(p)) = NULL; |
---|
183 | p_Write0(p, lmRing, tailRing); |
---|
184 | if (r!=NULL) |
---|
185 | { |
---|
186 | PrintS("+..."); |
---|
187 | pNext(pNext(p)) = r; |
---|
188 | } |
---|
189 | } |
---|
190 | } |
---|
191 | #else |
---|
192 | // this is for use with buckets |
---|
193 | void p_wrp(poly p, ring lmRing, ring tailRing) |
---|
194 | { |
---|
195 | poly r; |
---|
196 | |
---|
197 | if (p==NULL) PrintS("NULL"); |
---|
198 | else if (pNext(p)==NULL) p_Write0(p, lmRing); |
---|
199 | else |
---|
200 | { |
---|
201 | r = pNext(p); |
---|
202 | pNext(p) = NULL; |
---|
203 | p_Write0(p, lmRing, tailRing); |
---|
204 | pNext(p) = r; |
---|
205 | } |
---|
206 | } |
---|
207 | #endif |
---|