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