source: git/libpolys/polys/simpleideals.cc @ 9f7665

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