1 | </**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id$ */ |
---|
5 | |
---|
6 | /* |
---|
7 | * ABSTRACT - all basic methods to manipulate polynomials: |
---|
8 | * independent of representation |
---|
9 | */ |
---|
10 | |
---|
11 | /* includes */ |
---|
12 | #include <string.h> |
---|
13 | #include "polys/config.h" |
---|
14 | // #include <polys/options.h> |
---|
15 | #include <coeffs/numbers.h> |
---|
16 | #include <coeffs/ffields.h> |
---|
17 | #include <omalloc/omalloc.h> |
---|
18 | // #include <???/febase.h> |
---|
19 | // #include <???/weight.h> |
---|
20 | // #include <???/intvec.h> |
---|
21 | #include <polys/ext_fields/longalg.h> |
---|
22 | #include <polys/monomials/ring.h> |
---|
23 | // #include <???/ideals.h> |
---|
24 | #include <polys/polys.h> |
---|
25 | // #include "ipid.h" |
---|
26 | #ifdef HAVE_FACTORY |
---|
27 | // #include <???/clapsing.h> |
---|
28 | #endif |
---|
29 | |
---|
30 | #ifdef HAVE_RATGRING |
---|
31 | // #include <polys/ratgring.h> |
---|
32 | #endif |
---|
33 | |
---|
34 | |
---|
35 | /*3 |
---|
36 | * create binomial coef. |
---|
37 | */ |
---|
38 | static number* pnBin(int exp) |
---|
39 | { |
---|
40 | int e, i, h; |
---|
41 | number x, y, *bin=NULL; |
---|
42 | |
---|
43 | x = nInit(exp); |
---|
44 | if (nIsZero(x)) |
---|
45 | { |
---|
46 | nDelete(&x); |
---|
47 | return bin; |
---|
48 | } |
---|
49 | h = (exp >> 1) + 1; |
---|
50 | bin = (number *)omAlloc0(h*sizeof(number)); |
---|
51 | bin[1] = x; |
---|
52 | if (exp < 4) |
---|
53 | return bin; |
---|
54 | i = exp - 1; |
---|
55 | for (e=2; e<h; e++) |
---|
56 | { |
---|
57 | x = nInit(i); |
---|
58 | i--; |
---|
59 | y = nMult(x,bin[e-1]); |
---|
60 | nDelete(&x); |
---|
61 | x = nInit(e); |
---|
62 | bin[e] = nIntDiv(y,x); |
---|
63 | nDelete(&x); |
---|
64 | nDelete(&y); |
---|
65 | } |
---|
66 | return bin; |
---|
67 | } |
---|
68 | |
---|
69 | static void pnFreeBin(number *bin, int exp) |
---|
70 | { |
---|
71 | int e, h = (exp >> 1) + 1; |
---|
72 | |
---|
73 | if (bin[1] != NULL) |
---|
74 | { |
---|
75 | for (e=1; e<h; e++) |
---|
76 | nDelete(&(bin[e])); |
---|
77 | } |
---|
78 | omFreeSize((ADDRESS)bin, h*sizeof(number)); |
---|
79 | } |
---|
80 | |
---|
81 | /*2 |
---|
82 | * handle memory request for sets of polynomials (ideals) |
---|
83 | * l is the length of *p, increment is the difference (may be negative) |
---|
84 | */ |
---|
85 | void pEnlargeSet(polyset *p, int l, int increment) |
---|
86 | { |
---|
87 | polyset h; |
---|
88 | |
---|
89 | h=(polyset)omReallocSize((poly*)*p,l*sizeof(poly),(l+increment)*sizeof(poly)); |
---|
90 | if (increment>0) |
---|
91 | { |
---|
92 | //for (i=l; i<l+increment; i++) |
---|
93 | // h[i]=NULL; |
---|
94 | memset(&(h[l]),0,increment*sizeof(poly)); |
---|
95 | } |
---|
96 | *p=h; |
---|
97 | } |
---|
98 | |
---|
99 | poly ppJet(poly p, int m) |
---|
100 | { |
---|
101 | poly r=NULL; |
---|
102 | poly t=NULL; |
---|
103 | |
---|
104 | while (p!=NULL) |
---|
105 | { |
---|
106 | if (p_Totaldegree(p,currRing)<=m) |
---|
107 | { |
---|
108 | if (r==NULL) |
---|
109 | r=pHead(p); |
---|
110 | else |
---|
111 | if (t==NULL) |
---|
112 | { |
---|
113 | pNext(r)=pHead(p); |
---|
114 | t=pNext(r); |
---|
115 | } |
---|
116 | else |
---|
117 | { |
---|
118 | pNext(t)=pHead(p); |
---|
119 | pIter(t); |
---|
120 | } |
---|
121 | } |
---|
122 | pIter(p); |
---|
123 | } |
---|
124 | return r; |
---|
125 | } |
---|
126 | |
---|
127 | poly pJet(poly p, int m) |
---|
128 | { |
---|
129 | poly t=NULL; |
---|
130 | |
---|
131 | while((p!=NULL) && (p_Totaldegree(p,currRing)>m)) pLmDelete(&p); |
---|
132 | if (p==NULL) return NULL; |
---|
133 | poly r=p; |
---|
134 | while (pNext(p)!=NULL) |
---|
135 | { |
---|
136 | if (p_Totaldegree(pNext(p),currRing)>m) |
---|
137 | { |
---|
138 | pLmDelete(&pNext(p)); |
---|
139 | } |
---|
140 | else |
---|
141 | pIter(p); |
---|
142 | } |
---|
143 | return r; |
---|
144 | } |
---|
145 | |
---|
146 | poly ppJetW(poly p, int m, short *w) |
---|
147 | { |
---|
148 | poly r=NULL; |
---|
149 | poly t=NULL; |
---|
150 | while (p!=NULL) |
---|
151 | { |
---|
152 | if (totaldegreeWecart_IV(p,currRing,w)<=m) |
---|
153 | { |
---|
154 | if (r==NULL) |
---|
155 | r=pHead(p); |
---|
156 | else |
---|
157 | if (t==NULL) |
---|
158 | { |
---|
159 | pNext(r)=pHead(p); |
---|
160 | t=pNext(r); |
---|
161 | } |
---|
162 | else |
---|
163 | { |
---|
164 | pNext(t)=pHead(p); |
---|
165 | pIter(t); |
---|
166 | } |
---|
167 | } |
---|
168 | pIter(p); |
---|
169 | } |
---|
170 | return r; |
---|
171 | } |
---|
172 | |
---|
173 | poly pJetW(poly p, int m, short *w) |
---|
174 | { |
---|
175 | while((p!=NULL) && (totaldegreeWecart_IV(p,currRing,w)>m)) pLmDelete(&p); |
---|
176 | if (p==NULL) return NULL; |
---|
177 | poly r=p; |
---|
178 | while (pNext(p)!=NULL) |
---|
179 | { |
---|
180 | if (totaldegreeWecart_IV(pNext(p),currRing,w)>m) |
---|
181 | { |
---|
182 | pLmDelete(&pNext(p)); |
---|
183 | } |
---|
184 | else |
---|
185 | pIter(p); |
---|
186 | } |
---|
187 | return r; |
---|
188 | } |
---|
189 | |
---|
190 | int pMinDeg(poly p,intvec *w) |
---|
191 | { |
---|
192 | if(p==NULL) |
---|
193 | return -1; |
---|
194 | int d=-1; |
---|
195 | while(p!=NULL) |
---|
196 | { |
---|
197 | int d0=0; |
---|
198 | for(int j=0;j<pVariables;j++) |
---|
199 | if(w==NULL||j>=w->length()) |
---|
200 | d0+=pGetExp(p,j+1); |
---|
201 | else |
---|
202 | d0+=(*w)[j]*pGetExp(p,j+1); |
---|
203 | if(d0<d||d==-1) |
---|
204 | d=d0; |
---|
205 | pIter(p); |
---|
206 | } |
---|
207 | return d; |
---|
208 | } |
---|
209 | |
---|
210 | poly pSeries(int n,poly p,poly u, intvec *w) |
---|
211 | { |
---|
212 | short *ww=iv2array(w); |
---|
213 | if(p!=NULL) |
---|
214 | { |
---|
215 | if(u==NULL) |
---|
216 | p=pJetW(p,n,ww); |
---|
217 | else |
---|
218 | p=pJetW(pMult(p,pInvers(n-pMinDeg(p,w),u,w)),n,ww); |
---|
219 | } |
---|
220 | omFreeSize((ADDRESS)ww,(pVariables+1)*sizeof(short)); |
---|
221 | return p; |
---|
222 | } |
---|
223 | |
---|
224 | poly pInvers(int n,poly u,intvec *w) |
---|
225 | { |
---|
226 | short *ww=iv2array(w); |
---|
227 | if(n<0) |
---|
228 | return NULL; |
---|
229 | number u0=nInvers(pGetCoeff(u)); |
---|
230 | poly v=pNSet(u0); |
---|
231 | if(n==0) |
---|
232 | return v; |
---|
233 | poly u1=pJetW(pSub(pOne(),pMult_nn(u,u0)),n,ww); |
---|
234 | if(u1==NULL) |
---|
235 | return v; |
---|
236 | poly v1=pMult_nn(pCopy(u1),u0); |
---|
237 | v=pAdd(v,pCopy(v1)); |
---|
238 | for(int i=n/pMinDeg(u1,w);i>1;i--) |
---|
239 | { |
---|
240 | v1=pJetW(pMult(v1,pCopy(u1)),n,ww); |
---|
241 | v=pAdd(v,pCopy(v1)); |
---|
242 | } |
---|
243 | pDelete(&u1); |
---|
244 | pDelete(&v1); |
---|
245 | omFreeSize((ADDRESS)ww,(pVariables+1)*sizeof(short)); |
---|
246 | return v; |
---|
247 | } |
---|
248 | |
---|
249 | long pDegW(poly p, const short *w) |
---|
250 | { |
---|
251 | long r=-LONG_MAX; |
---|
252 | |
---|
253 | while (p!=NULL) |
---|
254 | { |
---|
255 | long t=totaldegreeWecart_IV(p,currRing,w); |
---|
256 | if (t>r) r=t; |
---|
257 | pIter(p); |
---|
258 | } |
---|
259 | return r; |
---|
260 | } |
---|
261 | |
---|
262 | /*-----------type conversions ----------------------------*/ |
---|
263 | #if 0 |
---|
264 | /*2 |
---|
265 | * convert a vector to a set of polys, |
---|
266 | * allocates the polyset, (entries 0..(*len)-1) |
---|
267 | * the vector will not be changed |
---|
268 | */ |
---|
269 | void pVec2Polys(poly v, polyset *p, int *len) |
---|
270 | { |
---|
271 | poly h; |
---|
272 | int k; |
---|
273 | |
---|
274 | *len=pMaxComp(v); |
---|
275 | if (*len==0) *len=1; |
---|
276 | *p=(polyset)omAlloc0((*len)*sizeof(poly)); |
---|
277 | while (v!=NULL) |
---|
278 | { |
---|
279 | h=pHead(v); |
---|
280 | k=pGetComp(h); |
---|
281 | pSetComp(h,0); |
---|
282 | (*p)[k-1]=pAdd((*p)[k-1],h); |
---|
283 | pIter(v); |
---|
284 | } |
---|
285 | } |
---|
286 | |
---|
287 | int p_Var(poly m,const ring r) |
---|
288 | { |
---|
289 | if (m==NULL) return 0; |
---|
290 | if (pNext(m)!=NULL) return 0; |
---|
291 | int i,e=0; |
---|
292 | for (i=r->N; i>0; i--) |
---|
293 | { |
---|
294 | int exp=p_GetExp(m,i,r); |
---|
295 | if (exp==1) |
---|
296 | { |
---|
297 | if (e==0) e=i; |
---|
298 | else return 0; |
---|
299 | } |
---|
300 | else if (exp!=0) |
---|
301 | { |
---|
302 | return 0; |
---|
303 | } |
---|
304 | } |
---|
305 | return e; |
---|
306 | } |
---|
307 | |
---|
308 | /*2 |
---|
309 | * returns TRUE if p1 = p2 |
---|
310 | */ |
---|
311 | BOOLEAN p_EqualPolys(poly p1,poly p2, const ring r) |
---|
312 | { |
---|
313 | while ((p1 != NULL) && (p2 != NULL)) |
---|
314 | { |
---|
315 | if (! p_LmEqual(p1, p2,r)) |
---|
316 | return FALSE; |
---|
317 | if (! n_Equal(p_GetCoeff(p1,r), p_GetCoeff(p2,r),r )) |
---|
318 | return FALSE; |
---|
319 | pIter(p1); |
---|
320 | pIter(p2); |
---|
321 | } |
---|
322 | return (p1==p2); |
---|
323 | } |
---|
324 | |
---|
325 | /*2 |
---|
326 | *returns TRUE if p1 is a skalar multiple of p2 |
---|
327 | *assume p1 != NULL and p2 != NULL |
---|
328 | */ |
---|
329 | BOOLEAN pComparePolys(poly p1,poly p2) |
---|
330 | { |
---|
331 | number n,nn; |
---|
332 | pAssume(p1 != NULL && p2 != NULL); |
---|
333 | |
---|
334 | if (!pLmEqual(p1,p2)) //compare leading mons |
---|
335 | return FALSE; |
---|
336 | if ((pNext(p1)==NULL) && (pNext(p2)!=NULL)) |
---|
337 | return FALSE; |
---|
338 | if ((pNext(p2)==NULL) && (pNext(p1)!=NULL)) |
---|
339 | return FALSE; |
---|
340 | if (pLength(p1) != pLength(p2)) |
---|
341 | return FALSE; |
---|
342 | #ifdef HAVE_RINGS |
---|
343 | if (rField_is_Ring(currRing)) |
---|
344 | { |
---|
345 | if (!nDivBy(pGetCoeff(p1), pGetCoeff(p2))) return FALSE; |
---|
346 | } |
---|
347 | #endif |
---|
348 | n=nDiv(pGetCoeff(p1),pGetCoeff(p2)); |
---|
349 | while ((p1 != NULL) /*&& (p2 != NULL)*/) |
---|
350 | { |
---|
351 | if ( ! pLmEqual(p1, p2)) |
---|
352 | { |
---|
353 | nDelete(&n); |
---|
354 | return FALSE; |
---|
355 | } |
---|
356 | if (!nEqual(pGetCoeff(p1),nn=nMult(pGetCoeff(p2),n))) |
---|
357 | { |
---|
358 | nDelete(&n); |
---|
359 | nDelete(&nn); |
---|
360 | return FALSE; |
---|
361 | } |
---|
362 | nDelete(&nn); |
---|
363 | pIter(p1); |
---|
364 | pIter(p2); |
---|
365 | } |
---|
366 | nDelete(&n); |
---|
367 | return TRUE; |
---|
368 | } |
---|