[0e1846] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
[f003a9] | 4 | /* $Id: binom.cc,v 1.14 1998-03-23 22:50:54 obachman Exp $ */ |
---|
[0e1846] | 5 | |
---|
| 6 | /* |
---|
| 7 | * ABSTRACT - set order (=number of monomial) for dp |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | /* includes */ |
---|
| 11 | #include "mod2.h" |
---|
[954622] | 12 | #include "structs.h" |
---|
| 13 | #include "binom.h" |
---|
| 14 | |
---|
| 15 | #ifdef TEST_MAC_ORDER |
---|
[0e1846] | 16 | #include "tok.h" |
---|
| 17 | #include "mmemory.h" |
---|
| 18 | #include "febase.h" |
---|
| 19 | #include "polys.h" |
---|
[965524c] | 20 | #include "polys-comp.h" |
---|
[954622] | 21 | #include "ring.h" |
---|
[0e1846] | 22 | |
---|
| 23 | extern int pComponentOrder; |
---|
| 24 | /* ----------- global variables, set by bBinomSet --------------------- */ |
---|
[13e3243] | 25 | int *bBinomials=NULL; |
---|
[0e1846] | 26 | static int bSize; |
---|
| 27 | int bHighdeg; |
---|
[13e3243] | 28 | int bHighdeg_1; |
---|
[954622] | 29 | BOOLEAN bNoAdd; |
---|
[0e1846] | 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 | { |
---|
[eb17bd3] | 39 | #ifdef TEST_MAC_DEBUG |
---|
[13e3243] | 40 | int ord = pTotaldegree(p); |
---|
[eb17bd3] | 41 | p->Order=ord; |
---|
[0e1846] | 42 | |
---|
[c9567bf] | 43 | if (ord<bHighdeg) |
---|
[0e1846] | 44 | { |
---|
[954622] | 45 | int i=1; |
---|
| 46 | ord = -INT_MAX; |
---|
[0e1846] | 47 | //int expsum=0; |
---|
| 48 | //while (i<=pVariables) |
---|
| 49 | //{ |
---|
| 50 | // expsum += pGetExp(p,i); |
---|
[954622] | 51 | // ord += bBinom(expsum,i); |
---|
[0e1846] | 52 | // expsum++; |
---|
| 53 | // i++; |
---|
| 54 | //} |
---|
| 55 | |
---|
| 56 | int *ip=bBinomials+pGetExp(p,1); |
---|
| 57 | loop |
---|
| 58 | { |
---|
[954622] | 59 | ord += (*ip); |
---|
[0e1846] | 60 | if (i==pVariables) break; |
---|
| 61 | i++; |
---|
[954622] | 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 |
---|
[0e1846] | 66 | ip+=bHighdeg_1+pGetExp(p,i); |
---|
| 67 | } |
---|
[13e3243] | 68 | if (ord>=0) |
---|
[c9567bf] | 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 | } |
---|
[0e1846] | 74 | } |
---|
[eb17bd3] | 75 | p->MOrder=ord; |
---|
| 76 | #else |
---|
[13e3243] | 77 | _bSetm(p); |
---|
[eb17bd3] | 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 | } |
---|
[c9567bf] | 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 | } |
---|
[eb17bd3] | 116 | p->MOrder=ord; |
---|
| 117 | #else |
---|
[13e3243] | 118 | _bSetm0(p); |
---|
[eb17bd3] | 119 | #endif |
---|
[0e1846] | 120 | } |
---|
| 121 | |
---|
[eb17bd3] | 122 | #ifdef TEST_MAC_DEBUG |
---|
| 123 | int bComp1dpc_org(poly p1, poly p2) |
---|
| 124 | #else |
---|
| 125 | int bComp1dpc(poly p1, poly p2) |
---|
| 126 | #endif |
---|
[0e1846] | 127 | { |
---|
[eb17bd3] | 128 | #ifdef TEST_MAC_DEBUG |
---|
| 129 | int o1=p1->MOrder, o2=p2->MOrder; |
---|
| 130 | #else |
---|
[0e1846] | 131 | int o1=p1->Order, o2=p2->Order; |
---|
[eb17bd3] | 132 | #endif |
---|
[965524c] | 133 | |
---|
[c2b529] | 134 | if (o1!=o2) |
---|
[965524c] | 135 | { |
---|
[c2b529] | 136 | if(o1>o2) return 1; |
---|
[965524c] | 137 | else return -1; |
---|
[c9567bf] | 138 | } |
---|
[0e1846] | 139 | |
---|
| 140 | /* now o1==o2: */ |
---|
| 141 | if (o1>0) |
---|
| 142 | { |
---|
[c2b529] | 143 | register long d; |
---|
[965524c] | 144 | if (pVariablesW >2 ) |
---|
[0e1846] | 145 | { |
---|
[965524c] | 146 | if (pVariablesW & 1) |
---|
| 147 | { |
---|
| 148 | _pMonCmp_2i_1(p1,p2, pVariablesW, d, goto NotEqual, return 0); |
---|
[c9567bf] | 149 | } |
---|
[965524c] | 150 | _pMonCmp_2i(p1,p2, pVariablesW, d, goto NotEqual, return 0); |
---|
[0e1846] | 151 | } |
---|
[965524c] | 152 | if (pVariablesW == 1) |
---|
| 153 | { |
---|
| 154 | _pMonCmp_1(p1,p2, d, goto NotEqual, return 0); |
---|
[c9567bf] | 155 | } |
---|
[965524c] | 156 | _pMonCmp_2(p1,p2, d, goto NotEqual, return 0); |
---|
| 157 | |
---|
| 158 | NotEqual: |
---|
| 159 | if (d>0) return -1; /*pLexSgn*/ |
---|
| 160 | return 1; /*-pLexSgn*/ |
---|
[0e1846] | 161 | } |
---|
[f92fa13] | 162 | o1=pGetComp(p1)-pGetComp(p2); |
---|
| 163 | if (o1 == 0) return 0; |
---|
| 164 | if (o1 > 0) return -pComponentOrder; |
---|
| 165 | return pComponentOrder; |
---|
[965524c] | 166 | return 0; |
---|
[0e1846] | 167 | } |
---|
| 168 | |
---|
[eb17bd3] | 169 | #ifdef TEST_MAC_DEBUG |
---|
[3d3ba58] | 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 | } |
---|
[eb17bd3] | 202 | int bComp1dpc(poly p1, poly p2) |
---|
| 203 | { |
---|
| 204 | int m=bComp1dpc_org(p1,p2); |
---|
[3d3ba58] | 205 | int c=comp1dpc(p1,p2); |
---|
[eb17bd3] | 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); |
---|
[c9567bf] | 213 | } |
---|
[eb17bd3] | 214 | return c; |
---|
[c9567bf] | 215 | } |
---|
[eb17bd3] | 216 | #endif |
---|
| 217 | |
---|
| 218 | int bComp1cdp(poly p1, poly p2) |
---|
[954622] | 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 | |
---|
[0e1846] | 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 | { |
---|
[51c163] | 251 | Exponent_t k=pGetComp(p); |
---|
[954622] | 252 | int o = pTotaldegree(p); |
---|
[0e1846] | 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 | { |
---|
[51c163] | 269 | Exponent_t k=pGetComp(p); |
---|
[0e1846] | 270 | int ll=1; |
---|
| 271 | |
---|
| 272 | while ((pNext(p)!=NULL) && (pGetComp(pNext(p))==k)) |
---|
| 273 | { |
---|
| 274 | pIter(p); |
---|
| 275 | ll++; |
---|
| 276 | } |
---|
| 277 | *l=ll; |
---|
[954622] | 278 | return (pTotaldegree(p)); |
---|
[0e1846] | 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 | */ |
---|
[954622] | 286 | void bBinomSet(int * orders) |
---|
[0e1846] | 287 | { |
---|
[954622] | 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--; |
---|
[c9567bf] | 306 | |
---|
[954622] | 307 | if(bBinomials!=NULL) Free((ADDRESS)bBinomials,bSize); |
---|
| 308 | bSize = pVariables*bHighdeg_1*sizeof(int); |
---|
| 309 | bBinomials = (int*)Alloc(bSize); |
---|
[c9567bf] | 310 | |
---|
[954622] | 311 | //Print("max deg=%d, table size=%d bytes\n",bHighdeg,bSize); |
---|
[c9567bf] | 312 | |
---|
[954622] | 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*/] |
---|
[c9567bf] | 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 | } |
---|
[954622] | 325 | } |
---|
| 326 | } |
---|
| 327 | for (int i=0;i<pVariables;i++) |
---|
| 328 | { |
---|
| 329 | bBinomials[i*(bHighdeg_1)/*i,0*/]=0; |
---|
| 330 | } |
---|
[c9567bf] | 331 | Print("last table entry:%d,%d - %d\n", pVariables,bHighdeg,pVariables*(bHighdeg_1)+bHighdeg); |
---|
[954622] | 332 | } |
---|
| 333 | #else |
---|
[0e1846] | 334 | bHighdeg=1; |
---|
| 335 | long long t=1; |
---|
| 336 | long long h_n=1+pVariables; |
---|
[954622] | 337 | while ((bHighdeg<256) |
---|
[0e1846] | 338 | && ((t=(((long long)t*(long long)h_n)/(long long)bHighdeg))<INT_MAX)) |
---|
| 339 | { |
---|
| 340 | bHighdeg++; |
---|
| 341 | h_n++; |
---|
| 342 | } |
---|
[13e3243] | 343 | bHighdeg-=2; |
---|
[0e1846] | 344 | bHighdeg_1=bHighdeg; |
---|
| 345 | bHighdeg--; |
---|
[c9567bf] | 346 | |
---|
[0e1846] | 347 | if(bBinomials!=NULL) Free((ADDRESS)bBinomials,bSize); |
---|
[3d3ba58] | 348 | bSize = (pVariables+1)*bHighdeg_1*sizeof(int); |
---|
[0e1846] | 349 | bBinomials = (int*)Alloc(bSize); |
---|
[c9567bf] | 350 | |
---|
[13e3243] | 351 | // Print("max deg=%d, table size=%d bytes\n",bHighdeg,bSize); |
---|
[c9567bf] | 352 | |
---|
[0e1846] | 353 | for(int j=1;j<=bHighdeg;j++) |
---|
| 354 | { |
---|
| 355 | bBinomials[j/*0,j*/] = j; |
---|
[c9567bf] | 356 | for (int i=1;i<=pVariables;i++) |
---|
[0e1846] | 357 | { |
---|
| 358 | bBinomials[i*(bHighdeg_1)+j/*i,j*/] |
---|
[c9567bf] | 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 | //} |
---|
[0e1846] | 365 | } |
---|
| 366 | } |
---|
| 367 | for (int i=0;i<pVariables;i++) |
---|
| 368 | { |
---|
| 369 | bBinomials[i*(bHighdeg_1)/*i,0*/]=0; |
---|
| 370 | } |
---|
[c9567bf] | 371 | // Print("last table entry:%d,%d - %d\n", pVariables,bHighdeg,pVariables*(bHighdeg_1)+bHighdeg); |
---|
| 372 | #endif |
---|
[0e1846] | 373 | pSetm =bSetm; |
---|
[954622] | 374 | if (orders[0]==ringorder_dp) |
---|
| 375 | pComp0=bComp1dpc; |
---|
| 376 | else if (orders[1]==ringorder_dp) |
---|
| 377 | pComp0=bComp1cdp; |
---|
[0e1846] | 378 | pFDeg =pTotaldegree; |
---|
| 379 | pLDeg =bLDegb; /* if pOrdSgn==1 */ |
---|
| 380 | } |
---|
| 381 | #endif |
---|