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

spielwiese
Last change on this file since af598e was af598e, checked in by Oleksandr Motsak <motsak@…>, 13 years ago
ADD: factory-related wrappers (clapsing.cc/.h) FIX: ring-dependent fixes for some factory-dependent code TODO: finish fixing clapsing.cc/.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 
21#include "monomials/p_polys.h"
22#include "weight.h"
23#include "matpol.h"
24#include "simpleideals.h"
25#include "sbuckets.h"
26
27omBin sip_sideal_bin;
28
29static poly * idpower;
30/*collects the monomials in makemonoms, must be allocated befor*/
31static int idpowerpoint;
32/*index of the actual monomial in idpower*/
33static poly * givenideal;
34/*the ideal from which a power is computed*/
35
36/*2
37* initialise an ideal
38*/
39ideal idInit(int idsize, int rank)
40{
41  /*- initialise an ideal -*/
42  ideal hh = (ideal )omAllocBin(sip_sideal_bin);
43  hh->nrows = 1;
44  hh->rank = rank;
45  IDELEMS(hh) = idsize;
46  if (idsize>0)
47  {
48    hh->m = (poly *)omAlloc0(idsize*sizeof(poly));
49  }
50  else
51    hh->m=NULL;
52  return hh;
53}
54
55#ifdef PDEBUG
56// this is only for outputting an ideal within the debugger
57void idShow(const ideal id, const ring lmRing, const ring tailRing, const int debugPrint)
58{
59  assume( debugPrint >= 0 );
60
61  if( id == NULL )
62    PrintS("(NULL)");
63  else
64  {
65    Print("Module of rank %ld,real rank %ld and %d generators.\n",
66          id->rank,id_RankFreeModule(id, lmRing, tailRing),IDELEMS(id));
67
68    int j = (id->ncols*id->nrows) - 1;
69    while ((j > 0) && (id->m[j]==NULL)) j--;
70    for (int i = 0; i <= j; i++)
71    {
72      Print("generator %d: ",i); p_DebugPrint(id->m[i], lmRing, tailRing, debugPrint);
73    }
74  }
75}
76#endif
77
78/* index of generator with leading term in ground ring (if any);
79   otherwise -1 */
80int id_PosConstant(ideal id, const ring r)
81{
82  int k;
83  for (k = IDELEMS(id)-1; k>=0; k--)
84  {
85    if (p_LmIsConstantComp(id->m[k], r) == TRUE)
86      return k;
87  }
88  return -1;
89}
90
91/*2
92* initialise the maximal ideal (at 0)
93*/
94ideal id_MaxIdeal (const ring r)
95{
96  int l;
97  ideal hh=NULL;
98
99  hh=idInit(rVar(r),1);
100  for (l=0; l<rVar(r); l++)
101  {
102    hh->m[l] = p_One(r);
103    p_SetExp(hh->m[l],l+1,1,r);
104    p_Setm(hh->m[l],r);
105  }
106  return hh;
107}
108
109/*2
110* deletes an ideal/matrix
111*/
112void id_Delete (ideal * h, ring r)
113{
114  int j,elems;
115  if (*h == NULL)
116    return;
117  elems=j=(*h)->nrows*(*h)->ncols;
118  if (j>0)
119  {
120    do
121    {
122      p_Delete(&((*h)->m[--j]), r);
123    }
124    while (j>0);
125    omFreeSize((ADDRESS)((*h)->m),sizeof(poly)*elems);
126  }
127  omFreeBin((ADDRESS)*h, sip_sideal_bin);
128  *h=NULL;
129}
130
131
132/*2
133* Shallowdeletes an ideal/matrix
134*/
135void id_ShallowDelete (ideal *h, ring r)
136{
137  int j,elems;
138  if (*h == NULL)
139    return;
140  elems=j=(*h)->nrows*(*h)->ncols;
141  if (j>0)
142  {
143    do
144    {
145      p_ShallowDelete(&((*h)->m[--j]), r);
146    }
147    while (j>0);
148    omFreeSize((ADDRESS)((*h)->m),sizeof(poly)*elems);
149  }
150  omFreeBin((ADDRESS)*h, sip_sideal_bin);
151  *h=NULL;
152}
153
154/*2
155*gives an ideal the minimal possible size
156*/
157void idSkipZeroes (ideal ide)
158{
159  int k;
160  int j = -1;
161  BOOLEAN change=FALSE;
162  for (k=0; k<IDELEMS(ide); k++)
163  {
164    if (ide->m[k] != NULL)
165    {
166      j++;
167      if (change)
168      {
169        ide->m[j] = ide->m[k];
170      }
171    }
172    else
173    {
174      change=TRUE;
175    }
176  }
177  if (change)
178  {
179    if (j == -1)
180      j = 0;
181    else
182    {
183      for (k=j+1; k<IDELEMS(ide); k++)
184        ide->m[k] = NULL;
185    }
186    pEnlargeSet(&(ide->m),IDELEMS(ide),j+1-IDELEMS(ide));
187    IDELEMS(ide) = j+1;
188  }
189}
190
191/*2
192* copies the first k (>= 1) entries of the given ideal
193* and returns these as a new ideal
194* (Note that the copied polynomials may be zero.)
195*/
196ideal id_CopyFirstK (const ideal ide, const int k,const ring r)
197{
198  ideal newI = idInit(k, 0);
199  for (int i = 0; i < k; i++)
200    newI->m[i] = p_Copy(ide->m[i],r);
201  return newI;
202}
203
204/*2
205* ideal id = (id[i])
206* result is leadcoeff(id[i]) = 1
207*/
208void id_Norm(ideal id, const ring r)
209{
210  for (int i=IDELEMS(id)-1; i>=0; i--)
211  {
212    if (id->m[i] != NULL)
213    {
214      p_Norm(id->m[i],r);
215    }
216  }
217}
218
219/*2
220* ideal id = (id[i]), c any unit
221* if id[i] = c*id[j] then id[j] is deleted for j > i
222*/
223void id_DelMultiples(ideal id, const ring r)
224{
225  int i, j;
226  int k = IDELEMS(id)-1;
227  for (i=k; i>=0; i--)
228  {
229    if (id->m[i]!=NULL)
230    {
231      for (j=k; j>i; j--)
232      {
233        if (id->m[j]!=NULL)
234        {
235#ifdef HAVE_RINGS
236          if (rField_is_Ring(r))
237          {
238            /* if id[j] = c*id[i] then delete id[j].
239               In the below cases of a ground field, we
240               check whether id[i] = c*id[j] and, if so,
241               delete id[j] for historical reasons (so
242               that previous output does not change) */
243            if (p_ComparePolys(id->m[j], id->m[i],r)) p_Delete(&id->m[j],r);
244          }
245          else
246          {
247            if (p_ComparePolys(id->m[i], id->m[j],r)) p_Delete(&id->m[j],r);
248          }
249#else
250          if (p_ComparePolys(id->m[i], id->m[j],r)) p_Delete(&id->m[j],r);
251#endif   
252        }
253      }
254    }
255  }
256}
257
258/*2
259* ideal id = (id[i])
260* if id[i] = id[j] then id[j] is deleted for j > i
261*/
262void id_DelEquals(ideal id, const ring r)
263{
264  int i, j;
265  int k = IDELEMS(id)-1;
266  for (i=k; i>=0; i--)
267  {
268    if (id->m[i]!=NULL)
269    {
270      for (j=k; j>i; j--)
271      {
272        if ((id->m[j]!=NULL)
273        && (p_EqualPolys(id->m[i], id->m[j],r)))
274        {
275          p_Delete(&id->m[j],r);
276        }
277      }
278    }
279  }
280}
281
282//
283// Delete id[j], if Lm(j) == Lm(i) and both LC(j), LC(i) are units and j > i
284//
285void id_DelLmEquals(ideal id, const ring r)
286{
287  int i, j;
288  int k = IDELEMS(id)-1;
289  for (i=k; i>=0; i--)
290  {
291    if (id->m[i] != NULL)
292    {
293      for (j=k; j>i; j--)
294      {
295        if ((id->m[j] != NULL)
296        && p_LmEqual(id->m[i], id->m[j],r)
297#ifdef HAVE_RINGS
298        && n_IsUnit(pGetCoeff(id->m[i]),r->cf) && n_IsUnit(pGetCoeff(id->m[j]),r->cf)
299#endif
300        )
301        {
302          p_Delete(&id->m[j],r);
303        }
304      }
305    }
306  }
307}
308
309//
310// delete id[j], if LT(j) == coeff*mon*LT(i) and vice versa, i.e.,
311// delete id[i], if LT(i) == coeff*mon*LT(j)
312//
313void id_DelDiv(ideal id, const ring r)
314{
315  int i, j;
316  int k = IDELEMS(id)-1;
317  for (i=k; i>=0; i--)
318  {
319    if (id->m[i] != NULL)
320    {
321      for (j=k; j>i; j--)
322      {
323        if (id->m[j]!=NULL)
324        {
325#ifdef HAVE_RINGS
326          if (rField_is_Ring(r))
327          {
328            if (p_DivisibleByRingCase(id->m[i], id->m[j],r))
329            {
330              p_Delete(&id->m[j],r);
331            }
332            else if (p_DivisibleByRingCase(id->m[j], id->m[i],r))
333            {
334              p_Delete(&id->m[i],r);
335              break;
336            }
337          }
338          else
339          {
340#endif
341          /* the case of a ground field: */
342          if (p_DivisibleBy(id->m[i], id->m[j],r))
343          {
344            p_Delete(&id->m[j],r);
345          }
346          else if (p_DivisibleBy(id->m[j], id->m[i],r))
347          {
348            p_Delete(&id->m[i],r);
349            break;
350          }
351#ifdef HAVE_RINGS
352          }
353#endif   
354        }
355      }
356    }
357  }
358}
359
360/*2
361*test if the ideal has only constant polynomials
362*/
363BOOLEAN id_IsConstant(ideal id, const ring r)
364{
365  int k;
366  for (k = IDELEMS(id)-1; k>=0; k--)
367  {
368    if (!p_IsConstantPoly(id->m[k],r))
369      return FALSE;
370  }
371  return TRUE;
372}
373
374/*2
375* copy an ideal
376*/
377ideal id_Copy(ideal h1, const ring r)
378{
379  int i;
380  ideal h2;
381
382//#ifdef TEST
383  if (h1 == NULL)
384  {
385    h2=idInit(1,1);
386  }
387  else
388//#endif
389  {
390    h2=idInit(IDELEMS(h1),h1->rank);
391    for (i=IDELEMS(h1)-1; i>=0; i--)
392      h2->m[i] = p_Copy(h1->m[i],r);
393  }
394  return h2;
395}
396
397#ifdef PDEBUG
398void id_DBTest(ideal h1, int level, const char *f,const int l, const ring r)
399{
400  int i;
401
402  if (h1 != NULL)
403  {
404    // assume(IDELEMS(h1) > 0); for ideal/module, does not apply to matrix
405    omCheckAddrSize(h1,sizeof(*h1));
406    omdebugAddrSize(h1->m,h1->ncols*h1->nrows*sizeof(poly));
407    /* to be able to test matrices: */
408    for (i=(h1->ncols*h1->nrows)-1; i>=0; i--)
409      _p_Test(h1->m[i], r, level);
410    int new_rk=id_RankFreeModule(h1,r);
411    if(new_rk > h1->rank)
412    {
413      dReportError("wrong rank %d (should be %d) in %s:%d\n",
414                   h1->rank, new_rk, f,l);
415      omPrintAddrInfo(stderr, h1, " for ideal");
416      h1->rank=new_rk;
417    }
418  }
419}
420#endif
421
422/*3
423* for idSort: compare a and b revlex inclusive module comp.
424*/
425static int p_Comp_RevLex(poly a, poly b,BOOLEAN nolex, const ring R)
426{
427  if (b==NULL) return 1;
428  if (a==NULL) return -1;
429
430  if (nolex)
431  {
432    int r=p_LmCmp(a,b,R);
433    if (r!=0) return r;
434    number h=n_Sub(pGetCoeff(a),pGetCoeff(b),R->cf);
435    r = -1+n_IsZero(h,R->cf)+2*n_GreaterZero(h,R->cf); /* -1: <, 0:==, 1: > */
436    n_Delete(&h, R->cf);
437    return r;
438  }
439  int l=rVar(R);
440  while ((l>0) && (p_GetExp(a,l,R)==p_GetExp(b,l,R))) l--;
441  if (l==0)
442  {
443    if (p_GetComp(a,R)==p_GetComp(b,R))
444    {
445      number h=n_Sub(pGetCoeff(a),pGetCoeff(b),R->cf);
446      int r = -1+n_IsZero(h,R->cf)+2*n_GreaterZero(h,R->cf); /* -1: <, 0:==, 1: > */
447      n_Delete(&h,R->cf);
448      return r;
449    }
450    if (p_GetComp(a,R)>p_GetComp(b,R)) return 1;
451  }
452  else if (p_GetExp(a,l,R)>p_GetExp(b,l,R))
453    return 1;
454  return -1;
455}
456
457/*2
458*sorts the ideal w.r.t. the actual ringordering
459*uses lex-ordering when nolex = FALSE
460*/
461intvec *id_Sort(ideal id,BOOLEAN nolex, const ring r)
462{
463  poly p,q;
464  intvec * result = new intvec(IDELEMS(id));
465  int i, j, actpos=0, newpos, l;
466  int diff, olddiff, lastcomp, newcomp;
467  BOOLEAN notFound;
468
469  for (i=0;i<IDELEMS(id);i++)
470  {
471    if (id->m[i]!=NULL)
472    {
473      notFound = TRUE;
474      newpos = actpos / 2;
475      diff = (actpos+1) / 2;
476      diff = (diff+1) / 2;
477      lastcomp = p_Comp_RevLex(id->m[i],id->m[(*result)[newpos]],nolex,r);
478      if (lastcomp<0)
479      {
480        newpos -= diff;
481      }
482      else if (lastcomp>0)
483      {
484        newpos += diff;
485      }
486      else
487      {
488        notFound = FALSE;
489      }
490      //while ((newpos>=0) && (newpos<actpos) && (notFound))
491      while (notFound && (newpos>=0) && (newpos<actpos))
492      {
493        newcomp = p_Comp_RevLex(id->m[i],id->m[(*result)[newpos]],nolex,r);
494        olddiff = diff;
495        if (diff>1)
496        {
497          diff = (diff+1) / 2;
498          if ((newcomp==1)
499          && (actpos-newpos>1)
500          && (diff>1)
501          && (newpos+diff>=actpos))
502          {
503            diff = actpos-newpos-1;
504          }
505          else if ((newcomp==-1)
506          && (diff>1)
507          && (newpos<diff))
508          {
509            diff = newpos;
510          }
511        }
512        if (newcomp<0)
513        {
514          if ((olddiff==1) && (lastcomp>0))
515            notFound = FALSE;
516          else
517            newpos -= diff;
518        }
519        else if (newcomp>0)
520        {
521          if ((olddiff==1) && (lastcomp<0))
522          {
523            notFound = FALSE;
524            newpos++;
525          }
526          else
527          {
528            newpos += diff;
529          }
530        }
531        else
532        {
533          notFound = FALSE;
534        }
535        lastcomp = newcomp;
536        if (diff==0) notFound=FALSE; /*hs*/
537      }
538      if (newpos<0) newpos = 0;
539      if (newpos>actpos) newpos = actpos;
540      while ((newpos<actpos) && (p_Comp_RevLex(id->m[i],id->m[(*result)[newpos]],nolex,r)==0))
541        newpos++;
542      for (j=actpos;j>newpos;j--)
543      {
544        (*result)[j] = (*result)[j-1];
545      }
546      (*result)[newpos] = i;
547      actpos++;
548    }
549  }
550  for (j=0;j<actpos;j++) (*result)[j]++;
551  return result;
552}
553
554/*2
555* concat the lists h1 and h2 without zeros
556*/
557ideal id_SimpleAdd (ideal h1,ideal h2, const ring R)
558{
559  int i,j,r,l;
560  ideal result;
561
562  if (h1==NULL) return id_Copy(h2,R);
563  if (h2==NULL) return id_Copy(h1,R);
564  j = IDELEMS(h1)-1;
565  while ((j >= 0) && (h1->m[j] == NULL)) j--;
566  i = IDELEMS(h2)-1;
567  while ((i >= 0) && (h2->m[i] == NULL)) i--;
568  r = si_max(h1->rank,h2->rank);
569  if (i+j==(-2))
570    return idInit(1,r);
571  else
572    result=idInit(i+j+2,r);
573  for (l=j; l>=0; l--)
574  {
575    result->m[l] = p_Copy(h1->m[l],R);
576  }
577  r = i+j+1;
578  for (l=i; l>=0; l--, r--)
579  {
580    result->m[r] = p_Copy(h2->m[l],R);
581  }
582  return result;
583}
584
585/*2
586* insert h2 into h1 (if h2 is not the zero polynomial)
587* return TRUE iff h2 was indeed inserted
588*/
589BOOLEAN idInsertPoly (ideal h1, poly h2)
590{
591  if (h2==NULL) return FALSE;
592  int j = IDELEMS(h1)-1;
593  while ((j >= 0) && (h1->m[j] == NULL)) j--;
594  j++;
595  if (j==IDELEMS(h1))
596  {
597    pEnlargeSet(&(h1->m),IDELEMS(h1),16);
598    IDELEMS(h1)+=16;
599  }
600  h1->m[j]=h2;
601  return TRUE;
602}
603
604/*2
605* insert h2 into h1 depending on the two boolean parameters:
606* - if zeroOk is true, then h2 will also be inserted when it is zero
607* - if duplicateOk is true, then h2 will also be inserted when it is
608*   already present in h1
609* return TRUE iff h2 was indeed inserted
610*/
611BOOLEAN id_InsertPolyWithTests (ideal h1, const int validEntries,
612  const poly h2, const bool zeroOk, const bool duplicateOk, const ring r)
613{
614  if ((!zeroOk) && (h2 == NULL)) return FALSE;
615  if (!duplicateOk)
616  {
617    bool h2FoundInH1 = false;
618    int i = 0;
619    while ((i < validEntries) && (!h2FoundInH1))
620    {
621      h2FoundInH1 = p_EqualPolys(h1->m[i], h2,r);
622      i++;
623    }
624    if (h2FoundInH1) return FALSE;
625  }
626  if (validEntries == IDELEMS(h1))
627  {
628    pEnlargeSet(&(h1->m), IDELEMS(h1), 16);
629    IDELEMS(h1) += 16;
630  }
631  h1->m[validEntries] = h2;
632  return TRUE;
633}
634
635/*2
636* h1 + h2
637*/
638ideal id_Add (ideal h1,ideal h2, const ring r)
639{
640  ideal result = id_SimpleAdd(h1,h2,r);
641  id_Compactify(result,r);
642  return result;
643}
644
645/*2
646* h1 * h2
647*/
648ideal  id_Mult (ideal h1,ideal  h2, const ring r)
649{
650  int i,j,k;
651  ideal  hh;
652
653  j = IDELEMS(h1);
654  while ((j > 0) && (h1->m[j-1] == NULL)) j--;
655  i = IDELEMS(h2);
656  while ((i > 0) && (h2->m[i-1] == NULL)) i--;
657  j = j * i;
658  if (j == 0)
659    hh = idInit(1,1);
660  else
661    hh=idInit(j,1);
662  if (h1->rank<h2->rank)
663    hh->rank = h2->rank;
664  else
665    hh->rank = h1->rank;
666  if (j==0) return hh;
667  k = 0;
668  for (i=0; i<IDELEMS(h1); i++)
669  {
670    if (h1->m[i] != NULL)
671    {
672      for (j=0; j<IDELEMS(h2); j++)
673      {
674        if (h2->m[j] != NULL)
675        {
676          hh->m[k] = pp_Mult_qq(h1->m[i],h2->m[j],r);
677          k++;
678        }
679      }
680    }
681  }
682  {
683    id_Compactify(hh,r);
684    return hh;
685  }
686}
687
688/*2
689*returns true if h is the zero ideal
690*/
691BOOLEAN idIs0 (ideal h)
692{
693  int i;
694
695  if (h == NULL) return TRUE;
696  i = IDELEMS(h)-1;
697  while ((i >= 0) && (h->m[i] == NULL))
698  {
699    i--;
700  }
701  if (i < 0)
702    return TRUE;
703  else
704    return FALSE;
705}
706
707/*2
708* return the maximal component number found in any polynomial in s
709*/
710long id_RankFreeModule (ideal s, ring lmRing, ring tailRing)
711{
712  if (s!=NULL)
713  {
714    int  j=0;
715
716    if (rRing_has_Comp(tailRing) && rRing_has_Comp(lmRing))
717    {
718      int  l=IDELEMS(s);
719      poly *p=s->m;
720      int  k;
721      for (; l != 0; l--)
722      {
723        if (*p!=NULL)
724        {
725          pp_Test(*p, lmRing, tailRing);
726          k = p_MaxComp(*p, lmRing, tailRing);
727          if (k>j) j = k;
728        }
729        p++;
730      }
731    }
732    return j;
733  }
734  return -1;
735}
736
737BOOLEAN idIsModule(ideal id, ring r)
738{
739  if (id != NULL && rRing_has_Comp(r))
740  {
741    int j, l = IDELEMS(id);
742    for (j=0; j<l; j++)
743    {
744      if (id->m[j] != NULL && p_GetComp(id->m[j], r) > 0) return TRUE;
745    }
746  }
747  return FALSE;
748}
749
750
751/*2
752*returns true if id is homogenous with respect to the aktual weights
753*/
754BOOLEAN id_HomIdeal (ideal id, ideal Q, const ring r)
755{
756  int i;
757  BOOLEAN     b;
758  if ((id == NULL) || (IDELEMS(id) == 0)) return TRUE;
759  i = 0;
760  b = TRUE;
761  while ((i < IDELEMS(id)) && b)
762  {
763    b = p_IsHomogeneous(id->m[i],r);
764    i++;
765  }
766  if ((b) && (Q!=NULL) && (IDELEMS(Q)>0))
767  {
768    i=0;
769    while ((i < IDELEMS(Q)) && b)
770    {
771      b = p_IsHomogeneous(Q->m[i],r);
772      i++;
773    }
774  }
775  return b;
776}
777
778/*2
779*initialized a field with r numbers between beg and end for the
780*procedure idNextChoise
781*/
782void idInitChoise (int r,int beg,int end,BOOLEAN  *endch,int * choise)
783{
784  /*returns the first choise of r numbers between beg and end*/
785  int i;
786  for (i=0; i<r; i++)
787  {
788    choise[i] = 0;
789  }
790  if (r <= end-beg+1)
791    for (i=0; i<r; i++)
792    {
793      choise[i] = beg+i;
794    }
795  if (r > end-beg+1)
796    *endch = TRUE;
797  else
798    *endch = FALSE;
799}
800
801/*2
802*returns the next choise of r numbers between beg and end
803*/
804void idGetNextChoise (int r,int end,BOOLEAN  *endch,int * choise)
805{
806  int i = r-1,j;
807  while ((i >= 0) && (choise[i] == end))
808  {
809    i--;
810    end--;
811  }
812  if (i == -1)
813    *endch = TRUE;
814  else
815  {
816    choise[i]++;
817    for (j=i+1; j<r; j++)
818    {
819      choise[j] = choise[i]+j-i;
820    }
821    *endch = FALSE;
822  }
823}
824
825/*2
826*takes the field choise of d numbers between beg and end, cancels the t-th
827*entree and searches for the ordinal number of that d-1 dimensional field
828* w.r.t. the algorithm of construction
829*/
830int idGetNumberOfChoise(int t, int d, int begin, int end, int * choise)
831{
832  int * localchoise,i,result=0;
833  BOOLEAN b=FALSE;
834
835  if (d<=1) return 1;
836  localchoise=(int*)omAlloc((d-1)*sizeof(int));
837  idInitChoise(d-1,begin,end,&b,localchoise);
838  while (!b)
839  {
840    result++;
841    i = 0;
842    while ((i<t) && (localchoise[i]==choise[i])) i++;
843    if (i>=t)
844    {
845      i = t+1;
846      while ((i<d) && (localchoise[i-1]==choise[i])) i++;
847      if (i>=d)
848      {
849        omFreeSize((ADDRESS)localchoise,(d-1)*sizeof(int));
850        return result;
851      }
852    }
853    idGetNextChoise(d-1,end,&b,localchoise);
854  }
855  omFreeSize((ADDRESS)localchoise,(d-1)*sizeof(int));
856  return 0;
857}
858
859/*2
860*computes the binomial coefficient
861*/
862int binom (int n,int r)
863{
864  int i,result;
865
866  if (r==0) return 1;
867  if (n-r<r) return binom(n,n-r);
868  result = n-r+1;
869  for (i=2;i<=r;i++)
870  {
871    result *= n-r+i;
872    if (result<0)
873    {
874      WarnS("overflow in binomials");
875      return 0;
876    }
877    result /= i;
878  }
879  return result;
880}
881
882/*2
883*the free module of rank i
884*/
885ideal id_FreeModule (int i, const ring r)
886{
887  int j;
888  ideal h;
889
890  h=idInit(i,i);
891  for (j=0; j<i; j++)
892  {
893    h->m[j] = p_One(r);
894    p_SetComp(h->m[j],j+1,r);
895    p_SetmComp(h->m[j],r);
896  }
897  return h;
898}
899
900/*2
901*computes recursively all monomials of a certain degree
902*in every step the actvar-th entry in the exponential
903*vector is incremented and the other variables are
904*computed by recursive calls of makemonoms
905*if the last variable is reached, the difference to the
906*degree is computed directly
907*vars is the number variables
908*actvar is the actual variable to handle
909*deg is the degree of the monomials to compute
910*monomdeg is the actual degree of the monomial in consideration
911*/
912static void makemonoms(int vars,int actvar,int deg,int monomdeg, const ring r)
913{
914  poly p;
915  int i=0;
916
917  if ((idpowerpoint == 0) && (actvar ==1))
918  {
919    idpower[idpowerpoint] = p_One(r);
920    monomdeg = 0;
921  }
922  while (i<=deg)
923  {
924    if (deg == monomdeg)
925    {
926      p_Setm(idpower[idpowerpoint],r);
927      idpowerpoint++;
928      return;
929    }
930    if (actvar == vars)
931    {
932      p_SetExp(idpower[idpowerpoint],actvar,deg-monomdeg,r);
933      p_Setm(idpower[idpowerpoint],r);
934      p_Test(idpower[idpowerpoint],r);
935      idpowerpoint++;
936      return;
937    }
938    else
939    {
940      p = p_Copy(idpower[idpowerpoint],r);
941      makemonoms(vars,actvar+1,deg,monomdeg,r);
942      idpower[idpowerpoint] = p;
943    }
944    monomdeg++;
945    p_SetExp(idpower[idpowerpoint],actvar,p_GetExp(idpower[idpowerpoint],actvar,r)+1,r);
946    p_Setm(idpower[idpowerpoint],r);
947    p_Test(idpower[idpowerpoint],r);
948    i++;
949  }
950}
951
952/*2
953*returns the deg-th power of the maximal ideal of 0
954*/
955ideal id_MaxIdeal(int deg, const ring r)
956{
957  if (deg < 0)
958  {
959    WarnS("maxideal: power must be non-negative");
960  }
961  if (deg < 1)
962  {
963    ideal I=idInit(1,1);
964    I->m[0]=p_One(r);
965    return I;
966  }
967  if (deg == 1)
968  {
969    return id_MaxIdeal(r);
970  }
971
972  int vars = rVar(r);
973  int i = binom(vars+deg-1,deg);
974  if (i<=0) return idInit(1,1);
975  ideal id=idInit(i,1);
976  idpower = id->m;
977  idpowerpoint = 0;
978  makemonoms(vars,1,deg,0,r);
979  idpower = NULL;
980  idpowerpoint = 0;
981  return id;
982}
983
984/*2
985*computes recursively all generators of a certain degree
986*of the ideal "givenideal"
987*elms is the number elements in the given ideal
988*actelm is the actual element to handle
989*deg is the degree of the power to compute
990*gendeg is the actual degree of the generator in consideration
991*/
992static void makepotence(int elms,int actelm,int deg,int gendeg, const ring r)
993{
994  poly p;
995  int i=0;
996
997  if ((idpowerpoint == 0) && (actelm ==1))
998  {
999    idpower[idpowerpoint] = p_One(r);
1000    gendeg = 0;
1001  }
1002  while (i<=deg)
1003  {
1004    if (deg == gendeg)
1005    {
1006      idpowerpoint++;
1007      return;
1008    }
1009    if (actelm == elms)
1010    {
1011      p=p_Power(p_Copy(givenideal[actelm-1],r),deg-gendeg,r);
1012      idpower[idpowerpoint]=p_Mult_q(idpower[idpowerpoint],p,r);
1013      idpowerpoint++;
1014      return;
1015    }
1016    else
1017    {
1018      p = p_Copy(idpower[idpowerpoint],r);
1019      makepotence(elms,actelm+1,deg,gendeg,r);
1020      idpower[idpowerpoint] = p;
1021    }
1022    gendeg++;
1023    idpower[idpowerpoint]=p_Mult_q(idpower[idpowerpoint],p_Copy(givenideal[actelm-1],r),r);
1024    i++;
1025  }
1026}
1027
1028/*2
1029*returns the deg-th power of the ideal gid
1030*/
1031//ideal idPower(ideal gid,int deg)
1032//{
1033//  int i;
1034//  ideal id;
1035//
1036//  if (deg < 1) deg = 1;
1037//  i = binom(IDELEMS(gid)+deg-1,deg);
1038//  id=idInit(i,1);
1039//  idpower = id->m;
1040//  givenideal = gid->m;
1041//  idpowerpoint = 0;
1042//  makepotence(IDELEMS(gid),1,deg,0);
1043//  idpower = NULL;
1044//  givenideal = NULL;
1045//  idpowerpoint = 0;
1046//  return id;
1047//}
1048static void id_NextPotence(ideal given, ideal result,
1049  int begin, int end, int deg, int restdeg, poly ap, const ring r)
1050{
1051  poly p;
1052  int i;
1053
1054  p = p_Power(p_Copy(given->m[begin],r),restdeg,r);
1055  i = result->nrows;
1056  result->m[i] = p_Mult_q(p_Copy(ap,r),p,r);
1057//PrintS(".");
1058  (result->nrows)++;
1059  if (result->nrows >= IDELEMS(result))
1060  {
1061    pEnlargeSet(&(result->m),IDELEMS(result),16);
1062    IDELEMS(result) += 16;
1063  }
1064  if (begin == end) return;
1065  for (i=restdeg-1;i>0;i--)
1066  {
1067    p = p_Power(p_Copy(given->m[begin],r),i,r);
1068    p = p_Mult_q(p_Copy(ap,r),p,r);
1069    id_NextPotence(given, result, begin+1, end, deg, restdeg-i, p,r);
1070    p_Delete(&p,r);
1071  }
1072  id_NextPotence(given, result, begin+1, end, deg, restdeg, ap,r);
1073}
1074
1075ideal id_Power(ideal given,int exp, const ring r)
1076{
1077  ideal result,temp;
1078  poly p1;
1079  int i;
1080
1081  if (idIs0(given)) return idInit(1,1);
1082  temp = id_Copy(given,r);
1083  idSkipZeroes(temp);
1084  i = binom(IDELEMS(temp)+exp-1,exp);
1085  result = idInit(i,1);
1086  result->nrows = 0;
1087//Print("ideal contains %d elements\n",i);
1088  p1=p_One(r);
1089  id_NextPotence(temp,result,0,IDELEMS(temp)-1,exp,exp,p1,r);
1090  p_Delete(&p1,r);
1091  id_Delete(&temp,r);
1092  result->nrows = 1;
1093  id_DelEquals(result,r);
1094  idSkipZeroes(result);
1095  return result;
1096}
1097
1098/*2
1099*skips all zeroes and double elements, searches also for units
1100*/
1101void id_Compactify(ideal id, const ring r)
1102{
1103  int i,j;
1104  BOOLEAN b=FALSE;
1105
1106  i = IDELEMS(id)-1;
1107  while ((! b) && (i>=0))
1108  {
1109    b=p_IsUnit(id->m[i],r);
1110    i--;
1111  }
1112  if (b)
1113  {
1114    for(i=IDELEMS(id)-1;i>=0;i--) p_Delete(&id->m[i],r);
1115    id->m[0]=p_One(r);
1116  }
1117  else
1118  {
1119    id_DelMultiples(id,r);
1120  }
1121  idSkipZeroes(id);
1122}
1123
1124/*2
1125* returns the ideals of initial terms
1126*/
1127ideal id_Head(ideal h,const ring r)
1128{
1129  ideal m = idInit(IDELEMS(h),h->rank);
1130  int i;
1131
1132  for (i=IDELEMS(h)-1;i>=0; i--)
1133  {
1134    if (h->m[i]!=NULL) m->m[i]=p_Head(h->m[i],r);
1135  }
1136  return m;
1137}
1138
1139ideal id_Homogen(ideal h, int varnum,const ring r)
1140{
1141  ideal m = idInit(IDELEMS(h),h->rank);
1142  int i;
1143
1144  for (i=IDELEMS(h)-1;i>=0; i--)
1145  {
1146    m->m[i]=p_Homogen(h->m[i],varnum,r);
1147  }
1148  return m;
1149}
1150
1151/*------------------type conversions----------------*/
1152ideal id_Vec2Ideal(poly vec, const ring R)
1153{
1154   ideal result=idInit(1,1);
1155   omFree((ADDRESS)result->m);
1156   result->m=NULL; // remove later
1157   p_Vec2Polys(vec, &(result->m), &(IDELEMS(result)),R);
1158   return result;
1159}
1160
1161
1162// converts mat to module, destroys mat
1163ideal id_Matrix2Module(matrix mat, const ring R)
1164{
1165  int mc=MATCOLS(mat);
1166  int mr=MATROWS(mat);
1167  ideal result = idInit(si_max(mc,1),si_max(mr,1));
1168  int i,j, l;
1169  poly h;
1170  poly p;
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,j;
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=nlChineseRemainder(x,q,rl); // kernel/clapconv.cc
1738
1739      for(j=rl-1;j>=0;j--)
1740      {
1741        x[j]=NULL; // nlInit(0...) takes no memory
1742      }
1743      if (nlIsZero(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.