source: git/libpolys/polys/simpleideals.cc @ 760a78f

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