[35aab3] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
[b96d71] | 4 | /* $Id: hilb.cc,v 1.3 2006-09-20 16:47:14 Singular Exp $ */ |
---|
[35aab3] | 5 | /* |
---|
| 6 | * ABSTRACT - Hilbert series |
---|
| 7 | */ |
---|
| 8 | |
---|
| 9 | #include "mod2.h" |
---|
| 10 | #include "structs.h" |
---|
| 11 | #include "febase.h" |
---|
| 12 | #include "omalloc.h" |
---|
| 13 | #include "polys.h" |
---|
| 14 | #include "intvec.h" |
---|
| 15 | #include "hutil.h" |
---|
| 16 | #include "stairc.h" |
---|
| 17 | |
---|
| 18 | static int **Qpol; |
---|
| 19 | static int *Q0, *Ql; |
---|
| 20 | static int hLength; |
---|
| 21 | |
---|
| 22 | static int hMinModulweight(intvec *modulweight) |
---|
| 23 | { |
---|
| 24 | int i,j,k; |
---|
| 25 | |
---|
| 26 | if(modulweight==NULL) return 0; |
---|
| 27 | j=(*modulweight)[0]; |
---|
| 28 | for(i=modulweight->rows()-1;i!=0;i--) |
---|
| 29 | { |
---|
| 30 | k=(*modulweight)[i]; |
---|
| 31 | if(k<j) j=k; |
---|
| 32 | } |
---|
| 33 | return j; |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | static void hHilbEst(scfmon stc, int Nstc, varset var, int Nvar) |
---|
| 37 | { |
---|
| 38 | int i, j; |
---|
| 39 | int x, y, z = 1; |
---|
| 40 | int *p; |
---|
| 41 | for (i = Nvar; i>0; i--) |
---|
| 42 | { |
---|
| 43 | x = 0; |
---|
| 44 | for (j = 0; j < Nstc; j++) |
---|
| 45 | { |
---|
| 46 | y = stc[j][var[i]]; |
---|
| 47 | if (y > x) |
---|
| 48 | x = y; |
---|
| 49 | } |
---|
| 50 | z += x; |
---|
| 51 | j = i - 1; |
---|
| 52 | if (z > Ql[j]) |
---|
| 53 | { |
---|
| 54 | p = (int *)omAlloc(z * sizeof(int)); |
---|
| 55 | if (Ql[j]!=0) |
---|
| 56 | { |
---|
| 57 | if (j==0) |
---|
| 58 | memcpy(p, Qpol[j], Ql[j] * sizeof(int)); |
---|
| 59 | omFreeSize((ADDRESS)Qpol[j], Ql[j] * sizeof(int)); |
---|
| 60 | } |
---|
| 61 | if (j==0) |
---|
| 62 | { |
---|
| 63 | for (x = Ql[j]; x < z; x++) |
---|
| 64 | p[x] = 0; |
---|
| 65 | } |
---|
| 66 | Ql[j] = z; |
---|
| 67 | Qpol[j] = p; |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | static int *hAddHilb(int Nv, int x, int *pol, int *lp) |
---|
| 73 | { |
---|
| 74 | int l = *lp, ln, i; |
---|
| 75 | int *pon; |
---|
| 76 | *lp = ln = l + x; |
---|
| 77 | pon = Qpol[Nv]; |
---|
| 78 | memcpy(pon, pol, l * sizeof(int)); |
---|
| 79 | if (l > x) |
---|
| 80 | { |
---|
| 81 | for (i = x; i < l; i++) |
---|
| 82 | pon[i] -= pol[i - x]; |
---|
| 83 | for (i = l; i < ln; i++) |
---|
| 84 | pon[i] = -pol[i - x]; |
---|
| 85 | } |
---|
| 86 | else |
---|
| 87 | { |
---|
| 88 | for (i = l; i < x; i++) |
---|
| 89 | pon[i] = 0; |
---|
| 90 | for (i = x; i < ln; i++) |
---|
| 91 | pon[i] = -pol[i - x]; |
---|
| 92 | } |
---|
| 93 | return pon; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | static void hLastHilb(scmon pure, int Nv, varset var, int *pol, int lp) |
---|
| 97 | { |
---|
| 98 | int l = lp, x, i, j; |
---|
| 99 | int *p, *pl; |
---|
| 100 | p = pol; |
---|
| 101 | for (i = Nv; i>0; i--) |
---|
| 102 | { |
---|
| 103 | x = pure[var[i + 1]]; |
---|
| 104 | if (x!=0) |
---|
| 105 | p = hAddHilb(i, x, p, &l); |
---|
| 106 | } |
---|
| 107 | pl = *Qpol; |
---|
| 108 | j = Q0[Nv + 1]; |
---|
| 109 | for (i = 0; i < l; i++) |
---|
| 110 | pl[i + j] += p[i]; |
---|
| 111 | x = pure[var[1]]; |
---|
| 112 | if (x!=0) |
---|
| 113 | { |
---|
| 114 | j += x; |
---|
| 115 | for (i = 0; i < l; i++) |
---|
| 116 | pl[i + j] -= p[i]; |
---|
| 117 | } |
---|
| 118 | j += l; |
---|
| 119 | if (j > hLength) |
---|
| 120 | hLength = j; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | static void hHilbStep(scmon pure, scfmon stc, int Nstc, varset var, |
---|
| 124 | int Nvar, int *pol, int Lpol) |
---|
| 125 | { |
---|
| 126 | int iv = Nvar -1, ln, a, a0, a1, b, i; |
---|
| 127 | int x, x0; |
---|
| 128 | scmon pn; |
---|
| 129 | scfmon sn; |
---|
| 130 | int *pon; |
---|
| 131 | if (Nstc==0) |
---|
| 132 | { |
---|
| 133 | hLastHilb(pure, iv, var, pol, Lpol); |
---|
| 134 | return; |
---|
| 135 | } |
---|
| 136 | x = a = 0; |
---|
| 137 | pn = hGetpure(pure); |
---|
| 138 | sn = hGetmem(Nstc, stc, stcmem[iv]); |
---|
| 139 | hStepS(sn, Nstc, var, Nvar, &a, &x); |
---|
| 140 | Q0[iv] = Q0[Nvar]; |
---|
| 141 | ln = Lpol; |
---|
| 142 | pon = pol; |
---|
| 143 | if (a == Nstc) |
---|
| 144 | { |
---|
| 145 | x = pure[var[Nvar]]; |
---|
| 146 | if (x!=0) |
---|
| 147 | pon = hAddHilb(iv, x, pon, &ln); |
---|
| 148 | hHilbStep(pn, sn, a, var, iv, pon, ln); |
---|
| 149 | return; |
---|
| 150 | } |
---|
| 151 | else |
---|
| 152 | { |
---|
| 153 | pon = hAddHilb(iv, x, pon, &ln); |
---|
| 154 | hHilbStep(pn, sn, a, var, iv, pon, ln); |
---|
| 155 | } |
---|
| 156 | b = a; |
---|
| 157 | x0 = 0; |
---|
| 158 | loop |
---|
| 159 | { |
---|
| 160 | Q0[iv] += (x - x0); |
---|
| 161 | a0 = a; |
---|
| 162 | x0 = x; |
---|
| 163 | hStepS(sn, Nstc, var, Nvar, &a, &x); |
---|
| 164 | hElimS(sn, &b, a0, a, var, iv); |
---|
| 165 | a1 = a; |
---|
| 166 | hPure(sn, a0, &a1, var, iv, pn, &i); |
---|
| 167 | hLex2S(sn, b, a0, a1, var, iv, hwork); |
---|
| 168 | b += (a1 - a0); |
---|
| 169 | ln = Lpol; |
---|
| 170 | if (a < Nstc) |
---|
| 171 | { |
---|
| 172 | pon = hAddHilb(iv, x - x0, pol, &ln); |
---|
| 173 | hHilbStep(pn, sn, b, var, iv, pon, ln); |
---|
| 174 | } |
---|
| 175 | else |
---|
| 176 | { |
---|
| 177 | x = pure[var[Nvar]]; |
---|
| 178 | if (x!=0) |
---|
| 179 | pon = hAddHilb(iv, x - x0, pol, &ln); |
---|
| 180 | else |
---|
| 181 | pon = pol; |
---|
| 182 | hHilbStep(pn, sn, b, var, iv, pon, ln); |
---|
| 183 | return; |
---|
| 184 | } |
---|
| 185 | } |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | /* |
---|
| 189 | *basic routines |
---|
| 190 | */ |
---|
| 191 | static void hWDegree(intvec *wdegree) |
---|
| 192 | { |
---|
| 193 | int i, k; |
---|
| 194 | int x; |
---|
| 195 | |
---|
| 196 | for (i=pVariables; i; i--) |
---|
| 197 | { |
---|
| 198 | x = (*wdegree)[i-1]; |
---|
| 199 | if (x != 1) |
---|
| 200 | { |
---|
| 201 | for (k=hNexist-1; k>=0; k--) |
---|
| 202 | { |
---|
| 203 | hexist[k][i] *= x; |
---|
| 204 | } |
---|
| 205 | } |
---|
| 206 | } |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | static intvec * hSeries(ideal S, intvec *modulweight, |
---|
| 210 | int notstc, intvec *wdegree, ideal Q, ring tailRing) |
---|
| 211 | { |
---|
| 212 | intvec *work, *hseries1=NULL; |
---|
| 213 | int mc; |
---|
| 214 | int *p0; |
---|
| 215 | int i, j, k, l, ii, mw; |
---|
| 216 | hexist = hInit(S, Q, &hNexist, tailRing); |
---|
| 217 | if (hNexist==0) |
---|
| 218 | { |
---|
| 219 | hseries1=new intvec(2); |
---|
| 220 | (*hseries1)[0]=1; |
---|
| 221 | (*hseries1)[1]=0; |
---|
| 222 | return hseries1; |
---|
| 223 | } |
---|
| 224 | |
---|
[b96d71] | 225 | #if 0 |
---|
[35aab3] | 226 | if (wdegree == NULL) |
---|
| 227 | hWeight(); |
---|
| 228 | else |
---|
| 229 | hWDegree(wdegree); |
---|
[b96d71] | 230 | #else |
---|
| 231 | if (wdegree != NULL) hWDegree(wdegree); |
---|
| 232 | #endif |
---|
[35aab3] | 233 | |
---|
| 234 | p0 = (int *)omAllocBin(int_bin); |
---|
| 235 | *p0 = 1; |
---|
| 236 | hwork = (scfmon)omAlloc(hNexist * sizeof(scmon)); |
---|
| 237 | hvar = (varset)omAlloc((pVariables + 1) * sizeof(int)); |
---|
| 238 | hpure = (scmon)omAlloc((1 + (pVariables * pVariables)) * sizeof(int)); |
---|
| 239 | stcmem = hCreate(pVariables - 1); |
---|
| 240 | Qpol = (int **)omAlloc((pVariables + 1) * sizeof(int *)); |
---|
| 241 | Ql = (int *)omAlloc0((pVariables + 1) * sizeof(int)); |
---|
| 242 | Q0 = (int *)omAlloc((pVariables + 1) * sizeof(int)); |
---|
| 243 | *Qpol = NULL; |
---|
| 244 | hLength = k = j = 0; |
---|
| 245 | mc = hisModule; |
---|
| 246 | if (mc!=0) |
---|
| 247 | { |
---|
| 248 | mw = hMinModulweight(modulweight); |
---|
| 249 | hstc = (scfmon)omAlloc(hNexist * sizeof(scmon)); |
---|
| 250 | } |
---|
| 251 | else |
---|
| 252 | { |
---|
| 253 | mw = 0; |
---|
| 254 | hstc = hexist; |
---|
| 255 | hNstc = hNexist; |
---|
| 256 | } |
---|
| 257 | loop |
---|
| 258 | { |
---|
| 259 | if (mc!=0) |
---|
| 260 | { |
---|
| 261 | hComp(hexist, hNexist, mc, hstc, &hNstc); |
---|
| 262 | if (modulweight != NULL) |
---|
| 263 | j = (*modulweight)[mc-1]-mw; |
---|
| 264 | } |
---|
| 265 | if (hNstc!=0) |
---|
| 266 | { |
---|
| 267 | hNvar = pVariables; |
---|
| 268 | for (i = hNvar; i>=0; i--) |
---|
| 269 | hvar[i] = i; |
---|
| 270 | //if (notstc) // TODO: no mon divides another |
---|
| 271 | hStaircase(hstc, &hNstc, hvar, hNvar); |
---|
| 272 | hSupp(hstc, hNstc, hvar, &hNvar); |
---|
| 273 | if (hNvar!=0) |
---|
| 274 | { |
---|
| 275 | if ((hNvar > 2) && (hNstc > 10)) |
---|
| 276 | hOrdSupp(hstc, hNstc, hvar, hNvar); |
---|
| 277 | hHilbEst(hstc, hNstc, hvar, hNvar); |
---|
| 278 | memset(hpure, 0, (pVariables + 1) * sizeof(int)); |
---|
| 279 | hPure(hstc, 0, &hNstc, hvar, hNvar, hpure, &hNpure); |
---|
| 280 | hLexS(hstc, hNstc, hvar, hNvar); |
---|
| 281 | Q0[hNvar] = 0; |
---|
| 282 | hHilbStep(hpure, hstc, hNstc, hvar, hNvar, p0, 1); |
---|
| 283 | } |
---|
| 284 | } |
---|
| 285 | else |
---|
| 286 | { |
---|
| 287 | if(*Qpol!=NULL) |
---|
| 288 | (**Qpol)++; |
---|
| 289 | else |
---|
| 290 | { |
---|
| 291 | *Qpol = (int *)omAllocBin(int_bin); |
---|
| 292 | hLength = *Ql = **Qpol = 1; |
---|
| 293 | } |
---|
| 294 | } |
---|
| 295 | if (*Qpol!=NULL) |
---|
| 296 | { |
---|
| 297 | i = hLength; |
---|
| 298 | while ((i > 0) && ((*Qpol)[i - 1] == 0)) |
---|
| 299 | i--; |
---|
| 300 | if (i > 0) |
---|
| 301 | { |
---|
| 302 | l = i + j; |
---|
| 303 | if (l > k) |
---|
| 304 | { |
---|
| 305 | work = new intvec(l); |
---|
| 306 | for (ii=0; ii<k; ii++) |
---|
| 307 | (*work)[ii] = (*hseries1)[ii]; |
---|
| 308 | if (hseries1 != NULL) |
---|
| 309 | delete hseries1; |
---|
| 310 | hseries1 = work; |
---|
| 311 | k = l; |
---|
| 312 | } |
---|
| 313 | while (i > 0) |
---|
| 314 | { |
---|
| 315 | (*hseries1)[i + j - 1] += (*Qpol)[i - 1]; |
---|
| 316 | (*Qpol)[i - 1] = 0; |
---|
| 317 | i--; |
---|
| 318 | } |
---|
| 319 | } |
---|
| 320 | } |
---|
| 321 | mc--; |
---|
| 322 | if (mc <= 0) |
---|
| 323 | break; |
---|
| 324 | } |
---|
| 325 | if (k==0) |
---|
| 326 | { |
---|
| 327 | hseries1=new intvec(2); |
---|
| 328 | (*hseries1)[0]=0; |
---|
| 329 | (*hseries1)[1]=0; |
---|
| 330 | } |
---|
| 331 | else |
---|
| 332 | { |
---|
| 333 | l = k+1; |
---|
| 334 | while ((*hseries1)[l-2]==0) l--; |
---|
| 335 | if (l!=k) |
---|
| 336 | { |
---|
| 337 | work = new intvec(l); |
---|
| 338 | for (ii=l-2; ii>=0; ii--) |
---|
| 339 | (*work)[ii] = (*hseries1)[ii]; |
---|
| 340 | delete hseries1; |
---|
| 341 | hseries1 = work; |
---|
| 342 | } |
---|
| 343 | (*hseries1)[l-1] = mw; |
---|
| 344 | } |
---|
| 345 | for (i = 0; i <= pVariables; i++) |
---|
| 346 | { |
---|
| 347 | if (Ql[i]!=0) |
---|
| 348 | omFreeSize((ADDRESS)Qpol[i], Ql[i] * sizeof(int)); |
---|
| 349 | } |
---|
| 350 | omFreeSize((ADDRESS)Q0, (pVariables + 1) * sizeof(int)); |
---|
| 351 | omFreeSize((ADDRESS)Ql, (pVariables + 1) * sizeof(int)); |
---|
| 352 | omFreeSize((ADDRESS)Qpol, (pVariables + 1) * sizeof(int *)); |
---|
| 353 | hKill(stcmem, pVariables - 1); |
---|
| 354 | omFreeSize((ADDRESS)hpure, (1 + (pVariables * pVariables)) * sizeof(int)); |
---|
| 355 | omFreeSize((ADDRESS)hvar, (pVariables + 1) * sizeof(int)); |
---|
| 356 | omFreeSize((ADDRESS)hwork, hNexist * sizeof(scmon)); |
---|
| 357 | hDelete(hexist, hNexist); |
---|
| 358 | omFreeBin((ADDRESS)p0, int_bin); |
---|
| 359 | if (hisModule!=0) |
---|
| 360 | omFreeSize((ADDRESS)hstc, hNexist * sizeof(scmon)); |
---|
| 361 | return hseries1; |
---|
| 362 | } |
---|
| 363 | |
---|
| 364 | |
---|
| 365 | intvec * hHstdSeries(ideal S, intvec *modulweight, intvec *wdegree, ideal Q, ring tailRing) |
---|
| 366 | { |
---|
| 367 | return hSeries(S, modulweight, 0, wdegree, Q, tailRing); |
---|
| 368 | } |
---|
| 369 | |
---|
| 370 | intvec * hFirstSeries(ideal S, intvec *modulweight, ideal Q, intvec *wdegree, ring tailRing) |
---|
| 371 | { |
---|
| 372 | return hSeries(S, modulweight, 1, wdegree, Q, tailRing); |
---|
| 373 | } |
---|
| 374 | |
---|
| 375 | intvec * hSecondSeries(intvec *hseries1) |
---|
| 376 | { |
---|
| 377 | intvec *work, *hseries2; |
---|
| 378 | int i, j, k, s, t, l; |
---|
| 379 | if (hseries1 == NULL) |
---|
| 380 | return NULL; |
---|
| 381 | work = new intvec(hseries1); |
---|
| 382 | k = l = work->length()-1; |
---|
| 383 | s = 0; |
---|
| 384 | for (i = k-1; i >= 0; i--) |
---|
| 385 | s += (*work)[i]; |
---|
| 386 | loop |
---|
| 387 | { |
---|
| 388 | if ((s != 0) || (k == 1)) |
---|
| 389 | break; |
---|
| 390 | s = 0; |
---|
| 391 | t = (*work)[k-1]; |
---|
| 392 | k--; |
---|
| 393 | for (i = k-1; i >= 0; i--) |
---|
| 394 | { |
---|
| 395 | j = (*work)[i]; |
---|
| 396 | (*work)[i] = -t; |
---|
| 397 | s += t; |
---|
| 398 | t += j; |
---|
| 399 | } |
---|
| 400 | } |
---|
| 401 | hseries2 = new intvec(k+1); |
---|
| 402 | for (i = k-1; i >= 0; i--) |
---|
| 403 | (*hseries2)[i] = (*work)[i]; |
---|
| 404 | (*hseries2)[k] = (*work)[l]; |
---|
| 405 | delete work; |
---|
| 406 | return hseries2; |
---|
| 407 | } |
---|
| 408 | |
---|
[0f7ce1] | 409 | void hDegreeSeries(intvec *s1, intvec *s2, int *co, int *mu) |
---|
[35aab3] | 410 | { |
---|
| 411 | int m, i, j, k; |
---|
| 412 | *co = *mu = 0; |
---|
| 413 | if ((s1 == NULL) || (s2 == NULL)) |
---|
| 414 | return; |
---|
| 415 | i = s1->length(); |
---|
| 416 | j = s2->length(); |
---|
| 417 | if (j > i) |
---|
| 418 | return; |
---|
| 419 | m = 0; |
---|
| 420 | for(k=j-2; k>=0; k--) |
---|
| 421 | m += (*s2)[k]; |
---|
| 422 | *mu = m; |
---|
| 423 | *co = i - j; |
---|
| 424 | } |
---|
| 425 | |
---|
| 426 | static void hPrintHilb(intvec *hseries) |
---|
| 427 | { |
---|
| 428 | int i, j, l, k; |
---|
| 429 | if (hseries == NULL) |
---|
| 430 | return; |
---|
| 431 | l = hseries->length()-1; |
---|
| 432 | k = (*hseries)[l]; |
---|
| 433 | for (i = 0; i < l; i++) |
---|
| 434 | { |
---|
| 435 | j = (*hseries)[i]; |
---|
| 436 | if (j != 0) |
---|
| 437 | { |
---|
| 438 | Print("// %8d t^%d\n", j, i+k); |
---|
| 439 | } |
---|
| 440 | } |
---|
| 441 | } |
---|
| 442 | |
---|
| 443 | /* |
---|
| 444 | *caller |
---|
| 445 | */ |
---|
| 446 | void hLookSeries(ideal S, intvec *modulweight, ideal Q) |
---|
| 447 | { |
---|
| 448 | int co, mu, l; |
---|
| 449 | intvec *hseries2; |
---|
| 450 | intvec *hseries1 = hFirstSeries(S, modulweight, Q); |
---|
| 451 | hPrintHilb(hseries1); |
---|
| 452 | l = hseries1->length()-1; |
---|
| 453 | if (l > 1) |
---|
| 454 | hseries2 = hSecondSeries(hseries1); |
---|
| 455 | else |
---|
| 456 | hseries2 = hseries1; |
---|
| 457 | hDegreeSeries(hseries1, hseries2, &co, &mu); |
---|
| 458 | PrintLn(); |
---|
| 459 | hPrintHilb(hseries2); |
---|
| 460 | if ((l == 1) &&(mu == 0)) |
---|
| 461 | scPrintDegree(pVariables+1, 0); |
---|
| 462 | else |
---|
| 463 | scPrintDegree(co, mu); |
---|
| 464 | if (l>1) |
---|
| 465 | delete hseries1; |
---|
| 466 | delete hseries2; |
---|
| 467 | } |
---|
| 468 | |
---|