1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id: hilb.cc,v 1.16 2000-11-08 15:34:55 obachman Exp $ */ |
---|
5 | /* |
---|
6 | * ABSTRACT - Hilbert series |
---|
7 | */ |
---|
8 | |
---|
9 | #include "mod2.h" |
---|
10 | #include "tok.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 | Exponent_t 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 | Exponent_t 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 | Exponent_t 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 | if (wdegree == NULL) |
---|
225 | hWeight(); |
---|
226 | else |
---|
227 | hWDegree(wdegree); |
---|
228 | p0 = (int *)omAllocBin(int_bin); |
---|
229 | *p0 = 1; |
---|
230 | hwork = (scfmon)omAlloc(hNexist * sizeof(scmon)); |
---|
231 | hvar = (varset)omAlloc((pVariables + 1) * sizeof(int)); |
---|
232 | hpure = (scmon)omAlloc((1 + (pVariables * pVariables)) * sizeof(Exponent_t)); |
---|
233 | stcmem = hCreate(pVariables - 1); |
---|
234 | Qpol = (int **)omAlloc((pVariables + 1) * sizeof(int *)); |
---|
235 | Ql = (int *)omAlloc0((pVariables + 1) * sizeof(int)); |
---|
236 | Q0 = (int *)omAlloc((pVariables + 1) * sizeof(int)); |
---|
237 | *Qpol = NULL; |
---|
238 | hLength = k = j = 0; |
---|
239 | mc = hisModule; |
---|
240 | if (mc!=0) |
---|
241 | { |
---|
242 | mw = hMinModulweight(modulweight); |
---|
243 | hstc = (scfmon)omAlloc(hNexist * sizeof(scmon)); |
---|
244 | } |
---|
245 | else |
---|
246 | { |
---|
247 | mw = 0; |
---|
248 | hstc = hexist; |
---|
249 | hNstc = hNexist; |
---|
250 | } |
---|
251 | loop |
---|
252 | { |
---|
253 | if (mc!=0) |
---|
254 | { |
---|
255 | hComp(hexist, hNexist, mc, hstc, &hNstc); |
---|
256 | if (modulweight != NULL) |
---|
257 | j = (*modulweight)[mc-1]-mw; |
---|
258 | } |
---|
259 | if (hNstc!=0) |
---|
260 | { |
---|
261 | hNvar = pVariables; |
---|
262 | for (i = hNvar; i; i--) |
---|
263 | hvar[i] = i; |
---|
264 | if (notstc) |
---|
265 | hStaircase(hstc, &hNstc, hvar, hNvar); |
---|
266 | hSupp(hstc, hNstc, hvar, &hNvar); |
---|
267 | if (hNvar!=0) |
---|
268 | { |
---|
269 | if ((hNvar > 2) && (hNstc > 10)) |
---|
270 | hOrdSupp(hstc, hNstc, hvar, hNvar); |
---|
271 | hHilbEst(hstc, hNstc, hvar, hNvar); |
---|
272 | memset(hpure, 0, (pVariables + 1) * sizeof(Exponent_t)); |
---|
273 | hPure(hstc, 0, &hNstc, hvar, hNvar, hpure, &hNpure); |
---|
274 | hLexS(hstc, hNstc, hvar, hNvar); |
---|
275 | Q0[hNvar] = 0; |
---|
276 | hHilbStep(hpure, hstc, hNstc, hvar, hNvar, p0, 1); |
---|
277 | } |
---|
278 | } |
---|
279 | else |
---|
280 | { |
---|
281 | if(*Qpol!=NULL) |
---|
282 | (**Qpol)++; |
---|
283 | else |
---|
284 | { |
---|
285 | *Qpol = (int *)omAllocBin(int_bin); |
---|
286 | hLength = *Ql = **Qpol = 1; |
---|
287 | } |
---|
288 | } |
---|
289 | if (*Qpol!=NULL) |
---|
290 | { |
---|
291 | i = hLength; |
---|
292 | while ((i > 0) && ((*Qpol)[i - 1] == 0)) |
---|
293 | i--; |
---|
294 | if (i > 0) |
---|
295 | { |
---|
296 | l = i + j; |
---|
297 | if (l > k) |
---|
298 | { |
---|
299 | work = new intvec(l); |
---|
300 | for (ii=0; ii<k; ii++) |
---|
301 | (*work)[ii] = (*hseries1)[ii]; |
---|
302 | if (hseries1 != NULL) |
---|
303 | delete hseries1; |
---|
304 | hseries1 = work; |
---|
305 | k = l; |
---|
306 | } |
---|
307 | while (i > 0) |
---|
308 | { |
---|
309 | (*hseries1)[i + j - 1] += (*Qpol)[i - 1]; |
---|
310 | (*Qpol)[i - 1] = 0; |
---|
311 | i--; |
---|
312 | } |
---|
313 | } |
---|
314 | } |
---|
315 | mc--; |
---|
316 | if (mc <= 0) |
---|
317 | break; |
---|
318 | } |
---|
319 | if (k==0) |
---|
320 | { |
---|
321 | hseries1=new intvec(2); |
---|
322 | (*hseries1)[0]=0; |
---|
323 | (*hseries1)[1]=0; |
---|
324 | } |
---|
325 | else |
---|
326 | { |
---|
327 | l = k+1; |
---|
328 | while ((*hseries1)[l-2]==0) l--; |
---|
329 | if (l!=k) |
---|
330 | { |
---|
331 | work = new intvec(l); |
---|
332 | for (ii=l-2; ii>=0; ii--) |
---|
333 | (*work)[ii] = (*hseries1)[ii]; |
---|
334 | delete hseries1; |
---|
335 | hseries1 = work; |
---|
336 | } |
---|
337 | (*hseries1)[l-1] = mw; |
---|
338 | } |
---|
339 | for (i = 0; i <= pVariables; i++) |
---|
340 | { |
---|
341 | if (Ql[i]!=0) |
---|
342 | omFreeSize((ADDRESS)Qpol[i], Ql[i] * sizeof(int)); |
---|
343 | } |
---|
344 | omFreeSize((ADDRESS)Q0, (pVariables + 1) * sizeof(int)); |
---|
345 | omFreeSize((ADDRESS)Ql, (pVariables + 1) * sizeof(int)); |
---|
346 | omFreeSize((ADDRESS)Qpol, (pVariables + 1) * sizeof(int *)); |
---|
347 | hKill(stcmem, pVariables - 1); |
---|
348 | omFreeSize((ADDRESS)hpure, (1 + (pVariables * pVariables)) * sizeof(Exponent_t)); |
---|
349 | omFreeSize((ADDRESS)hvar, (pVariables + 1) * sizeof(int)); |
---|
350 | omFreeSize((ADDRESS)hwork, hNexist * sizeof(scmon)); |
---|
351 | hDelete(hexist, hNexist); |
---|
352 | omFreeBin((ADDRESS)p0, int_bin); |
---|
353 | if (hisModule!=0) |
---|
354 | omFreeSize((ADDRESS)hstc, hNexist * sizeof(scmon)); |
---|
355 | return hseries1; |
---|
356 | } |
---|
357 | |
---|
358 | |
---|
359 | intvec * hHstdSeries(ideal S, intvec *modulweight, intvec *wdegree, ideal Q, ring tailRing) |
---|
360 | { |
---|
361 | return hSeries(S, modulweight, 0, wdegree, Q, tailRing); |
---|
362 | } |
---|
363 | |
---|
364 | intvec * hFirstSeries(ideal S, intvec *modulweight, ideal Q, intvec *wdegree, ring tailRing) |
---|
365 | { |
---|
366 | return hSeries(S, modulweight, 1, wdegree, Q, tailRing); |
---|
367 | } |
---|
368 | |
---|
369 | intvec * hSecondSeries(intvec *hseries1) |
---|
370 | { |
---|
371 | intvec *work, *hseries2; |
---|
372 | int i, j, k, s, t, l; |
---|
373 | if (hseries1 == NULL) |
---|
374 | return NULL; |
---|
375 | work = new intvec(hseries1); |
---|
376 | k = l = work->length()-1; |
---|
377 | s = 0; |
---|
378 | for (i = k-1; i >= 0; i--) |
---|
379 | s += (*work)[i]; |
---|
380 | loop |
---|
381 | { |
---|
382 | if ((s != 0) || (k == 1)) |
---|
383 | break; |
---|
384 | s = 0; |
---|
385 | t = (*work)[k-1]; |
---|
386 | k--; |
---|
387 | for (i = k-1; i >= 0; i--) |
---|
388 | { |
---|
389 | j = (*work)[i]; |
---|
390 | (*work)[i] = -t; |
---|
391 | s += t; |
---|
392 | t += j; |
---|
393 | } |
---|
394 | } |
---|
395 | hseries2 = new intvec(k+1); |
---|
396 | for (i = k-1; i >= 0; i--) |
---|
397 | (*hseries2)[i] = (*work)[i]; |
---|
398 | (*hseries2)[k] = (*work)[l]; |
---|
399 | delete work; |
---|
400 | return hseries2; |
---|
401 | } |
---|
402 | |
---|
403 | static void hDegreeSeries(intvec *s1, intvec *s2, int *co, int *mu) |
---|
404 | { |
---|
405 | int m, i, j, k; |
---|
406 | *co = *mu = 0; |
---|
407 | if ((s1 == NULL) || (s2 == NULL)) |
---|
408 | return; |
---|
409 | i = s1->length(); |
---|
410 | j = s2->length(); |
---|
411 | if (j > i) |
---|
412 | return; |
---|
413 | m = 0; |
---|
414 | for(k=j-2; k>=0; k--) |
---|
415 | m += (*s2)[k]; |
---|
416 | *mu = m; |
---|
417 | *co = i - j; |
---|
418 | } |
---|
419 | |
---|
420 | static void hPrintHilb(intvec *hseries) |
---|
421 | { |
---|
422 | int i, j, l, k; |
---|
423 | if (hseries == NULL) |
---|
424 | return; |
---|
425 | l = hseries->length()-1; |
---|
426 | k = (*hseries)[l]; |
---|
427 | for (i = 0; i < l; i++) |
---|
428 | { |
---|
429 | j = (*hseries)[i]; |
---|
430 | if (j != 0) |
---|
431 | { |
---|
432 | Print("// %8d t^%d\n", j, i+k); |
---|
433 | } |
---|
434 | } |
---|
435 | } |
---|
436 | |
---|
437 | static void hPrintDegree(int co, int mu) |
---|
438 | { |
---|
439 | if (pOrdSgn == 1) |
---|
440 | Print("// codimension = %d\n// dimension = %d\n// degree = %d\n", |
---|
441 | co, pVariables - co, mu); |
---|
442 | else |
---|
443 | Print("// codimension = %d\n// dimension = %d\n// multiplicity = %d\n", |
---|
444 | co, pVariables - co, mu); |
---|
445 | } |
---|
446 | |
---|
447 | /* |
---|
448 | *caller |
---|
449 | */ |
---|
450 | void hLookSeries(ideal S, intvec *modulweight, ideal Q) |
---|
451 | { |
---|
452 | int co, mu, l; |
---|
453 | intvec *hseries2; |
---|
454 | intvec *hseries1 = hFirstSeries(S, modulweight, Q); |
---|
455 | hPrintHilb(hseries1); |
---|
456 | l = hseries1->length()-1; |
---|
457 | if (l > 1) |
---|
458 | hseries2 = hSecondSeries(hseries1); |
---|
459 | else |
---|
460 | hseries2 = hseries1; |
---|
461 | hDegreeSeries(hseries1, hseries2, &co, &mu); |
---|
462 | PrintLn(); |
---|
463 | hPrintHilb(hseries2); |
---|
464 | if ((l == 1) &&(mu == 0)) |
---|
465 | hPrintDegree(pVariables+1, 0); |
---|
466 | else |
---|
467 | hPrintDegree(co, mu); |
---|
468 | if (l>1) |
---|
469 | delete hseries1; |
---|
470 | delete hseries2; |
---|
471 | } |
---|
472 | |
---|