1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id: binom.cc,v 1.14 1998-03-23 22:50:54 obachman Exp $ */ |
---|
5 | |
---|
6 | /* |
---|
7 | * ABSTRACT - set order (=number of monomial) for dp |
---|
8 | */ |
---|
9 | |
---|
10 | /* includes */ |
---|
11 | #include "mod2.h" |
---|
12 | #include "structs.h" |
---|
13 | #include "binom.h" |
---|
14 | |
---|
15 | #ifdef TEST_MAC_ORDER |
---|
16 | #include "tok.h" |
---|
17 | #include "mmemory.h" |
---|
18 | #include "febase.h" |
---|
19 | #include "polys.h" |
---|
20 | #include "polys-comp.h" |
---|
21 | #include "ring.h" |
---|
22 | |
---|
23 | extern int pComponentOrder; |
---|
24 | /* ----------- global variables, set by bBinomSet --------------------- */ |
---|
25 | int *bBinomials=NULL; |
---|
26 | static int bSize; |
---|
27 | int bHighdeg; |
---|
28 | int bHighdeg_1; |
---|
29 | BOOLEAN bNoAdd; |
---|
30 | |
---|
31 | /*0 implementation*/ |
---|
32 | static inline int bBinom(int i,int j) |
---|
33 | { |
---|
34 | return bBinomials[(j-1)*(bHighdeg_1)+i-j+1/*j-1,i-j*/]; |
---|
35 | } |
---|
36 | |
---|
37 | void bSetm(poly p) |
---|
38 | { |
---|
39 | #ifdef TEST_MAC_DEBUG |
---|
40 | int ord = pTotaldegree(p); |
---|
41 | p->Order=ord; |
---|
42 | |
---|
43 | if (ord<bHighdeg) |
---|
44 | { |
---|
45 | int i=1; |
---|
46 | ord = -INT_MAX; |
---|
47 | //int expsum=0; |
---|
48 | //while (i<=pVariables) |
---|
49 | //{ |
---|
50 | // expsum += pGetExp(p,i); |
---|
51 | // ord += bBinom(expsum,i); |
---|
52 | // expsum++; |
---|
53 | // i++; |
---|
54 | //} |
---|
55 | |
---|
56 | int *ip=bBinomials+pGetExp(p,1); |
---|
57 | loop |
---|
58 | { |
---|
59 | ord += (*ip); |
---|
60 | if (i==pVariables) break; |
---|
61 | i++; |
---|
62 | #ifdef PDEBUG |
---|
63 | if(pGetExp(p,i)<0) |
---|
64 | Print("neg. Exp %d:%d in %s:%d\n",i,pGetExp(p,i),__FILE__,__LINE__); |
---|
65 | #endif |
---|
66 | ip+=bHighdeg_1+pGetExp(p,i); |
---|
67 | } |
---|
68 | if (ord>=0) |
---|
69 | { |
---|
70 | wrp(p); |
---|
71 | Print("maxdeg exceeded in bSetm:bHighdeg=%d, ord=%d,deg=%d,B[%d]i,Bsize=%d\n", |
---|
72 | bHighdeg,ord,pTotaldegree(p),ip-bBinomials,bSize/sizeof(int)); |
---|
73 | } |
---|
74 | } |
---|
75 | p->MOrder=ord; |
---|
76 | #else |
---|
77 | _bSetm(p); |
---|
78 | #endif |
---|
79 | } |
---|
80 | |
---|
81 | // avoid checking for overflow: |
---|
82 | void bSetm0(poly p) |
---|
83 | { |
---|
84 | #ifdef TEST_MAC_DEBUG |
---|
85 | p->Order=pTotaldegree(p); |
---|
86 | |
---|
87 | int i=1; |
---|
88 | int ord = -INT_MAX; |
---|
89 | //int expsum=0; |
---|
90 | //while (i<=pVariables) |
---|
91 | //{ |
---|
92 | // expsum += pGetExp(p,i); |
---|
93 | // ord += bBinom(expsum,i); |
---|
94 | // expsum++; |
---|
95 | // i++; |
---|
96 | //} |
---|
97 | |
---|
98 | int *ip=bBinomials+pGetExp(p,1); |
---|
99 | loop |
---|
100 | { |
---|
101 | ord += (*ip); |
---|
102 | if (i==pVariables) break; |
---|
103 | i++; |
---|
104 | #ifdef PDEBUG |
---|
105 | if(pGetExp(p,i)<0) |
---|
106 | Print("neg. Exp %d:%d in %s:%d\n",i,pGetExp(p,i),__FILE__,__LINE__); |
---|
107 | #endif |
---|
108 | ip+=bHighdeg_1+pGetExp(p,i); |
---|
109 | } |
---|
110 | if (ord>=0) |
---|
111 | { |
---|
112 | wrp(p); |
---|
113 | Print("maxdeg exceeded in bSetm0:bHighdeg=%d, ord=%d,deg=%d,B[%d],Bsize:%d\n", |
---|
114 | bHighdeg,ord,pTotaldegree(p),ip-bBinomials,bSize/sizeof(int)); |
---|
115 | } |
---|
116 | p->MOrder=ord; |
---|
117 | #else |
---|
118 | _bSetm0(p); |
---|
119 | #endif |
---|
120 | } |
---|
121 | |
---|
122 | #ifdef TEST_MAC_DEBUG |
---|
123 | int bComp1dpc_org(poly p1, poly p2) |
---|
124 | #else |
---|
125 | int bComp1dpc(poly p1, poly p2) |
---|
126 | #endif |
---|
127 | { |
---|
128 | #ifdef TEST_MAC_DEBUG |
---|
129 | int o1=p1->MOrder, o2=p2->MOrder; |
---|
130 | #else |
---|
131 | int o1=p1->Order, o2=p2->Order; |
---|
132 | #endif |
---|
133 | |
---|
134 | if (o1!=o2) |
---|
135 | { |
---|
136 | if(o1>o2) return 1; |
---|
137 | else return -1; |
---|
138 | } |
---|
139 | |
---|
140 | /* now o1==o2: */ |
---|
141 | if (o1>0) |
---|
142 | { |
---|
143 | register long d; |
---|
144 | if (pVariablesW >2 ) |
---|
145 | { |
---|
146 | if (pVariablesW & 1) |
---|
147 | { |
---|
148 | _pMonCmp_2i_1(p1,p2, pVariablesW, d, goto NotEqual, return 0); |
---|
149 | } |
---|
150 | _pMonCmp_2i(p1,p2, pVariablesW, d, goto NotEqual, return 0); |
---|
151 | } |
---|
152 | if (pVariablesW == 1) |
---|
153 | { |
---|
154 | _pMonCmp_1(p1,p2, d, goto NotEqual, return 0); |
---|
155 | } |
---|
156 | _pMonCmp_2(p1,p2, d, goto NotEqual, return 0); |
---|
157 | |
---|
158 | NotEqual: |
---|
159 | if (d>0) return -1; /*pLexSgn*/ |
---|
160 | return 1; /*-pLexSgn*/ |
---|
161 | } |
---|
162 | o1=pGetComp(p1)-pGetComp(p2); |
---|
163 | if (o1 == 0) return 0; |
---|
164 | if (o1 > 0) return -pComponentOrder; |
---|
165 | return pComponentOrder; |
---|
166 | return 0; |
---|
167 | } |
---|
168 | |
---|
169 | #ifdef TEST_MAC_DEBUG |
---|
170 | static int comp1dpc(poly p1, poly p2) |
---|
171 | { |
---|
172 | /*4 compare monomials by order then revlex*/ |
---|
173 | int d=p1->Order - p2->Order; |
---|
174 | if (d == 0 /*p1->Order == p2->Order*/) |
---|
175 | { |
---|
176 | int i = pVariables; |
---|
177 | if ((p1->exp[i] == p2->exp[i])) |
---|
178 | { |
---|
179 | do |
---|
180 | { |
---|
181 | i--; |
---|
182 | if (i <= 1) |
---|
183 | { |
---|
184 | /*4 handle module case:*/ |
---|
185 | if (p1->exp[0]==p2->exp[0]) |
---|
186 | return 0; |
---|
187 | else if (p1->exp[0] > p2->exp[0]) |
---|
188 | return -pComponentOrder; |
---|
189 | else |
---|
190 | return pComponentOrder; |
---|
191 | } |
---|
192 | } while ((p1->exp[i] == p2->exp[i])); |
---|
193 | } |
---|
194 | if (p1->exp[i] < p2->exp[i]) |
---|
195 | return 1; |
---|
196 | return -1; |
---|
197 | } |
---|
198 | else if (d > 0 /*p1->Order > p2->Order*/) |
---|
199 | return 1; |
---|
200 | return -1; |
---|
201 | } |
---|
202 | int bComp1dpc(poly p1, poly p2) |
---|
203 | { |
---|
204 | int m=bComp1dpc_org(p1,p2); |
---|
205 | int c=comp1dpc(p1,p2); |
---|
206 | if (c!=m) |
---|
207 | { |
---|
208 | Print("comp mismatch\n"); |
---|
209 | wrp(p1); |
---|
210 | Print(" "); |
---|
211 | wrp(p2); |
---|
212 | Print("mac:%d, org:%d\n",m,c); |
---|
213 | } |
---|
214 | return c; |
---|
215 | } |
---|
216 | #endif |
---|
217 | |
---|
218 | int bComp1cdp(poly p1, poly p2) |
---|
219 | { |
---|
220 | int o1=pGetComp(p1)-pGetComp(p2); |
---|
221 | if (o1 > 0) return -pComponentOrder; |
---|
222 | if (o1 < 0) return pComponentOrder; |
---|
223 | |
---|
224 | o1=p1->Order; int o2=p2->Order; |
---|
225 | if (o1 > o2) return 1; |
---|
226 | if (o1 < o2) return -1; |
---|
227 | |
---|
228 | /* now o1==o2: */ |
---|
229 | if (o1>0) |
---|
230 | { |
---|
231 | int i = pVariables; |
---|
232 | while ((pGetExp(p1,i)==pGetExp(p2,i)) && (i>1)) |
---|
233 | i--; |
---|
234 | if (i>1) |
---|
235 | { |
---|
236 | if (pGetExp(p1,i) < pGetExp(p2,i)) return 1; |
---|
237 | return -1; |
---|
238 | } |
---|
239 | } |
---|
240 | return 0; |
---|
241 | } |
---|
242 | |
---|
243 | /*2 |
---|
244 | * compute the length of a polynomial (in l) |
---|
245 | * and the degree of the monomial with maximal degree: the first one |
---|
246 | * this works for the polynomial case with degree orderings |
---|
247 | * (both c,dp and dp,c) |
---|
248 | */ |
---|
249 | static int bLDegb(poly p,int *l) |
---|
250 | { |
---|
251 | Exponent_t k=pGetComp(p); |
---|
252 | int o = pTotaldegree(p); |
---|
253 | int ll=1; |
---|
254 | |
---|
255 | while (((p=pNext(p))!=NULL) && (pGetComp(p)==k)) |
---|
256 | { |
---|
257 | ll++; |
---|
258 | } |
---|
259 | *l=ll; |
---|
260 | return o; |
---|
261 | } |
---|
262 | |
---|
263 | /*2 |
---|
264 | * compute the length of a polynomial (in l) |
---|
265 | * and the degree of the monomial with maximal degree: the last one |
---|
266 | */ |
---|
267 | static int bLDeg0(poly p,int *l) |
---|
268 | { |
---|
269 | Exponent_t k=pGetComp(p); |
---|
270 | int ll=1; |
---|
271 | |
---|
272 | while ((pNext(p)!=NULL) && (pGetComp(pNext(p))==k)) |
---|
273 | { |
---|
274 | pIter(p); |
---|
275 | ll++; |
---|
276 | } |
---|
277 | *l=ll; |
---|
278 | return (pTotaldegree(p)); |
---|
279 | } |
---|
280 | |
---|
281 | /* |
---|
282 | * (i,j) in bBinomials[j-1,i-j+1] |
---|
283 | * table size is: pVariables * (bHighdeg+1) |
---|
284 | * bHighdeg_1==bHighdeg+1 |
---|
285 | */ |
---|
286 | void bBinomSet(int * orders) |
---|
287 | { |
---|
288 | bNoAdd=TRUE; |
---|
289 | #if 0 |
---|
290 | int bbHighdeg=1; |
---|
291 | long long t=1; |
---|
292 | long long h_n=1+pVariables; |
---|
293 | while ((bbHighdeg<256) |
---|
294 | && ((t=(((long long)t*(long long)h_n)/(long long)bbHighdeg))<INT_MAX)) |
---|
295 | { |
---|
296 | bbHighdeg++; |
---|
297 | h_n++; |
---|
298 | } |
---|
299 | if((bBinomials==NULL) |
---|
300 | ||(bbHighdeg!=bHighdeg) |
---|
301 | ||(bSize!=(pVariables*bbHighdeg*sizeof(int)))) |
---|
302 | { |
---|
303 | bHighdeg=bbHighdeg; |
---|
304 | bHighdeg_1=bbHighdeg; |
---|
305 | bHighdeg--; |
---|
306 | |
---|
307 | if(bBinomials!=NULL) Free((ADDRESS)bBinomials,bSize); |
---|
308 | bSize = pVariables*bHighdeg_1*sizeof(int); |
---|
309 | bBinomials = (int*)Alloc(bSize); |
---|
310 | |
---|
311 | //Print("max deg=%d, table size=%d bytes\n",bHighdeg,bSize); |
---|
312 | |
---|
313 | for(int j=1;j<=bHighdeg;j++) |
---|
314 | { |
---|
315 | bBinomials[j/*0,j*/] = j; |
---|
316 | for (int i=1;i<pVariables;i++) |
---|
317 | { |
---|
318 | bBinomials[i*(bHighdeg_1)+j/*i,j*/] |
---|
319 | = ((long long)bBinomials[(i-1)*(bHighdeg_1)+j/*i-1,j*/]) |
---|
320 | *((long long)(j+i))/((long long)(i+1)); |
---|
321 | if (bBinomials[i*(bHighdeg_1)+j/*i,j*/]<=0) |
---|
322 | { |
---|
323 | Print("overflow in bBinomials:i=%s,j=%d\n",i,j); |
---|
324 | } |
---|
325 | } |
---|
326 | } |
---|
327 | for (int i=0;i<pVariables;i++) |
---|
328 | { |
---|
329 | bBinomials[i*(bHighdeg_1)/*i,0*/]=0; |
---|
330 | } |
---|
331 | Print("last table entry:%d,%d - %d\n", pVariables,bHighdeg,pVariables*(bHighdeg_1)+bHighdeg); |
---|
332 | } |
---|
333 | #else |
---|
334 | bHighdeg=1; |
---|
335 | long long t=1; |
---|
336 | long long h_n=1+pVariables; |
---|
337 | while ((bHighdeg<256) |
---|
338 | && ((t=(((long long)t*(long long)h_n)/(long long)bHighdeg))<INT_MAX)) |
---|
339 | { |
---|
340 | bHighdeg++; |
---|
341 | h_n++; |
---|
342 | } |
---|
343 | bHighdeg-=2; |
---|
344 | bHighdeg_1=bHighdeg; |
---|
345 | bHighdeg--; |
---|
346 | |
---|
347 | if(bBinomials!=NULL) Free((ADDRESS)bBinomials,bSize); |
---|
348 | bSize = (pVariables+1)*bHighdeg_1*sizeof(int); |
---|
349 | bBinomials = (int*)Alloc(bSize); |
---|
350 | |
---|
351 | // Print("max deg=%d, table size=%d bytes\n",bHighdeg,bSize); |
---|
352 | |
---|
353 | for(int j=1;j<=bHighdeg;j++) |
---|
354 | { |
---|
355 | bBinomials[j/*0,j*/] = j; |
---|
356 | for (int i=1;i<=pVariables;i++) |
---|
357 | { |
---|
358 | bBinomials[i*(bHighdeg_1)+j/*i,j*/] |
---|
359 | = ((long long)bBinomials[(i-1)*(bHighdeg_1)+j/*i-1,j*/]) |
---|
360 | *((long long)(j+i))/((long long)(i+1)); |
---|
361 | //if (bBinomials[i*(bHighdeg_1)+j/*i,j*/]<=0) |
---|
362 | //{ |
---|
363 | // Print("overflow in bBinomials:i=%s,j=%d\n",i,j); |
---|
364 | //} |
---|
365 | } |
---|
366 | } |
---|
367 | for (int i=0;i<pVariables;i++) |
---|
368 | { |
---|
369 | bBinomials[i*(bHighdeg_1)/*i,0*/]=0; |
---|
370 | } |
---|
371 | // Print("last table entry:%d,%d - %d\n", pVariables,bHighdeg,pVariables*(bHighdeg_1)+bHighdeg); |
---|
372 | #endif |
---|
373 | pSetm =bSetm; |
---|
374 | if (orders[0]==ringorder_dp) |
---|
375 | pComp0=bComp1dpc; |
---|
376 | else if (orders[1]==ringorder_dp) |
---|
377 | pComp0=bComp1cdp; |
---|
378 | pFDeg =pTotaldegree; |
---|
379 | pLDeg =bLDegb; /* if pOrdSgn==1 */ |
---|
380 | } |
---|
381 | #endif |
---|