source: git/libpolys/polys/simpleideals.cc @ 7829fb

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