source: git/libpolys/polys/simpleideals.cc @ ad3aa7

spielwiese
Last change on this file since ad3aa7 was ad3aa7, checked in by Hans Schoenemann <hannes@…>, 13 years ago
id_MinDegW -> simpleideals.h, simpleideals.cc
  • Property mode set to 100644
File size: 38.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
437* for idSort: compare a and b revlex inclusive module comp.
438*/
439static int p_Comp_RevLex(poly a, poly b,BOOLEAN nolex, const ring R)
440{
441  if (b==NULL) return 1;
442  if (a==NULL) return -1;
443
444  if (nolex)
445  {
446    int r=p_LmCmp(a,b,R);
447    if (r!=0) return r;
448    number h=n_Sub(pGetCoeff(a),pGetCoeff(b),R->cf);
449    r = -1+n_IsZero(h,R->cf)+2*n_GreaterZero(h,R->cf); /* -1: <, 0:==, 1: > */
450    n_Delete(&h, R->cf);
451    return r;
452  }
453  int l=rVar(R);
454  while ((l>0) && (p_GetExp(a,l,R)==p_GetExp(b,l,R))) l--;
455  if (l==0)
456  {
457    if (p_GetComp(a,R)==p_GetComp(b,R))
458    {
459      number h=n_Sub(pGetCoeff(a),pGetCoeff(b),R->cf);
460      int r = -1+n_IsZero(h,R->cf)+2*n_GreaterZero(h,R->cf); /* -1: <, 0:==, 1: > */
461      n_Delete(&h,R->cf);
462      return r;
463    }
464    if (p_GetComp(a,R)>p_GetComp(b,R)) return 1;
465  }
466  else if (p_GetExp(a,l,R)>p_GetExp(b,l,R))
467    return 1;
468  return -1;
469}
470
471/*2
472*sorts the ideal w.r.t. the actual ringordering
473*uses lex-ordering when nolex = FALSE
474*/
475intvec *id_Sort(ideal id,BOOLEAN nolex, const ring r)
476{
477  intvec * result = new intvec(IDELEMS(id));
478  int i, j, actpos=0, newpos;
479  int diff, olddiff, lastcomp, newcomp;
480  BOOLEAN notFound;
481
482  for (i=0;i<IDELEMS(id);i++)
483  {
484    if (id->m[i]!=NULL)
485    {
486      notFound = TRUE;
487      newpos = actpos / 2;
488      diff = (actpos+1) / 2;
489      diff = (diff+1) / 2;
490      lastcomp = p_Comp_RevLex(id->m[i],id->m[(*result)[newpos]],nolex,r);
491      if (lastcomp<0)
492      {
493        newpos -= diff;
494      }
495      else if (lastcomp>0)
496      {
497        newpos += diff;
498      }
499      else
500      {
501        notFound = FALSE;
502      }
503      //while ((newpos>=0) && (newpos<actpos) && (notFound))
504      while (notFound && (newpos>=0) && (newpos<actpos))
505      {
506        newcomp = p_Comp_RevLex(id->m[i],id->m[(*result)[newpos]],nolex,r);
507        olddiff = diff;
508        if (diff>1)
509        {
510          diff = (diff+1) / 2;
511          if ((newcomp==1)
512          && (actpos-newpos>1)
513          && (diff>1)
514          && (newpos+diff>=actpos))
515          {
516            diff = actpos-newpos-1;
517          }
518          else if ((newcomp==-1)
519          && (diff>1)
520          && (newpos<diff))
521          {
522            diff = newpos;
523          }
524        }
525        if (newcomp<0)
526        {
527          if ((olddiff==1) && (lastcomp>0))
528            notFound = FALSE;
529          else
530            newpos -= diff;
531        }
532        else if (newcomp>0)
533        {
534          if ((olddiff==1) && (lastcomp<0))
535          {
536            notFound = FALSE;
537            newpos++;
538          }
539          else
540          {
541            newpos += diff;
542          }
543        }
544        else
545        {
546          notFound = FALSE;
547        }
548        lastcomp = newcomp;
549        if (diff==0) notFound=FALSE; /*hs*/
550      }
551      if (newpos<0) newpos = 0;
552      if (newpos>actpos) newpos = actpos;
553      while ((newpos<actpos) && (p_Comp_RevLex(id->m[i],id->m[(*result)[newpos]],nolex,r)==0))
554        newpos++;
555      for (j=actpos;j>newpos;j--)
556      {
557        (*result)[j] = (*result)[j-1];
558      }
559      (*result)[newpos] = i;
560      actpos++;
561    }
562  }
563  for (j=0;j<actpos;j++) (*result)[j]++;
564  return result;
565}
566
567/*2
568* concat the lists h1 and h2 without zeros
569*/
570ideal id_SimpleAdd (ideal h1,ideal h2, const ring R)
571{
572  int i,j,r,l;
573  ideal result;
574
575  if (h1==NULL) return id_Copy(h2,R);
576  if (h2==NULL) return id_Copy(h1,R);
577  j = IDELEMS(h1)-1;
578  while ((j >= 0) && (h1->m[j] == NULL)) j--;
579  i = IDELEMS(h2)-1;
580  while ((i >= 0) && (h2->m[i] == NULL)) i--;
581  r = si_max(h1->rank,h2->rank);
582  if (i+j==(-2))
583    return idInit(1,r);
584  else
585    result=idInit(i+j+2,r);
586  for (l=j; l>=0; l--)
587  {
588    result->m[l] = p_Copy(h1->m[l],R);
589  }
590  r = i+j+1;
591  for (l=i; l>=0; l--, r--)
592  {
593    result->m[r] = p_Copy(h2->m[l],R);
594  }
595  return result;
596}
597
598/*2
599* insert h2 into h1 (if h2 is not the zero polynomial)
600* return TRUE iff h2 was indeed inserted
601*/
602BOOLEAN idInsertPoly (ideal h1, poly h2)
603{
604  if (h2==NULL) return FALSE;
605  int j = IDELEMS(h1)-1;
606  while ((j >= 0) && (h1->m[j] == NULL)) j--;
607  j++;
608  if (j==IDELEMS(h1))
609  {
610    pEnlargeSet(&(h1->m),IDELEMS(h1),16);
611    IDELEMS(h1)+=16;
612  }
613  h1->m[j]=h2;
614  return TRUE;
615}
616
617/*2
618* insert h2 into h1 depending on the two boolean parameters:
619* - if zeroOk is true, then h2 will also be inserted when it is zero
620* - if duplicateOk is true, then h2 will also be inserted when it is
621*   already present in h1
622* return TRUE iff h2 was indeed inserted
623*/
624BOOLEAN id_InsertPolyWithTests (ideal h1, const int validEntries,
625  const poly h2, const bool zeroOk, const bool duplicateOk, const ring r)
626{
627  if ((!zeroOk) && (h2 == NULL)) return FALSE;
628  if (!duplicateOk)
629  {
630    bool h2FoundInH1 = false;
631    int i = 0;
632    while ((i < validEntries) && (!h2FoundInH1))
633    {
634      h2FoundInH1 = p_EqualPolys(h1->m[i], h2,r);
635      i++;
636    }
637    if (h2FoundInH1) return FALSE;
638  }
639  if (validEntries == IDELEMS(h1))
640  {
641    pEnlargeSet(&(h1->m), IDELEMS(h1), 16);
642    IDELEMS(h1) += 16;
643  }
644  h1->m[validEntries] = h2;
645  return TRUE;
646}
647
648/*2
649* h1 + h2
650*/
651ideal id_Add (ideal h1,ideal h2, const ring r)
652{
653  ideal result = id_SimpleAdd(h1,h2,r);
654  id_Compactify(result,r);
655  return result;
656}
657
658/*2
659* h1 * h2
660*/
661ideal  id_Mult (ideal h1,ideal  h2, const ring r)
662{
663  int i,j,k;
664  ideal  hh;
665
666  j = IDELEMS(h1);
667  while ((j > 0) && (h1->m[j-1] == NULL)) j--;
668  i = IDELEMS(h2);
669  while ((i > 0) && (h2->m[i-1] == NULL)) i--;
670  j = j * i;
671  if (j == 0)
672    hh = idInit(1,1);
673  else
674    hh=idInit(j,1);
675  if (h1->rank<h2->rank)
676    hh->rank = h2->rank;
677  else
678    hh->rank = h1->rank;
679  if (j==0) return hh;
680  k = 0;
681  for (i=0; i<IDELEMS(h1); i++)
682  {
683    if (h1->m[i] != NULL)
684    {
685      for (j=0; j<IDELEMS(h2); j++)
686      {
687        if (h2->m[j] != NULL)
688        {
689          hh->m[k] = pp_Mult_qq(h1->m[i],h2->m[j],r);
690          k++;
691        }
692      }
693    }
694  }
695  {
696    id_Compactify(hh,r);
697    return hh;
698  }
699}
700
701/*2
702*returns true if h is the zero ideal
703*/
704BOOLEAN idIs0 (ideal h)
705{
706  int i;
707
708  if (h == NULL) return TRUE;
709  i = IDELEMS(h)-1;
710  while ((i >= 0) && (h->m[i] == NULL))
711  {
712    i--;
713  }
714  if (i < 0)
715    return TRUE;
716  else
717    return FALSE;
718}
719
720/*2
721* return the maximal component number found in any polynomial in s
722*/
723long id_RankFreeModule (ideal s, ring lmRing, ring tailRing)
724{
725  if (s!=NULL)
726  {
727    int  j=0;
728
729    if (rRing_has_Comp(tailRing) && rRing_has_Comp(lmRing))
730    {
731      int  l=IDELEMS(s);
732      poly *p=s->m;
733      int  k;
734      for (; l != 0; l--)
735      {
736        if (*p!=NULL)
737        {
738          pp_Test(*p, lmRing, tailRing);
739          k = p_MaxComp(*p, lmRing, tailRing);
740          if (k>j) j = k;
741        }
742        p++;
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  int i,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(1,pGetComp(h));     // 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 -> %d\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
1430// uses glabl vars via pSetModDeg
1431//BOOLEAN idTestHomModule(ideal m, ideal Q, intvec *w)
1432//{
1433//  if ((Q!=NULL) && (!idHomIdeal(Q,NULL)))  { PrintS(" Q not hom\n"); return FALSE;}
1434//  if (idIs0(m)) return TRUE;
1435//
1436//  int cmax=-1;
1437//  int i;
1438//  poly p=NULL;
1439//  int length=IDELEMS(m);
1440//  poly* P=m->m;
1441//  for (i=length-1;i>=0;i--)
1442//  {
1443//    p=P[i];
1444//    if (p!=NULL) cmax=si_max(cmax,(int)pMaxComp(p)+1);
1445//  }
1446//  if (w != NULL)
1447//  if (w->length()+1 < cmax)
1448//  {
1449//    // Print("length: %d - %d \n", w->length(),cmax);
1450//    return FALSE;
1451//  }
1452//
1453//  if(w!=NULL)
1454//    pSetModDeg(w);
1455//
1456//  for (i=length-1;i>=0;i--)
1457//  {
1458//    p=P[i];
1459//    poly q=p;
1460//    if (p!=NULL)
1461//    {
1462//      int d=pFDeg(p,currRing);
1463//      loop
1464//      {
1465//        pIter(p);
1466//        if (p==NULL) break;
1467//        if (d!=pFDeg(p,currRing))
1468//        {
1469//          //pWrite(q); wrp(p); Print(" -> %d - %d\n",d,pFDeg(p,currRing));
1470//          if(w!=NULL)
1471//            pSetModDeg(NULL);
1472//          return FALSE;
1473//        }
1474//      }
1475//    }
1476//  }
1477//
1478//  if(w!=NULL)
1479//    pSetModDeg(NULL);
1480//
1481//  return TRUE;
1482//}
1483
1484ideal id_Jet(ideal i,int d, const ring R)
1485{
1486  ideal r=idInit((i->nrows)*(i->ncols),i->rank);
1487  r->nrows = i-> nrows;
1488  r->ncols = i-> ncols;
1489  //r->rank = i-> rank;
1490  int k;
1491  for(k=(i->nrows)*(i->ncols)-1;k>=0; k--)
1492  {
1493    r->m[k]=pp_Jet(i->m[k],d,R);
1494  }
1495  return r;
1496}
1497
1498ideal id_JetW(ideal i,int d, intvec * iv, const ring R)
1499{
1500  ideal r=idInit(IDELEMS(i),i->rank);
1501  if (ecartWeights!=NULL)
1502  {
1503    WerrorS("cannot compute weighted jets now");
1504  }
1505  else
1506  {
1507    short *w=iv2array(iv,R);
1508    int k;
1509    for(k=0; k<IDELEMS(i); k++)
1510    {
1511      r->m[k]=pp_JetW(i->m[k],d,w,R);
1512    }
1513    omFreeSize((ADDRESS)w,(rVar(R)+1)*sizeof(short));
1514  }
1515  return r;
1516}
1517
1518/*3
1519* searches for the next unit in the components of the module arg and
1520* returns the first one;
1521*/
1522static int id_ReadOutPivot(ideal arg,int* comp, const ring r)
1523{
1524  if (idIs0(arg)) return -1;
1525  int i=0,j, generator=-1;
1526  int rk_arg=arg->rank; //idRankFreeModule(arg);
1527  int * componentIsUsed =(int *)omAlloc((rk_arg+1)*sizeof(int));
1528  poly p;
1529
1530  while ((generator<0) && (i<IDELEMS(arg)))
1531  {
1532    memset(componentIsUsed,0,(rk_arg+1)*sizeof(int));
1533    p = arg->m[i];
1534    while (p!=NULL)
1535    {
1536      j = p_GetComp(p,r);
1537      if (componentIsUsed[j]==0)
1538      {
1539#ifdef HAVE_RINGS
1540        if (p_LmIsConstantComp(p,r) &&
1541            (!rField_is_Ring(r) || n_IsUnit(pGetCoeff(p),r->cf)))
1542        {
1543#else
1544        if (p_LmIsConstantComp(p,r))
1545        {
1546#endif
1547          generator = i;
1548          componentIsUsed[j] = 1;
1549        }
1550        else
1551        {
1552          componentIsUsed[j] = -1;
1553        }
1554      }
1555      else if (componentIsUsed[j]>0)
1556      {
1557        (componentIsUsed[j])++;
1558      }
1559      pIter(p);
1560    }
1561    i++;
1562  }
1563  i = 0;
1564  *comp = -1;
1565  for (j=0;j<=rk_arg;j++)
1566  {
1567    if (componentIsUsed[j]>0)
1568    {
1569      if ((*comp==-1) || (componentIsUsed[j]<i))
1570      {
1571        *comp = j;
1572        i= componentIsUsed[j];
1573      }
1574    }
1575  }
1576  omFree(componentIsUsed);
1577  return generator;
1578}
1579
1580#if 0
1581static void idDeleteComp(ideal arg,int red_comp)
1582{
1583  int i,j;
1584  poly p;
1585
1586  for (i=IDELEMS(arg)-1;i>=0;i--)
1587  {
1588    p = arg->m[i];
1589    while (p!=NULL)
1590    {
1591      j = pGetComp(p);
1592      if (j>red_comp)
1593      {
1594        pSetComp(p,j-1);
1595        pSetm(p);
1596      }
1597      pIter(p);
1598    }
1599  }
1600  (arg->rank)--;
1601}
1602#endif
1603
1604intvec * id_QHomWeight(ideal id, const ring r)
1605{
1606  poly head, tail;
1607  int k;
1608  int in=IDELEMS(id)-1, ready=0, all=0,
1609      coldim=rVar(r), rowmax=2*coldim;
1610  if (in<0) return NULL;
1611  intvec *imat=new intvec(rowmax+1,coldim,0);
1612
1613  do
1614  {
1615    head = id->m[in--];
1616    if (head!=NULL)
1617    {
1618      tail = pNext(head);
1619      while (tail!=NULL)
1620      {
1621        all++;
1622        for (k=1;k<=coldim;k++)
1623          IMATELEM(*imat,all,k) = p_GetExpDiff(head,tail,k,r);
1624        if (all==rowmax)
1625        {
1626          ivTriangIntern(imat, ready, all);
1627          if (ready==coldim)
1628          {
1629            delete imat;
1630            return NULL;
1631          }
1632        }
1633        pIter(tail);
1634      }
1635    }
1636  } while (in>=0);
1637  if (all>ready)
1638  {
1639    ivTriangIntern(imat, ready, all);
1640    if (ready==coldim)
1641    {
1642      delete imat;
1643      return NULL;
1644    }
1645  }
1646  intvec *result = ivSolveKern(imat, ready);
1647  delete imat;
1648  return result;
1649}
1650
1651BOOLEAN id_IsZeroDim(ideal I, const ring r)
1652{
1653  BOOLEAN *UsedAxis=(BOOLEAN *)omAlloc0(rVar(r)*sizeof(BOOLEAN));
1654  int i,n;
1655  poly po;
1656  BOOLEAN res=TRUE;
1657  for(i=IDELEMS(I)-1;i>=0;i--)
1658  {
1659    po=I->m[i];
1660    if ((po!=NULL) &&((n=p_IsPurePower(po,r))!=0)) UsedAxis[n-1]=TRUE;
1661  }
1662  for(i=rVar(r)-1;i>=0;i--)
1663  {
1664    if(UsedAxis[i]==FALSE) {res=FALSE; break;} // not zero-dim.
1665  }
1666  omFreeSize(UsedAxis,rVar(r)*sizeof(BOOLEAN));
1667  return res;
1668}
1669
1670void id_Normalize(ideal I,const ring r)
1671{
1672  if (rField_has_simple_inverse(r)) return; /* Z/p, GF(p,n), R, long R/C */
1673  int i;
1674  for(i=IDELEMS(I)-1;i>=0;i--)
1675  {
1676    p_Normalize(I->m[i],r);
1677  }
1678}
1679
1680int id_MinDegW(ideal M,intvec *w, const ring r)
1681{
1682  int d=-1;
1683  for(int i=0;i<IDELEMS(M);i++)
1684  {
1685    if (M->m[i]!=NULL)
1686    {
1687      int d0=p_MinDeg(M->m[i],w,r);
1688      if(-1<d0&&((d0<d)||(d==-1)))
1689        d=d0;
1690    }
1691  }
1692  return d;
1693}
1694
1695// #include <kernel/clapsing.h>
1696
1697#ifdef HAVE_FACTORY
1698#if 0
1699poly id_GCD(poly f, poly g, const ring r)
1700{
1701  ring save_r=r;
1702  rChangeCurrRing(r);
1703  ideal I=idInit(2,1); I->m[0]=f; I->m[1]=g;
1704  intvec *w = NULL;
1705
1706  ideal S=idSyzygies(I,testHomog,&w);
1707
1708  if (w!=NULL) delete w;
1709  poly gg=pTakeOutComp(&(S->m[0]),2);
1710  id_Delete(&S, r);
1711  poly gcd_p=singclap_pdivide(f,gg);
1712  pDelete(&gg);
1713  rChangeCurrRing(save_r);
1714  return gcd_p;
1715}
1716#endif
1717#endif
1718
1719/*2
1720* xx,q: arrays of length 0..rl-1
1721* xx[i]: SB mod q[i]
1722* assume: char=0
1723* assume: q[i]!=0
1724* destroys xx
1725*/
1726#ifdef HAVE_FACTORY
1727ideal id_ChineseRemainder(ideal *xx, number *q, int rl, const ring R)
1728{
1729  int cnt=IDELEMS(xx[0])*xx[0]->nrows;
1730  ideal result=idInit(cnt,xx[0]->rank);
1731  result->nrows=xx[0]->nrows; // for lifting matrices
1732  result->ncols=xx[0]->ncols; // for lifting matrices
1733  int i,j;
1734  poly r,h,hh,res_p;
1735  number *x=(number *)omAlloc(rl*sizeof(number));
1736  for(i=cnt-1;i>=0;i--)
1737  {
1738    res_p=NULL;
1739    loop
1740    {
1741      r=NULL;
1742      for(j=rl-1;j>=0;j--)
1743      {
1744        h=xx[j]->m[i];
1745        if ((h!=NULL)
1746        &&((r==NULL)||(p_LmCmp(r,h,R)==-1)))
1747          r=h;
1748      }
1749      if (r==NULL) break;
1750      h=p_Head(r, R);
1751      for(j=rl-1;j>=0;j--)
1752      {
1753        hh=xx[j]->m[i];
1754        if ((hh!=NULL) && (p_LmCmp(r,hh, R)==0))
1755        {
1756          x[j]=p_GetCoeff(hh, R);
1757          hh=p_LmFreeAndNext(hh, R);
1758          xx[j]->m[i]=hh;
1759        }
1760        else
1761          x[j]=n_Init(0, R->cf); // is R->cf really n_Q???, yes!
1762      }
1763       
1764      number n=nChineseRemainder(x,q,rl, R->cf);
1765
1766      for(j=rl-1;j>=0;j--)
1767      {
1768        x[j]=NULL; // nlInit(0...) takes no memory
1769      }
1770      if (n_IsZero(n, R->cf)) p_Delete(&h, R);
1771      else
1772      {
1773        p_SetCoeff(h,n, R);
1774        //Print("new mon:");pWrite(h);
1775        res_p=p_Add_q(res_p, h, R);
1776      }
1777    }
1778    result->m[i]=res_p;
1779  }
1780  omFree(x);
1781  for(i=rl-1;i>=0;i--) id_Delete(&(xx[i]), R);
1782  omFree(xx);
1783  return result;
1784}
1785#endif
1786
1787/*2
1788* transpose a module
1789*/
1790ideal id_Transp(ideal a, const ring rRing)
1791{
1792  int r = a->rank, c = IDELEMS(a);
1793  ideal b =  idInit(r,c);
1794
1795  for (int i=c; i>0; i--)
1796  {
1797    poly p=a->m[i-1];
1798    while(p!=NULL)
1799    {
1800      poly h=p_Head(p, rRing);
1801      int co=p_GetComp(h, rRing)-1;
1802      p_SetComp(h, i, rRing);
1803      p_Setm(h, rRing);
1804      b->m[co] = p_Add_q(b->m[co], h, rRing);
1805      pIter(p);
1806    }
1807  }
1808  return b;
1809}
1810
1811
1812
1813/*2
1814* The following is needed to compute the image of certain map used in
1815* the computation of cohomologies via BGG
1816* let M = { w_1, ..., w_k }, k = size(M) == ncols(M), n = nvars(currRing).
1817* assuming that nrows(M) <= m*n; the procedure computes:
1818* transpose(M) * transpose( var(1) I_m | ... | var(n) I_m ) :== transpose(module{f_1, ... f_k}),
1819* where f_i = \sum_{j=1}^{m} (w_i, v_j) gen(j),  (w_i, v_j) is a `scalar` multiplication.
1820* that is, if w_i = (a^1_1, ... a^1_m) | (a^2_1, ..., a^2_m) | ... | (a^n_1, ..., a^n_m) then
1821
1822  (a^1_1, ... a^1_m) | (a^2_1, ..., a^2_m) | ... | (a^n_1, ..., a^n_m)
1823*  var_1  ... var_1  |  var_2  ...  var_2  | ... |  var_n  ...  var(n)
1824*  gen_1  ... gen_m  |  gen_1  ...  gen_m  | ... |  gen_1  ...  gen_m
1825+ =>
1826  f_i =
1827
1828   a^1_1 * var(1) * gen(1) + ... + a^1_m * var(1) * gen(m) +
1829   a^2_1 * var(2) * gen(1) + ... + a^2_m * var(2) * gen(m) +
1830                             ...
1831   a^n_1 * var(n) * gen(1) + ... + a^n_m * var(n) * gen(m);
1832
1833   NOTE: for every f_i we run only ONCE along w_i saving partial sums into a temporary array of polys of size m
1834*/
1835ideal id_TensorModuleMult(const int m, const ideal M, const ring rRing)
1836{
1837// #ifdef DEBU
1838//  WarnS("tensorModuleMult!!!!");
1839
1840  assume(m > 0);
1841  assume(M != NULL);
1842
1843  const int n = rRing->N;
1844
1845  assume(M->rank <= m * n);
1846
1847  const int k = IDELEMS(M);
1848
1849  ideal idTemp =  idInit(k,m); // = {f_1, ..., f_k }
1850
1851  for( int i = 0; i < k; i++ ) // for every w \in M
1852  {
1853    poly pTempSum = NULL;
1854
1855    poly w = M->m[i];
1856
1857    while(w != NULL) // for each term of w...
1858    {
1859      poly h = p_Head(w, rRing);
1860
1861      const int  gen = p_GetComp(h, rRing); // 1 ...
1862
1863      assume(gen > 0);
1864      assume(gen <= n*m);
1865
1866      // TODO: write a formula with %, / instead of while!
1867      /*
1868      int c = gen;
1869      int v = 1;
1870      while(c > m)
1871      {
1872        c -= m;
1873        v++;
1874      }
1875      */
1876
1877      int cc = gen % m;
1878      if( cc == 0) cc = m;
1879      int vv = 1 + (gen - cc) / m;
1880
1881//      assume( cc == c );
1882//      assume( vv == v );
1883
1884      //  1<= c <= m
1885      assume( cc > 0 );
1886      assume( cc <= m );
1887
1888      assume( vv > 0 );
1889      assume( vv <= n );
1890
1891      assume( (cc + (vv-1)*m) == gen );
1892
1893      p_IncrExp(h, vv, rRing); // h *= var(j) && //      p_AddExp(h, vv, 1, rRing);
1894      p_SetComp(h, cc, rRing);
1895
1896      p_Setm(h, rRing);         // addjust degree after the previous steps!
1897
1898      pTempSum = p_Add_q(pTempSum, h, rRing); // it is slow since h will be usually put to the back of pTempSum!!!
1899
1900      pIter(w);
1901    }
1902
1903    idTemp->m[i] = pTempSum;
1904  }
1905
1906  // simplify idTemp???
1907
1908  ideal idResult = id_Transp(idTemp, rRing);
1909
1910  id_Delete(&idTemp, rRing);
1911
1912  return(idResult);
1913}
Note: See TracBrowser for help on using the repository browser.