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