source: git/libpolys/polys/simpleideals.cc @ 18dab28

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