source: git/libpolys/polys/simpleideals.cc @ 9d68fd

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