source: git/libpolys/polys/simpleideals.cc @ 6c084af

fieker-DuValspielwiese
Last change on this file since 6c084af was 9765f3, checked in by Hans Schoenemann <hannes@…>, 13 years ago
fixed pFDeg (not a global variable, but a member of ring) fixed sip_sideal_bin
  • Property mode set to 100644
File size: 38.7 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id$ */
5/*
6* ABSTRACT - all basic methods to manipulate ideals
7*/
8
9
10/* includes */
11#include "config.h"
12#include <misc/auxiliary.h>
13#include <misc/options.h>
14#include <omalloc/omalloc.h>
15#include <polys/monomials/p_polys.h>
16#include <polys/weight.h>
17#include <polys/matpol.h>
18#include <misc/intvec.h>
19#include <polys/simpleideals.h>
20#include "sbuckets.h"
21
22omBin sip_sideal_bin;
23
24static poly * idpower;
25/*collects the monomials in makemonoms, must be allocated befor*/
26static int idpowerpoint;
27/*index of the actual monomial in idpower*/
28static poly * givenideal;
29/*the ideal from which a power is computed*/
30
31/*2
32* initialise an ideal
33*/
34ideal idInit(int idsize, int rank)
35{
36  /*- initialise an ideal -*/
37  ideal hh = (ideal )omAllocBin(sip_sideal_bin);
38  hh->nrows = 1;
39  hh->rank = rank;
40  IDELEMS(hh) = idsize;
41  if (idsize>0)
42  {
43    hh->m = (poly *)omAlloc0(idsize*sizeof(poly));
44  }
45  else
46    hh->m=NULL;
47  return hh;
48}
49
50#ifdef PDEBUG
51// this is only for outputting an ideal within the debugger
52void idShow(const ideal id, const ring lmRing, const ring tailRing, const int debugPrint)
53{
54  assume( debugPrint >= 0 );
55
56  if( id == NULL )
57    PrintS("(NULL)");
58  else
59  {
60    Print("Module of rank %ld,real rank %ld and %d generators.\n",
61          id->rank,id_RankFreeModule(id, lmRing, tailRing),IDELEMS(id));
62
63    int j = (id->ncols*id->nrows) - 1;
64    while ((j > 0) && (id->m[j]==NULL)) j--;
65    for (int i = 0; i <= j; i++)
66    {
67      Print("generator %d: ",i); p_DebugPrint(id->m[i], lmRing, tailRing, debugPrint);
68    }
69  }
70}
71#endif
72
73/* index of generator with leading term in ground ring (if any);
74   otherwise -1 */
75int id_PosConstant(ideal id, const ring r)
76{
77  int k;
78  for (k = IDELEMS(id)-1; k>=0; k--)
79  {
80    if (p_LmIsConstantComp(id->m[k], r) == TRUE)
81      return k;
82  }
83  return -1;
84}
85
86/*2
87* initialise the maximal ideal (at 0)
88*/
89ideal id_MaxIdeal (const ring r)
90{
91  int l;
92  ideal hh=NULL;
93
94  hh=idInit(rVar(r),1);
95  for (l=0; l<rVar(r); l++)
96  {
97    hh->m[l] = p_One(r);
98    p_SetExp(hh->m[l],l+1,1,r);
99    p_Setm(hh->m[l],r);
100  }
101  return hh;
102}
103
104/*2
105* deletes an ideal/matrix
106*/
107void id_Delete (ideal * h, ring r)
108{
109  int j,elems;
110  if (*h == NULL)
111    return;
112  elems=j=(*h)->nrows*(*h)->ncols;
113  if (j>0)
114  {
115    do
116    {
117      p_Delete(&((*h)->m[--j]), r);
118    }
119    while (j>0);
120    omFreeSize((ADDRESS)((*h)->m),sizeof(poly)*elems);
121  }
122  omFreeBin((ADDRESS)*h, sip_sideal_bin);
123  *h=NULL;
124}
125
126
127/*2
128* Shallowdeletes an ideal/matrix
129*/
130void id_ShallowDelete (ideal *h, ring r)
131{
132  int j,elems;
133  if (*h == NULL)
134    return;
135  elems=j=(*h)->nrows*(*h)->ncols;
136  if (j>0)
137  {
138    do
139    {
140      p_ShallowDelete(&((*h)->m[--j]), r);
141    }
142    while (j>0);
143    omFreeSize((ADDRESS)((*h)->m),sizeof(poly)*elems);
144  }
145  omFreeBin((ADDRESS)*h, sip_sideal_bin);
146  *h=NULL;
147}
148
149/*2
150*gives an ideal the minimal possible size
151*/
152void idSkipZeroes (ideal ide)
153{
154  int k;
155  int j = -1;
156  BOOLEAN change=FALSE;
157  for (k=0; k<IDELEMS(ide); k++)
158  {
159    if (ide->m[k] != NULL)
160    {
161      j++;
162      if (change)
163      {
164        ide->m[j] = ide->m[k];
165      }
166    }
167    else
168    {
169      change=TRUE;
170    }
171  }
172  if (change)
173  {
174    if (j == -1)
175      j = 0;
176    else
177    {
178      for (k=j+1; k<IDELEMS(ide); k++)
179        ide->m[k] = NULL;
180    }
181    pEnlargeSet(&(ide->m),IDELEMS(ide),j+1-IDELEMS(ide));
182    IDELEMS(ide) = j+1;
183  }
184}
185
186/*2
187* copies the first k (>= 1) entries of the given ideal
188* and returns these as a new ideal
189* (Note that the copied polynomials may be zero.)
190*/
191ideal id_CopyFirstK (const ideal ide, const int k,const ring r)
192{
193  ideal newI = idInit(k, 0);
194  for (int i = 0; i < k; i++)
195    newI->m[i] = p_Copy(ide->m[i],r);
196  return newI;
197}
198
199/*2
200* ideal id = (id[i])
201* result is leadcoeff(id[i]) = 1
202*/
203void id_Norm(ideal id, const ring r)
204{
205  for (int i=IDELEMS(id)-1; i>=0; i--)
206  {
207    if (id->m[i] != NULL)
208    {
209      p_Norm(id->m[i],r);
210    }
211  }
212}
213
214/*2
215* ideal id = (id[i]), c any unit
216* if id[i] = c*id[j] then id[j] is deleted for j > i
217*/
218void id_DelMultiples(ideal id, const ring r)
219{
220  int i, j;
221  int k = IDELEMS(id)-1;
222  for (i=k; i>=0; i--)
223  {
224    if (id->m[i]!=NULL)
225    {
226      for (j=k; j>i; j--)
227      {
228        if (id->m[j]!=NULL)
229        {
230#ifdef HAVE_RINGS
231          if (rField_is_Ring(r))
232          {
233            /* if id[j] = c*id[i] then delete id[j].
234               In the below cases of a ground field, we
235               check whether id[i] = c*id[j] and, if so,
236               delete id[j] for historical reasons (so
237               that previous output does not change) */
238            if (p_ComparePolys(id->m[j], id->m[i],r)) p_Delete(&id->m[j],r);
239          }
240          else
241          {
242            if (p_ComparePolys(id->m[i], id->m[j],r)) p_Delete(&id->m[j],r);
243          }
244#else
245          if (p_ComparePolys(id->m[i], id->m[j],r)) p_Delete(&id->m[j],r);
246#endif   
247        }
248      }
249    }
250  }
251}
252
253/*2
254* ideal id = (id[i])
255* if id[i] = id[j] then id[j] is deleted for j > i
256*/
257void id_DelEquals(ideal id, const ring r)
258{
259  int i, j;
260  int k = IDELEMS(id)-1;
261  for (i=k; i>=0; i--)
262  {
263    if (id->m[i]!=NULL)
264    {
265      for (j=k; j>i; j--)
266      {
267        if ((id->m[j]!=NULL)
268        && (p_EqualPolys(id->m[i], id->m[j],r)))
269        {
270          p_Delete(&id->m[j],r);
271        }
272      }
273    }
274  }
275}
276
277//
278// Delete id[j], if Lm(j) == Lm(i) and both LC(j), LC(i) are units and j > i
279//
280void id_DelLmEquals(ideal id, const ring r)
281{
282  int i, j;
283  int k = IDELEMS(id)-1;
284  for (i=k; i>=0; i--)
285  {
286    if (id->m[i] != NULL)
287    {
288      for (j=k; j>i; j--)
289      {
290        if ((id->m[j] != NULL)
291        && p_LmEqual(id->m[i], id->m[j],r)
292#ifdef HAVE_RINGS
293        && n_IsUnit(pGetCoeff(id->m[i]),r->cf) && n_IsUnit(pGetCoeff(id->m[j]),r->cf)
294#endif
295        )
296        {
297          p_Delete(&id->m[j],r);
298        }
299      }
300    }
301  }
302}
303
304//
305// delete id[j], if LT(j) == coeff*mon*LT(i) and vice versa, i.e.,
306// delete id[i], if LT(i) == coeff*mon*LT(j)
307//
308void id_DelDiv(ideal id, const ring r)
309{
310  int i, j;
311  int k = IDELEMS(id)-1;
312  for (i=k; i>=0; i--)
313  {
314    if (id->m[i] != NULL)
315    {
316      for (j=k; j>i; j--)
317      {
318        if (id->m[j]!=NULL)
319        {
320#ifdef HAVE_RINGS
321          if (rField_is_Ring(r))
322          {
323            if (p_DivisibleByRingCase(id->m[i], id->m[j],r))
324            {
325              p_Delete(&id->m[j],r);
326            }
327            else if (p_DivisibleByRingCase(id->m[j], id->m[i],r))
328            {
329              p_Delete(&id->m[i],r);
330              break;
331            }
332          }
333          else
334          {
335#endif
336          /* the case of a ground field: */
337          if (p_DivisibleBy(id->m[i], id->m[j],r))
338          {
339            p_Delete(&id->m[j],r);
340          }
341          else if (p_DivisibleBy(id->m[j], id->m[i],r))
342          {
343            p_Delete(&id->m[i],r);
344            break;
345          }
346#ifdef HAVE_RINGS
347          }
348#endif   
349        }
350      }
351    }
352  }
353}
354
355/*2
356*test if the ideal has only constant polynomials
357*/
358BOOLEAN id_IsConstant(ideal id, const ring r)
359{
360  int k;
361  for (k = IDELEMS(id)-1; k>=0; k--)
362  {
363    if (!p_IsConstantPoly(id->m[k],r))
364      return FALSE;
365  }
366  return TRUE;
367}
368
369/*2
370* copy an ideal
371*/
372#ifdef PDEBUG
373ideal id_DBCopy(ideal h1,const char *f,int l, const ring r)
374{
375  int i;
376  ideal h2;
377
378  id_DBTest(h1,PDEBUG,f,l,r);
379//#ifdef TEST
380  if (h1 == NULL)
381  {
382    h2=idDBInit(1,1,f,l);
383  }
384  else
385//#endif
386  {
387    h2=idDBInit(IDELEMS(h1),h1->rank,f,l);
388    for (i=IDELEMS(h1)-1; i>=0; i--)
389      h2->m[i] = p_Copy(h1->m[i],r);
390  }
391  return h2;
392}
393#else
394ideal id_Copy(ideal h1, const ring r)
395{
396  int i;
397  ideal h2;
398
399//#ifdef TEST
400  if (h1 == NULL)
401  {
402    h2=idInit(1,1);
403  }
404  else
405//#endif
406  {
407    h2=idInit(IDELEMS(h1),h1->rank);
408    for (i=IDELEMS(h1)-1; i>=0; i--)
409      h2->m[i] = p_Copy(h1->m[i],r);
410  }
411  return h2;
412}
413#endif
414
415#ifdef PDEBUG
416void id_DBTest(ideal h1, int level, const char *f,const int l, const ring r)
417{
418  int i;
419
420  if (h1 != NULL)
421  {
422    // assume(IDELEMS(h1) > 0); for ideal/module, does not apply to matrix
423    omCheckAddrSize(h1,sizeof(*h1));
424    omdebugAddrSize(h1->m,h1->ncols*h1->nrows*sizeof(poly));
425    /* to be able to test matrices: */
426    for (i=(h1->ncols*h1->nrows)-1; i>=0; i--)
427      _p_Test(h1->m[i], r, level);
428    int new_rk=id_RankFreeModule(h1,r);
429    if(new_rk > h1->rank)
430    {
431      dReportError("wrong rank %d (should be %d) in %s:%d\n",
432                   h1->rank, new_rk, f,l);
433      omPrintAddrInfo(stderr, h1, " for ideal");
434      h1->rank=new_rk;
435    }
436  }
437}
438#endif
439
440/*3
441* for idSort: compare a and b revlex inclusive module comp.
442*/
443static int p_Comp_RevLex(poly a, poly b,BOOLEAN nolex, const ring R)
444{
445  if (b==NULL) return 1;
446  if (a==NULL) return -1;
447
448  if (nolex)
449  {
450    int r=p_LmCmp(a,b,R);
451    if (r!=0) return r;
452    number h=n_Sub(pGetCoeff(a),pGetCoeff(b),R->cf);
453    r = -1+n_IsZero(h,R->cf)+2*n_GreaterZero(h,R->cf); /* -1: <, 0:==, 1: > */
454    n_Delete(&h, R->cf);
455    return r;
456  }
457  int l=rVar(R);
458  while ((l>0) && (p_GetExp(a,l,R)==p_GetExp(b,l,R))) l--;
459  if (l==0)
460  {
461    if (p_GetComp(a,R)==p_GetComp(b,R))
462    {
463      number h=n_Sub(pGetCoeff(a),pGetCoeff(b),R->cf);
464      int r = -1+n_IsZero(h,R->cf)+2*n_GreaterZero(h,R->cf); /* -1: <, 0:==, 1: > */
465      n_Delete(&h,R->cf);
466      return r;
467    }
468    if (p_GetComp(a,R)>p_GetComp(b,R)) return 1;
469  }
470  else if (p_GetExp(a,l,R)>p_GetExp(b,l,R))
471    return 1;
472  return -1;
473}
474
475/*2
476*sorts the ideal w.r.t. the actual ringordering
477*uses lex-ordering when nolex = FALSE
478*/
479intvec *id_Sort(ideal id,BOOLEAN nolex, const ring r)
480{
481  poly p,q;
482  intvec * result = new intvec(IDELEMS(id));
483  int i, j, actpos=0, newpos, l;
484  int diff, olddiff, lastcomp, newcomp;
485  BOOLEAN notFound;
486
487  for (i=0;i<IDELEMS(id);i++)
488  {
489    if (id->m[i]!=NULL)
490    {
491      notFound = TRUE;
492      newpos = actpos / 2;
493      diff = (actpos+1) / 2;
494      diff = (diff+1) / 2;
495      lastcomp = p_Comp_RevLex(id->m[i],id->m[(*result)[newpos]],nolex,r);
496      if (lastcomp<0)
497      {
498        newpos -= diff;
499      }
500      else if (lastcomp>0)
501      {
502        newpos += diff;
503      }
504      else
505      {
506        notFound = FALSE;
507      }
508      //while ((newpos>=0) && (newpos<actpos) && (notFound))
509      while (notFound && (newpos>=0) && (newpos<actpos))
510      {
511        newcomp = p_Comp_RevLex(id->m[i],id->m[(*result)[newpos]],nolex,r);
512        olddiff = diff;
513        if (diff>1)
514        {
515          diff = (diff+1) / 2;
516          if ((newcomp==1)
517          && (actpos-newpos>1)
518          && (diff>1)
519          && (newpos+diff>=actpos))
520          {
521            diff = actpos-newpos-1;
522          }
523          else if ((newcomp==-1)
524          && (diff>1)
525          && (newpos<diff))
526          {
527            diff = newpos;
528          }
529        }
530        if (newcomp<0)
531        {
532          if ((olddiff==1) && (lastcomp>0))
533            notFound = FALSE;
534          else
535            newpos -= diff;
536        }
537        else if (newcomp>0)
538        {
539          if ((olddiff==1) && (lastcomp<0))
540          {
541            notFound = FALSE;
542            newpos++;
543          }
544          else
545          {
546            newpos += diff;
547          }
548        }
549        else
550        {
551          notFound = FALSE;
552        }
553        lastcomp = newcomp;
554        if (diff==0) notFound=FALSE; /*hs*/
555      }
556      if (newpos<0) newpos = 0;
557      if (newpos>actpos) newpos = actpos;
558      while ((newpos<actpos) && (p_Comp_RevLex(id->m[i],id->m[(*result)[newpos]],nolex,r)==0))
559        newpos++;
560      for (j=actpos;j>newpos;j--)
561      {
562        (*result)[j] = (*result)[j-1];
563      }
564      (*result)[newpos] = i;
565      actpos++;
566    }
567  }
568  for (j=0;j<actpos;j++) (*result)[j]++;
569  return result;
570}
571
572/*2
573* concat the lists h1 and h2 without zeros
574*/
575ideal id_SimpleAdd (ideal h1,ideal h2, const ring R)
576{
577  int i,j,r,l;
578  ideal result;
579
580  if (h1==NULL) return id_Copy(h2,R);
581  if (h2==NULL) return id_Copy(h1,R);
582  j = IDELEMS(h1)-1;
583  while ((j >= 0) && (h1->m[j] == NULL)) j--;
584  i = IDELEMS(h2)-1;
585  while ((i >= 0) && (h2->m[i] == NULL)) i--;
586  r = si_max(h1->rank,h2->rank);
587  if (i+j==(-2))
588    return idInit(1,r);
589  else
590    result=idInit(i+j+2,r);
591  for (l=j; l>=0; l--)
592  {
593    result->m[l] = p_Copy(h1->m[l],R);
594  }
595  r = i+j+1;
596  for (l=i; l>=0; l--, r--)
597  {
598    result->m[r] = p_Copy(h2->m[l],R);
599  }
600  return result;
601}
602
603/*2
604* insert h2 into h1 (if h2 is not the zero polynomial)
605* return TRUE iff h2 was indeed inserted
606*/
607BOOLEAN idInsertPoly (ideal h1, poly h2)
608{
609  if (h2==NULL) return FALSE;
610  int j = IDELEMS(h1)-1;
611  while ((j >= 0) && (h1->m[j] == NULL)) j--;
612  j++;
613  if (j==IDELEMS(h1))
614  {
615    pEnlargeSet(&(h1->m),IDELEMS(h1),16);
616    IDELEMS(h1)+=16;
617  }
618  h1->m[j]=h2;
619  return TRUE;
620}
621
622/*2
623* insert h2 into h1 depending on the two boolean parameters:
624* - if zeroOk is true, then h2 will also be inserted when it is zero
625* - if duplicateOk is true, then h2 will also be inserted when it is
626*   already present in h1
627* return TRUE iff h2 was indeed inserted
628*/
629BOOLEAN id_InsertPolyWithTests (ideal h1, const int validEntries,
630  const poly h2, const bool zeroOk, const bool duplicateOk, const ring r)
631{
632  if ((!zeroOk) && (h2 == NULL)) return FALSE;
633  if (!duplicateOk)
634  {
635    bool h2FoundInH1 = false;
636    int i = 0;
637    while ((i < validEntries) && (!h2FoundInH1))
638    {
639      h2FoundInH1 = p_EqualPolys(h1->m[i], h2,r);
640      i++;
641    }
642    if (h2FoundInH1) return FALSE;
643  }
644  if (validEntries == IDELEMS(h1))
645  {
646    pEnlargeSet(&(h1->m), IDELEMS(h1), 16);
647    IDELEMS(h1) += 16;
648  }
649  h1->m[validEntries] = h2;
650  return TRUE;
651}
652
653/*2
654* h1 + h2
655*/
656ideal id_Add (ideal h1,ideal h2, const ring r)
657{
658  ideal result = id_SimpleAdd(h1,h2,r);
659  id_Compactify(result,r);
660  return result;
661}
662
663/*2
664* h1 * h2
665*/
666ideal  id_Mult (ideal h1,ideal  h2, const ring r)
667{
668  int i,j,k;
669  ideal  hh;
670
671  j = IDELEMS(h1);
672  while ((j > 0) && (h1->m[j-1] == NULL)) j--;
673  i = IDELEMS(h2);
674  while ((i > 0) && (h2->m[i-1] == NULL)) i--;
675  j = j * i;
676  if (j == 0)
677    hh = idInit(1,1);
678  else
679    hh=idInit(j,1);
680  if (h1->rank<h2->rank)
681    hh->rank = h2->rank;
682  else
683    hh->rank = h1->rank;
684  if (j==0) return hh;
685  k = 0;
686  for (i=0; i<IDELEMS(h1); i++)
687  {
688    if (h1->m[i] != NULL)
689    {
690      for (j=0; j<IDELEMS(h2); j++)
691      {
692        if (h2->m[j] != NULL)
693        {
694          hh->m[k] = pp_Mult_qq(h1->m[i],h2->m[j],r);
695          k++;
696        }
697      }
698    }
699  }
700  {
701    id_Compactify(hh,r);
702    return hh;
703  }
704}
705
706/*2
707*returns true if h is the zero ideal
708*/
709BOOLEAN idIs0 (ideal h)
710{
711  int i;
712
713  if (h == NULL) return TRUE;
714  i = IDELEMS(h)-1;
715  while ((i >= 0) && (h->m[i] == NULL))
716  {
717    i--;
718  }
719  if (i < 0)
720    return TRUE;
721  else
722    return FALSE;
723}
724
725/*2
726* return the maximal component number found in any polynomial in s
727*/
728long id_RankFreeModule (ideal s, ring lmRing, ring tailRing)
729{
730  if (s!=NULL)
731  {
732    int  j=0;
733
734    if (rRing_has_Comp(tailRing) && rRing_has_Comp(lmRing))
735    {
736      int  l=IDELEMS(s);
737      poly *p=s->m;
738      int  k;
739      for (; l != 0; l--)
740      {
741        if (*p!=NULL)
742        {
743          pp_Test(*p, lmRing, tailRing);
744          k = p_MaxComp(*p, lmRing, tailRing);
745          if (k>j) j = k;
746        }
747        p++;
748      }
749    }
750    return j;
751  }
752  return -1;
753}
754
755BOOLEAN idIsModule(ideal id, ring r)
756{
757  if (id != NULL && rRing_has_Comp(r))
758  {
759    int j, l = IDELEMS(id);
760    for (j=0; j<l; j++)
761    {
762      if (id->m[j] != NULL && p_GetComp(id->m[j], r) > 0) return TRUE;
763    }
764  }
765  return FALSE;
766}
767
768
769/*2
770*returns true if id is homogenous with respect to the aktual weights
771*/
772BOOLEAN id_HomIdeal (ideal id, ideal Q, const ring r)
773{
774  int i;
775  BOOLEAN     b;
776  if ((id == NULL) || (IDELEMS(id) == 0)) return TRUE;
777  i = 0;
778  b = TRUE;
779  while ((i < IDELEMS(id)) && b)
780  {
781    b = p_IsHomogeneous(id->m[i],r);
782    i++;
783  }
784  if ((b) && (Q!=NULL) && (IDELEMS(Q)>0))
785  {
786    i=0;
787    while ((i < IDELEMS(Q)) && b)
788    {
789      b = p_IsHomogeneous(Q->m[i],r);
790      i++;
791    }
792  }
793  return b;
794}
795
796/*2
797*initialized a field with r numbers between beg and end for the
798*procedure idNextChoise
799*/
800void idInitChoise (int r,int beg,int end,BOOLEAN  *endch,int * choise)
801{
802  /*returns the first choise of r numbers between beg and end*/
803  int i;
804  for (i=0; i<r; i++)
805  {
806    choise[i] = 0;
807  }
808  if (r <= end-beg+1)
809    for (i=0; i<r; i++)
810    {
811      choise[i] = beg+i;
812    }
813  if (r > end-beg+1)
814    *endch = TRUE;
815  else
816    *endch = FALSE;
817}
818
819/*2
820*returns the next choise of r numbers between beg and end
821*/
822void idGetNextChoise (int r,int end,BOOLEAN  *endch,int * choise)
823{
824  int i = r-1,j;
825  while ((i >= 0) && (choise[i] == end))
826  {
827    i--;
828    end--;
829  }
830  if (i == -1)
831    *endch = TRUE;
832  else
833  {
834    choise[i]++;
835    for (j=i+1; j<r; j++)
836    {
837      choise[j] = choise[i]+j-i;
838    }
839    *endch = FALSE;
840  }
841}
842
843/*2
844*takes the field choise of d numbers between beg and end, cancels the t-th
845*entree and searches for the ordinal number of that d-1 dimensional field
846* w.r.t. the algorithm of construction
847*/
848int idGetNumberOfChoise(int t, int d, int begin, int end, int * choise)
849{
850  int * localchoise,i,result=0;
851  BOOLEAN b=FALSE;
852
853  if (d<=1) return 1;
854  localchoise=(int*)omAlloc((d-1)*sizeof(int));
855  idInitChoise(d-1,begin,end,&b,localchoise);
856  while (!b)
857  {
858    result++;
859    i = 0;
860    while ((i<t) && (localchoise[i]==choise[i])) i++;
861    if (i>=t)
862    {
863      i = t+1;
864      while ((i<d) && (localchoise[i-1]==choise[i])) i++;
865      if (i>=d)
866      {
867        omFreeSize((ADDRESS)localchoise,(d-1)*sizeof(int));
868        return result;
869      }
870    }
871    idGetNextChoise(d-1,end,&b,localchoise);
872  }
873  omFreeSize((ADDRESS)localchoise,(d-1)*sizeof(int));
874  return 0;
875}
876
877/*2
878*computes the binomial coefficient
879*/
880int binom (int n,int r)
881{
882  int i,result;
883
884  if (r==0) return 1;
885  if (n-r<r) return binom(n,n-r);
886  result = n-r+1;
887  for (i=2;i<=r;i++)
888  {
889    result *= n-r+i;
890    if (result<0)
891    {
892      WarnS("overflow in binomials");
893      return 0;
894    }
895    result /= i;
896  }
897  return result;
898}
899
900/*2
901*the free module of rank i
902*/
903ideal id_FreeModule (int i, const ring r)
904{
905  int j;
906  ideal h;
907
908  h=idInit(i,i);
909  for (j=0; j<i; j++)
910  {
911    h->m[j] = p_One(r);
912    p_SetComp(h->m[j],j+1,r);
913    p_SetmComp(h->m[j],r);
914  }
915  return h;
916}
917
918/*2
919*computes recursively all monomials of a certain degree
920*in every step the actvar-th entry in the exponential
921*vector is incremented and the other variables are
922*computed by recursive calls of makemonoms
923*if the last variable is reached, the difference to the
924*degree is computed directly
925*vars is the number variables
926*actvar is the actual variable to handle
927*deg is the degree of the monomials to compute
928*monomdeg is the actual degree of the monomial in consideration
929*/
930static void makemonoms(int vars,int actvar,int deg,int monomdeg, const ring r)
931{
932  poly p;
933  int i=0;
934
935  if ((idpowerpoint == 0) && (actvar ==1))
936  {
937    idpower[idpowerpoint] = p_One(r);
938    monomdeg = 0;
939  }
940  while (i<=deg)
941  {
942    if (deg == monomdeg)
943    {
944      p_Setm(idpower[idpowerpoint],r);
945      idpowerpoint++;
946      return;
947    }
948    if (actvar == vars)
949    {
950      p_SetExp(idpower[idpowerpoint],actvar,deg-monomdeg,r);
951      p_Setm(idpower[idpowerpoint],r);
952      p_Test(idpower[idpowerpoint],r);
953      idpowerpoint++;
954      return;
955    }
956    else
957    {
958      p = p_Copy(idpower[idpowerpoint],r);
959      makemonoms(vars,actvar+1,deg,monomdeg,r);
960      idpower[idpowerpoint] = p;
961    }
962    monomdeg++;
963    p_SetExp(idpower[idpowerpoint],actvar,p_GetExp(idpower[idpowerpoint],actvar,r)+1,r);
964    p_Setm(idpower[idpowerpoint],r);
965    p_Test(idpower[idpowerpoint],r);
966    i++;
967  }
968}
969
970/*2
971*returns the deg-th power of the maximal ideal of 0
972*/
973ideal id_MaxIdeal(int deg, const ring r)
974{
975  if (deg < 0)
976  {
977    WarnS("maxideal: power must be non-negative");
978  }
979  if (deg < 1)
980  {
981    ideal I=idInit(1,1);
982    I->m[0]=p_One(r);
983    return I;
984  }
985  if (deg == 1)
986  {
987    return id_MaxIdeal(r);
988  }
989
990  int vars = rVar(r);
991  int i = binom(vars+deg-1,deg);
992  if (i<=0) return idInit(1,1);
993  ideal id=idInit(i,1);
994  idpower = id->m;
995  idpowerpoint = 0;
996  makemonoms(vars,1,deg,0,r);
997  idpower = NULL;
998  idpowerpoint = 0;
999  return id;
1000}
1001
1002/*2
1003*computes recursively all generators of a certain degree
1004*of the ideal "givenideal"
1005*elms is the number elements in the given ideal
1006*actelm is the actual element to handle
1007*deg is the degree of the power to compute
1008*gendeg is the actual degree of the generator in consideration
1009*/
1010static void makepotence(int elms,int actelm,int deg,int gendeg, const ring r)
1011{
1012  poly p;
1013  int i=0;
1014
1015  if ((idpowerpoint == 0) && (actelm ==1))
1016  {
1017    idpower[idpowerpoint] = p_One(r);
1018    gendeg = 0;
1019  }
1020  while (i<=deg)
1021  {
1022    if (deg == gendeg)
1023    {
1024      idpowerpoint++;
1025      return;
1026    }
1027    if (actelm == elms)
1028    {
1029      p=p_Power(p_Copy(givenideal[actelm-1],r),deg-gendeg,r);
1030      idpower[idpowerpoint]=p_Mult_q(idpower[idpowerpoint],p,r);
1031      idpowerpoint++;
1032      return;
1033    }
1034    else
1035    {
1036      p = p_Copy(idpower[idpowerpoint],r);
1037      makepotence(elms,actelm+1,deg,gendeg,r);
1038      idpower[idpowerpoint] = p;
1039    }
1040    gendeg++;
1041    idpower[idpowerpoint]=p_Mult_q(idpower[idpowerpoint],p_Copy(givenideal[actelm-1],r),r);
1042    i++;
1043  }
1044}
1045
1046/*2
1047*returns the deg-th power of the ideal gid
1048*/
1049//ideal idPower(ideal gid,int deg)
1050//{
1051//  int i;
1052//  ideal id;
1053//
1054//  if (deg < 1) deg = 1;
1055//  i = binom(IDELEMS(gid)+deg-1,deg);
1056//  id=idInit(i,1);
1057//  idpower = id->m;
1058//  givenideal = gid->m;
1059//  idpowerpoint = 0;
1060//  makepotence(IDELEMS(gid),1,deg,0);
1061//  idpower = NULL;
1062//  givenideal = NULL;
1063//  idpowerpoint = 0;
1064//  return id;
1065//}
1066static void id_NextPotence(ideal given, ideal result,
1067  int begin, int end, int deg, int restdeg, poly ap, const ring r)
1068{
1069  poly p;
1070  int i;
1071
1072  p = p_Power(p_Copy(given->m[begin],r),restdeg,r);
1073  i = result->nrows;
1074  result->m[i] = p_Mult_q(p_Copy(ap,r),p,r);
1075//PrintS(".");
1076  (result->nrows)++;
1077  if (result->nrows >= IDELEMS(result))
1078  {
1079    pEnlargeSet(&(result->m),IDELEMS(result),16);
1080    IDELEMS(result) += 16;
1081  }
1082  if (begin == end) return;
1083  for (i=restdeg-1;i>0;i--)
1084  {
1085    p = p_Power(p_Copy(given->m[begin],r),i,r);
1086    p = p_Mult_q(p_Copy(ap,r),p,r);
1087    id_NextPotence(given, result, begin+1, end, deg, restdeg-i, p,r);
1088    p_Delete(&p,r);
1089  }
1090  id_NextPotence(given, result, begin+1, end, deg, restdeg, ap,r);
1091}
1092
1093ideal id_Power(ideal given,int exp, const ring r)
1094{
1095  ideal result,temp;
1096  poly p1;
1097  int i;
1098
1099  if (idIs0(given)) return idInit(1,1);
1100  temp = id_Copy(given,r);
1101  idSkipZeroes(temp);
1102  i = binom(IDELEMS(temp)+exp-1,exp);
1103  result = idInit(i,1);
1104  result->nrows = 0;
1105//Print("ideal contains %d elements\n",i);
1106  p1=p_One(r);
1107  id_NextPotence(temp,result,0,IDELEMS(temp)-1,exp,exp,p1,r);
1108  p_Delete(&p1,r);
1109  id_Delete(&temp,r);
1110  result->nrows = 1;
1111  id_DelEquals(result,r);
1112  idSkipZeroes(result);
1113  return result;
1114}
1115
1116/*2
1117*skips all zeroes and double elements, searches also for units
1118*/
1119void id_Compactify(ideal id, const ring r)
1120{
1121  int i,j;
1122  BOOLEAN b=FALSE;
1123
1124  i = IDELEMS(id)-1;
1125  while ((! b) && (i>=0))
1126  {
1127    b=p_IsUnit(id->m[i],r);
1128    i--;
1129  }
1130  if (b)
1131  {
1132    for(i=IDELEMS(id)-1;i>=0;i--) p_Delete(&id->m[i],r);
1133    id->m[0]=p_One(r);
1134  }
1135  else
1136  {
1137    id_DelMultiples(id,r);
1138  }
1139  idSkipZeroes(id);
1140}
1141
1142/*2
1143* returns the ideals of initial terms
1144*/
1145ideal id_Head(ideal h,const ring r)
1146{
1147  ideal m = idInit(IDELEMS(h),h->rank);
1148  int i;
1149
1150  for (i=IDELEMS(h)-1;i>=0; i--)
1151  {
1152    if (h->m[i]!=NULL) m->m[i]=p_Head(h->m[i],r);
1153  }
1154  return m;
1155}
1156
1157ideal id_Homogen(ideal h, int varnum,const ring r)
1158{
1159  ideal m = idInit(IDELEMS(h),h->rank);
1160  int i;
1161
1162  for (i=IDELEMS(h)-1;i>=0; i--)
1163  {
1164    m->m[i]=p_Homogen(h->m[i],varnum,r);
1165  }
1166  return m;
1167}
1168
1169/*------------------type conversions----------------*/
1170ideal id_Vec2Ideal(poly vec, const ring R)
1171{
1172   ideal result=idInit(1,1);
1173   omFree((ADDRESS)result->m);
1174   result->m=NULL; // remove later
1175   p_Vec2Polys(vec, &(result->m), &(IDELEMS(result)),R);
1176   return result;
1177}
1178
1179
1180// converts mat to module, destroys mat
1181ideal id_Matrix2Module(matrix mat, const ring R)
1182{
1183  int mc=MATCOLS(mat);
1184  int mr=MATROWS(mat);
1185  ideal result = idInit(si_max(mc,1),si_max(mr,1));
1186  int i,j, l;
1187  poly h;
1188  poly p;
1189  sBucket_pt bucket = sBucketCreate(R);
1190
1191  for(j=0;j<mc /*MATCOLS(mat)*/;j++) /* j is also index in result->m */
1192  {
1193    for (i=1;i<=mr /*MATROWS(mat)*/;i++)
1194    {
1195      h = MATELEM(mat,i,j+1);
1196      if (h!=NULL)
1197      {
1198        l=pLength(h);
1199        MATELEM(mat,i,j+1)=NULL;
1200        p_SetCompP(h,i, R);
1201        sBucket_Merge_p(bucket, h, l);
1202      }
1203    }
1204    sBucketClearMerge(bucket, &(result->m[j]), &l);
1205  }
1206  sBucketDestroy(&bucket);
1207
1208  // obachman: need to clean this up
1209  id_Delete((ideal*) &mat,R);
1210  return result;
1211}
1212
1213/*2
1214* converts a module into a matrix, destroyes the input
1215*/
1216matrix id_Module2Matrix(ideal mod, const ring R)
1217{
1218  matrix result = mpNew(mod->rank,IDELEMS(mod));
1219  int i,cp;
1220  poly p,h;
1221
1222  for(i=0;i<IDELEMS(mod);i++)
1223  {
1224    p=pReverse(mod->m[i]);
1225    mod->m[i]=NULL;
1226    while (p!=NULL)
1227    {
1228      h=p;
1229      pIter(p);
1230      pNext(h)=NULL;
1231//      cp = si_max(1,pGetComp(h));     // if used for ideals too
1232      cp = p_GetComp(h,R);
1233      p_SetComp(h,0,R);
1234      p_SetmComp(h,R);
1235#ifdef TEST
1236      if (cp>mod->rank)
1237      {
1238        Print("## inv. rank %ld -> %d\n",mod->rank,cp);
1239        int k,l,o=mod->rank;
1240        mod->rank=cp;
1241        matrix d=mpNew(mod->rank,IDELEMS(mod));
1242        for (l=1; l<=o; l++)
1243        {
1244          for (k=1; k<=IDELEMS(mod); k++)
1245          {
1246            MATELEM(d,l,k)=MATELEM(result,l,k);
1247            MATELEM(result,l,k)=NULL;
1248          }
1249        }
1250        id_Delete((ideal *)&result,R);
1251        result=d;
1252      }
1253#endif
1254      MATELEM(result,cp,i+1) = p_Add_q(MATELEM(result,cp,i+1),h,R);
1255    }
1256  }
1257  // obachman 10/99: added the following line, otherwise memory leack!
1258  id_Delete(&mod,R);
1259  return result;
1260}
1261
1262matrix id_Module2formatedMatrix(ideal mod,int rows, int cols, const ring R)
1263{
1264  matrix result = mpNew(rows,cols);
1265  int i,cp,r=id_RankFreeModule(mod,R),c=IDELEMS(mod);
1266  poly p,h;
1267
1268  if (r>rows) r = rows;
1269  if (c>cols) c = cols;
1270  for(i=0;i<c;i++)
1271  {
1272    p=pReverse(mod->m[i]);
1273    mod->m[i]=NULL;
1274    while (p!=NULL)
1275    {
1276      h=p;
1277      pIter(p);
1278      pNext(h)=NULL;
1279      cp = p_GetComp(h,R);
1280      if (cp<=r)
1281      {
1282        p_SetComp(h,0,R);
1283        p_SetmComp(h,R);
1284        MATELEM(result,cp,i+1) = p_Add_q(MATELEM(result,cp,i+1),h,R);
1285      }
1286      else
1287        p_Delete(&h,R);
1288    }
1289  }
1290  id_Delete(&mod,R);
1291  return result;
1292}
1293
1294/*2
1295* substitute the n-th variable by the monomial e in id
1296* destroy id
1297*/
1298ideal  id_Subst(ideal id, int n, poly e, const ring r)
1299{
1300  int k=MATROWS((matrix)id)*MATCOLS((matrix)id);
1301  ideal res=(ideal)mpNew(MATROWS((matrix)id),MATCOLS((matrix)id));
1302
1303  res->rank = id->rank;
1304  for(k--;k>=0;k--)
1305  {
1306    res->m[k]=p_Subst(id->m[k],n,e,r);
1307    id->m[k]=NULL;
1308  }
1309  id_Delete(&id,r);
1310  return res;
1311}
1312
1313BOOLEAN id_HomModule(ideal m, ideal Q, intvec **w, const ring R)
1314{
1315  if (w!=NULL) *w=NULL;
1316  if ((Q!=NULL) && (!id_HomIdeal(Q,NULL,R))) return FALSE;
1317  if (idIs0(m))
1318  {
1319    if (w!=NULL) (*w)=new intvec(m->rank);
1320    return TRUE;
1321  }
1322
1323  long cmax=1,order=0,ord,* diff,diffmin=32000;
1324  int *iscom;
1325  int i,j;
1326  poly p=NULL;
1327  pFDegProc d;
1328  if (R->pLexOrder && (R->order[0]==ringorder_lp))
1329     d=p_Totaldegree;
1330  else
1331     d=R->pFDeg;
1332  int length=IDELEMS(m);
1333  poly* P=m->m;
1334  poly* F=(poly*)omAlloc(length*sizeof(poly));
1335  for (i=length-1;i>=0;i--)
1336  {
1337    p=F[i]=P[i];
1338    cmax=si_max(cmax,(long)p_MaxComp(p,R));
1339  }
1340  cmax++;
1341  diff = (long *)omAlloc0(cmax*sizeof(long));
1342  if (w!=NULL) *w=new intvec(cmax-1);
1343  iscom = (int *)omAlloc0(cmax*sizeof(int));
1344  i=0;
1345  while (i<=length)
1346  {
1347    if (i<length)
1348    {
1349      p=F[i];
1350      while ((p!=NULL) && (iscom[p_GetComp(p,R)]==0)) pIter(p);
1351    }
1352    if ((p==NULL) && (i<length))
1353    {
1354      i++;
1355    }
1356    else
1357    {
1358      if (p==NULL) /* && (i==length) */
1359      {
1360        i=0;
1361        while ((i<length) && (F[i]==NULL)) i++;
1362        if (i>=length) break;
1363        p = F[i];
1364      }
1365      //if (pLexOrder && (currRing->order[0]==ringorder_lp))
1366      //  order=pTotaldegree(p);
1367      //else
1368      //  order = p->order;
1369      //  order = pFDeg(p,currRing);
1370      order = d(p,R) +diff[p_GetComp(p,R)];
1371      //order += diff[pGetComp(p)];
1372      p = F[i];
1373//Print("Actual p=F[%d]: ",i);pWrite(p);
1374      F[i] = NULL;
1375      i=0;
1376    }
1377    while (p!=NULL)
1378    {
1379      if (R->pLexOrder && (R->order[0]==ringorder_lp))
1380        ord=p_Totaldegree(p,R);
1381      else
1382      //  ord = p->order;
1383        ord = R->pFDeg(p,R);
1384      if (iscom[p_GetComp(p,R)]==0)
1385      {
1386        diff[p_GetComp(p,R)] = order-ord;
1387        iscom[p_GetComp(p,R)] = 1;
1388/*
1389*PrintS("new diff: ");
1390*for (j=0;j<cmax;j++) Print("%d ",diff[j]);
1391*PrintLn();
1392*PrintS("new iscom: ");
1393*for (j=0;j<cmax;j++) Print("%d ",iscom[j]);
1394*PrintLn();
1395*Print("new set %d, order %d, ord %d, diff %d\n",pGetComp(p),order,ord,diff[pGetComp(p)]);
1396*/
1397      }
1398      else
1399      {
1400/*
1401*PrintS("new diff: ");
1402*for (j=0;j<cmax;j++) Print("%d ",diff[j]);
1403*PrintLn();
1404*Print("order %d, ord %d, diff %d\n",order,ord,diff[pGetComp(p)]);
1405*/
1406        if (order != (ord+diff[p_GetComp(p,R)]))
1407        {
1408          omFreeSize((ADDRESS) iscom,cmax*sizeof(int));
1409          omFreeSize((ADDRESS) diff,cmax*sizeof(long));
1410          omFreeSize((ADDRESS) F,length*sizeof(poly));
1411          delete *w;*w=NULL;
1412          return FALSE;
1413        }
1414      }
1415      pIter(p);
1416    }
1417  }
1418  omFreeSize((ADDRESS) iscom,cmax*sizeof(int));
1419  omFreeSize((ADDRESS) F,length*sizeof(poly));
1420  for (i=1;i<cmax;i++) (**w)[i-1]=(int)(diff[i]);
1421  for (i=1;i<cmax;i++)
1422  {
1423    if (diff[i]<diffmin) diffmin=diff[i];
1424  }
1425  if (w!=NULL)
1426  {
1427    for (i=1;i<cmax;i++)
1428    {
1429      (**w)[i-1]=(int)(diff[i]-diffmin);
1430    }
1431  }
1432  omFreeSize((ADDRESS) diff,cmax*sizeof(long));
1433  return TRUE;
1434}
1435
1436// uses glabl vars via pSetModDeg
1437//BOOLEAN idTestHomModule(ideal m, ideal Q, intvec *w)
1438//{
1439//  if ((Q!=NULL) && (!idHomIdeal(Q,NULL)))  { PrintS(" Q not hom\n"); return FALSE;}
1440//  if (idIs0(m)) return TRUE;
1441//
1442//  int cmax=-1;
1443//  int i;
1444//  poly p=NULL;
1445//  int length=IDELEMS(m);
1446//  poly* P=m->m;
1447//  for (i=length-1;i>=0;i--)
1448//  {
1449//    p=P[i];
1450//    if (p!=NULL) cmax=si_max(cmax,(int)pMaxComp(p)+1);
1451//  }
1452//  if (w != NULL)
1453//  if (w->length()+1 < cmax)
1454//  {
1455//    // Print("length: %d - %d \n", w->length(),cmax);
1456//    return FALSE;
1457//  }
1458//
1459//  if(w!=NULL)
1460//    pSetModDeg(w);
1461//
1462//  for (i=length-1;i>=0;i--)
1463//  {
1464//    p=P[i];
1465//    poly q=p;
1466//    if (p!=NULL)
1467//    {
1468//      int d=pFDeg(p,currRing);
1469//      loop
1470//      {
1471//        pIter(p);
1472//        if (p==NULL) break;
1473//        if (d!=pFDeg(p,currRing))
1474//        {
1475//          //pWrite(q); wrp(p); Print(" -> %d - %d\n",d,pFDeg(p,currRing));
1476//          if(w!=NULL)
1477//            pSetModDeg(NULL);
1478//          return FALSE;
1479//        }
1480//      }
1481//    }
1482//  }
1483//
1484//  if(w!=NULL)
1485//    pSetModDeg(NULL);
1486//
1487//  return TRUE;
1488//}
1489
1490ideal id_Jet(ideal i,int d, const ring R)
1491{
1492  ideal r=idInit((i->nrows)*(i->ncols),i->rank);
1493  r->nrows = i-> nrows;
1494  r->ncols = i-> ncols;
1495  //r->rank = i-> rank;
1496  int k;
1497  for(k=(i->nrows)*(i->ncols)-1;k>=0; k--)
1498  {
1499    r->m[k]=pp_Jet(i->m[k],d,R);
1500  }
1501  return r;
1502}
1503
1504ideal id_JetW(ideal i,int d, intvec * iv, const ring R)
1505{
1506  ideal r=idInit(IDELEMS(i),i->rank);
1507  if (ecartWeights!=NULL)
1508  {
1509    WerrorS("cannot compute weighted jets now");
1510  }
1511  else
1512  {
1513    short *w=iv2array(iv,R);
1514    int k;
1515    for(k=0; k<IDELEMS(i); k++)
1516    {
1517      r->m[k]=pp_JetW(i->m[k],d,w,R);
1518    }
1519    omFreeSize((ADDRESS)w,(rVar(R)+1)*sizeof(short));
1520  }
1521  return r;
1522}
1523
1524/*3
1525* searches for the next unit in the components of the module arg and
1526* returns the first one;
1527*/
1528static int id_ReadOutPivot(ideal arg,int* comp, const ring r)
1529{
1530  if (idIs0(arg)) return -1;
1531  int i=0,j, generator=-1;
1532  int rk_arg=arg->rank; //idRankFreeModule(arg);
1533  int * componentIsUsed =(int *)omAlloc((rk_arg+1)*sizeof(int));
1534  poly p;
1535
1536  while ((generator<0) && (i<IDELEMS(arg)))
1537  {
1538    memset(componentIsUsed,0,(rk_arg+1)*sizeof(int));
1539    p = arg->m[i];
1540    while (p!=NULL)
1541    {
1542      j = p_GetComp(p,r);
1543      if (componentIsUsed[j]==0)
1544      {
1545#ifdef HAVE_RINGS
1546        if (p_LmIsConstantComp(p,r) &&
1547            (!rField_is_Ring(r) || n_IsUnit(pGetCoeff(p),r->cf)))
1548        {
1549#else
1550        if (p_LmIsConstantComp(p,r))
1551        {
1552#endif
1553          generator = i;
1554          componentIsUsed[j] = 1;
1555        }
1556        else
1557        {
1558          componentIsUsed[j] = -1;
1559        }
1560      }
1561      else if (componentIsUsed[j]>0)
1562      {
1563        (componentIsUsed[j])++;
1564      }
1565      pIter(p);
1566    }
1567    i++;
1568  }
1569  i = 0;
1570  *comp = -1;
1571  for (j=0;j<=rk_arg;j++)
1572  {
1573    if (componentIsUsed[j]>0)
1574    {
1575      if ((*comp==-1) || (componentIsUsed[j]<i))
1576      {
1577        *comp = j;
1578        i= componentIsUsed[j];
1579      }
1580    }
1581  }
1582  omFree(componentIsUsed);
1583  return generator;
1584}
1585
1586#if 0
1587static void idDeleteComp(ideal arg,int red_comp)
1588{
1589  int i,j;
1590  poly p;
1591
1592  for (i=IDELEMS(arg)-1;i>=0;i--)
1593  {
1594    p = arg->m[i];
1595    while (p!=NULL)
1596    {
1597      j = pGetComp(p);
1598      if (j>red_comp)
1599      {
1600        pSetComp(p,j-1);
1601        pSetm(p);
1602      }
1603      pIter(p);
1604    }
1605  }
1606  (arg->rank)--;
1607}
1608#endif
1609
1610intvec * id_QHomWeight(ideal id, const ring r)
1611{
1612  poly head, tail;
1613  int k;
1614  int in=IDELEMS(id)-1, ready=0, all=0,
1615      coldim=rVar(r), rowmax=2*coldim;
1616  if (in<0) return NULL;
1617  intvec *imat=new intvec(rowmax+1,coldim,0);
1618
1619  do
1620  {
1621    head = id->m[in--];
1622    if (head!=NULL)
1623    {
1624      tail = pNext(head);
1625      while (tail!=NULL)
1626      {
1627        all++;
1628        for (k=1;k<=coldim;k++)
1629          IMATELEM(*imat,all,k) = p_GetExpDiff(head,tail,k,r);
1630        if (all==rowmax)
1631        {
1632          ivTriangIntern(imat, ready, all);
1633          if (ready==coldim)
1634          {
1635            delete imat;
1636            return NULL;
1637          }
1638        }
1639        pIter(tail);
1640      }
1641    }
1642  } while (in>=0);
1643  if (all>ready)
1644  {
1645    ivTriangIntern(imat, ready, all);
1646    if (ready==coldim)
1647    {
1648      delete imat;
1649      return NULL;
1650    }
1651  }
1652  intvec *result = ivSolveKern(imat, ready);
1653  delete imat;
1654  return result;
1655}
1656
1657BOOLEAN id_IsZeroDim(ideal I, const ring r)
1658{
1659  BOOLEAN *UsedAxis=(BOOLEAN *)omAlloc0(rVar(r)*sizeof(BOOLEAN));
1660  int i,n;
1661  poly po;
1662  BOOLEAN res=TRUE;
1663  for(i=IDELEMS(I)-1;i>=0;i--)
1664  {
1665    po=I->m[i];
1666    if ((po!=NULL) &&((n=p_IsPurePower(po,r))!=0)) UsedAxis[n-1]=TRUE;
1667  }
1668  for(i=rVar(r)-1;i>=0;i--)
1669  {
1670    if(UsedAxis[i]==FALSE) {res=FALSE; break;} // not zero-dim.
1671  }
1672  omFreeSize(UsedAxis,rVar(r)*sizeof(BOOLEAN));
1673  return res;
1674}
1675
1676void id_Normalize(ideal I,const ring r)
1677{
1678  if (rField_has_simple_inverse(r)) return; /* Z/p, GF(p,n), R, long R/C */
1679  int i;
1680  for(i=IDELEMS(I)-1;i>=0;i--)
1681  {
1682    p_Normalize(I->m[i],r);
1683  }
1684}
1685
1686// #include <kernel/clapsing.h>
1687
1688#ifdef HAVE_FACTORY
1689poly id_GCD(poly f, poly g, const ring r)
1690{
1691  ring save_r=r;
1692  rChangeCurrRing(r);
1693  ideal I=idInit(2,1); I->m[0]=f; I->m[1]=g;
1694  intvec *w = NULL;
1695  ideal S=idSyzygies(I,testHomog,&w);
1696  if (w!=NULL) delete w;
1697  poly gg=pTakeOutComp(&(S->m[0]),2);
1698  idDelete(&S);
1699  poly gcd_p=singclap_pdivide(f,gg);
1700  pDelete(&gg);
1701  rChangeCurrRing(save_r);
1702  return gcd_p;
1703}
1704#endif
1705
1706/*2
1707* xx,q: arrays of length 0..rl-1
1708* xx[i]: SB mod q[i]
1709* assume: char=0
1710* assume: q[i]!=0
1711* destroys xx
1712*/
1713#ifdef HAVE_FACTORY
1714ideal idChineseRemainder(ideal *xx, number *q, int rl, const ring r)
1715{
1716  int cnt=IDELEMS(xx[0])*xx[0]->nrows;
1717  ideal result=idInit(cnt,xx[0]->rank);
1718  result->nrows=xx[0]->nrows; // for lifting matrices
1719  result->ncols=xx[0]->ncols; // for lifting matrices
1720  int i,j;
1721  poly r,h,hh,res_p;
1722  number *x=(number *)omAlloc(rl*sizeof(number));
1723  for(i=cnt-1;i>=0;i--)
1724  {
1725    res_p=NULL;
1726    loop
1727    {
1728      r=NULL;
1729      for(j=rl-1;j>=0;j--)
1730      {
1731        h=xx[j]->m[i];
1732        if ((h!=NULL)
1733        &&((r==NULL)||(pLmCmp(r,h)==-1)))
1734          r=h;
1735      }
1736      if (r==NULL) break;
1737      h=pHead(r);
1738      for(j=rl-1;j>=0;j--)
1739      {
1740        hh=xx[j]->m[i];
1741        if ((hh!=NULL) && (pLmCmp(r,hh)==0))
1742        {
1743          x[j]=pGetCoeff(hh);
1744          hh=pLmFreeAndNext(hh);
1745          xx[j]->m[i]=hh;
1746        }
1747        else
1748          x[j]=nlInit(0, r);
1749      }
1750      number n=nlChineseRemainder(x,q,rl);
1751      for(j=rl-1;j>=0;j--)
1752      {
1753        x[j]=NULL; // nlInit(0...) takes no memory
1754      }
1755      if (nlIsZero(n)) pDelete(&h);
1756      else
1757      {
1758        pSetCoeff(h,n);
1759        //Print("new mon:");pWrite(h);
1760        res_p=pAdd(res_p,h);
1761      }
1762    }
1763    result->m[i]=res_p;
1764  }
1765  omFree(x);
1766  for(i=rl-1;i>=0;i--) idDelete(&(xx[i]));
1767  omFree(xx);
1768  return result;
1769}
1770#endif
1771
1772/*2
1773* transpose a module
1774*/
1775ideal id_Transp(ideal a, const ring rRing)
1776{
1777  int r = a->rank, c = IDELEMS(a);
1778  ideal b =  idInit(r,c);
1779
1780  for (int i=c; i>0; i--)
1781  {
1782    poly p=a->m[i-1];
1783    while(p!=NULL)
1784    {
1785      poly h=p_Head(p, rRing);
1786      int co=p_GetComp(h, rRing)-1;
1787      p_SetComp(h, i, rRing);
1788      p_Setm(h, rRing);
1789      b->m[co] = p_Add_q(b->m[co], h, rRing);
1790      pIter(p);
1791    }
1792  }
1793  return b;
1794}
1795
1796
1797
1798/*2
1799* The following is needed to compute the image of certain map used in
1800* the computation of cohomologies via BGG
1801* let M = { w_1, ..., w_k }, k = size(M) == ncols(M), n = nvars(currRing).
1802* assuming that nrows(M) <= m*n; the procedure computes:
1803* transpose(M) * transpose( var(1) I_m | ... | var(n) I_m ) :== transpose(module{f_1, ... f_k}),
1804* where f_i = \sum_{j=1}^{m} (w_i, v_j) gen(j),  (w_i, v_j) is a `scalar` multiplication.
1805* that is, if w_i = (a^1_1, ... a^1_m) | (a^2_1, ..., a^2_m) | ... | (a^n_1, ..., a^n_m) then
1806
1807  (a^1_1, ... a^1_m) | (a^2_1, ..., a^2_m) | ... | (a^n_1, ..., a^n_m)
1808*  var_1  ... var_1  |  var_2  ...  var_2  | ... |  var_n  ...  var(n)
1809*  gen_1  ... gen_m  |  gen_1  ...  gen_m  | ... |  gen_1  ...  gen_m
1810+ =>
1811  f_i =
1812
1813   a^1_1 * var(1) * gen(1) + ... + a^1_m * var(1) * gen(m) +
1814   a^2_1 * var(2) * gen(1) + ... + a^2_m * var(2) * gen(m) +
1815                             ...
1816   a^n_1 * var(n) * gen(1) + ... + a^n_m * var(n) * gen(m);
1817
1818   NOTE: for every f_i we run only ONCE along w_i saving partial sums into a temporary array of polys of size m
1819*/
1820ideal id_TensorModuleMult(const int m, const ideal M, const ring rRing)
1821{
1822// #ifdef DEBU
1823//  WarnS("tensorModuleMult!!!!");
1824
1825  assume(m > 0);
1826  assume(M != NULL);
1827
1828  const int n = rRing->N;
1829
1830  assume(M->rank <= m * n);
1831
1832  const int k = IDELEMS(M);
1833
1834  ideal idTemp =  idInit(k,m); // = {f_1, ..., f_k }
1835
1836  for( int i = 0; i < k; i++ ) // for every w \in M
1837  {
1838    poly pTempSum = NULL;
1839
1840    poly w = M->m[i];
1841
1842    while(w != NULL) // for each term of w...
1843    {
1844      poly h = p_Head(w, rRing);
1845
1846      const int  gen = p_GetComp(h, rRing); // 1 ...
1847
1848      assume(gen > 0);
1849      assume(gen <= n*m);
1850
1851      // TODO: write a formula with %, / instead of while!
1852      /*
1853      int c = gen;
1854      int v = 1;
1855      while(c > m)
1856      {
1857        c -= m;
1858        v++;
1859      }
1860      */
1861
1862      int cc = gen % m;
1863      if( cc == 0) cc = m;
1864      int vv = 1 + (gen - cc) / m;
1865
1866//      assume( cc == c );
1867//      assume( vv == v );
1868
1869      //  1<= c <= m
1870      assume( cc > 0 );
1871      assume( cc <= m );
1872
1873      assume( vv > 0 );
1874      assume( vv <= n );
1875
1876      assume( (cc + (vv-1)*m) == gen );
1877
1878      p_IncrExp(h, vv, rRing); // h *= var(j) && //      p_AddExp(h, vv, 1, rRing);
1879      p_SetComp(h, cc, rRing);
1880
1881      p_Setm(h, rRing);         // addjust degree after the previous steps!
1882
1883      pTempSum = p_Add_q(pTempSum, h, rRing); // it is slow since h will be usually put to the back of pTempSum!!!
1884
1885      pIter(w);
1886    }
1887
1888    idTemp->m[i] = pTempSum;
1889  }
1890
1891  // simplify idTemp???
1892
1893  ideal idResult = id_Transp(idTemp, rRing);
1894
1895  id_Delete(&idTemp, rRing);
1896
1897  return(idResult);
1898}
Note: See TracBrowser for help on using the repository browser.