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 "mod2.h" |
---|
12 | #include "structs.h" |
---|
13 | #include "numbers.h" |
---|
14 | #include "ring.h" |
---|
15 | #include "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 | */ |
---|
22 | static void writemon(poly p, int ko, ring r) |
---|
23 | { |
---|
24 | BOOLEAN wroteCoef=FALSE,writeGen=FALSE; |
---|
25 | |
---|
26 | if (pGetCoeff(p)!=NULL) |
---|
27 | n_Normalize(pGetCoeff(p),r); |
---|
28 | |
---|
29 | if (((p_GetComp(p,r) == (short)ko) |
---|
30 | &&(p_LmIsConstantComp(p, r))) |
---|
31 | || ((!n_IsOne(pGetCoeff(p),r)) |
---|
32 | && (!n_IsMOne(pGetCoeff(p),r)) |
---|
33 | ) |
---|
34 | ) |
---|
35 | { |
---|
36 | n_Write(p->coef,r); |
---|
37 | wroteCoef=(rShortOut(r) == FALSE ||(r->parameter!=NULL)); |
---|
38 | writeGen=TRUE; |
---|
39 | } |
---|
40 | else if (n_IsMOne(pGetCoeff(p),r)) |
---|
41 | { |
---|
42 | if (n_GreaterZero(pGetCoeff(p),r)) |
---|
43 | { |
---|
44 | n_Write(p->coef,r); |
---|
45 | wroteCoef=(rShortOut(r) == FALSE ||(r->parameter!=NULL)); |
---|
46 | writeGen=TRUE; |
---|
47 | } |
---|
48 | else |
---|
49 | StringAppendS("-"); |
---|
50 | } |
---|
51 | |
---|
52 | int i; |
---|
53 | for (i=0; i<r->N; i++) |
---|
54 | { |
---|
55 | { |
---|
56 | Exponent_t ee = p_GetExp(p,i+1,r); |
---|
57 | if (ee!=0) |
---|
58 | { |
---|
59 | if (wroteCoef) |
---|
60 | StringAppendS("*"); |
---|
61 | //else |
---|
62 | wroteCoef=(rShortOut(r) == FALSE); |
---|
63 | writeGen=TRUE; |
---|
64 | StringAppendS(rRingVar(i, r)); |
---|
65 | if (ee != 1) |
---|
66 | { |
---|
67 | if (rShortOut(r)==0) StringAppendS("^"); |
---|
68 | StringAppend("%d", ee); |
---|
69 | } |
---|
70 | } |
---|
71 | } |
---|
72 | } |
---|
73 | //StringAppend("{%d}",p->Order); |
---|
74 | if (p_GetComp(p, r) != (Exponent_t)ko) |
---|
75 | { |
---|
76 | if (writeGen) StringAppendS("*"); |
---|
77 | StringAppend("gen(%d)", p_GetComp(p, r)); |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | char* p_String0(poly p, ring lmRing, ring tailRing) |
---|
82 | { |
---|
83 | if (p == NULL) |
---|
84 | { |
---|
85 | return StringAppendS("0"); |
---|
86 | } |
---|
87 | if ((p_GetComp(p, lmRing) == 0) || (!lmRing->VectorOut)) |
---|
88 | { |
---|
89 | writemon(p,0, lmRing); |
---|
90 | p = pNext(p); |
---|
91 | while (p!=NULL) |
---|
92 | { |
---|
93 | if ((p->coef==NULL)||n_GreaterZero(p->coef,tailRing)) |
---|
94 | StringAppendS("+"); |
---|
95 | writemon(p,0, tailRing); |
---|
96 | p = pNext(p); |
---|
97 | } |
---|
98 | return StringAppendS(""); |
---|
99 | } |
---|
100 | |
---|
101 | Exponent_t k = 1; |
---|
102 | StringAppendS("["); |
---|
103 | loop |
---|
104 | { |
---|
105 | while (k < p_GetComp(p,lmRing)) |
---|
106 | { |
---|
107 | StringAppendS("0,"); |
---|
108 | k++; |
---|
109 | } |
---|
110 | writemon(p,k,lmRing); |
---|
111 | pIter(p); |
---|
112 | while ((p!=NULL) && (k == p_GetComp(p, tailRing))) |
---|
113 | { |
---|
114 | if (n_GreaterZero(p->coef,tailRing)) StringAppendS("+"); |
---|
115 | writemon(p,k,tailRing); |
---|
116 | pIter(p); |
---|
117 | } |
---|
118 | if (p == NULL) break; |
---|
119 | StringAppendS(","); |
---|
120 | k++; |
---|
121 | } |
---|
122 | return StringAppendS("]"); |
---|
123 | } |
---|
124 | |
---|
125 | char* p_String(poly p, ring lmRing, ring tailRing) |
---|
126 | { |
---|
127 | StringSetS(""); |
---|
128 | return p_String0(p, lmRing, tailRing); |
---|
129 | } |
---|
130 | |
---|
131 | /*2 |
---|
132 | * writes a polynomial p to stdout |
---|
133 | */ |
---|
134 | void p_Write0(poly p, ring lmRing, ring tailRing) |
---|
135 | { |
---|
136 | PrintS(p_String(p, lmRing, tailRing)); |
---|
137 | } |
---|
138 | |
---|
139 | /*2 |
---|
140 | * writes a polynomial p to stdout followed by \n |
---|
141 | */ |
---|
142 | void p_Write(poly p, ring lmRing, ring tailRing) |
---|
143 | { |
---|
144 | p_Write0(p, lmRing, tailRing); |
---|
145 | PrintLn(); |
---|
146 | } |
---|
147 | |
---|
148 | /*2 |
---|
149 | *the standard debugging output: |
---|
150 | *print the first two monomials of the poly (wrp) or only the lead term (wrp0), |
---|
151 | *possibly followed by the string "+..." |
---|
152 | */ |
---|
153 | void p_wrp0(poly p, ring ri) |
---|
154 | { |
---|
155 | poly r; |
---|
156 | |
---|
157 | if (p==NULL) PrintS("NULL"); |
---|
158 | else if (pNext(p)==NULL) p_Write0(p, ri); |
---|
159 | else |
---|
160 | { |
---|
161 | r = pNext(p); |
---|
162 | pNext(p) = NULL; |
---|
163 | p_Write0(p, ri); |
---|
164 | if (r!=NULL) |
---|
165 | { |
---|
166 | PrintS("+..."); |
---|
167 | pNext(p) = r; |
---|
168 | } |
---|
169 | } |
---|
170 | } |
---|
171 | //#if !defined(__OPTIMIZE__) || defined(KDEBUG): used in list1 |
---|
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 |
---|
208 | //#endif |
---|