source: git/libpolys/polys/simpleideals.cc @ 805d0b1

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