source: git/kernel/hilb.cc @ 68349d

spielwiese
Last change on this file since 68349d was 35aab3, checked in by Hans Schönemann <hannes@…>, 21 years ago
This commit was generated by cvs2svn to compensate for changes in r6879, which included commits to RCS files with non-trunk default branches. git-svn-id: file:///usr/local/Singular/svn/trunk@6880 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 9.3 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: hilb.cc,v 1.1.1.1 2003-10-06 12:15:53 Singular Exp $ */
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
18static int  **Qpol;
19static int  *Q0, *Ql;
20static int  hLength;
21
22static 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
36static 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
72static 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
96static 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
123static 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*/
191static 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
209static 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
225  if (wdegree == NULL)
226    hWeight();
227  else
228    hWDegree(wdegree);
229
230  p0 = (int *)omAllocBin(int_bin);
231  *p0 = 1;
232  hwork = (scfmon)omAlloc(hNexist * sizeof(scmon));
233  hvar = (varset)omAlloc((pVariables + 1) * sizeof(int));
234  hpure = (scmon)omAlloc((1 + (pVariables * pVariables)) * sizeof(int));
235  stcmem = hCreate(pVariables - 1);
236  Qpol = (int **)omAlloc((pVariables + 1) * sizeof(int *));
237  Ql = (int *)omAlloc0((pVariables + 1) * sizeof(int));
238  Q0 = (int *)omAlloc((pVariables + 1) * sizeof(int));
239  *Qpol = NULL;
240  hLength = k = j = 0;
241  mc = hisModule;
242  if (mc!=0)
243  {
244    mw = hMinModulweight(modulweight);
245    hstc = (scfmon)omAlloc(hNexist * sizeof(scmon));
246  }
247  else
248  {
249    mw = 0;
250    hstc = hexist;
251    hNstc = hNexist;
252  }
253  loop
254  {
255    if (mc!=0)
256    {
257      hComp(hexist, hNexist, mc, hstc, &hNstc);
258      if (modulweight != NULL)
259        j = (*modulweight)[mc-1]-mw;
260    }
261    if (hNstc!=0)
262    {
263      hNvar = pVariables;
264      for (i = hNvar; i>=0; i--)
265        hvar[i] = i;
266      //if (notstc) // TODO: no mon divides another
267        hStaircase(hstc, &hNstc, hvar, hNvar);
268      hSupp(hstc, hNstc, hvar, &hNvar);
269      if (hNvar!=0)
270      {
271        if ((hNvar > 2) && (hNstc > 10))
272          hOrdSupp(hstc, hNstc, hvar, hNvar);
273        hHilbEst(hstc, hNstc, hvar, hNvar);
274        memset(hpure, 0, (pVariables + 1) * sizeof(int));
275        hPure(hstc, 0, &hNstc, hvar, hNvar, hpure, &hNpure);
276        hLexS(hstc, hNstc, hvar, hNvar);
277        Q0[hNvar] = 0;
278        hHilbStep(hpure, hstc, hNstc, hvar, hNvar, p0, 1);
279      }
280    }
281    else
282    {
283      if(*Qpol!=NULL)
284        (**Qpol)++;
285      else
286      {
287        *Qpol = (int *)omAllocBin(int_bin);
288        hLength = *Ql = **Qpol = 1;
289      }
290    }
291    if (*Qpol!=NULL)
292    {
293      i = hLength;
294      while ((i > 0) && ((*Qpol)[i - 1] == 0))
295        i--;
296      if (i > 0)
297      {
298        l = i + j;
299        if (l > k)
300        {
301          work = new intvec(l);
302          for (ii=0; ii<k; ii++)
303            (*work)[ii] = (*hseries1)[ii];
304          if (hseries1 != NULL)
305            delete hseries1;
306          hseries1 = work;
307          k = l;
308        }
309        while (i > 0)
310        {
311          (*hseries1)[i + j - 1] += (*Qpol)[i - 1];
312          (*Qpol)[i - 1] = 0;
313          i--;
314        }
315      }
316    }
317    mc--;
318    if (mc <= 0)
319      break;
320  }
321  if (k==0)
322  {
323    hseries1=new intvec(2);
324    (*hseries1)[0]=0;
325    (*hseries1)[1]=0;
326  }
327  else
328  {
329    l = k+1;
330    while ((*hseries1)[l-2]==0) l--;
331    if (l!=k)
332    {
333      work = new intvec(l);
334      for (ii=l-2; ii>=0; ii--)
335        (*work)[ii] = (*hseries1)[ii];
336      delete hseries1;
337      hseries1 = work;
338    }
339    (*hseries1)[l-1] = mw;
340  }
341  for (i = 0; i <= pVariables; i++)
342  {
343    if (Ql[i]!=0)
344      omFreeSize((ADDRESS)Qpol[i], Ql[i] * sizeof(int));
345  }
346  omFreeSize((ADDRESS)Q0, (pVariables + 1) * sizeof(int));
347  omFreeSize((ADDRESS)Ql, (pVariables + 1) * sizeof(int));
348  omFreeSize((ADDRESS)Qpol, (pVariables + 1) * sizeof(int *));
349  hKill(stcmem, pVariables - 1);
350  omFreeSize((ADDRESS)hpure, (1 + (pVariables * pVariables)) * sizeof(int));
351  omFreeSize((ADDRESS)hvar, (pVariables + 1) * sizeof(int));
352  omFreeSize((ADDRESS)hwork, hNexist * sizeof(scmon));
353  hDelete(hexist, hNexist);
354  omFreeBin((ADDRESS)p0,  int_bin);
355  if (hisModule!=0)
356    omFreeSize((ADDRESS)hstc, hNexist * sizeof(scmon));
357  return hseries1;
358}
359
360
361intvec * hHstdSeries(ideal S, intvec *modulweight, intvec *wdegree, ideal Q, ring tailRing)
362{
363  return hSeries(S, modulweight, 0, wdegree, Q, tailRing);
364}
365
366intvec * hFirstSeries(ideal S, intvec *modulweight, ideal Q, intvec *wdegree, ring tailRing)
367{
368  return hSeries(S, modulweight, 1, wdegree, Q, tailRing);
369}
370
371intvec * hSecondSeries(intvec *hseries1)
372{
373  intvec *work, *hseries2;
374  int i, j, k, s, t, l;
375  if (hseries1 == NULL)
376    return NULL;
377  work = new intvec(hseries1);
378  k = l = work->length()-1;
379  s = 0;
380  for (i = k-1; i >= 0; i--)
381    s += (*work)[i];
382  loop
383  {
384    if ((s != 0) || (k == 1))
385      break;
386    s = 0;
387    t = (*work)[k-1];
388    k--;
389    for (i = k-1; i >= 0; i--)
390    {
391      j = (*work)[i];
392      (*work)[i] = -t;
393      s += t;
394      t += j;
395    }
396  }
397  hseries2 = new intvec(k+1);
398  for (i = k-1; i >= 0; i--)
399    (*hseries2)[i] = (*work)[i];
400  (*hseries2)[k] = (*work)[l];
401  delete work;
402  return hseries2;
403}
404
405static void hDegreeSeries(intvec *s1, intvec *s2, int *co, int *mu)
406{
407  int m, i, j, k;
408  *co = *mu = 0;
409  if ((s1 == NULL) || (s2 == NULL))
410    return;
411  i = s1->length();
412  j = s2->length();
413  if (j > i)
414    return;
415  m = 0;
416  for(k=j-2; k>=0; k--)
417    m += (*s2)[k];
418  *mu = m;
419  *co = i - j;
420}
421
422static void hPrintHilb(intvec *hseries)
423{
424  int  i, j, l, k;
425  if (hseries == NULL)
426    return;
427  l = hseries->length()-1;
428  k = (*hseries)[l];
429  for (i = 0; i < l; i++)
430  {
431    j = (*hseries)[i];
432    if (j != 0)
433    {
434      Print("//  %8d t^%d\n", j, i+k);
435    }
436  }
437}
438
439/*
440*caller
441*/
442void hLookSeries(ideal S, intvec *modulweight, ideal Q)
443{
444  int co, mu, l;
445  intvec *hseries2;
446  intvec *hseries1 = hFirstSeries(S, modulweight, Q);
447  hPrintHilb(hseries1);
448  l = hseries1->length()-1;
449  if (l > 1)
450    hseries2 = hSecondSeries(hseries1);
451  else
452    hseries2 = hseries1;
453  hDegreeSeries(hseries1, hseries2, &co, &mu);
454  PrintLn();
455  hPrintHilb(hseries2);
456  if ((l == 1) &&(mu == 0))
457    scPrintDegree(pVariables+1, 0);
458  else
459    scPrintDegree(co, mu);
460  if (l>1)
461    delete hseries1;
462  delete hseries2;
463}
464
Note: See TracBrowser for help on using the repository browser.