source: git/libpolys/polys/monomials/ring.cc @ 7ac612

spielwiese
Last change on this file since 7ac612 was 7ac612, checked in by Hans Schoenemann <hannes@…>, 13 years ago
cahnged: maFindPerm (uses cf->type instead of cf->ch)
  • Property mode set to 100644
File size: 133.2 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id$ */
5
6/*
7* ABSTRACT - the interpreter related ring operations
8*/
9
10/* includes */
11#include <math.h>
12
13#include <omalloc/omalloc.h>
14#include <misc/options.h>
15#include <misc/intvec.h>
16
17#include <coeffs/numbers.h>
18#include <coeffs/coeffs.h>
19
20#include <polys/monomials/p_polys.h>
21#include <polys/simpleideals.h>
22// #include <???/febase.h>
23// #include <???/intvec.h>
24#include <polys/ext_fields/longalg.h>
25#include <polys/ext_fields/longtrans.h>
26// #include <coeffs/ffields.h>
27#include <polys/monomials/ring.h>
28#include <polys/monomials/maps.h>
29#include <polys/prCopy.h>
30// #include "../Singular/ipshell.h"
31#include <polys/templates/p_Procs.h>
32
33#include <polys/matpol.h>
34
35#include <polys/monomials/ring.h>
36
37#ifdef HAVE_PLURAL
38#include <polys/nc/nc.h>
39#include <polys/nc/sca.h>
40#endif
41// #include <???/maps.h>
42// #include <???/matpol.h>
43
44#define BITS_PER_LONG 8*SIZEOF_LONG
45
46omBin sip_sring_bin = omGetSpecBin(sizeof(ip_sring));
47
48static const char * const ringorder_name[] =
49{
50  " ?", ///< ringorder_no = 0,
51  "a", ///< ringorder_a,
52  "A", ///< ringorder_a64,
53  "c", ///< ringorder_c,
54  "C", ///< ringorder_C,
55  "M", ///< ringorder_M,
56  "S", ///< ringorder_S,
57  "s", ///< ringorder_s,
58  "lp", ///< ringorder_lp,
59  "dp", ///< ringorder_dp,
60  "rp", ///< ringorder_rp,
61  "Dp", ///< ringorder_Dp,
62  "wp", ///< ringorder_wp,
63  "Wp", ///< ringorder_Wp,
64  "ls", ///< ringorder_ls,
65  "ds", ///< ringorder_ds,
66  "Ds", ///< ringorder_Ds,
67  "ws", ///< ringorder_ws,
68  "Ws", ///< ringorder_Ws,
69  "L", ///< ringorder_L,
70  "aa", ///< ringorder_aa
71  "rs", ///< ringorder_rs,
72  "IS", ///<  ringorder_IS
73  " _" ///< ringorder_unspec
74};
75
76#define  rInternalChar(r) ((r)->cf->ch)
77
78const char * rSimpleOrdStr(int ord)
79{
80  return ringorder_name[ord];
81}
82
83/// unconditionally deletes fields in r
84void rDelete(ring r);
85/// set r->VarL_Size, r->VarL_Offset, r->VarL_LowIndex
86static void rSetVarL(ring r);
87/// get r->divmask depending on bits per exponent
88static unsigned long rGetDivMask(int bits);
89/// right-adjust r->VarOffset
90static void rRightAdjustVarOffset(ring r);
91static void rOptimizeLDeg(ring r);
92
93/*0 implementation*/
94//BOOLEAN rField_is_R(ring r)
95//{
96//  if (r->cf->ch== -1)
97//  {
98//    if (r->float_len==(short)0) return TRUE;
99//  }
100//  return FALSE;
101//}
102
103/*
104
105/// internally changes the gloabl ring and resets the relevant
106/// global variables:
107/// SHOULD BE DEPRECATED NOW...?
108void rChangeCurrRing(ring r)
109{
110 // if (!rMinpolyIsNULL(currRing))
111 // {
112 //   omCheckAddr(currRing->cf->minpoly);
113 // }
114  //------------ set global ring vars --------------------------------
115  //currRing = r;
116  //currQuotient=NULL;
117  if (r != NULL)
118  {
119    rTest(r);
120    //------------ set global ring vars --------------------------------
121    //currQuotient=r->qideal;
122
123    //------------ global variables related to coefficients ------------
124    nSetChar(r->cf);
125
126    //------------ global variables related to polys -------------------
127    p_SetGlobals(r);
128    //------------ global variables related to factory -----------------
129  }
130}
131*/
132
133ring rDefault(const coeffs cf, int N, char **n,int ord_size, int *ord, int *block0, int *block1)
134{
135  assume( cf != NULL);
136  ring r=(ring) omAlloc0Bin(sip_sring_bin);
137  r->N     = N;
138  r->cf = cf;
139  /*rPar(r)  = 0; Alloc0 */
140  /*names*/
141  r->names = (char **) omAlloc0(N * sizeof(char *));
142  int i;
143  for(i=0;i<N;i++)
144  {
145    r->names[i]  = omStrDup(n[i]);
146  }
147  /*weights: entries for 2 blocks: NULL*/
148  r->wvhdl = (int **)omAlloc0((ord_size+1) * sizeof(int *));
149  r->order = ord;
150  r->block0 = block0;
151  r->block1 = block1;
152  /*polynomial ring*/
153  r->OrdSgn    = 1;
154
155  /* complete ring intializations */
156  rComplete(r);
157  return r;
158}
159ring rDefault(int ch, int N, char **n,int ord_size, int *ord, int *block0, int *block1)
160{
161  coeffs cf;
162  if (ch==0) cf=nInitChar(n_Q,NULL);
163  else       cf=nInitChar(n_Zp,(void*)(long)ch);
164  assume( cf != NULL);
165  return rDefault(cf,N,n,ord_size,ord,block0,block1);
166}
167ring   rDefault(const coeffs cf, int N, char **n)
168{
169  assume( cf != NULL);
170  /*order: lp,0*/
171  int *order = (int *) omAlloc(2* sizeof(int));
172  int *block0 = (int *)omAlloc0(2 * sizeof(int));
173  int *block1 = (int *)omAlloc0(2 * sizeof(int));
174  /* ringorder dp for the first block: var 1..N */
175  order[0]  = ringorder_lp;
176  block0[0] = 1;
177  block1[0] = N;
178  /* the last block: everything is 0 */
179  order[1]  = 0;
180
181  return rDefault(cf,N,n,2,order,block0,block1);
182}
183
184ring rDefault(int ch, int N, char **n)
185{
186  coeffs cf;
187  if (ch==0) cf=nInitChar(n_Q,NULL);
188  else       cf=nInitChar(n_Zp,(void*)(long)ch);
189  assume( cf != NULL);
190  return rDefault(cf,N,n);
191}
192
193///////////////////////////////////////////////////////////////////////////
194//
195// rInit: define a new ring from sleftv's
196//
197//-> ipshell.cc
198
199/////////////////////////////
200// Auxillary functions
201//
202
203// check intvec, describing the ordering
204BOOLEAN rCheckIV(intvec *iv)
205{
206  if ((iv->length()!=2)&&(iv->length()!=3))
207  {
208    WerrorS("weights only for orderings wp,ws,Wp,Ws,a,M");
209    return TRUE;
210  }
211  return FALSE;
212}
213
214int rTypeOfMatrixOrder(intvec * order)
215{
216  int i=0,j,typ=1;
217  int sz = (int)sqrt((double)(order->length()-2));
218  if ((sz*sz)!=(order->length()-2))
219  {
220    WerrorS("Matrix order is not a square matrix");
221    typ=0;
222  }
223  while ((i<sz) && (typ==1))
224  {
225    j=0;
226    while ((j<sz) && ((*order)[j*sz+i+2]==0)) j++;
227    if (j>=sz)
228    {
229      typ = 0;
230      WerrorS("Matrix order not complete");
231    }
232    else if ((*order)[j*sz+i+2]<0)
233      typ = -1;
234    else
235      i++;
236  }
237  return typ;
238}
239
240/*2
241 * set a new ring from the data:
242 s: name, chr: ch, varnames: rv, ordering: ord, typ: typ
243 */
244
245int r_IsRingVar(const char *n, ring r)
246{
247  if ((r!=NULL) && (r->names!=NULL))
248  {
249    for (int i=0; i<r->N; i++)
250    {
251      if (r->names[i]==NULL) return -1;
252      if (strcmp(n,r->names[i]) == 0) return (int)i;
253    }
254  }
255  return -1;
256}
257
258
259void rWrite(ring r)
260{
261  if ((r==NULL)||(r->order==NULL))
262    return; /*to avoid printing after errors....*/
263
264  int nblocks=rBlocks(r);
265
266  // omCheckAddrSize(r,sizeof(ip_sring));
267  omCheckAddrSize(r->order,nblocks*sizeof(int));
268  omCheckAddrSize(r->block0,nblocks*sizeof(int));
269  omCheckAddrSize(r->block1,nblocks*sizeof(int));
270  omCheckAddrSize(r->wvhdl,nblocks*sizeof(int *));
271  omCheckAddrSize(r->names,r->N*sizeof(char *));
272
273  nblocks--;
274
275  n_CoeffWrite(r->cf);
276#if 0
277  {
278    PrintS("//   characteristic : ");
279    if (rParameter(r)!=NULL)
280    {
281      Print ("//   %d parameter    : ",rPar(r));
282      char **sp= rParameter(r);
283      int nop=0;
284      while (nop<rPar(r))
285      {
286        PrintS(*sp);
287        PrintS(" ");
288        sp++; nop++;
289      }
290      PrintS("\n//   minpoly        : ");
291      if ( rField_is_long_C(r) )
292      {
293        // i^2+1:
294        Print("(%s^2+1)\n",rParameter(r)[0]);
295      }
296      else if (rMinpolyIsNULL(r))
297      {
298        PrintS("0\n");
299      }
300      else
301      {
302        StringSetS(""); n_Write(r->cf->minpoly,r->cf); PrintS(StringAppendS("\n"));
303      }
304      //if (r->minideal!=NULL)
305      //{
306      //  iiWriteMatrix((matrix)r->minideal,"//   minpolys",1,0);
307      //  PrintLn();
308      //}
309    }
310  }
311#endif
312  Print("//   number of vars : %d",r->N);
313
314  //for (nblocks=0; r->order[nblocks]; nblocks++);
315  nblocks=rBlocks(r)-1;
316
317  for (int l=0, nlen=0 ; l<nblocks; l++)
318  {
319    int i;
320    Print("\n//        block %3d : ",l+1);
321
322    Print("ordering %s", rSimpleOrdStr(r->order[l]));
323
324
325    if (r->order[l] == ringorder_s)
326    {
327      assume( l == 0 );
328#ifndef NDEBUG
329      Print("  syzcomp at %d",r->typ[l].data.syz.limit);
330#endif
331      continue;
332    }
333    else if (r->order[l] == ringorder_IS)
334    {
335      assume( r->block0[l] == r->block1[l] );
336      const int s = r->block0[l];
337      assume( (-2 < s) && (s < 2) );
338      Print("(%d)", s); // 0 => prefix! +/-1 => suffix!
339      continue;
340    }
341    else if (
342    (  (r->order[l] >= ringorder_lp)
343    ||(r->order[l] == ringorder_M)
344    ||(r->order[l] == ringorder_a)
345    ||(r->order[l] == ringorder_a64)
346    ||(r->order[l] == ringorder_aa) ) && (r->order[l] < ringorder_IS) )
347    {
348      PrintS("\n//                  : names   ");
349      for (i = r->block0[l]-1; i<r->block1[l]; i++)
350      {
351        nlen = strlen(r->names[i]);
352        Print(" %s",r->names[i]);
353      }
354    }
355
356    if (r->wvhdl[l]!=NULL)
357    {
358      for (int j= 0;
359           j<(r->block1[l]-r->block0[l]+1)*(r->block1[l]-r->block0[l]+1);
360           j+=i)
361      {
362        PrintS("\n//                  : weights ");
363        for (i = 0; i<=r->block1[l]-r->block0[l]; i++)
364        {
365          if (r->order[l] == ringorder_a64)
366          {
367            int64 *w=(int64 *)r->wvhdl[l];
368            #if SIZEOF_LONG == 4
369            Print("%*lld " ,nlen,w[i+j]);
370            #else
371            Print(" %*ld"  ,nlen,w[i+j]);
372            #endif
373          }
374          else
375            Print(" %*d" ,nlen,r->wvhdl[l][i+j]);
376        }
377        if (r->order[l]!=ringorder_M) break;
378      }
379    }
380  }
381#ifdef HAVE_PLURAL
382  if(rIsPluralRing(r))
383  {
384    PrintS("\n//   noncommutative relations:");
385    poly pl=NULL;
386    int nl;
387    int i,j;
388    for (i = 1; i<r->N; i++)
389    {
390      for (j = i+1; j<=r->N; j++)
391      {
392        nl = n_IsOne(p_GetCoeff(MATELEM(r->GetNC()->C,i,j),r), r->cf);
393        if ( (MATELEM(r->GetNC()->D,i,j)!=NULL) || (!nl) )
394        {
395          Print("\n//    %s%s=",r->names[j-1],r->names[i-1]);
396          pl = MATELEM(r->GetNC()->MT[UPMATELEM(i,j,r->N)],1,1);
397          p_Write0(pl, r, r);
398        }
399      }
400    }
401#if 0  /*Singularg should not differ from Singular except in error case*/
402    Print("\n//   noncommutative type:%d", (int)ncRingType(r));
403    Print("\n//      is skew constant:%d",r->GetNC()->IsSkewConstant);
404    if( rIsSCA(r) )
405    {
406      Print("\n//   alternating variables: [%d, %d]", scaFirstAltVar(r), scaLastAltVar(r));
407      const ideal Q = SCAQuotient(r); // resides within r!
408      PrintS("\n//   quotient of sca by ideal");
409
410      if (Q!=NULL)
411      {
412//        if (r==currRing)
413//        {
414//          PrintLn();
415//          iiWriteMatrix((matrix)Q,"scaQ",1);
416//        }
417//        else
418            PrintS(" ...");
419      }
420      else
421        PrintS(" (NULL)");
422    }
423#endif
424  }
425#endif
426  //if (r->qideal!=NULL)
427  //{
428  //  PrintS("\n// quotient ring from ideal\n");
429  //  iiWriteMatrix((matrix)r->qideal,"_",1);
430  //}
431}
432
433void rDelete(ring r)
434{
435  int i, j;
436
437  if (r == NULL) return;
438
439#ifdef HAVE_PLURAL
440  if (rIsPluralRing(r))
441    nc_rKill(r);
442#endif
443
444  nKillChar(r->cf); r->cf = NULL;
445  rUnComplete(r);
446  // delete order stuff
447  if (r->order != NULL)
448  {
449    i=rBlocks(r);
450    assume(r->block0 != NULL && r->block1 != NULL && r->wvhdl != NULL);
451    // delete order
452    omFreeSize((ADDRESS)r->order,i*sizeof(int));
453    omFreeSize((ADDRESS)r->block0,i*sizeof(int));
454    omFreeSize((ADDRESS)r->block1,i*sizeof(int));
455    // delete weights
456    for (j=0; j<i; j++)
457    {
458      if (r->wvhdl[j]!=NULL)
459        omFree(r->wvhdl[j]);
460    }
461    omFreeSize((ADDRESS)r->wvhdl,i*sizeof(int *));
462  }
463  else
464  {
465    assume(r->block0 == NULL && r->block1 == NULL && r->wvhdl == NULL);
466  }
467
468  // delete varnames
469  if(r->names!=NULL)
470  {
471    for (i=0; i<r->N; i++)
472    {
473      if (r->names[i] != NULL) omFree((ADDRESS)r->names[i]);
474    }
475    omFreeSize((ADDRESS)r->names,r->N*sizeof(char *));
476  }
477
478//   // delete parameter
479//   if (rParameter(r)!=NULL)
480//   {
481//     char **s= rParameter(r);
482//     j = 0;
483//     while (j < rPar(r))
484//     {
485//       if (*s != NULL) omFree((ADDRESS)*s);
486//       s++;
487//       j++;
488//     }
489//     omFreeSize((ADDRESS)rParameter(r),rPar(r)*sizeof(char *));
490//   }
491  omFreeBin(r, sip_sring_bin);
492}
493
494int rOrderName(char * ordername)
495{
496  int order=ringorder_unspec;
497  while (order!= 0)
498  {
499    if (strcmp(ordername,rSimpleOrdStr(order))==0)
500      break;
501    order--;
502  }
503  if (order==0) Werror("wrong ring order `%s`",ordername);
504  omFree((ADDRESS)ordername);
505  return order;
506}
507
508char * rOrdStr(ring r)
509{
510  if ((r==NULL)||(r->order==NULL)) return omStrDup("");
511  int nblocks,l,i;
512
513  for (nblocks=0; r->order[nblocks]; nblocks++);
514  nblocks--;
515
516  StringSetS("");
517  for (l=0; ; l++)
518  {
519    StringAppendS((char *)rSimpleOrdStr(r->order[l]));
520    if (
521           (r->order[l] != ringorder_c)
522        && (r->order[l] != ringorder_C)
523        && (r->order[l] != ringorder_s)
524        && (r->order[l] != ringorder_S)
525        && (r->order[l] != ringorder_IS)
526       )
527    {
528      if (r->wvhdl[l]!=NULL)
529      {
530        StringAppendS("(");
531        for (int j= 0;
532             j<(r->block1[l]-r->block0[l]+1)*(r->block1[l]-r->block0[l]+1);
533             j+=i+1)
534        {
535          char c=',';
536          if(r->order[l]==ringorder_a64)
537          {
538            int64 * w=(int64 *)r->wvhdl[l];
539            for (i = 0; i<r->block1[l]-r->block0[l]; i++)
540            {
541              StringAppend("%lld," ,w[i]);
542            }
543            StringAppend("%lld)" ,w[i]);
544            break;
545          }
546          else
547          {
548            for (i = 0; i<r->block1[l]-r->block0[l]; i++)
549            {
550              StringAppend("%d," ,r->wvhdl[l][i+j]);
551            }
552          }
553          if (r->order[l]!=ringorder_M)
554          {
555            StringAppend("%d)" ,r->wvhdl[l][i+j]);
556            break;
557          }
558          if (j+i+1==(r->block1[l]-r->block0[l]+1)*(r->block1[l]-r->block0[l]+1))
559            c=')';
560          StringAppend("%d%c" ,r->wvhdl[l][i+j],c);
561        }
562      }
563      else
564        StringAppend("(%d)",r->block1[l]-r->block0[l]+1);
565    }
566    else if (r->order[l] == ringorder_IS)
567    {
568      assume( r->block0[l] == r->block1[l] );
569      const int s = r->block0[l];
570      assume( (-2 < s) && (s < 2) );
571
572      StringAppend("(%d)", s);
573    }
574
575    if (l==nblocks) return omStrDup(StringAppendS(""));
576    StringAppendS(",");
577  }
578}
579
580char * rVarStr(ring r)
581{
582  if ((r==NULL)||(r->names==NULL)) return omStrDup("");
583  int i;
584  int l=2;
585  char *s;
586
587  for (i=0; i<r->N; i++)
588  {
589    l+=strlen(r->names[i])+1;
590  }
591  s=(char *)omAlloc((long)l);
592  s[0]='\0';
593  for (i=0; i<r->N-1; i++)
594  {
595    strcat(s,r->names[i]);
596    strcat(s,",");
597  }
598  strcat(s,r->names[i]);
599  return s;
600}
601
602char * rCharStr(ring r)
603{
604  char *s;
605  int i;
606
607#ifdef HAVE_RINGS
608  if (rField_is_Ring_Z(r))
609  {
610    s=omStrDup("integer");                   // Z
611    return s;
612  }
613  if(rField_is_Ring_2toM(r))
614  {
615    char* s = (char*) omAlloc(7+10+2);
616    sprintf(s,"integer,%lu",r->cf->modExponent);
617    return s;
618  }
619  if(rField_is_Ring_ModN(r))
620  {
621    long l = (long)mpz_sizeinbase(r->cf->modBase, 10) + 2+7;
622    char* s = (char*) omAlloc(l);
623    gmp_sprintf(s,"integer,%Zd",r->cf->modBase);
624    return s;
625  }
626  if(rField_is_Ring_PtoM(r))
627  {
628    long l = (long)mpz_sizeinbase(r->cf->modBase, 10) + 2+7+10;
629    char* s = (char*) omAlloc(l);
630    gmp_sprintf(s,"integer,%Zd^%lu",r->cf->modBase,r->cf->modExponent);
631    return s;
632  }
633#endif
634  if (rParameter(r)==NULL)
635  {
636    i=r->cf->ch;
637    if(i==-1)
638      s=omStrDup("real");                    /* R */
639    else
640    {
641      s=(char *)omAlloc(MAX_INT_LEN+1);
642      sprintf(s,"%d",i);                   /* Q, Z/p */
643    }
644    return s;
645  }
646  if (rField_is_long_C(r))
647  {
648    s=(char *)omAlloc(21+strlen(rParameter(r)[0]));
649    sprintf(s,"complex,%d,%s",r->float_len,rParameter(r)[0]);   /* C */
650    return s;
651  }
652  int l=0;
653  for(i=0; i<rPar(r);i++)
654  {
655    l+=(strlen(rParameter(r)[i])+1);
656  }
657  s=(char *)omAlloc((long)(l+MAX_INT_LEN+1));
658  s[0]='\0';
659  if (r->cf->ch<0)       sprintf(s,"%d",-r->cf->ch); /* Fp(a) */
660  else if (r->cf->ch==1) sprintf(s,"0");         /* Q(a)  */
661  else
662  {
663    sprintf(s,"%d,%s",r->cf->ch,rParameter(r)[0]); /* GF(q)  */
664    return s;
665  }
666  char tt[2];
667  tt[0]=',';
668  tt[1]='\0';
669  for(i=0; i<rPar(r);i++)
670  {
671    strcat(s,tt);
672    strcat(s,rParameter(r)[i]);
673  }
674  return s;
675}
676
677char * rParStr(ring r)
678{
679  if ((r==NULL)||(rParameter(r)==NULL)) return omStrDup("");
680
681  int i;
682  int l=2;
683
684  for (i=0; i<rPar(r); i++)
685  {
686    l+=strlen(rParameter(r)[i])+1;
687  }
688  char *s=(char *)omAlloc((long)l);
689  s[0]='\0';
690  for (i=0; i<rPar(r)-1; i++)
691  {
692    strcat(s,rParameter(r)[i]);
693    strcat(s,",");
694  }
695  strcat(s,rParameter(r)[i]);
696  return s;
697}
698
699char * rString(ring r)
700{
701  char *ch=rCharStr(r);
702  char *var=rVarStr(r);
703  char *ord=rOrdStr(r);
704  char *res=(char *)omAlloc(strlen(ch)+strlen(var)+strlen(ord)+9);
705  sprintf(res,"(%s),(%s),(%s)",ch,var,ord);
706  omFree((ADDRESS)ch);
707  omFree((ADDRESS)var);
708  omFree((ADDRESS)ord);
709  return res;
710}
711
712
713static int binaryPower (const int a, const int b)
714{
715  /* computes a^b according to the binary representation of b,
716     i.e., a^7 = a^4 * a^2 * a^1. This saves some multiplications. */
717  int result = 1;
718  int factor = a;
719  int bb = b;
720  while (bb != 0)
721  {
722    if (bb % 2 != 0) result = result * factor;
723    bb = bb / 2;
724    factor = factor * factor;
725  }
726  return result;
727}
728
729/* we keep this otherwise superfluous method for compatibility reasons
730   towards the SINGULAR svn trunk */
731int rChar(ring r) { return r->cf->ch; }
732
733typedef char *             char_ptr;
734omBin char_ptr_bin = omGetSpecBin(sizeof(char_ptr)); // deallocation?
735
736
737// creates a commutative nc extension; "converts" comm.ring to a Plural ring
738#ifdef HAVE_PLURAL
739ring nc_rCreateNCcomm_rCopy(ring r)
740{
741  r = rCopy(r);
742  if (rIsPluralRing(r))
743    return r;
744
745  matrix C = mpNew(r->N,r->N); // ring-independent!?!
746  matrix D = mpNew(r->N,r->N);
747
748  for(int i=1; i<r->N; i++)
749    for(int j=i+1; j<=r->N; j++)
750      MATELEM(C,i,j) = p_One( r);
751
752  if (nc_CallPlural(C, D, NULL, NULL, r, false, true, false, r/*??currRing??*/, TRUE)) // TODO: what about quotient ideal?
753    WarnS("Error initializing multiplication!"); // No reaction!???
754
755  return r;
756}
757#endif
758
759
760/*2
761 *returns -1 for not compatible, (sum is undefined)
762 *         1 for compatible (and sum)
763 */
764/* vartest: test for variable/paramter names
765* dp_dp: for comm. rings: use block order dp + dp/ds/wp
766*/
767int rSumInternal(ring r1, ring r2, ring &sum, BOOLEAN vartest, BOOLEAN dp_dp)
768{
769
770  ip_sring tmpR;
771  memset(&tmpR,0,sizeof(tmpR));
772  /* check coeff. field =====================================================*/
773
774  if (r1->cf==r2->cf)
775  {
776    tmpR.cf=r1->cf;
777    r1->cf->ref++;
778  }
779  else /* different type */
780  {
781    if (getCoeffType(r1->cf)==n_Zp)
782    {
783      if (getCoeffType(r2->cf)==n_Q)
784      {
785        tmpR.cf=r1->cf;
786        r1->cf->ref++;
787      }
788      else
789      {
790        WerrorS("Z/p+...");
791        return -1;
792      }
793    }
794    else if (getCoeffType(r1->cf)==n_R)
795    {
796      WerrorS("R+..");
797      return -1;
798    }
799    else if (getCoeffType(r1->cf)==n_Q)
800    {
801      if (getCoeffType(r2->cf)==n_Zp)
802      {
803        tmpR.cf=r2->cf;
804        r2->cf->ref++;
805      }
806      else
807      {
808        WerrorS("Q+...");
809        return -1;
810      }
811    }
812    else
813    {
814      WerrorS("coeff sum not yet implemented");
815      return -1;
816    }
817  }
818  /* variable names ========================================================*/
819  int i,j,k;
820  int l=r1->N+r2->N;
821  char **names=(char **)omAlloc0(l*sizeof(char *));
822  k=0;
823
824  // collect all varnames from r1, except those which are parameters
825  // of r2, or those which are the empty string
826  for (i=0;i<r1->N;i++)
827  {
828    BOOLEAN b=TRUE;
829
830    if (*(r1->names[i]) == '\0')
831      b = FALSE;
832    else if ((rParameter(r2)!=NULL) && (strlen(r1->names[i])==1))
833    {
834      if (vartest)
835      {
836        for(j=0;j<rPar(r2);j++)
837        {
838          if (strcmp(r1->names[i],rParameter(r2)[j])==0)
839          {
840            b=FALSE;
841            break;
842          }
843        }
844      }
845    }
846
847    if (b)
848    {
849      //Print("name : %d: %s\n",k,r1->names[i]);
850      names[k]=omStrDup(r1->names[i]);
851      k++;
852    }
853    //else
854    //  Print("no name (par1) %s\n",r1->names[i]);
855  }
856  // Add variables from r2, except those which are parameters of r1
857  // those which are empty strings, and those which equal a var of r1
858  for(i=0;i<r2->N;i++)
859  {
860    BOOLEAN b=TRUE;
861
862    if (*(r2->names[i]) == '\0')
863      b = FALSE;
864    else if ((rParameter(r1)!=NULL) && (strlen(r2->names[i])==1))
865    {
866      if (vartest)
867      {
868        for(j=0;j<rPar(r1);j++)
869        {
870          if (strcmp(r2->names[i],rParameter(r1)[j])==0)
871          {
872            b=FALSE;
873            break;
874          }
875        }
876      }
877    }
878
879    if (b)
880    {
881      if (vartest)
882      {
883        for(j=0;j<r1->N;j++)
884        {
885          if (strcmp(r1->names[j],r2->names[i])==0)
886          {
887            b=FALSE;
888            break;
889          }
890        }
891      }
892      if (b)
893      {
894        //Print("name : %d : %s\n",k,r2->names[i]);
895        names[k]=omStrDup(r2->names[i]);
896        k++;
897      }
898      //else
899      //  Print("no name (var): %s\n",r2->names[i]);
900    }
901    //else
902    //  Print("no name (par): %s\n",r2->names[i]);
903  }
904  // check whether we found any vars at all
905  if (k == 0)
906  {
907    names[k]=omStrDup("");
908    k=1;
909  }
910  tmpR.N=k;
911  tmpR.names=names;
912  /* ordering *======================================================== */
913  tmpR.OrdSgn=1;
914  if (dp_dp
915#ifdef HAVE_PLURAL
916      && !rIsPluralRing(r1) && !rIsPluralRing(r2)
917#endif
918     )
919  {
920    tmpR.order=(int*)omAlloc(4*sizeof(int));
921    tmpR.block0=(int*)omAlloc0(4*sizeof(int));
922    tmpR.block1=(int*)omAlloc0(4*sizeof(int));
923    tmpR.wvhdl=(int**)omAlloc0(4*sizeof(int *));
924    tmpR.order[0]=ringorder_dp;
925    tmpR.block0[0]=1;
926    tmpR.block1[0]=rVar(r1);
927    if (r2->OrdSgn==1)
928    {
929      if ((r2->block0[0]==1)
930      && (r2->block1[0]==rVar(r2))
931      && ((r2->order[0]==ringorder_wp)
932        || (r2->order[0]==ringorder_Wp)
933        || (r2->order[0]==ringorder_Dp))
934     )
935     {
936       tmpR.order[1]=r2->order[0];
937       if (r2->wvhdl[0]!=NULL)
938         tmpR.wvhdl[1]=(int *)omMemDup(r2->wvhdl[0]);
939     }
940     else
941        tmpR.order[1]=ringorder_dp;
942    }
943    else
944    {
945      tmpR.order[1]=ringorder_ds;
946      tmpR.OrdSgn=-1;
947    }
948    tmpR.block0[1]=rVar(r1)+1;
949    tmpR.block1[1]=rVar(r1)+rVar(r2);
950    tmpR.order[2]=ringorder_C;
951    tmpR.order[3]=0;
952  }
953  else
954  {
955    if ((r1->order[0]==ringorder_unspec)
956        && (r2->order[0]==ringorder_unspec))
957    {
958      tmpR.order=(int*)omAlloc(3*sizeof(int));
959      tmpR.block0=(int*)omAlloc(3*sizeof(int));
960      tmpR.block1=(int*)omAlloc(3*sizeof(int));
961      tmpR.wvhdl=(int**)omAlloc0(3*sizeof(int *));
962      tmpR.order[0]=ringorder_unspec;
963      tmpR.order[1]=ringorder_C;
964      tmpR.order[2]=0;
965      tmpR.block0[0]=1;
966      tmpR.block1[0]=tmpR.N;
967    }
968    else if (l==k) /* r3=r1+r2 */
969    {
970      int b;
971      ring rb;
972      if (r1->order[0]==ringorder_unspec)
973      {
974        /* extend order of r2 to r3 */
975        b=rBlocks(r2);
976        rb=r2;
977        tmpR.OrdSgn=r2->OrdSgn;
978      }
979      else if (r2->order[0]==ringorder_unspec)
980      {
981        /* extend order of r1 to r3 */
982        b=rBlocks(r1);
983        rb=r1;
984        tmpR.OrdSgn=r1->OrdSgn;
985      }
986      else
987      {
988        b=rBlocks(r1)+rBlocks(r2)-2; /* for only one order C, only one 0 */
989        rb=NULL;
990      }
991      tmpR.order=(int*)omAlloc0(b*sizeof(int));
992      tmpR.block0=(int*)omAlloc0(b*sizeof(int));
993      tmpR.block1=(int*)omAlloc0(b*sizeof(int));
994      tmpR.wvhdl=(int**)omAlloc0(b*sizeof(int *));
995      /* weights not implemented yet ...*/
996      if (rb!=NULL)
997      {
998        for (i=0;i<b;i++)
999        {
1000          tmpR.order[i]=rb->order[i];
1001          tmpR.block0[i]=rb->block0[i];
1002          tmpR.block1[i]=rb->block1[i];
1003          if (rb->wvhdl[i]!=NULL)
1004            WarnS("rSum: weights not implemented");
1005        }
1006        tmpR.block0[0]=1;
1007      }
1008      else /* ring sum for complete rings */
1009      {
1010        for (i=0;r1->order[i]!=0;i++)
1011        {
1012          tmpR.order[i]=r1->order[i];
1013          tmpR.block0[i]=r1->block0[i];
1014          tmpR.block1[i]=r1->block1[i];
1015          if (r1->wvhdl[i]!=NULL)
1016            tmpR.wvhdl[i] = (int*) omMemDup(r1->wvhdl[i]);
1017        }
1018        j=i;
1019        i--;
1020        if ((r1->order[i]==ringorder_c)
1021            ||(r1->order[i]==ringorder_C))
1022        {
1023          j--;
1024          tmpR.order[b-2]=r1->order[i];
1025        }
1026        for (i=0;r2->order[i]!=0;i++)
1027        {
1028          if ((r2->order[i]!=ringorder_c)
1029              &&(r2->order[i]!=ringorder_C))
1030          {
1031            tmpR.order[j]=r2->order[i];
1032            tmpR.block0[j]=r2->block0[i]+rVar(r1);
1033            tmpR.block1[j]=r2->block1[i]+rVar(r1);
1034            if (r2->wvhdl[i]!=NULL)
1035            {
1036              tmpR.wvhdl[j] = (int*) omMemDup(r2->wvhdl[i]);
1037            }
1038            j++;
1039          }
1040        }
1041        if((r1->OrdSgn==-1)||(r2->OrdSgn==-1))
1042          tmpR.OrdSgn=-1;
1043      }
1044    }
1045    else if ((k==rVar(r1)) && (k==rVar(r2))) /* r1 and r2 are "quite"
1046                                                the same ring */
1047      /* copy r1, because we have the variables from r1 */
1048    {
1049      int b=rBlocks(r1);
1050
1051      tmpR.order=(int*)omAlloc0(b*sizeof(int));
1052      tmpR.block0=(int*)omAlloc0(b*sizeof(int));
1053      tmpR.block1=(int*)omAlloc0(b*sizeof(int));
1054      tmpR.wvhdl=(int**)omAlloc0(b*sizeof(int *));
1055      /* weights not implemented yet ...*/
1056      for (i=0;i<b;i++)
1057      {
1058        tmpR.order[i]=r1->order[i];
1059        tmpR.block0[i]=r1->block0[i];
1060        tmpR.block1[i]=r1->block1[i];
1061        if (r1->wvhdl[i]!=NULL)
1062        {
1063          tmpR.wvhdl[i] = (int*) omMemDup(r1->wvhdl[i]);
1064        }
1065      }
1066      tmpR.OrdSgn=r1->OrdSgn;
1067    }
1068    else
1069    {
1070      for(i=0;i<k;i++) omFree((ADDRESS)tmpR.names[i]);
1071      omFreeSize((ADDRESS)names,tmpR.N*sizeof(char *));
1072      Werror("difficulties with variables: %d,%d -> %d",rVar(r1),rVar(r2),k);
1073      return -1;
1074    }
1075  }
1076  sum=(ring)omAllocBin(sip_sring_bin);
1077  memcpy(sum,&tmpR,sizeof(ip_sring));
1078  rComplete(sum);
1079
1080//#ifdef RDEBUG
1081//  rDebugPrint(sum);
1082//#endif
1083
1084
1085
1086#ifdef HAVE_PLURAL
1087  if(1)
1088  {
1089//    ring old_ring = currRing;
1090
1091    BOOLEAN R1_is_nc = rIsPluralRing(r1);
1092    BOOLEAN R2_is_nc = rIsPluralRing(r2);
1093
1094    if ( (R1_is_nc) || (R2_is_nc))
1095    {
1096      ring R1 = nc_rCreateNCcomm_rCopy(r1);
1097      assume( rIsPluralRing(R1) );
1098
1099#if 0
1100#ifdef RDEBUG
1101      rWrite(R1);
1102      rDebugPrint(R1);
1103#endif
1104#endif
1105      ring R2 = nc_rCreateNCcomm_rCopy(r2);
1106#if 0
1107#ifdef RDEBUG
1108      rWrite(R2);
1109      rDebugPrint(R2);
1110#endif
1111#endif
1112
1113//      rChangeCurrRing(sum); // ?
1114
1115      // Projections from R_i into Sum:
1116      /* multiplication matrices business: */
1117      /* find permutations of vars and pars */
1118      int *perm1 = (int *)omAlloc0((rVar(R1)+1)*sizeof(int));
1119      int *par_perm1 = NULL;
1120      if (rPar(R1)!=0) par_perm1=(int *)omAlloc0((rPar(R1)+1)*sizeof(int));
1121
1122      int *perm2 = (int *)omAlloc0((rVar(R2)+1)*sizeof(int));
1123      int *par_perm2 = NULL;
1124      if (rPar(R2)!=0) par_perm2=(int *)omAlloc0((rPar(R2)+1)*sizeof(int));
1125
1126      maFindPerm(R1->names,  rVar(R1),  rParameter(R1),  rPar(R1),
1127                 sum->names, rVar(sum), rParameter(sum), rPar(sum),
1128                 perm1, par_perm1, sum->cf->type);
1129
1130      maFindPerm(R2->names,  rVar(R2),  rParameter(R2),  rPar(R2),
1131                 sum->names, rVar(sum), rParameter(sum), rPar(sum),
1132                 perm2, par_perm2, sum->cf->type);
1133
1134
1135      matrix C1 = R1->GetNC()->C, C2 = R2->GetNC()->C;
1136      matrix D1 = R1->GetNC()->D, D2 = R2->GetNC()->D;
1137
1138      // !!!! BUG? C1 and C2 might live in different baserings!!!
1139
1140      int l = rVar(R1) + rVar(R2);
1141
1142      matrix C  = mpNew(l,l);
1143      matrix D  = mpNew(l,l);
1144
1145      for (i = 1; i <= rVar(R1); i++)
1146        for (j= rVar(R1)+1; j <= l; j++)
1147          MATELEM(C,i,j) = p_One(sum); // in 'sum'
1148
1149      id_Test((ideal)C, sum);
1150
1151      nMapFunc nMap1 = n_SetMap(R1->cf,sum->cf); /* can change something global: not usable
1152                                                    after the next nSetMap call :( */
1153      // Create blocked C and D matrices:
1154      for (i=1; i<= rVar(R1); i++)
1155        for (j=i+1; j<=rVar(R1); j++)
1156        {
1157          assume(MATELEM(C1,i,j) != NULL);
1158          MATELEM(C,i,j) = p_PermPoly(MATELEM(C1,i,j), perm1, R1, sum, nMap1, par_perm1, rPar(R1)); // need ADD + CMP ops.
1159
1160          if (MATELEM(D1,i,j) != NULL)
1161            MATELEM(D,i,j) = p_PermPoly(MATELEM(D1,i,j), perm1, R1, sum, nMap1, par_perm1, rPar(R1));
1162        }
1163
1164      id_Test((ideal)C, sum);
1165      id_Test((ideal)D, sum);
1166
1167
1168      nMapFunc nMap2 = n_SetMap(R2->cf,sum->cf); /* can change something global: not usable
1169                                                    after the next nSetMap call :( */
1170      for (i=1; i<= rVar(R2); i++)
1171        for (j=i+1; j<=rVar(R2); j++)
1172        {
1173          assume(MATELEM(C2,i,j) != NULL);
1174          MATELEM(C,rVar(R1)+i,rVar(R1)+j) = p_PermPoly(MATELEM(C2,i,j),perm2,R2,sum, nMap2,par_perm2,rPar(R2));
1175
1176          if (MATELEM(D2,i,j) != NULL)
1177            MATELEM(D,rVar(R1)+i,rVar(R1)+j) = p_PermPoly(MATELEM(D2,i,j),perm2,R2,sum, nMap2,par_perm2,rPar(R2));
1178        }
1179
1180      id_Test((ideal)C, sum);
1181      id_Test((ideal)D, sum);
1182
1183      // Now sum is non-commutative with blocked structure constants!
1184      if (nc_CallPlural(C, D, NULL, NULL, sum, false, false, true, sum))
1185        WarnS("Error initializing non-commutative multiplication!");
1186
1187      /* delete R1, R2*/
1188
1189#if 0
1190#ifdef RDEBUG
1191      rWrite(sum);
1192      rDebugPrint(sum);
1193
1194      Print("\nRefs: R1: %d, R2: %d\n", R1->GetNC()->ref, R2->GetNC()->ref);
1195
1196#endif
1197#endif
1198
1199
1200      rDelete(R1);
1201      rDelete(R2);
1202
1203      /* delete perm arrays */
1204      if (perm1!=NULL) omFree((ADDRESS)perm1);
1205      if (perm2!=NULL) omFree((ADDRESS)perm2);
1206      if (par_perm1!=NULL) omFree((ADDRESS)par_perm1);
1207      if (par_perm2!=NULL) omFree((ADDRESS)par_perm2);
1208
1209//      rChangeCurrRing(old_ring);
1210    }
1211
1212  }
1213#endif
1214
1215  ideal Q=NULL;
1216  ideal Q1=NULL, Q2=NULL;
1217  if (r1->qideal!=NULL)
1218  {
1219//    rChangeCurrRing(sum);
1220//     if (r2->qideal!=NULL)
1221//     {
1222//       WerrorS("todo: qring+qring");
1223//       return -1;
1224//     }
1225//     else
1226//     {}
1227    /* these were defined in the Plural Part above... */
1228    int *perm1 = (int *)omAlloc0((rVar(r1)+1)*sizeof(int));
1229    int *par_perm1 = NULL;
1230    if (rPar(r1)!=0) par_perm1=(int *)omAlloc0((rPar(r1)+1)*sizeof(int));
1231    maFindPerm(r1->names,  rVar(r1),  rParameter(r1),  rPar(r1),
1232               sum->names, rVar(sum), rParameter(sum), rPar(sum),
1233               perm1, par_perm1, sum->cf->type);
1234    nMapFunc nMap1 = n_SetMap(r1->cf,sum->cf);
1235    Q1 = idInit(IDELEMS(r1->qideal),1);
1236
1237    for (int for_i=0;for_i<IDELEMS(r1->qideal);for_i++)
1238      Q1->m[for_i] = p_PermPoly(
1239                                r1->qideal->m[for_i], perm1,
1240                                r1, sum,
1241                                nMap1,
1242                                par_perm1, rPar(r1));
1243
1244    omFree((ADDRESS)perm1);
1245  }
1246
1247  if (r2->qideal!=NULL)
1248  {
1249    //if (currRing!=sum)
1250    //  rChangeCurrRing(sum);
1251    int *perm2 = (int *)omAlloc0((rVar(r2)+1)*sizeof(int));
1252    int *par_perm2 = NULL;
1253    if (rPar(r2)!=0) par_perm2=(int *)omAlloc0((rPar(r2)+1)*sizeof(int));
1254    maFindPerm(r2->names,  rVar(r2),  rParameter(r2),  rPar(r2),
1255               sum->names, rVar(sum), rParameter(sum), rPar(sum),
1256               perm2, par_perm2, sum->cf->type);
1257    nMapFunc nMap2 = n_SetMap(r2->cf,sum->cf);
1258    Q2 = idInit(IDELEMS(r2->qideal),1);
1259
1260    for (int for_i=0;for_i<IDELEMS(r2->qideal);for_i++)
1261      Q2->m[for_i] = p_PermPoly(
1262                  r2->qideal->m[for_i], perm2,
1263                  r2, sum,
1264                  nMap2,
1265                  par_perm2, rPar(r2));
1266
1267    omFree((ADDRESS)perm2);
1268  }
1269  if ( (Q1!=NULL) || ( Q2!=NULL))
1270  {
1271    Q = id_SimpleAdd(Q1,Q2,sum);
1272  }
1273  sum->qideal = Q;
1274
1275#ifdef HAVE_PLURAL
1276  if( rIsPluralRing(sum) )
1277    nc_SetupQuotient( sum );
1278#endif
1279  return 1;
1280}
1281
1282/*2
1283 *returns -1 for not compatible, (sum is undefined)
1284 *         0 for equal, (and sum)
1285 *         1 for compatible (and sum)
1286 */
1287int rSum(ring r1, ring r2, ring &sum)
1288{
1289  if (r1==r2)
1290  {
1291    sum=r1;
1292    r1->ref++;
1293    return 0;
1294  }
1295  return rSumInternal(r1,r2,sum,TRUE,FALSE);
1296}
1297
1298/*2
1299 * create a copy of the ring r, which must be equivalent to currRing
1300 * used for qring definition,..
1301 * (i.e.: normal rings: same nCopy as currRing;
1302 *        qring:        same nCopy, same idCopy as currRing)
1303 * DOES NOT CALL rComplete
1304 */
1305ring rCopy0(const ring r, BOOLEAN copy_qideal, BOOLEAN copy_ordering)
1306{
1307  if (r == NULL) return NULL;
1308  int i,j;
1309  ring res=(ring)omAllocBin(sip_sring_bin);
1310  memset(res,0,sizeof(ip_sring));
1311  //memcpy(res,r,sizeof(ip_sring));
1312  //memset: res->idroot=NULL; /* local objects */
1313  //ideal      minideal;
1314  res->options=r->options; /* ring dependent options */
1315
1316  //memset: res->ordsgn=NULL;
1317  //memset: res->typ=NULL;
1318  //memset: res->VarOffset=NULL;
1319  //memset: res->firstwv=NULL;
1320
1321  //struct omBin   PolyBin; /* Bin from where monoms are allocated */
1322  //memset: res->PolyBin=NULL; // rComplete
1323  res->cf=r->cf;     /* coeffs */
1324  res->cf->ref++;
1325
1326  //memset: res->ref=0; /* reference counter to the ring */
1327
1328  res->float_len=r->float_len; /* additional char-flags */
1329  res->float_len2=r->float_len2; /* additional char-flags */
1330
1331  res->N=rVar(r);      /* number of vars */
1332  res->OrdSgn=r->OrdSgn; /* 1 for polynomial rings, -1 otherwise */
1333
1334  res->firstBlockEnds=r->firstBlockEnds;
1335#ifdef HAVE_PLURAL
1336  res->real_var_start=r->real_var_start;
1337  res->real_var_end=r->real_var_end;
1338#endif
1339
1340#ifdef HAVE_SHIFTBBA
1341  res->isLPring=r->isLPring; /* 0 for non-letterplace rings, otherwise the number of LP blocks, at least 1, known also as lV */
1342#endif
1343
1344  res->VectorOut=r->VectorOut;
1345  res->ShortOut=r->ShortOut;
1346  res->CanShortOut=r->CanShortOut;
1347  res->LexOrder=r->LexOrder; // TRUE if the monomial ordering has polynomial and power series blocks
1348  res->MixedOrder=r->MixedOrder; // ?? 1 for lex ordering (except ls), -1 otherwise
1349  res->ComponentOrder=r->ComponentOrder;
1350
1351  //memset: res->ExpL_Size=0;
1352  //memset: res->CmpL_Size=0;
1353  //memset: res->VarL_Size=0;
1354  //memset: res->pCompIndex=0;
1355  //memset: res->pOrdIndex=0;
1356  //memset: res->OrdSize=0;
1357  //memset: res->VarL_LowIndex=0;
1358  //memset: res->MinExpPerLong=0;
1359  //memset: res->NegWeightL_Size=0;
1360  //memset: res->NegWeightL_Offset=NULL;
1361  //memset: res->VarL_Offset=NULL;
1362
1363  // the following are set by rComplete unless predefined
1364  // therefore, we copy these values: maybe they are non-standard
1365  /* mask for getting single exponents */
1366  res->bitmask=r->bitmask;
1367  res->divmask=r->divmask;
1368  res->BitsPerExp = r->BitsPerExp;
1369  res->ExpPerLong =  r->ExpPerLong;
1370
1371  //memset: res->p_Procs=NULL;
1372  //memset: res->pFDeg=NULL;
1373  //memset: res->pLDeg=NULL;
1374  //memset: res->pFDegOrig=NULL;
1375  //memset: res->pLDegOrig=NULL;
1376  //memset: res->p_Setm=NULL;
1377  //memset: res->cf=NULL;
1378  res->options=r->options;
1379
1380/*
1381  if (r->extRing!=NULL)
1382    r->extRing->ref++;
1383 
1384  res->extRing=r->extRing; 
1385  //memset: res->minideal=NULL;
1386*/
1387 
1388 
1389  if (copy_ordering == TRUE)
1390  {
1391    i=rBlocks(r);
1392    res->wvhdl   = (int **)omAlloc(i * sizeof(int *));
1393    res->order   = (int *) omAlloc(i * sizeof(int));
1394    res->block0  = (int *) omAlloc(i * sizeof(int));
1395    res->block1  = (int *) omAlloc(i * sizeof(int));
1396    for (j=0; j<i; j++)
1397    {
1398      if (r->wvhdl[j]!=NULL)
1399      {
1400        res->wvhdl[j] = (int*) omMemDup(r->wvhdl[j]);
1401      }
1402      else
1403        res->wvhdl[j]=NULL;
1404    }
1405    memcpy(res->order,r->order,i * sizeof(int));
1406    memcpy(res->block0,r->block0,i * sizeof(int));
1407    memcpy(res->block1,r->block1,i * sizeof(int));
1408  }
1409  //memset: else
1410  //memset: {
1411  //memset:   res->wvhdl = NULL;
1412  //memset:   res->order = NULL;
1413  //memset:   res->block0 = NULL;
1414  //memset:   res->block1 = NULL;
1415  //memset: }
1416
1417  res->names   = (char **)omAlloc0(rVar(r) * sizeof(char *));
1418  for (i=0; i<rVar(res); i++)
1419  {
1420    res->names[i] = omStrDup(r->names[i]);
1421  }
1422  if (r->qideal!=NULL)
1423  {
1424    if (copy_qideal)
1425    {
1426      #ifndef NDEBUG
1427      if (!copy_ordering)
1428        WerrorS("internal error: rCopy0(Q,TRUE,FALSE)");
1429      else
1430      #endif
1431      {
1432      #ifndef NDEBUG
1433        WarnS("internal bad stuff: rCopy0(Q,TRUE,TRUE)");
1434      #endif
1435        rComplete(res);
1436        res->qideal= idrCopyR_NoSort(r->qideal, r, res);
1437        rUnComplete(res);
1438      }
1439    }
1440    //memset: else res->qideal = NULL;
1441  }
1442  //memset: else res->qideal = NULL;
1443  //memset: res->GetNC() = NULL; // copy is purely commutative!!!
1444  return res;
1445}
1446
1447/*2
1448 * create a copy of the ring r, which must be equivalent to currRing
1449 * used for qring definition,..
1450 * (i.e.: normal rings: same nCopy as currRing;
1451 *        qring:        same nCopy, same idCopy as currRing)
1452 */
1453ring rCopy(ring r)
1454{
1455  if (r == NULL) return NULL;
1456  ring res=rCopy0(r,FALSE,TRUE);
1457  rComplete(res, 1); // res is purely commutative so far
1458  if (r->qideal!=NULL) res->qideal=idrCopyR_NoSort(r->qideal, r, res);
1459
1460#ifdef HAVE_PLURAL
1461  if (rIsPluralRing(r))
1462    if( nc_rCopy(res, r, true) );
1463#endif
1464
1465  return res;
1466}
1467
1468// returns TRUE, if r1 equals r2 FALSE, otherwise Equality is
1469// determined componentwise, if qr == 1, then qrideal equality is
1470// tested, as well
1471BOOLEAN rEqual(ring r1, ring r2, BOOLEAN qr)
1472{
1473  int i, j;
1474
1475  if (r1 == r2) return TRUE;
1476
1477  if (r1 == NULL || r2 == NULL) return FALSE;
1478
1479  if ((r1->cf->type != r2->cf->type)
1480  || (r1->float_len != r2->float_len)
1481  || (r1->float_len2 != r2->float_len2)
1482  || (rVar(r1) != rVar(r2))
1483  || (r1->OrdSgn != r2->OrdSgn)
1484  || (rPar(r1) != rPar(r2)))
1485    return FALSE;
1486
1487  for (i=0; i<rVar(r1); i++)
1488  {
1489    if (r1->names[i] != NULL && r2->names[i] != NULL)
1490    {
1491      if (strcmp(r1->names[i], r2->names[i])) return FALSE;
1492    }
1493    else if ((r1->names[i] != NULL) ^ (r2->names[i] != NULL))
1494    {
1495      return FALSE;
1496    }
1497  }
1498
1499  i=0;
1500  while (r1->order[i] != 0)
1501  {
1502    if (r2->order[i] == 0) return FALSE;
1503    if ((r1->order[i] != r2->order[i])
1504    || (r1->block0[i] != r2->block0[i])
1505    || (r1->block1[i] != r2->block1[i]))
1506      return FALSE;
1507    if (r1->wvhdl[i] != NULL)
1508    {
1509      if (r2->wvhdl[i] == NULL)
1510        return FALSE;
1511      for (j=0; j<r1->block1[i]-r1->block0[i]+1; j++)
1512        if (r2->wvhdl[i][j] != r1->wvhdl[i][j])
1513          return FALSE;
1514    }
1515    else if (r2->wvhdl[i] != NULL) return FALSE;
1516    i++;
1517  }
1518  if (r2->order[i] != 0) return FALSE;
1519
1520  for (i=0; i<rPar(r1);i++)
1521  {
1522      if (strcmp(rParameter(r1)[i], rParameter(r2)[i])!=0)
1523        return FALSE;
1524  }
1525
1526  if ( !rMinpolyIsNULL(r1) )
1527  {
1528    if ( rMinpolyIsNULL(r2) ) return FALSE;
1529    if (! p_EqualPolys(r1->cf->extRing->minideal->m[0],
1530                  r2->cf->extRing->minideal->m[0], 
1531                  r1->cf->extRing))
1532      return FALSE;
1533  }
1534  else if (!rMinpolyIsNULL(r2)) return FALSE;
1535
1536  if (qr)
1537  {
1538    if (r1->qideal != NULL)
1539    {
1540      ideal id1 = r1->qideal, id2 = r2->qideal;
1541      int i, n;
1542      poly *m1, *m2;
1543
1544      if (id2 == NULL) return FALSE;
1545      if ((n = IDELEMS(id1)) != IDELEMS(id2)) return FALSE;
1546
1547      {
1548        m1 = id1->m;
1549        m2 = id2->m;
1550        for (i=0; i<n; i++)
1551          if (! p_EqualPolys(m1[i],m2[i],r1)) return FALSE;
1552      }
1553    }
1554    else if (r2->qideal != NULL) return FALSE;
1555  }
1556
1557  return TRUE;
1558}
1559
1560// returns TRUE, if r1 and r2 represents the monomials in the same way
1561// FALSE, otherwise
1562// this is an analogue to rEqual but not so strict
1563BOOLEAN rSamePolyRep(ring r1, ring r2)
1564{
1565  int i, j;
1566
1567  if (r1 == r2) return TRUE;
1568
1569  if (r1 == NULL || r2 == NULL) return FALSE;
1570
1571  if ((r1->cf->type != r2->cf->type)
1572  || (r1->float_len != r2->float_len)
1573  || (r1->float_len2 != r2->float_len2)
1574  || (rVar(r1) != rVar(r2))
1575  || (r1->OrdSgn != r2->OrdSgn)
1576  || (rPar(r1) != rPar(r2)))
1577    return FALSE;
1578
1579  if (rVar(r1)!=rVar(r2)) return FALSE;
1580  if (rPar(r1)!=rPar(r2)) return FALSE;
1581
1582  i=0;
1583  while (r1->order[i] != 0)
1584  {
1585    if (r2->order[i] == 0) return FALSE;
1586    if ((r1->order[i] != r2->order[i])
1587    || (r1->block0[i] != r2->block0[i])
1588    || (r1->block1[i] != r2->block1[i]))
1589      return FALSE;
1590    if (r1->wvhdl[i] != NULL)
1591    {
1592      if (r2->wvhdl[i] == NULL)
1593        return FALSE;
1594      for (j=0; j<r1->block1[i]-r1->block0[i]+1; j++)
1595        if (r2->wvhdl[i][j] != r1->wvhdl[i][j])
1596          return FALSE;
1597    }
1598    else if (r2->wvhdl[i] != NULL) return FALSE;
1599    i++;
1600  }
1601  if (r2->order[i] != 0) return FALSE;
1602
1603  // we do not check minpoly/minideal
1604  // we do not check qideal
1605
1606  return TRUE;
1607}
1608
1609rOrderType_t rGetOrderType(ring r)
1610{
1611  // check for simple ordering
1612  if (rHasSimpleOrder(r))
1613  {
1614    if ((r->order[1] == ringorder_c)
1615    || (r->order[1] == ringorder_C))
1616    {
1617      switch(r->order[0])
1618      {
1619          case ringorder_dp:
1620          case ringorder_wp:
1621          case ringorder_ds:
1622          case ringorder_ws:
1623          case ringorder_ls:
1624          case ringorder_unspec:
1625            if (r->order[1] == ringorder_C
1626            ||  r->order[0] == ringorder_unspec)
1627              return rOrderType_ExpComp;
1628            return rOrderType_Exp;
1629
1630          default:
1631            assume(r->order[0] == ringorder_lp ||
1632                   r->order[0] == ringorder_rs ||
1633                   r->order[0] == ringorder_Dp ||
1634                   r->order[0] == ringorder_Wp ||
1635                   r->order[0] == ringorder_Ds ||
1636                   r->order[0] == ringorder_Ws);
1637
1638            if (r->order[1] == ringorder_c) return rOrderType_ExpComp;
1639            return rOrderType_Exp;
1640      }
1641    }
1642    else
1643    {
1644      assume((r->order[0]==ringorder_c)||(r->order[0]==ringorder_C));
1645      return rOrderType_CompExp;
1646    }
1647  }
1648  else
1649    return rOrderType_General;
1650}
1651
1652BOOLEAN rHasSimpleOrder(const ring r)
1653{
1654  if (r->order[0] == ringorder_unspec) return TRUE;
1655  int blocks = rBlocks(r) - 1;
1656  assume(blocks >= 1);
1657  if (blocks == 1) return TRUE;
1658
1659  int s = 0;
1660  while( (s < blocks) && (r->order[s] == ringorder_IS) && (r->order[blocks-1] == ringorder_IS) )
1661  {
1662    s++;
1663    blocks--;
1664  }
1665
1666  if ((blocks - s) > 2)  return FALSE;
1667
1668  assume( blocks == s + 2 );
1669
1670  if (
1671     (r->order[s] != ringorder_c)
1672  && (r->order[s] != ringorder_C)
1673  && (r->order[s+1] != ringorder_c)
1674  && (r->order[s+1] != ringorder_C)
1675     )
1676    return FALSE;
1677  if ((r->order[s+1] == ringorder_M)
1678  || (r->order[s] == ringorder_M))
1679    return FALSE;
1680  return TRUE;
1681}
1682
1683// returns TRUE, if simple lp or ls ordering
1684BOOLEAN rHasSimpleLexOrder(const ring r)
1685{
1686  return rHasSimpleOrder(r) &&
1687    (r->order[0] == ringorder_ls ||
1688     r->order[0] == ringorder_lp ||
1689     r->order[1] == ringorder_ls ||
1690     r->order[1] == ringorder_lp);
1691}
1692
1693BOOLEAN rOrder_is_DegOrdering(const rRingOrder_t order)
1694{
1695  switch(order)
1696  {
1697      case ringorder_dp:
1698      case ringorder_Dp:
1699      case ringorder_ds:
1700      case ringorder_Ds:
1701      case ringorder_Ws:
1702      case ringorder_Wp:
1703      case ringorder_ws:
1704      case ringorder_wp:
1705        return TRUE;
1706
1707      default:
1708        return FALSE;
1709  }
1710}
1711
1712BOOLEAN rOrder_is_WeightedOrdering(rRingOrder_t order)
1713{
1714  switch(order)
1715  {
1716      case ringorder_Ws:
1717      case ringorder_Wp:
1718      case ringorder_ws:
1719      case ringorder_wp:
1720        return TRUE;
1721
1722      default:
1723        return FALSE;
1724  }
1725}
1726
1727BOOLEAN rHasSimpleOrderAA(ring r)
1728{
1729  if (r->order[0] == ringorder_unspec) return TRUE;
1730  int blocks = rBlocks(r) - 1;
1731  assume(blocks >= 1);
1732  if (blocks == 1) return TRUE;
1733
1734  int s = 0;
1735  while( (s < blocks) && (r->order[s] == ringorder_IS) && (r->order[blocks-1] == ringorder_IS) )
1736  {
1737    s++;
1738    blocks--;
1739  }
1740
1741  if ((blocks - s) > 3)  return FALSE;
1742
1743//  if ((blocks > 3) || (blocks < 2)) return FALSE;
1744  if ((blocks - s) == 3)
1745  {
1746    return (((r->order[s] == ringorder_aa) && (r->order[s+1] != ringorder_M) &&
1747             ((r->order[s+2] == ringorder_c) || (r->order[s+2] == ringorder_C))) ||
1748            (((r->order[s] == ringorder_c) || (r->order[s] == ringorder_C)) &&
1749             (r->order[s+1] == ringorder_aa) && (r->order[s+2] != ringorder_M)));
1750  }
1751  else
1752  {
1753    return ((r->order[s] == ringorder_aa) && (r->order[s+1] != ringorder_M));
1754  }
1755}
1756
1757// return TRUE if p_SetComp requires p_Setm
1758BOOLEAN rOrd_SetCompRequiresSetm(ring r)
1759{
1760  if (r->typ != NULL)
1761  {
1762    int pos;
1763    for (pos=0;pos<r->OrdSize;pos++)
1764    {
1765      sro_ord* o=&(r->typ[pos]);
1766      if ((o->ord_typ == ro_syzcomp) || (o->ord_typ == ro_syz) || (o->ord_typ == ro_is) || (o->ord_typ == ro_isTemp)) return TRUE;
1767    }
1768  }
1769  return FALSE;
1770}
1771
1772// return TRUE if p->exp[r->pOrdIndex] holds total degree of p */
1773BOOLEAN rOrd_is_Totaldegree_Ordering(ring r)
1774{
1775  // Hmm.... what about Syz orderings?
1776  return (rVar(r) > 1 &&
1777          ((rHasSimpleOrder(r) &&
1778           (rOrder_is_DegOrdering((rRingOrder_t)r->order[0]) ||
1779            rOrder_is_DegOrdering(( rRingOrder_t)r->order[1]))) ||
1780           (rHasSimpleOrderAA(r) &&
1781            (rOrder_is_DegOrdering((rRingOrder_t)r->order[1]) ||
1782             rOrder_is_DegOrdering((rRingOrder_t)r->order[2])))));
1783}
1784
1785// return TRUE if p->exp[r->pOrdIndex] holds a weighted degree of p */
1786BOOLEAN rOrd_is_WeightedDegree_Ordering(ring r )
1787{
1788  // Hmm.... what about Syz orderings?
1789  return ((rVar(r) > 1) &&
1790          rHasSimpleOrder(r) &&
1791          (rOrder_is_WeightedOrdering((rRingOrder_t)r->order[0]) ||
1792           rOrder_is_WeightedOrdering(( rRingOrder_t)r->order[1])));
1793}
1794
1795BOOLEAN rIsPolyVar(int v, ring r)
1796{
1797  int  i=0;
1798  while(r->order[i]!=0)
1799  {
1800    if((r->block0[i]<=v)
1801    && (r->block1[i]>=v))
1802    {
1803      switch(r->order[i])
1804      {
1805        case ringorder_a:
1806          return (r->wvhdl[i][v-r->block0[i]]>0);
1807        case ringorder_M:
1808          return 2; /*don't know*/
1809        case ringorder_a64: /* assume: all weight are non-negative!*/
1810        case ringorder_lp:
1811        case ringorder_rs:
1812        case ringorder_dp:
1813        case ringorder_Dp:
1814        case ringorder_wp:
1815        case ringorder_Wp:
1816          return TRUE;
1817        case ringorder_ls:
1818        case ringorder_ds:
1819        case ringorder_Ds:
1820        case ringorder_ws:
1821        case ringorder_Ws:
1822          return FALSE;
1823        default:
1824          break;
1825      }
1826    }
1827    i++;
1828  }
1829  return 3; /* could not find var v*/
1830}
1831
1832#ifdef RDEBUG
1833// This should eventually become a full-fledge ring check, like pTest
1834BOOLEAN rDBTest(ring r, const char* fn, const int l)
1835{
1836  int i,j;
1837
1838  if (r == NULL)
1839  {
1840    dReportError("Null ring in %s:%d", fn, l);
1841    return FALSE;
1842  }
1843
1844
1845  if (r->N == 0) return TRUE;
1846
1847//  omCheckAddrSize(r,sizeof(ip_sring));
1848#if OM_CHECK > 0
1849  i=rBlocks(r);
1850  omCheckAddrSize(r->order,i*sizeof(int));
1851  omCheckAddrSize(r->block0,i*sizeof(int));
1852  omCheckAddrSize(r->block1,i*sizeof(int));
1853  if (r->wvhdl!=NULL)
1854  {
1855    omCheckAddrSize(r->wvhdl,i*sizeof(int *));
1856    for (j=0;j<i; j++)
1857    {
1858      if (r->wvhdl[j] != NULL) omCheckAddr(r->wvhdl[j]);
1859    }
1860  }
1861#endif
1862  if (r->VarOffset == NULL)
1863  {
1864    dReportError("Null ring VarOffset -- no rComplete (?) in n %s:%d", fn, l);
1865    return FALSE;
1866  }
1867  omCheckAddrSize(r->VarOffset,(r->N+1)*sizeof(int));
1868
1869  if ((r->OrdSize==0)!=(r->typ==NULL))
1870  {
1871    dReportError("mismatch OrdSize and typ-pointer in %s:%d");
1872    return FALSE;
1873  }
1874  omcheckAddrSize(r->typ,r->OrdSize*sizeof(*(r->typ)));
1875  omCheckAddrSize(r->VarOffset,(r->N+1)*sizeof(*(r->VarOffset)));
1876  // test assumptions:
1877  for(i=0;i<=r->N;i++) // for all variables (i = 0..N)
1878  {
1879    if(r->typ!=NULL)
1880    {
1881      for(j=0;j<r->OrdSize;j++) // for all ordering blocks (j =0..OrdSize-1)
1882      {
1883        if(r->typ[j].ord_typ == ro_isTemp)
1884        {
1885          const int p = r->typ[j].data.isTemp.suffixpos;
1886
1887          if(p <= j)
1888            dReportError("ordrec prefix %d is unmatched",j);
1889
1890          assume( p < r->OrdSize );
1891
1892          if(r->typ[p].ord_typ != ro_is)
1893            dReportError("ordrec prefix %d is unmatched (suffix: %d is wrong!!!)",j, p);
1894
1895          // Skip all intermediate blocks for undone variables:
1896          if(r->typ[j].data.isTemp.pVarOffset[i] != -1) // Check i^th variable
1897          {
1898            j = p - 1; // SKIP ALL INTERNAL BLOCKS...???
1899            continue; // To make for check OrdSize bound...
1900          }
1901        }
1902        else if (r->typ[j].ord_typ == ro_is)
1903        {
1904          // Skip all intermediate blocks for undone variables:
1905          if(r->typ[j].data.is.pVarOffset[i] != -1)
1906          {
1907            // ???
1908          }
1909
1910        }
1911        else
1912        {
1913          if (r->typ[j].ord_typ==ro_cp)
1914          {
1915            if(((short)r->VarOffset[i]) == r->typ[j].data.cp.place)
1916              dReportError("ordrec %d conflicts with var %d",j,i);
1917          }
1918          else
1919            if ((r->typ[j].ord_typ!=ro_syzcomp)
1920            && (r->VarOffset[i] == r->typ[j].data.dp.place))
1921              dReportError("ordrec %d conflicts with var %d",j,i);
1922        }
1923      }
1924    }
1925    int tmp;
1926      tmp=r->VarOffset[i] & 0xffffff;
1927      #if SIZEOF_LONG == 8
1928        if ((r->VarOffset[i] >> 24) >63)
1929      #else
1930        if ((r->VarOffset[i] >> 24) >31)
1931      #endif
1932          dReportError("bit_start out of range:%d",r->VarOffset[i] >> 24);
1933      if (i > 0 && ((tmp<0) ||(tmp>r->ExpL_Size-1)))
1934      {
1935        dReportError("varoffset out of range for var %d: %d",i,tmp);
1936      }
1937  }
1938  if(r->typ!=NULL)
1939  {
1940    for(j=0;j<r->OrdSize;j++)
1941    {
1942      if ((r->typ[j].ord_typ==ro_dp)
1943      || (r->typ[j].ord_typ==ro_wp)
1944      || (r->typ[j].ord_typ==ro_wp_neg))
1945      {
1946        if (r->typ[j].data.dp.start > r->typ[j].data.dp.end)
1947          dReportError("in ordrec %d: start(%d) > end(%d)",j,
1948            r->typ[j].data.dp.start, r->typ[j].data.dp.end);
1949        if ((r->typ[j].data.dp.start < 1)
1950        || (r->typ[j].data.dp.end > r->N))
1951          dReportError("in ordrec %d: start(%d)<1 or end(%d)>vars(%d)",j,
1952            r->typ[j].data.dp.start, r->typ[j].data.dp.end,r->N);
1953      }
1954    }
1955  }
1956
1957  if (!rMinpolyIsNULL(r))
1958    omCheckAddr(r->cf->extRing->minideal->m[0]);
1959
1960  //assume(r->cf!=NULL);
1961
1962  return TRUE;
1963}
1964#endif
1965
1966static void rO_Align(int &place, int &bitplace)
1967{
1968  // increment place to the next aligned one
1969  // (count as Exponent_t,align as longs)
1970  if (bitplace!=BITS_PER_LONG)
1971  {
1972    place++;
1973    bitplace=BITS_PER_LONG;
1974  }
1975}
1976
1977static void rO_TDegree(int &place, int &bitplace, int start, int end,
1978    long *o, sro_ord &ord_struct)
1979{
1980  // degree (aligned) of variables v_start..v_end, ordsgn 1
1981  rO_Align(place,bitplace);
1982  ord_struct.ord_typ=ro_dp;
1983  ord_struct.data.dp.start=start;
1984  ord_struct.data.dp.end=end;
1985  ord_struct.data.dp.place=place;
1986  o[place]=1;
1987  place++;
1988  rO_Align(place,bitplace);
1989}
1990
1991static void rO_TDegree_neg(int &place, int &bitplace, int start, int end,
1992    long *o, sro_ord &ord_struct)
1993{
1994  // degree (aligned) of variables v_start..v_end, ordsgn -1
1995  rO_Align(place,bitplace);
1996  ord_struct.ord_typ=ro_dp;
1997  ord_struct.data.dp.start=start;
1998  ord_struct.data.dp.end=end;
1999  ord_struct.data.dp.place=place;
2000  o[place]=-1;
2001  place++;
2002  rO_Align(place,bitplace);
2003}
2004
2005static void rO_WDegree(int &place, int &bitplace, int start, int end,
2006    long *o, sro_ord &ord_struct, int *weights)
2007{
2008  // weighted degree (aligned) of variables v_start..v_end, ordsgn 1
2009  while((start<end) && (weights[0]==0)) { start++; weights++; }
2010  while((start<end) && (weights[end-start]==0)) { end--; }
2011  int i;
2012  int pure_tdeg=1;
2013  for(i=start;i<=end;i++)
2014  {
2015    if(weights[i-start]!=1)
2016    {
2017      pure_tdeg=0;
2018      break;
2019    }
2020  }
2021  if (pure_tdeg)
2022  {
2023    rO_TDegree(place,bitplace,start,end,o,ord_struct);
2024    return;
2025  }
2026  rO_Align(place,bitplace);
2027  ord_struct.ord_typ=ro_wp;
2028  ord_struct.data.wp.start=start;
2029  ord_struct.data.wp.end=end;
2030  ord_struct.data.wp.place=place;
2031  ord_struct.data.wp.weights=weights;
2032  o[place]=1;
2033  place++;
2034  rO_Align(place,bitplace);
2035  for(i=start;i<=end;i++)
2036  {
2037    if(weights[i-start]<0)
2038    {
2039      ord_struct.ord_typ=ro_wp_neg;
2040      break;
2041    }
2042  }
2043}
2044
2045static void rO_WDegree64(int &place, int &bitplace, int start, int end,
2046    long *o, sro_ord &ord_struct, int64 *weights)
2047{
2048  // weighted degree (aligned) of variables v_start..v_end, ordsgn 1,
2049  // reserved 2 places
2050  rO_Align(place,bitplace);
2051  ord_struct.ord_typ=ro_wp64;
2052  ord_struct.data.wp64.start=start;
2053  ord_struct.data.wp64.end=end;
2054  ord_struct.data.wp64.place=place;
2055  ord_struct.data.wp64.weights64=weights;
2056  o[place]=1;
2057  place++;
2058  o[place]=1;
2059  place++;
2060  rO_Align(place,bitplace);
2061}
2062
2063static void rO_WDegree_neg(int &place, int &bitplace, int start, int end,
2064    long *o, sro_ord &ord_struct, int *weights)
2065{
2066  // weighted degree (aligned) of variables v_start..v_end, ordsgn -1
2067  while((start<end) && (weights[0]==0)) { start++; weights++; }
2068  while((start<end) && (weights[end-start]==0)) { end--; }
2069  rO_Align(place,bitplace);
2070  ord_struct.ord_typ=ro_wp;
2071  ord_struct.data.wp.start=start;
2072  ord_struct.data.wp.end=end;
2073  ord_struct.data.wp.place=place;
2074  ord_struct.data.wp.weights=weights;
2075  o[place]=-1;
2076  place++;
2077  rO_Align(place,bitplace);
2078  int i;
2079  for(i=start;i<=end;i++)
2080  {
2081    if(weights[i-start]<0)
2082    {
2083      ord_struct.ord_typ=ro_wp_neg;
2084      break;
2085    }
2086  }
2087}
2088
2089static void rO_LexVars(int &place, int &bitplace, int start, int end,
2090  int &prev_ord, long *o,int *v, int bits, int opt_var)
2091{
2092  // a block of variables v_start..v_end with lex order, ordsgn 1
2093  int k;
2094  int incr=1;
2095  if(prev_ord==-1) rO_Align(place,bitplace);
2096
2097  if (start>end)
2098  {
2099    incr=-1;
2100  }
2101  for(k=start;;k+=incr)
2102  {
2103    bitplace-=bits;
2104    if (bitplace < 0) { bitplace=BITS_PER_LONG-bits; place++; }
2105    o[place]=1;
2106    v[k]= place | (bitplace << 24);
2107    if (k==end) break;
2108  }
2109  prev_ord=1;
2110  if (opt_var!= -1)
2111  {
2112    assume((opt_var == end+1) ||(opt_var == end-1));
2113    if((opt_var != end+1) &&(opt_var != end-1)) WarnS("hier-2");
2114    int save_bitplace=bitplace;
2115    bitplace-=bits;
2116    if (bitplace < 0)
2117    {
2118      bitplace=save_bitplace;
2119      return;
2120    }
2121    // there is enough space for the optional var
2122    v[opt_var]=place | (bitplace << 24);
2123  }
2124}
2125
2126static void rO_LexVars_neg(int &place, int &bitplace, int start, int end,
2127  int &prev_ord, long *o,int *v, int bits, int opt_var)
2128{
2129  // a block of variables v_start..v_end with lex order, ordsgn -1
2130  int k;
2131  int incr=1;
2132  if(prev_ord==1) rO_Align(place,bitplace);
2133
2134  if (start>end)
2135  {
2136    incr=-1;
2137  }
2138  for(k=start;;k+=incr)
2139  {
2140    bitplace-=bits;
2141    if (bitplace < 0) { bitplace=BITS_PER_LONG-bits; place++; }
2142    o[place]=-1;
2143    v[k]=place | (bitplace << 24);
2144    if (k==end) break;
2145  }
2146  prev_ord=-1;
2147//  #if 0
2148  if (opt_var!= -1)
2149  {
2150    assume((opt_var == end+1) ||(opt_var == end-1));
2151    if((opt_var != end+1) &&(opt_var != end-1)) WarnS("hier-1");
2152    int save_bitplace=bitplace;
2153    bitplace-=bits;
2154    if (bitplace < 0)
2155    {
2156      bitplace=save_bitplace;
2157      return;
2158    }
2159    // there is enough space for the optional var
2160    v[opt_var]=place | (bitplace << 24);
2161  }
2162//  #endif
2163}
2164
2165static void rO_Syzcomp(int &place, int &bitplace, int &prev_ord,
2166    long *o, sro_ord &ord_struct)
2167{
2168  // ordering is derived from component number
2169  rO_Align(place,bitplace);
2170  ord_struct.ord_typ=ro_syzcomp;
2171  ord_struct.data.syzcomp.place=place;
2172  ord_struct.data.syzcomp.Components=NULL;
2173  ord_struct.data.syzcomp.ShiftedComponents=NULL;
2174  o[place]=1;
2175  prev_ord=1;
2176  place++;
2177  rO_Align(place,bitplace);
2178}
2179
2180static void rO_Syz(int &place, int &bitplace, int &prev_ord,
2181    long *o, sro_ord &ord_struct)
2182{
2183  // ordering is derived from component number
2184  // let's reserve one Exponent_t for it
2185  if ((prev_ord== 1) || (bitplace!=BITS_PER_LONG))
2186    rO_Align(place,bitplace);
2187  ord_struct.ord_typ=ro_syz;
2188  ord_struct.data.syz.place=place;
2189  ord_struct.data.syz.limit=0;
2190  ord_struct.data.syz.syz_index = NULL;
2191  ord_struct.data.syz.curr_index = 1;
2192  o[place]= -1;
2193  prev_ord=-1;
2194  place++;
2195}
2196
2197#ifndef NDEBUG
2198# define MYTEST 0
2199#else /* ifndef NDEBUG */
2200# define MYTEST 0
2201#endif /* ifndef NDEBUG */
2202
2203static void rO_ISPrefix(int &place, int &bitplace, int &prev_ord,
2204    long *o, int /*N*/, int *v, sro_ord &ord_struct)
2205{
2206  if ((prev_ord== 1) || (bitplace!=BITS_PER_LONG))
2207    rO_Align(place,bitplace);
2208  // since we add something afterwards - it's better to start with anew!?
2209
2210  ord_struct.ord_typ = ro_isTemp;
2211  ord_struct.data.isTemp.start = place;
2212  ord_struct.data.isTemp.pVarOffset = (int *)omMemDup(v);
2213  ord_struct.data.isTemp.suffixpos = -1;
2214
2215  // We will act as rO_Syz on our own!!!
2216  // Here we allocate an exponent as a level placeholder
2217  o[place]= -1;
2218  prev_ord=-1;
2219  place++;
2220}
2221static void rO_ISSuffix(int &place, int &bitplace, int &prev_ord, long *o,
2222  int N, int *v, sro_ord *tmp_typ, int &typ_i, int sgn)
2223{
2224
2225  // Let's find previous prefix:
2226  int typ_j = typ_i - 1;
2227  while(typ_j >= 0)
2228  {
2229    if( tmp_typ[typ_j].ord_typ == ro_isTemp)
2230      break;
2231    typ_j --;
2232  }
2233
2234  assume( typ_j >= 0 );
2235
2236  if( typ_j < 0 ) // Found NO prefix!!! :(
2237    return;
2238
2239  assume( tmp_typ[typ_j].ord_typ == ro_isTemp );
2240
2241  // Get saved state:
2242  const int start = tmp_typ[typ_j].data.isTemp.start;
2243  int *pVarOffset = tmp_typ[typ_j].data.isTemp.pVarOffset;
2244
2245/*
2246  // shift up all blocks
2247  while(typ_j < (typ_i-1))
2248  {
2249    tmp_typ[typ_j] = tmp_typ[typ_j+1];
2250    typ_j++;
2251  }
2252  typ_j = typ_i - 1; // No increment for typ_i
2253*/
2254  tmp_typ[typ_j].data.isTemp.suffixpos = typ_i;
2255
2256  // Let's keep that dummy for now...
2257  typ_j = typ_i; // the typ to change!
2258  typ_i++; // Just for now...
2259
2260
2261  for( int i = 0; i <= N; i++ ) // Note [0] == component !!! No Skip?
2262  {
2263    // Was i-th variable allocated inbetween?
2264    if( v[i] != pVarOffset[i] )
2265    {
2266      pVarOffset[i] = v[i]; // Save for later...
2267      v[i] = -1; // Undo!
2268      assume( pVarOffset[i] != -1 );
2269    }
2270    else
2271      pVarOffset[i] = -1; // No change here...
2272  }
2273
2274  if( pVarOffset[0] != -1 )
2275    pVarOffset[0] &= 0x0fff;
2276
2277  sro_ord &ord_struct = tmp_typ[typ_j];
2278
2279
2280  ord_struct.ord_typ = ro_is;
2281  ord_struct.data.is.start = start;
2282  ord_struct.data.is.end   = place;
2283  ord_struct.data.is.pVarOffset = pVarOffset;
2284
2285
2286  // What about component???
2287//   if( v[0] != -1 ) // There is a component already...???
2288//     if( o[ v[0] & 0x0fff ] == sgn )
2289//     {
2290//       pVarOffset[0] = -1; // NEVER USED Afterwards...
2291//       return;
2292//     }
2293
2294
2295  // Moreover: we need to allocate the module component (v[0]) here!
2296  if( v[0] == -1) // It's possible that there was module component v0 at the begining (before prefix)!
2297  {
2298    // Start with a whole long exponent
2299    if( bitplace != BITS_PER_LONG )
2300      rO_Align(place, bitplace);
2301
2302    assume( bitplace == BITS_PER_LONG );
2303    bitplace -= BITS_PER_LONG;
2304    assume(bitplace == 0);
2305    v[0] = place | (bitplace << 24); // Never mind whether pVarOffset[0] > 0!!!
2306    o[place] = sgn; // Singnum for component ordering
2307    prev_ord = sgn;
2308  }
2309}
2310
2311
2312static unsigned long rGetExpSize(unsigned long bitmask, int & bits)
2313{
2314  if (bitmask == 0)
2315  {
2316    bits=16; bitmask=0xffff;
2317  }
2318  else if (bitmask <= 1L)
2319  {
2320    bits=1; bitmask = 1L;
2321  }
2322  else if (bitmask <= 3L)
2323  {
2324    bits=2; bitmask = 3L;
2325  }
2326  else if (bitmask <= 7L)
2327  {
2328    bits=3; bitmask=7L;
2329  }
2330  else if (bitmask <= 0xfL)
2331  {
2332    bits=4; bitmask=0xfL;
2333  }
2334  else if (bitmask <= 0x1fL)
2335  {
2336    bits=5; bitmask=0x1fL;
2337  }
2338  else if (bitmask <= 0x3fL)
2339  {
2340    bits=6; bitmask=0x3fL;
2341  }
2342#if SIZEOF_LONG == 8
2343  else if (bitmask <= 0x7fL)
2344  {
2345    bits=7; bitmask=0x7fL; /* 64 bit longs only */
2346  }
2347#endif
2348  else if (bitmask <= 0xffL)
2349  {
2350    bits=8; bitmask=0xffL;
2351  }
2352#if SIZEOF_LONG == 8
2353  else if (bitmask <= 0x1ffL)
2354  {
2355    bits=9; bitmask=0x1ffL; /* 64 bit longs only */
2356  }
2357#endif
2358  else if (bitmask <= 0x3ffL)
2359  {
2360    bits=10; bitmask=0x3ffL;
2361  }
2362#if SIZEOF_LONG == 8
2363  else if (bitmask <= 0xfffL)
2364  {
2365    bits=12; bitmask=0xfff; /* 64 bit longs only */
2366  }
2367#endif
2368  else if (bitmask <= 0xffffL)
2369  {
2370    bits=16; bitmask=0xffffL;
2371  }
2372#if SIZEOF_LONG == 8
2373  else if (bitmask <= 0xfffffL)
2374  {
2375    bits=20; bitmask=0xfffffL; /* 64 bit longs only */
2376  }
2377  else if (bitmask <= 0xffffffffL)
2378  {
2379    bits=32; bitmask=0xffffffffL;
2380  }
2381  else if (bitmask <= 0x7fffffffffffffffL)
2382  {
2383    bits=63; bitmask=0x7fffffffffffffffL; /* for overflow tests*/
2384  }
2385  else
2386  {
2387    bits=63; bitmask=0x7fffffffffffffffL; /* for overflow tests*/
2388  }
2389#else
2390  else if (bitmask <= 0x7fffffff)
2391  {
2392    bits=31; bitmask=0x7fffffff; /* for overflow tests*/
2393  }
2394  else
2395  {
2396    bits=31; bitmask=0x7fffffffL; /* for overflow tests*/
2397  }
2398#endif
2399  return bitmask;
2400}
2401
2402/*2
2403* optimize rGetExpSize for a block of N variables, exp <=bitmask
2404*/
2405static unsigned long rGetExpSize(unsigned long bitmask, int & bits, int N)
2406{
2407  bitmask =rGetExpSize(bitmask, bits);
2408  int vars_per_long=BIT_SIZEOF_LONG/bits;
2409  int bits1;
2410  loop
2411  {
2412    if (bits == BIT_SIZEOF_LONG-1)
2413    {
2414      bits =  BIT_SIZEOF_LONG - 1;
2415      return LONG_MAX;
2416    }
2417    unsigned long bitmask1 =rGetExpSize(bitmask+1, bits1);
2418    int vars_per_long1=BIT_SIZEOF_LONG/bits1;
2419    if ((((N+vars_per_long-1)/vars_per_long) ==
2420         ((N+vars_per_long1-1)/vars_per_long1)))
2421    {
2422      vars_per_long=vars_per_long1;
2423      bits=bits1;
2424      bitmask=bitmask1;
2425    }
2426    else
2427    {
2428      return bitmask; /* and bits */
2429    }
2430  }
2431}
2432
2433
2434/*2
2435 * create a copy of the ring r, which must be equivalent to currRing
2436 * used for std computations
2437 * may share data structures with currRing
2438 * DOES CALL rComplete
2439 */
2440ring rModifyRing(ring r, BOOLEAN omit_degree,
2441                         BOOLEAN omit_comp,
2442                         unsigned long exp_limit)
2443{
2444  assume (r != NULL );
2445  assume (exp_limit > 1);
2446  BOOLEAN need_other_ring;
2447  BOOLEAN omitted_degree = FALSE;
2448
2449  int iNeedInducedOrderingSetup = 0; ///< How many induced ordering block do we have?
2450  int bits;
2451
2452  exp_limit=rGetExpSize(exp_limit, bits, r->N);
2453  need_other_ring = (exp_limit != r->bitmask);
2454
2455  int nblocks=rBlocks(r);
2456  int *order=(int*)omAlloc0((nblocks+1)*sizeof(int));
2457  int *block0=(int*)omAlloc0((nblocks+1)*sizeof(int));
2458  int *block1=(int*)omAlloc0((nblocks+1)*sizeof(int));
2459  int **wvhdl=(int**)omAlloc0((nblocks+1)*sizeof(int *));
2460
2461  int i=0;
2462  int j=0; /*  i index in r, j index in res */
2463
2464  for( int r_ord=r->order[i]; (r_ord != 0) && (i < nblocks); j++, r_ord=r->order[++i])
2465  {
2466    BOOLEAN copy_block_index=TRUE;
2467
2468    if (r->block0[i]==r->block1[i])
2469    {
2470      switch(r_ord)
2471      {
2472        case ringorder_wp:
2473        case ringorder_dp:
2474        case ringorder_Wp:
2475        case ringorder_Dp:
2476          r_ord=ringorder_lp;
2477          break;
2478        case ringorder_Ws:
2479        case ringorder_Ds:
2480        case ringorder_ws:
2481        case ringorder_ds:
2482          r_ord=ringorder_ls;
2483          break;
2484        default:
2485          break;
2486      }
2487    }
2488    switch(r_ord)
2489    {
2490      case ringorder_S:
2491      {
2492#ifndef NDEBUG
2493        Warn("Error: unhandled ordering in rModifyRing: ringorder_S = [%d]", r_ord);
2494#endif
2495        order[j]=r_ord; /*r->order[i];*/
2496        break;
2497      }
2498      case ringorder_C:
2499      case ringorder_c:
2500        if (!omit_comp)
2501        {
2502          order[j]=r_ord; /*r->order[i]*/;
2503        }
2504        else
2505        {
2506          j--;
2507          need_other_ring=TRUE;
2508          omit_comp=FALSE;
2509          copy_block_index=FALSE;
2510        }
2511        break;
2512      case ringorder_wp:
2513      case ringorder_dp:
2514      case ringorder_ws:
2515      case ringorder_ds:
2516        if(!omit_degree)
2517        {
2518          order[j]=r_ord; /*r->order[i]*/;
2519        }
2520        else
2521        {
2522          order[j]=ringorder_rs;
2523          need_other_ring=TRUE;
2524          omit_degree=FALSE;
2525          omitted_degree = TRUE;
2526        }
2527        break;
2528      case ringorder_Wp:
2529      case ringorder_Dp:
2530      case ringorder_Ws:
2531      case ringorder_Ds:
2532        if(!omit_degree)
2533        {
2534          order[j]=r_ord; /*r->order[i];*/
2535        }
2536        else
2537        {
2538          order[j]=ringorder_lp;
2539          need_other_ring=TRUE;
2540          omit_degree=FALSE;
2541          omitted_degree = TRUE;
2542        }
2543        break;
2544      case ringorder_IS:
2545      {
2546        if (omit_comp)
2547        {
2548#ifndef NDEBUG
2549          Warn("Error: WRONG USAGE of rModifyRing: cannot omit component due to the ordering block [%d]: %d (ringorder_IS)", i, r_ord);
2550#endif
2551          omit_comp = FALSE;
2552        }
2553        order[j]=r_ord; /*r->order[i];*/
2554        iNeedInducedOrderingSetup++;
2555        break;
2556      }
2557      case ringorder_s:
2558      {
2559        assume((i == 0) && (j == 0));
2560        if (omit_comp)
2561        {
2562#ifndef NDEBUG
2563          Warn("WRONG USAGE? of rModifyRing: omitting component due to the ordering block [%d]: %d (ringorder_s)", i, r_ord);
2564#endif
2565          omit_comp = FALSE;
2566        }
2567        order[j]=r_ord; /*r->order[i];*/
2568        break;
2569      }
2570      default:
2571        order[j]=r_ord; /*r->order[i];*/
2572        break;
2573    }
2574    if (copy_block_index)
2575    {
2576      block0[j]=r->block0[i];
2577      block1[j]=r->block1[i];
2578      wvhdl[j]=r->wvhdl[i];
2579    }
2580
2581    // order[j]=ringorder_no; //  done by omAlloc0
2582  }
2583  if(!need_other_ring)
2584  {
2585    omFreeSize(order,(nblocks+1)*sizeof(int));
2586    omFreeSize(block0,(nblocks+1)*sizeof(int));
2587    omFreeSize(block1,(nblocks+1)*sizeof(int));
2588    omFreeSize(wvhdl,(nblocks+1)*sizeof(int *));
2589    return r;
2590  }
2591  ring res=(ring)omAlloc0Bin(sip_sring_bin);
2592  *res = *r;
2593
2594#ifdef HAVE_PLURAL
2595  res->GetNC() = NULL;
2596#endif
2597
2598  // res->qideal, res->idroot ???
2599  res->wvhdl=wvhdl;
2600  res->order=order;
2601  res->block0=block0;
2602  res->block1=block1;
2603  res->bitmask=exp_limit;
2604  int tmpref=r->cf->ref;
2605  rComplete(res, 1);
2606  r->cf->ref=tmpref;
2607
2608  // adjust res->pFDeg: if it was changed globally, then
2609  // it must also be changed for new ring
2610  if (r->pFDegOrig != res->pFDegOrig &&
2611           rOrd_is_WeightedDegree_Ordering(r))
2612  {
2613    // still might need adjustment for weighted orderings
2614    // and omit_degree
2615    res->firstwv = r->firstwv;
2616    res->firstBlockEnds = r->firstBlockEnds;
2617    res->pFDeg = res->pFDegOrig = p_WFirstTotalDegree;
2618  }
2619  if (omitted_degree)
2620    res->pLDeg = res->pLDegOrig = r->pLDegOrig;
2621
2622  rOptimizeLDeg(res);
2623
2624  // set syzcomp
2625  if (res->typ != NULL)
2626  {
2627    if( res->typ[0].ord_typ == ro_syz) // "s" Always on [0] place!
2628    {
2629      res->typ[0] = r->typ[0]; // Copy struct!? + setup the same limit!
2630
2631      if (r->typ[0].data.syz.limit > 0)
2632      {
2633        res->typ[0].data.syz.syz_index
2634          = (int*) omAlloc((r->typ[0].data.syz.limit +1)*sizeof(int));
2635        memcpy(res->typ[0].data.syz.syz_index, r->typ[0].data.syz.syz_index,
2636              (r->typ[0].data.syz.limit +1)*sizeof(int));
2637      }
2638    }
2639
2640    if( iNeedInducedOrderingSetup > 0 )
2641    {
2642      for(j = 0, i = 0; (i < nblocks) && (iNeedInducedOrderingSetup > 0); i++)
2643        if( res->typ[i].ord_typ == ro_is ) // Search for suffixes!
2644        {
2645          ideal F = idrHeadR(r->typ[i].data.is.F, r, res); // Copy F from r into res!
2646          assume(
2647            rSetISReference( res,
2648              F,  // WILL BE COPIED!
2649              r->typ[i].data.is.limit,
2650              j++,
2651              r->typ[i].data.is.componentWeights // WILL BE COPIED
2652              )
2653            );
2654          id_Delete(&F, res);
2655          iNeedInducedOrderingSetup--;
2656        }
2657    } // Process all induced Ordering blocks! ...
2658  }
2659  // the special case: homog (omit_degree) and 1 block rs: that is global:
2660  // it comes from dp
2661  res->OrdSgn=r->OrdSgn;
2662
2663
2664#ifdef HAVE_PLURAL
2665  if (rIsPluralRing(r))
2666  {
2667    if ( nc_rComplete(r, res, false) ) // no qideal!
2668    {
2669#ifndef NDEBUG
2670      WarnS("error in nc_rComplete");
2671#endif
2672      // cleanup?
2673
2674//      rDelete(res);
2675//      return r;
2676
2677      // just go on..
2678    }
2679
2680    if( rIsSCA(r) )
2681    {
2682      if( !sca_Force(res, scaFirstAltVar(r), scaLastAltVar(r)) )
2683      WarnS("error in sca_Force!");
2684    }
2685  }
2686#endif
2687
2688  return res;
2689}
2690
2691// construct Wp,C ring
2692ring rModifyRing_Wp(ring r, int* weights)
2693{
2694  ring res=(ring)omAlloc0Bin(sip_sring_bin);
2695  *res = *r;
2696#ifdef HAVE_PLURAL
2697  res->GetNC() = NULL;
2698#endif
2699
2700  /*weights: entries for 3 blocks: NULL*/
2701  res->wvhdl = (int **)omAlloc0(3 * sizeof(int *));
2702  /*order: Wp,C,0*/
2703  res->order = (int *) omAlloc(3 * sizeof(int *));
2704  res->block0 = (int *)omAlloc0(3 * sizeof(int *));
2705  res->block1 = (int *)omAlloc0(3 * sizeof(int *));
2706  /* ringorder Wp for the first block: var 1..r->N */
2707  res->order[0]  = ringorder_Wp;
2708  res->block0[0] = 1;
2709  res->block1[0] = r->N;
2710  res->wvhdl[0] = weights;
2711  /* ringorder C for the second block: no vars */
2712  res->order[1]  = ringorder_C;
2713  /* the last block: everything is 0 */
2714  res->order[2]  = 0;
2715  /*polynomial ring*/
2716  res->OrdSgn    = 1;
2717
2718  int tmpref=r->cf->ref;
2719  rComplete(res, 1);
2720  r->cf->ref=tmpref;
2721#ifdef HAVE_PLURAL
2722  if (rIsPluralRing(r))
2723  {
2724    if ( nc_rComplete(r, res, false) ) // no qideal!
2725    {
2726#ifndef NDEBUG
2727      WarnS("error in nc_rComplete");
2728#endif
2729      // cleanup?
2730
2731//      rDelete(res);
2732//      return r;
2733
2734      // just go on..
2735    }
2736  }
2737#endif
2738  return res;
2739}
2740
2741// construct lp, C ring with r->N variables, r->names vars....
2742ring rModifyRing_Simple(ring r, BOOLEAN ommit_degree, BOOLEAN ommit_comp, unsigned long exp_limit, BOOLEAN &simple)
2743{
2744  simple=TRUE;
2745  if (!rHasSimpleOrder(r))
2746  {
2747    simple=FALSE; // sorting needed
2748    assume (r != NULL );
2749    assume (exp_limit > 1);
2750    int bits;
2751
2752    exp_limit=rGetExpSize(exp_limit, bits, r->N);
2753
2754    int nblocks=1+(ommit_comp!=0);
2755    int *order=(int*)omAlloc0((nblocks+1)*sizeof(int));
2756    int *block0=(int*)omAlloc0((nblocks+1)*sizeof(int));
2757    int *block1=(int*)omAlloc0((nblocks+1)*sizeof(int));
2758    int **wvhdl=(int**)omAlloc0((nblocks+1)*sizeof(int *));
2759
2760    order[0]=ringorder_lp;
2761    block0[0]=1;
2762    block1[0]=r->N;
2763    if (!ommit_comp)
2764    {
2765      order[1]=ringorder_C;
2766    }
2767    ring res=(ring)omAlloc0Bin(sip_sring_bin);
2768    *res = *r;
2769#ifdef HAVE_PLURAL
2770    res->GetNC() = NULL;
2771#endif
2772    // res->qideal, res->idroot ???
2773    res->wvhdl=wvhdl;
2774    res->order=order;
2775    res->block0=block0;
2776    res->block1=block1;
2777    res->bitmask=exp_limit;
2778    int tmpref=r->cf->ref;
2779    rComplete(res, 1);
2780    r->cf->ref=tmpref;
2781
2782#ifdef HAVE_PLURAL
2783    if (rIsPluralRing(r))
2784    {
2785      if ( nc_rComplete(r, res, false) ) // no qideal!
2786      {
2787#ifndef NDEBUG
2788        WarnS("error in nc_rComplete");
2789#endif
2790        // cleanup?
2791
2792//      rDelete(res);
2793//      return r;
2794
2795      // just go on..
2796      }
2797    }
2798#endif
2799
2800    rOptimizeLDeg(res);
2801
2802    return res;
2803  }
2804  return rModifyRing(r, ommit_degree, ommit_comp, exp_limit);
2805}
2806
2807void rKillModifiedRing_Simple(ring r)
2808{
2809  rKillModifiedRing(r);
2810}
2811
2812
2813void rKillModifiedRing(ring r)
2814{
2815  rUnComplete(r);
2816  omFree(r->order);
2817  omFree(r->block0);
2818  omFree(r->block1);
2819  omFree(r->wvhdl);
2820  omFreeBin(r,sip_sring_bin);
2821}
2822
2823void rKillModified_Wp_Ring(ring r)
2824{
2825  rUnComplete(r);
2826  omFree(r->order);
2827  omFree(r->block0);
2828  omFree(r->block1);
2829  omFree(r->wvhdl[0]);
2830  omFree(r->wvhdl);
2831  omFreeBin(r,sip_sring_bin);
2832}
2833
2834static void rSetOutParams(ring r)
2835{
2836  r->VectorOut = (r->order[0] == ringorder_c);
2837  r->ShortOut = TRUE;
2838  {
2839    int i;
2840    if (rParameter(r)!=NULL)
2841    {
2842      for (i=0;i<rPar(r);i++)
2843      {
2844        if(strlen(rParameter(r)[i])>1)
2845        {
2846          r->ShortOut=FALSE;
2847          break;
2848        }
2849      }
2850    }
2851    if (r->ShortOut)
2852    {
2853      // Hmm... sometimes (e.g., from maGetPreimage) new variables
2854      // are introduced, but their names are never set
2855      // hence, we do the following awkward trick
2856      int N = omSizeWOfAddr(r->names);
2857      if (r->N < N) N = r->N;
2858
2859      for (i=(N-1);i>=0;i--)
2860      {
2861        if(r->names[i] != NULL && strlen(r->names[i])>1)
2862        {
2863          r->ShortOut=FALSE;
2864          break;
2865        }
2866      }
2867    }
2868  }
2869  r->CanShortOut = r->ShortOut;
2870}
2871
2872/*2
2873* sets r->MixedOrder and r->ComponentOrder for orderings with more than one block
2874* block of variables (ip is the block number, o_r the number of the ordering)
2875* o is the position of the orderingering in r
2876*/
2877static void rHighSet(ring r, int o_r, int o)
2878{
2879  switch(o_r)
2880  {
2881    case ringorder_lp:
2882    case ringorder_dp:
2883    case ringorder_Dp:
2884    case ringorder_wp:
2885    case ringorder_Wp:
2886    case ringorder_rp:
2887    case ringorder_a:
2888    case ringorder_aa:
2889    case ringorder_a64:
2890      if (r->OrdSgn==-1) r->MixedOrder=TRUE;
2891      break;
2892    case ringorder_ls:
2893    case ringorder_rs:
2894    case ringorder_ds:
2895    case ringorder_Ds:
2896    case ringorder_s:
2897      break;
2898    case ringorder_ws:
2899    case ringorder_Ws:
2900      if (r->wvhdl[o]!=NULL)
2901      {
2902        int i;
2903        for(i=r->block1[o]-r->block0[o];i>=0;i--)
2904          if (r->wvhdl[o][i]<0) { r->MixedOrder=TRUE; break; }
2905      }
2906      break;
2907    case ringorder_c:
2908      r->ComponentOrder=1;
2909      break;
2910    case ringorder_C:
2911    case ringorder_S:
2912      r->ComponentOrder=-1;
2913      break;
2914    case ringorder_M:
2915      r->LexOrder=TRUE;
2916      break;
2917    case ringorder_IS:
2918    { // TODO: What is r->ComponentOrder???
2919      r->MixedOrder=TRUE;
2920/*
2921      if( r->block0[o] != 0 ) // Suffix has the comonent
2922        r->ComponentOrder = r->block0[o];
2923      else // Prefix has level...
2924        r->ComponentOrder=-1;
2925*/
2926      break;
2927    }
2928
2929    default:
2930      dReportError("wrong internal ordering:%d at %s, l:%d\n",o_r,__FILE__,__LINE__);
2931  }
2932}
2933
2934static void rSetFirstWv(ring r, int i, int* order, int* block1, int** wvhdl)
2935{
2936  // cheat for ringorder_aa
2937  if (order[i] == ringorder_aa)
2938    i++;
2939  if(block1[i]!=r->N) r->LexOrder=TRUE;
2940  r->firstBlockEnds=block1[i];
2941  r->firstwv = wvhdl[i];
2942  if ((order[i]== ringorder_ws)
2943  || (order[i]==ringorder_Ws)
2944  || (order[i]== ringorder_wp)
2945  || (order[i]==ringorder_Wp)
2946  || (order[i]== ringorder_a)
2947   /*|| (order[i]==ringorder_A)*/)
2948  {
2949    int j;
2950    for(j=block1[i]-r->block0[i];j>=0;j--)
2951    {
2952      if (r->firstwv[j]<0) r->MixedOrder=TRUE;
2953      if (r->firstwv[j]==0) r->LexOrder=TRUE;
2954    }
2955  }
2956  else if (order[i]==ringorder_a64)
2957  {
2958    int j;
2959    int64 *w=rGetWeightVec(r);
2960    for(j=block1[i]-r->block0[i];j>=0;j--)
2961    {
2962      if (w[j]==0) r->LexOrder=TRUE;
2963    }
2964  }
2965}
2966
2967static void rOptimizeLDeg(ring r)
2968{
2969  if (r->pFDeg == p_Deg)
2970  {
2971    if (r->pLDeg == pLDeg1)
2972      r->pLDeg = pLDeg1_Deg;
2973    if (r->pLDeg == pLDeg1c)
2974      r->pLDeg = pLDeg1c_Deg;
2975  }
2976  else if (r->pFDeg == p_Totaldegree)
2977  {
2978    if (r->pLDeg == pLDeg1)
2979      r->pLDeg = pLDeg1_Totaldegree;
2980    if (r->pLDeg == pLDeg1c)
2981      r->pLDeg = pLDeg1c_Totaldegree;
2982  }
2983  else if (r->pFDeg == p_WFirstTotalDegree)
2984  {
2985    if (r->pLDeg == pLDeg1)
2986      r->pLDeg = pLDeg1_WFirstTotalDegree;
2987    if (r->pLDeg == pLDeg1c)
2988      r->pLDeg = pLDeg1c_WFirstTotalDegree;
2989  }
2990}
2991
2992// set pFDeg, pLDeg, MixOrder, ComponentOrder, etc
2993static void rSetDegStuff(ring r)
2994{
2995  int* order = r->order;
2996  int* block0 = r->block0;
2997  int* block1 = r->block1;
2998  int** wvhdl = r->wvhdl;
2999
3000  if (order[0]==ringorder_S ||order[0]==ringorder_s || order[0]==ringorder_IS)
3001  {
3002    order++;
3003    block0++;
3004    block1++;
3005    wvhdl++;
3006  }
3007  r->LexOrder = FALSE;
3008  r->MixedOrder = FALSE;
3009  r->ComponentOrder = 1;
3010  r->pFDeg = p_Totaldegree;
3011  r->pLDeg = (r->OrdSgn == 1 ? pLDegb : pLDeg0);
3012
3013  /*======== ordering type is (_,c) =========================*/
3014  if ((order[0]==ringorder_unspec) || (order[1] == 0)
3015      ||(
3016    ((order[1]==ringorder_c)||(order[1]==ringorder_C)
3017     ||(order[1]==ringorder_S)
3018     ||(order[1]==ringorder_s))
3019    && (order[0]!=ringorder_M)
3020    && (order[2]==0))
3021    )
3022  {
3023    if ((order[0]!=ringorder_unspec)
3024    && ((order[1]==ringorder_C)||(order[1]==ringorder_S)||
3025        (order[1]==ringorder_s)))
3026      r->ComponentOrder=-1;
3027    if (r->OrdSgn == -1) r->pLDeg = pLDeg0c;
3028    if ((order[0] == ringorder_lp)
3029    || (order[0] == ringorder_ls)
3030    || (order[0] == ringorder_rp)
3031    || (order[0] == ringorder_rs))
3032    {
3033      r->LexOrder=TRUE;
3034      r->pLDeg = pLDeg1c;
3035      r->pFDeg = p_Totaldegree;
3036    }
3037    if ((order[0] == ringorder_a)
3038    || (order[0] == ringorder_wp)
3039    || (order[0] == ringorder_Wp)
3040    || (order[0] == ringorder_ws)
3041    || (order[0] == ringorder_Ws))
3042      r->pFDeg = p_WFirstTotalDegree;
3043    r->firstBlockEnds=block1[0];
3044    r->firstwv = wvhdl[0];
3045  }
3046  /*======== ordering type is (c,_) =========================*/
3047  else if (((order[0]==ringorder_c)
3048            ||(order[0]==ringorder_C)
3049            ||(order[0]==ringorder_S)
3050            ||(order[0]==ringorder_s))
3051  && (order[1]!=ringorder_M)
3052  &&  (order[2]==0))
3053  {
3054    if ((order[0]==ringorder_C)||(order[0]==ringorder_S)||
3055        order[0]==ringorder_s)
3056      r->ComponentOrder=-1;
3057    if ((order[1] == ringorder_lp)
3058    || (order[1] == ringorder_ls)
3059    || (order[1] == ringorder_rp)
3060    || order[1] == ringorder_rs)
3061    {
3062      r->LexOrder=TRUE;
3063      r->pLDeg = pLDeg1c;
3064      r->pFDeg = p_Totaldegree;
3065    }
3066    r->firstBlockEnds=block1[1];
3067    if (wvhdl!=NULL) r->firstwv = wvhdl[1];
3068    if ((order[1] == ringorder_a)
3069    || (order[1] == ringorder_wp)
3070    || (order[1] == ringorder_Wp)
3071    || (order[1] == ringorder_ws)
3072    || (order[1] == ringorder_Ws))
3073      r->pFDeg = p_WFirstTotalDegree;
3074  }
3075  /*------- more than one block ----------------------*/
3076  else
3077  {
3078    if ((r->VectorOut)||(order[0]==ringorder_C)||(order[0]==ringorder_S)||(order[0]==ringorder_s))
3079    {
3080      rSetFirstWv(r, 1, order, block1, wvhdl);
3081    }
3082    else
3083      rSetFirstWv(r, 0, order, block1, wvhdl);
3084
3085    /*the number of orderings:*/
3086    int i = 0;
3087    while (order[++i] != 0);
3088    do
3089    {
3090      i--;
3091      rHighSet(r, order[i],i);
3092    }
3093    while (i != 0);
3094
3095    if ((order[0]!=ringorder_c)
3096        && (order[0]!=ringorder_C)
3097        && (order[0]!=ringorder_S)
3098        && (order[0]!=ringorder_s))
3099    {
3100      r->pLDeg = pLDeg1c;
3101    }
3102    else
3103    {
3104      r->pLDeg = pLDeg1;
3105    }
3106    r->pFDeg = p_WTotaldegree; // may be improved: p_Totaldegree for lp/dp/ls/.. blocks
3107  }
3108
3109  if (rOrd_is_Totaldegree_Ordering(r) || rOrd_is_WeightedDegree_Ordering(r))
3110    r->pFDeg = p_Deg;
3111
3112  r->pFDegOrig = r->pFDeg;
3113  r->pLDegOrig = r->pLDeg;
3114  rOptimizeLDeg(r);
3115}
3116
3117/*2
3118* set NegWeightL_Size, NegWeightL_Offset
3119*/
3120static void rSetNegWeight(ring r)
3121{
3122  int i,l;
3123  if (r->typ!=NULL)
3124  {
3125    l=0;
3126    for(i=0;i<r->OrdSize;i++)
3127    {
3128      if(r->typ[i].ord_typ==ro_wp_neg) l++;
3129    }
3130    if (l>0)
3131    {
3132      r->NegWeightL_Size=l;
3133      r->NegWeightL_Offset=(int *) omAlloc(l*sizeof(int));
3134      l=0;
3135      for(i=0;i<r->OrdSize;i++)
3136      {
3137        if(r->typ[i].ord_typ==ro_wp_neg)
3138        {
3139          r->NegWeightL_Offset[l]=r->typ[i].data.wp.place;
3140          l++;
3141        }
3142      }
3143      return;
3144    }
3145  }
3146  r->NegWeightL_Size = 0;
3147  r->NegWeightL_Offset = NULL;
3148}
3149
3150static void rSetOption(ring r)
3151{
3152  // set redthrough
3153  if (!TEST_OPT_OLDSTD && r->OrdSgn == 1 && ! r->LexOrder)
3154    r->options |= Sy_bit(OPT_REDTHROUGH);
3155  else
3156    r->options &= ~Sy_bit(OPT_REDTHROUGH);
3157
3158  // set intStrategy
3159#ifdef HAVE_RINGS
3160  if (
3161         rField_is_Extension(r)
3162      || rField_is_Q(r)
3163      || rField_is_Ring(r))
3164#else
3165  if (rField_is_Extension(r) || rField_is_Q(r))
3166#endif
3167    r->options |= Sy_bit(OPT_INTSTRATEGY);
3168  else
3169    r->options &= ~Sy_bit(OPT_INTSTRATEGY);
3170
3171  // set redTail
3172  if (r->LexOrder || r->OrdSgn == -1 || rField_is_Extension(r))
3173    r->options &= ~Sy_bit(OPT_REDTAIL);
3174  else
3175    r->options |= Sy_bit(OPT_REDTAIL);
3176}
3177
3178static void rCheckOrdSgn(ring r,int i/*current block*/);
3179
3180BOOLEAN rComplete(ring r, int force)
3181{
3182  if (r->VarOffset!=NULL && force == 0) return FALSE;
3183  rSetOutParams(r);
3184  int n=rBlocks(r)-1;
3185  int i;
3186  int bits;
3187  r->bitmask=rGetExpSize(r->bitmask,bits,r->N);
3188  r->BitsPerExp = bits;
3189  r->ExpPerLong = BIT_SIZEOF_LONG / bits;
3190  r->divmask=rGetDivMask(bits);
3191
3192  // will be used for ordsgn:
3193  long *tmp_ordsgn=(long *)omAlloc0(3*(n+r->N)*sizeof(long));
3194  // will be used for VarOffset:
3195  int *v=(int *)omAlloc((r->N+1)*sizeof(int));
3196  for(i=r->N; i>=0 ; i--)
3197  {
3198    v[i]=-1;
3199  }
3200  sro_ord *tmp_typ=(sro_ord *)omAlloc0(3*(n+r->N)*sizeof(sro_ord));
3201  int typ_i=0;
3202  int prev_ordsgn=0;
3203
3204  // fill in v, tmp_typ, tmp_ordsgn, determine typ_i (== ordSize)
3205  int j=0;
3206  int j_bits=BITS_PER_LONG;
3207
3208  BOOLEAN need_to_add_comp=FALSE; // Only for ringorder_s and ringorder_S!
3209
3210  for(i=0;i<n;i++)
3211  {
3212    tmp_typ[typ_i].order_index=i;
3213    switch (r->order[i])
3214    {
3215      case ringorder_a:
3216      case ringorder_aa:
3217        rO_WDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,tmp_typ[typ_i],
3218                   r->wvhdl[i]);
3219        typ_i++;
3220        break;
3221
3222      case ringorder_a64:
3223        rO_WDegree64(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3224                     tmp_typ[typ_i], (int64 *)(r->wvhdl[i]));
3225        typ_i++;
3226        break;
3227
3228      case ringorder_c:
3229        rO_Align(j, j_bits);
3230        rO_LexVars_neg(j, j_bits, 0,0, prev_ordsgn,tmp_ordsgn,v,BITS_PER_LONG, -1);
3231        break;
3232
3233      case ringorder_C:
3234        rO_Align(j, j_bits);
3235        rO_LexVars(j, j_bits, 0,0, prev_ordsgn,tmp_ordsgn,v,BITS_PER_LONG, -1);
3236        break;
3237
3238      case ringorder_M:
3239        {
3240          int k,l;
3241          k=r->block1[i]-r->block0[i]+1; // number of vars
3242          for(l=0;l<k;l++)
3243          {
3244            rO_WDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3245                       tmp_typ[typ_i],
3246                       r->wvhdl[i]+(r->block1[i]-r->block0[i]+1)*l);
3247            typ_i++;
3248          }
3249          break;
3250        }
3251
3252      case ringorder_lp:
3253        rO_LexVars(j, j_bits, r->block0[i],r->block1[i], prev_ordsgn,
3254                   tmp_ordsgn,v,bits, -1);
3255        break;
3256
3257      case ringorder_ls:
3258        rO_LexVars_neg(j, j_bits, r->block0[i],r->block1[i], prev_ordsgn,
3259                       tmp_ordsgn,v, bits, -1);
3260        rCheckOrdSgn(r,i);
3261        break;
3262
3263      case ringorder_rs:
3264        rO_LexVars_neg(j, j_bits, r->block1[i],r->block0[i], prev_ordsgn,
3265                       tmp_ordsgn,v, bits, -1);
3266        rCheckOrdSgn(r,i);
3267        break;
3268
3269      case ringorder_rp:
3270        rO_LexVars(j, j_bits, r->block1[i],r->block0[i], prev_ordsgn,
3271                       tmp_ordsgn,v, bits, -1);
3272        break;
3273
3274      case ringorder_dp:
3275        if (r->block0[i]==r->block1[i])
3276        {
3277          rO_LexVars(j, j_bits, r->block0[i],r->block0[i], prev_ordsgn,
3278                     tmp_ordsgn,v, bits, -1);
3279        }
3280        else
3281        {
3282          rO_TDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3283                     tmp_typ[typ_i]);
3284          typ_i++;
3285          rO_LexVars_neg(j, j_bits, r->block1[i],r->block0[i]+1,
3286                         prev_ordsgn,tmp_ordsgn,v,bits, r->block0[i]);
3287        }
3288        break;
3289
3290      case ringorder_Dp:
3291        if (r->block0[i]==r->block1[i])
3292        {
3293          rO_LexVars(j, j_bits, r->block0[i],r->block0[i], prev_ordsgn,
3294                     tmp_ordsgn,v, bits, -1);
3295        }
3296        else
3297        {
3298          rO_TDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3299                     tmp_typ[typ_i]);
3300          typ_i++;
3301          rO_LexVars(j, j_bits, r->block0[i],r->block1[i]-1, prev_ordsgn,
3302                     tmp_ordsgn,v, bits, r->block1[i]);
3303        }
3304        break;
3305
3306      case ringorder_ds:
3307        if (r->block0[i]==r->block1[i])
3308        {
3309          rO_LexVars_neg(j, j_bits,r->block0[i],r->block1[i],prev_ordsgn,
3310                         tmp_ordsgn,v,bits, -1);
3311        }
3312        else
3313        {
3314          rO_TDegree_neg(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3315                         tmp_typ[typ_i]);
3316          typ_i++;
3317          rO_LexVars_neg(j, j_bits, r->block1[i],r->block0[i]+1,
3318                         prev_ordsgn,tmp_ordsgn,v,bits, r->block0[i]);
3319        }
3320        rCheckOrdSgn(r,i);
3321        break;
3322
3323      case ringorder_Ds:
3324        if (r->block0[i]==r->block1[i])
3325        {
3326          rO_LexVars_neg(j, j_bits, r->block0[i],r->block0[i],prev_ordsgn,
3327                         tmp_ordsgn,v, bits, -1);
3328        }
3329        else
3330        {
3331          rO_TDegree_neg(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3332                         tmp_typ[typ_i]);
3333          typ_i++;
3334          rO_LexVars(j, j_bits, r->block0[i],r->block1[i]-1, prev_ordsgn,
3335                     tmp_ordsgn,v, bits, r->block1[i]);
3336        }
3337        rCheckOrdSgn(r,i);
3338        break;
3339
3340      case ringorder_wp:
3341        rO_WDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3342                   tmp_typ[typ_i], r->wvhdl[i]);
3343        typ_i++;
3344        { // check for weights <=0
3345          int jj;
3346          BOOLEAN have_bad_weights=FALSE;
3347          for(jj=r->block1[i]-r->block0[i];jj>=0; jj--)
3348          {
3349            if (r->wvhdl[i][jj]<=0) have_bad_weights=TRUE;
3350          }
3351          if (have_bad_weights)
3352          {
3353             rO_TDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3354                                     tmp_typ[typ_i]);
3355             typ_i++;
3356             rCheckOrdSgn(r,i);
3357          }
3358        }
3359        if (r->block1[i]!=r->block0[i])
3360        {
3361          rO_LexVars_neg(j, j_bits,r->block1[i],r->block0[i]+1, prev_ordsgn,
3362                         tmp_ordsgn, v,bits, r->block0[i]);
3363        }
3364        break;
3365
3366      case ringorder_Wp:
3367        rO_WDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3368                   tmp_typ[typ_i], r->wvhdl[i]);
3369        typ_i++;
3370        { // check for weights <=0
3371          int jj;
3372          BOOLEAN have_bad_weights=FALSE;
3373          for(jj=r->block1[i]-r->block0[i];jj>=0; jj--)
3374          {
3375            if (r->wvhdl[i][jj]<=0) have_bad_weights=TRUE;
3376          }
3377          if (have_bad_weights)
3378          {
3379             rO_TDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3380                                     tmp_typ[typ_i]);
3381             typ_i++;
3382             rCheckOrdSgn(r,i);
3383          }
3384        }
3385        if (r->block1[i]!=r->block0[i])
3386        {
3387          rO_LexVars(j, j_bits,r->block0[i],r->block1[i]-1, prev_ordsgn,
3388                     tmp_ordsgn,v, bits, r->block1[i]);
3389        }
3390        break;
3391
3392      case ringorder_ws:
3393        rO_WDegree_neg(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3394                       tmp_typ[typ_i], r->wvhdl[i]);
3395        typ_i++;
3396        if (r->block1[i]!=r->block0[i])
3397        {
3398          rO_LexVars_neg(j, j_bits,r->block1[i],r->block0[i]+1, prev_ordsgn,
3399                         tmp_ordsgn, v,bits, r->block0[i]);
3400        }
3401        rCheckOrdSgn(r,i);
3402        break;
3403
3404      case ringorder_Ws:
3405        rO_WDegree_neg(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3406                       tmp_typ[typ_i], r->wvhdl[i]);
3407        typ_i++;
3408        if (r->block1[i]!=r->block0[i])
3409        {
3410          rO_LexVars(j, j_bits,r->block0[i],r->block1[i]-1, prev_ordsgn,
3411                     tmp_ordsgn,v, bits, r->block1[i]);
3412        }
3413        rCheckOrdSgn(r,i);
3414        break;
3415
3416      case ringorder_S:
3417        assume(typ_i == 1); // For LaScala3 only: on the 2nd place ([1])!
3418        // TODO: for K[x]: it is 0...?!
3419        rO_Syzcomp(j, j_bits,prev_ordsgn, tmp_ordsgn,tmp_typ[typ_i]);
3420        need_to_add_comp=TRUE;
3421        typ_i++;
3422        break;
3423
3424      case ringorder_s:
3425        assume(typ_i == 0 && j == 0);
3426        rO_Syz(j, j_bits, prev_ordsgn, tmp_ordsgn, tmp_typ[typ_i]); // set syz-limit?
3427        need_to_add_comp=TRUE;
3428        typ_i++;
3429        break;
3430
3431      case ringorder_IS:
3432      {
3433
3434        assume( r->block0[i] == r->block1[i] );
3435        const int s = r->block0[i];
3436        assume( -2 < s && s < 2);
3437
3438        if(s == 0) // Prefix IS
3439          rO_ISPrefix(j, j_bits, prev_ordsgn, tmp_ordsgn, r->N, v, tmp_typ[typ_i++]); // What about prev_ordsgn?
3440        else // s = +1 or -1 // Note: typ_i might be incrimented here inside!
3441        {
3442          rO_ISSuffix(j, j_bits, prev_ordsgn, tmp_ordsgn, r->N, v, tmp_typ, typ_i, s); // Suffix.
3443          need_to_add_comp=FALSE;
3444        }
3445
3446        break;
3447      }
3448      case ringorder_unspec:
3449      case ringorder_no:
3450      default:
3451        dReportError("undef. ringorder used\n");
3452        break;
3453    }
3454  }
3455
3456  int j0=j; // save j
3457  int j_bits0=j_bits; // save jbits
3458  rO_Align(j,j_bits);
3459  r->CmpL_Size = j;
3460
3461  j_bits=j_bits0; j=j0;
3462
3463  // fill in some empty slots with variables not already covered
3464  // v0 is special, is therefore normally already covered
3465  // now we do have rings without comp...
3466  if((need_to_add_comp) && (v[0]== -1))
3467  {
3468    if (prev_ordsgn==1)
3469    {
3470      rO_Align(j, j_bits);
3471      rO_LexVars(j, j_bits, 0,0, prev_ordsgn,tmp_ordsgn,v,BITS_PER_LONG, -1);
3472    }
3473    else
3474    {
3475      rO_Align(j, j_bits);
3476      rO_LexVars_neg(j, j_bits, 0,0, prev_ordsgn,tmp_ordsgn,v,BITS_PER_LONG, -1);
3477    }
3478  }
3479  // the variables
3480  for(i=1 ; i<=r->N ; i++)
3481  {
3482    if(v[i]==(-1))
3483    {
3484      if (prev_ordsgn==1)
3485      {
3486        rO_LexVars(j, j_bits, i,i, prev_ordsgn,tmp_ordsgn,v,bits, -1);
3487      }
3488      else
3489      {
3490        rO_LexVars_neg(j,j_bits,i,i, prev_ordsgn,tmp_ordsgn,v,bits, -1);
3491      }
3492    }
3493  }
3494
3495  rO_Align(j,j_bits);
3496  // ----------------------------
3497  // finished with constructing the monomial, computing sizes:
3498
3499  r->ExpL_Size=j;
3500  r->PolyBin = omGetSpecBin(POLYSIZE + (r->ExpL_Size)*sizeof(long));
3501  assume(r->PolyBin != NULL);
3502
3503  // ----------------------------
3504  // indices and ordsgn vector for comparison
3505  //
3506  // r->pCompHighIndex already set
3507  r->ordsgn=(long *)omAlloc0(r->ExpL_Size*sizeof(long));
3508
3509  for(j=0;j<r->CmpL_Size;j++)
3510  {
3511    r->ordsgn[j] = tmp_ordsgn[j];
3512  }
3513
3514  omFreeSize((ADDRESS)tmp_ordsgn,(3*(n+r->N)*sizeof(long)));
3515
3516  // ----------------------------
3517  // description of orderings for setm:
3518  //
3519  r->OrdSize=typ_i;
3520  if (typ_i==0) r->typ=NULL;
3521  else
3522  {
3523    r->typ=(sro_ord*)omAlloc(typ_i*sizeof(sro_ord));
3524    memcpy(r->typ,tmp_typ,typ_i*sizeof(sro_ord));
3525  }
3526  omFreeSize((ADDRESS)tmp_typ,(3*(n+r->N)*sizeof(sro_ord)));
3527
3528  // ----------------------------
3529  // indices for (first copy of ) variable entries in exp.e vector (VarOffset):
3530  r->VarOffset=v;
3531
3532  // ----------------------------
3533  // other indicies
3534  r->pCompIndex=(r->VarOffset[0] & 0xffff); //r->VarOffset[0];
3535  i=0; // position
3536  j=0; // index in r->typ
3537  if (i==r->pCompIndex) i++; // IS???
3538  while ((j < r->OrdSize)
3539         && ((r->typ[j].ord_typ==ro_syzcomp) ||
3540             (r->typ[j].ord_typ==ro_syz) || (r->typ[j].ord_typ==ro_isTemp) || (r->typ[j].ord_typ==ro_is) ||
3541             (r->order[r->typ[j].order_index] == ringorder_aa)))
3542  {
3543    i++; j++;
3544  }
3545  // No use of j anymore!!!????
3546
3547  if (i==r->pCompIndex) i++;
3548  r->pOrdIndex=i; // How came it is "i" here???!!!! exp[r->pOrdIndex] is order of a poly... This may be wrong!!! IS
3549
3550  // ----------------------------
3551  rSetDegStuff(r);
3552  rSetOption(r);
3553  // ----------------------------
3554  // r->p_Setm
3555  r->p_Setm = p_GetSetmProc(r);
3556
3557  // ----------------------------
3558  // set VarL_*
3559  rSetVarL(r);
3560
3561  //  ----------------------------
3562  // right-adjust VarOffset
3563  rRightAdjustVarOffset(r);
3564
3565  // ----------------------------
3566  // set NegWeightL*
3567  rSetNegWeight(r);
3568
3569  // ----------------------------
3570  // p_Procs: call AFTER NegWeightL
3571  r->p_Procs = (p_Procs_s*)omAlloc(sizeof(p_Procs_s));
3572  p_ProcsSet(r, r->p_Procs);
3573  return FALSE;
3574}
3575
3576static void rCheckOrdSgn(ring r,int i/*current block*/)
3577{ // set r->OrdSgn
3578  if ( r->OrdSgn==1)
3579  {
3580    int oo=-1;
3581    int jj;
3582    for(jj=i-1;jj>=0;jj--)
3583    {
3584      if(((r->order[jj]==ringorder_a)
3585        ||(r->order[jj]==ringorder_aa)
3586        ||(r->order[jj]==ringorder_a64))
3587      &&(r->block0[jj]<=r->block0[i])
3588      &&(r->block1[jj]>=r->block1[i]))
3589      { oo=1; break;}
3590    }
3591    r->OrdSgn=oo;
3592  }
3593}
3594
3595
3596void rUnComplete(ring r)
3597{
3598  if (r == NULL) return;
3599  if (r->VarOffset != NULL)
3600  {
3601    if (r->OrdSize!=0 && r->typ != NULL)
3602    {
3603      for(int i = 0; i < r->OrdSize; i++)
3604        if( r->typ[i].ord_typ == ro_is) // Search for suffixes! (prefix have the same VarOffset)
3605        {
3606          id_Delete(&r->typ[i].data.is.F, r);
3607          r->typ[i].data.is.F = NULL; // ?
3608
3609          if( r->typ[i].data.is.componentWeights != NULL )
3610          {
3611            delete r->typ[i].data.is.componentWeights;
3612            r->typ[i].data.is.componentWeights = NULL; // ?
3613          }
3614
3615          if( r->typ[i].data.is.pVarOffset != NULL )
3616          {
3617            omFreeSize((ADDRESS)r->typ[i].data.is.pVarOffset, (r->N +1)*sizeof(int));
3618            r->typ[i].data.is.pVarOffset = NULL; // ?
3619          }
3620        }
3621        else if (r->typ[i].ord_typ == ro_syz)
3622        {
3623          if(r->typ[i].data.syz.limit > 0)
3624            omFreeSize(r->typ[i].data.syz.syz_index, ((r->typ[i].data.syz.limit) +1)*sizeof(int));
3625          r->typ[i].data.syz.syz_index = NULL;
3626        }
3627        else if (r->typ[i].ord_typ == ro_syzcomp)
3628        {
3629          assume( r->typ[i].data.syzcomp.ShiftedComponents == NULL );
3630          assume( r->typ[i].data.syzcomp.Components        == NULL );
3631//          WarnS( "rUnComplete : ord_typ == ro_syzcomp was unhandled!!! Possibly memory leak!!!"  );
3632#ifndef NDEBUG
3633//          assume(0);
3634#endif
3635        }
3636
3637      omFreeSize((ADDRESS)r->typ,r->OrdSize*sizeof(sro_ord)); r->typ = NULL;
3638    }
3639
3640    if (r->order != NULL)
3641    {
3642      // delete r->order!!!???
3643    }
3644
3645    if (r->PolyBin != NULL)
3646      omUnGetSpecBin(&(r->PolyBin));
3647
3648    omFreeSize((ADDRESS)r->VarOffset, (r->N +1)*sizeof(int));
3649
3650    if (r->ordsgn != NULL && r->CmpL_Size != 0)
3651      omFreeSize((ADDRESS)r->ordsgn,r->ExpL_Size*sizeof(long));
3652    if (r->p_Procs != NULL)
3653      omFreeSize(r->p_Procs, sizeof(p_Procs_s));
3654    omfreeSize(r->VarL_Offset, r->VarL_Size*sizeof(int));
3655  }
3656  if (r->NegWeightL_Offset!=NULL)
3657  {
3658    omFreeSize(r->NegWeightL_Offset, r->NegWeightL_Size*sizeof(int));
3659    r->NegWeightL_Offset=NULL;
3660  }
3661}
3662
3663// set r->VarL_Size, r->VarL_Offset, r->VarL_LowIndex
3664static void rSetVarL(ring r)
3665{
3666  int  min = INT_MAX, min_j = -1;
3667  int* VarL_Number = (int*) omAlloc0(r->ExpL_Size*sizeof(int));
3668
3669  int i,j;
3670
3671  // count how often a var long is occupied by an exponent
3672  for (i=1; i<=r->N; i++)
3673  {
3674    VarL_Number[r->VarOffset[i] & 0xffffff]++;
3675  }
3676
3677  // determine how many and min
3678  for (i=0, j=0; i<r->ExpL_Size; i++)
3679  {
3680    if (VarL_Number[i] != 0)
3681    {
3682      if (min > VarL_Number[i])
3683      {
3684        min = VarL_Number[i];
3685        min_j = j;
3686      }
3687      j++;
3688    }
3689  }
3690
3691  r->VarL_Size = j; // number of long with exp. entries in
3692                    //  in p->exp
3693  r->VarL_Offset = (int*) omAlloc(r->VarL_Size*sizeof(int));
3694  r->VarL_LowIndex = 0;
3695
3696  // set VarL_Offset
3697  for (i=0, j=0; i<r->ExpL_Size; i++)
3698  {
3699    if (VarL_Number[i] != 0)
3700    {
3701      r->VarL_Offset[j] = i;
3702      if (j > 0 && r->VarL_Offset[j-1] != r->VarL_Offset[j] - 1)
3703        r->VarL_LowIndex = -1;
3704      j++;
3705    }
3706  }
3707  if (r->VarL_LowIndex >= 0)
3708    r->VarL_LowIndex = r->VarL_Offset[0];
3709
3710  r->MinExpPerLong = min;
3711  if (min_j != 0)
3712  {
3713    j = r->VarL_Offset[min_j];
3714    r->VarL_Offset[min_j] = r->VarL_Offset[0];
3715    r->VarL_Offset[0] = j;
3716  }
3717  omFree(VarL_Number);
3718}
3719
3720static void rRightAdjustVarOffset(ring r)
3721{
3722  int* shifts = (int*) omAlloc(r->ExpL_Size*sizeof(int));
3723  int i;
3724  // initialize shifts
3725  for (i=0;i<r->ExpL_Size;i++)
3726    shifts[i] = BIT_SIZEOF_LONG;
3727
3728  // find minimal bit shift in each long exp entry
3729  for (i=1;i<=r->N;i++)
3730  {
3731    if (shifts[r->VarOffset[i] & 0xffffff] > r->VarOffset[i] >> 24)
3732      shifts[r->VarOffset[i] & 0xffffff] = r->VarOffset[i] >> 24;
3733  }
3734  // reset r->VarOffset: set the minimal shift to 0
3735  for (i=1;i<=r->N;i++)
3736  {
3737    if (shifts[r->VarOffset[i] & 0xffffff] != 0)
3738      r->VarOffset[i]
3739        = (r->VarOffset[i] & 0xffffff) |
3740        (((r->VarOffset[i] >> 24) - shifts[r->VarOffset[i] & 0xffffff]) << 24);
3741  }
3742  omFree(shifts);
3743}
3744
3745// get r->divmask depending on bits per exponent
3746static unsigned long rGetDivMask(int bits)
3747{
3748  unsigned long divmask = 1;
3749  int i = bits;
3750
3751  while (i < BIT_SIZEOF_LONG)
3752  {
3753    divmask |= (((unsigned long) 1) << (unsigned long) i);
3754    i += bits;
3755  }
3756  return divmask;
3757}
3758
3759#ifdef RDEBUG
3760void rDebugPrint(ring r)
3761{
3762  if (r==NULL)
3763  {
3764    PrintS("NULL ?\n");
3765    return;
3766  }
3767  // corresponds to ro_typ from ring.h:
3768  const char *TYP[]={"ro_dp","ro_wp","ro_wp64","ro_wp_neg","ro_cp",
3769                     "ro_syzcomp", "ro_syz", "ro_isTemp", "ro_is", "ro_none"};
3770  int i,j;
3771
3772  Print("ExpL_Size:%d ",r->ExpL_Size);
3773  Print("CmpL_Size:%d ",r->CmpL_Size);
3774  Print("VarL_Size:%d\n",r->VarL_Size);
3775  Print("bitmask=0x%lx (expbound=%ld) \n",r->bitmask, r->bitmask);
3776  Print("BitsPerExp=%d ExpPerLong=%d MinExpPerLong=%d at L[%d]\n", r->BitsPerExp, r->ExpPerLong, r->MinExpPerLong, r->VarL_Offset[0]);
3777  PrintS("varoffset:\n");
3778  if (r->VarOffset==NULL) PrintS(" NULL\n");
3779  else
3780    for(j=0;j<=r->N;j++)
3781      Print("  v%d at e-pos %d, bit %d\n",
3782            j,r->VarOffset[j] & 0xffffff, r->VarOffset[j] >>24);
3783  Print("divmask=%lx\n", r->divmask);
3784  PrintS("ordsgn:\n");
3785  for(j=0;j<r->CmpL_Size;j++)
3786    Print("  ordsgn %ld at pos %d\n",r->ordsgn[j],j);
3787  Print("OrdSgn:%d\n",r->OrdSgn);
3788  PrintS("ordrec:\n");
3789  for(j=0;j<r->OrdSize;j++)
3790  {
3791    Print("  typ %s", TYP[r->typ[j].ord_typ]);
3792
3793
3794    if (r->typ[j].ord_typ==ro_syz)
3795    {
3796      const short place = r->typ[j].data.syz.place;
3797      const int limit = r->typ[j].data.syz.limit;
3798      const int curr_index = r->typ[j].data.syz.curr_index;
3799      const int* syz_index = r->typ[j].data.syz.syz_index;
3800
3801      Print("  limit %d (place: %d, curr_index: %d), syz_index: ", limit, place, curr_index);
3802
3803      if( syz_index == NULL )
3804        PrintS("(NULL)");
3805      else
3806      {
3807        Print("{");
3808        for( i=0; i <= limit; i++ )
3809          Print("%d ", syz_index[i]);
3810        Print("}");
3811      }
3812
3813    }
3814    else if (r->typ[j].ord_typ==ro_isTemp)
3815    {
3816      Print("  start (level) %d, suffixpos: %d, VO: ",r->typ[j].data.isTemp.start, r->typ[j].data.isTemp.suffixpos);
3817
3818    }
3819    else if (r->typ[j].ord_typ==ro_is)
3820    {
3821      Print("  start %d, end: %d: ",r->typ[j].data.is.start, r->typ[j].data.is.end);
3822
3823//      for( int k = 0; k <= r->N; k++) if (r->typ[j].data.is.pVarOffset[k] != -1) Print("[%2d]: %04x; ", k, r->typ[j].data.is.pVarOffset[k]);
3824
3825      Print("  limit %d\n",r->typ[j].data.is.limit);
3826      #ifndef NDEBUG
3827      //PrintS("  F: ");idShow(r->typ[j].data.is.F, r, r, 1);
3828      #endif
3829
3830      PrintS("weights: ");
3831
3832      if( r->typ[j].data.is.componentWeights == NULL )
3833        PrintS("NULL == [0,...0]\n");
3834      else
3835      {
3836        (r->typ[j].data.is.componentWeights)->show(); PrintLn();
3837      }
3838    }
3839    else
3840    {
3841      Print("  place %d",r->typ[j].data.dp.place);
3842
3843      if (r->typ[j].ord_typ!=ro_syzcomp  && r->typ[j].ord_typ!=ro_syz)
3844      {
3845        Print("  start %d",r->typ[j].data.dp.start);
3846        Print("  end %d",r->typ[j].data.dp.end);
3847        if ((r->typ[j].ord_typ==ro_wp)
3848        || (r->typ[j].ord_typ==ro_wp_neg))
3849        {
3850          PrintS(" w:");
3851          for(int l=r->typ[j].data.wp.start;l<=r->typ[j].data.wp.end;l++)
3852            Print(" %d",r->typ[j].data.wp.weights[l-r->typ[j].data.wp.start]);
3853        }
3854        else if (r->typ[j].ord_typ==ro_wp64)
3855        {
3856          PrintS(" w64:");
3857          int l;
3858          for(l=r->typ[j].data.wp64.start;l<=r->typ[j].data.wp64.end;l++)
3859            Print(" %ld",(long)(((int64*)r->typ[j].data.wp64.weights64)+l-r->typ[j].data.wp64.start));
3860          }
3861        }
3862    }
3863    PrintLn();
3864  }
3865  Print("pOrdIndex:%d pCompIndex:%d\n", r->pOrdIndex, r->pCompIndex);
3866  Print("OrdSize:%d\n",r->OrdSize);
3867  PrintS("--------------------\n");
3868  for(j=0;j<r->ExpL_Size;j++)
3869  {
3870    Print("L[%d]: ",j);
3871    if (j< r->CmpL_Size)
3872      Print("ordsgn %ld ", r->ordsgn[j]);
3873    else
3874      PrintS("no comp ");
3875    i=1;
3876    for(;i<=r->N;i++)
3877    {
3878      if( (r->VarOffset[i] & 0xffffff) == j )
3879      {  Print("v%d at e[%d], bit %d; ", i,r->VarOffset[i] & 0xffffff,
3880                                         r->VarOffset[i] >>24 ); }
3881    }
3882    if( r->pCompIndex==j ) PrintS("v0; ");
3883    for(i=0;i<r->OrdSize;i++)
3884    {
3885      if (r->typ[i].data.dp.place == j)
3886      {
3887        Print("ordrec:%s (start:%d, end:%d) ",TYP[r->typ[i].ord_typ],
3888          r->typ[i].data.dp.start, r->typ[i].data.dp.end);
3889      }
3890    }
3891
3892    if (j==r->pOrdIndex)
3893      PrintS("pOrdIndex\n");
3894    else
3895      PrintLn();
3896  }
3897  Print("LexOrder:%d, MixedOrder:%d\n",r->LexOrder, r->MixedOrder);
3898
3899  // p_Procs stuff
3900  p_Procs_s proc_names;
3901  const char* field;
3902  const char* length;
3903  const char* ord;
3904  p_Debug_GetProcNames(r, &proc_names); // changes p_Procs!!!
3905  p_Debug_GetSpecNames(r, field, length, ord);
3906
3907  Print("p_Spec  : %s, %s, %s\n", field, length, ord);
3908  PrintS("p_Procs :\n");
3909  for (i=0; i<(int) (sizeof(p_Procs_s)/sizeof(void*)); i++)
3910  {
3911    Print(" %s,\n", ((char**) &proc_names)[i]);
3912  }
3913
3914  {
3915#define pFDeg_CASE(A) if(r->pFDeg == A) PrintS( "" #A "" )
3916    Print("\npFDeg   : ");
3917
3918    pFDeg_CASE(p_Totaldegree); else
3919      pFDeg_CASE(p_WFirstTotalDegree); else
3920      pFDeg_CASE(p_WTotaldegree); else
3921      pFDeg_CASE(p_Deg); else
3922      Print("(%p)", (void*)(r->pFDeg)); // default case
3923
3924    PrintS("\n");
3925#undef pFDeg_CASE
3926  }
3927
3928}
3929
3930void p_DebugPrint(poly p, const ring r)
3931{
3932  int i,j;
3933  p_Write(p,r);
3934  j=2;
3935  while(p!=NULL)
3936  {
3937    Print("\nexp[0..%d]\n",r->ExpL_Size-1);
3938    for(i=0;i<r->ExpL_Size;i++)
3939      Print("%ld ",p->exp[i]);
3940    PrintLn();
3941    Print("v0:%ld ",p_GetComp(p, r));
3942    for(i=1;i<=r->N;i++) Print(" v%d:%ld",i,p_GetExp(p,i, r));
3943    PrintLn();
3944    pIter(p);
3945    j--;
3946    if (j==0) { PrintS("...\n"); break; }
3947  }
3948}
3949
3950#endif // RDEBUG
3951
3952/// debug-print monomial poly/vector p, assuming that it lives in the ring R
3953static inline void m_DebugPrint(const poly p, const ring R)
3954{
3955  Print("\nexp[0..%d]\n", R->ExpL_Size - 1);
3956  for(int i = 0; i < R->ExpL_Size; i++)
3957    Print("%09lx ", p->exp[i]);
3958  PrintLn();
3959  Print("v0:%9ld ", p_GetComp(p, R));
3960  for(int i = 1; i <= R->N; i++) Print(" v%d:%5ld",i, p_GetExp(p, i, R));
3961  PrintLn();
3962}
3963
3964
3965#ifndef NDEBUG
3966/// debug-print at most nTerms (2 by default) terms from poly/vector p,
3967/// assuming that lt(p) lives in lmRing and tail(p) lives in tailRing.
3968void p_DebugPrint(const poly p, const ring lmRing, const ring tailRing, const int nTerms)
3969{
3970  assume( nTerms >= 0 );
3971  if( p != NULL )
3972  {
3973    assume( p != NULL );
3974
3975    p_Write(p, lmRing, tailRing);
3976
3977    if( (p != NULL) && (nTerms > 0) )
3978    {
3979      assume( p != NULL );
3980      assume( nTerms > 0 );
3981
3982      // debug pring leading term
3983      m_DebugPrint(p, lmRing);
3984
3985      poly q = pNext(p); // q = tail(p)
3986
3987      // debug pring tail (at most nTerms-1 terms from it)
3988      for(int j = nTerms - 1; (q !=NULL) && (j > 0); pIter(q), --j)
3989        m_DebugPrint(q, tailRing);
3990
3991      if (q != NULL)
3992        PrintS("...\n");
3993    }
3994  }
3995  else
3996    PrintS("0\n");
3997}
3998#endif
3999
4000
4001//    F = system("ISUpdateComponents", F, V, MIN );
4002//    // replace gen(i) -> gen(MIN + V[i-MIN]) for all i > MIN in all terms from F!
4003void pISUpdateComponents(ideal F, const intvec *const V, const int MIN, const ring r )
4004{
4005  assume( V != NULL );
4006  assume( MIN >= 0 );
4007
4008  if( F == NULL )
4009    return;
4010
4011  for( int j = (F->ncols*F->nrows) - 1; j >= 0; j-- )
4012  {
4013#ifdef PDEBUG
4014    Print("F[%d]:", j);
4015    p_DebugPrint(F->m[j], r, r, 0);
4016#endif
4017
4018    for( poly p = F->m[j]; p != NULL; pIter(p) )
4019    {
4020      int c = p_GetComp(p, r);
4021
4022      if( c > MIN )
4023      {
4024#ifdef PDEBUG
4025        Print("gen[%d] -> gen(%d)\n", c, MIN + (*V)[ c - MIN - 1 ]);
4026#endif
4027
4028        p_SetComp( p, MIN + (*V)[ c - MIN - 1 ], r );
4029      }
4030    }
4031#ifdef PDEBUG
4032    Print("new F[%d]:", j);
4033    p_Test(F->m[j], r);
4034    p_DebugPrint(F->m[j], r, r, 0);
4035#endif
4036  }
4037
4038}
4039
4040
4041
4042
4043/*2
4044* asssume that rComplete was called with r
4045* assume that the first block ist ringorder_S
4046* change the block to reflect the sequence given by appending v
4047*/
4048
4049#ifdef PDEBUG
4050void rDBChangeSComps(int* currComponents,
4051                     long* currShiftedComponents,
4052                     int length,
4053                     ring r)
4054{
4055  assume(r->typ[1].ord_typ == ro_syzcomp);
4056
4057  r->typ[1].data.syzcomp.length = length;
4058  rNChangeSComps( currComponents, currShiftedComponents, r);
4059}
4060void rDBGetSComps(int** currComponents,
4061                 long** currShiftedComponents,
4062                 int *length,
4063                 ring r)
4064{
4065  assume(r->typ[1].ord_typ == ro_syzcomp);
4066
4067  *length = r->typ[1].data.syzcomp.length;
4068  rNGetSComps( currComponents, currShiftedComponents, r);
4069}
4070#endif
4071
4072void rNChangeSComps(int* currComponents, long* currShiftedComponents, ring r)
4073{
4074  assume(r->typ[1].ord_typ == ro_syzcomp);
4075
4076  r->typ[1].data.syzcomp.ShiftedComponents = currShiftedComponents;
4077  r->typ[1].data.syzcomp.Components = currComponents;
4078}
4079
4080void rNGetSComps(int** currComponents, long** currShiftedComponents, ring r)
4081{
4082  assume(r->typ[1].ord_typ == ro_syzcomp);
4083
4084  *currShiftedComponents = r->typ[1].data.syzcomp.ShiftedComponents;
4085  *currComponents =   r->typ[1].data.syzcomp.Components;
4086}
4087
4088/////////////////////////////////////////////////////////////////////////////
4089//
4090// The following routines all take as input a ring r, and return R
4091// where R has a certain property. R might be equal r in which case r
4092// had already this property
4093//
4094ring rAssure_SyzComp(const ring r, BOOLEAN complete)
4095{
4096  if ( (r->order[0] == ringorder_s) ) return r;
4097
4098  if ( (r->order[0] == ringorder_IS) )
4099  {
4100#ifndef NDEBUG
4101    WarnS("rAssure_SyzComp: input ring has an IS-ordering!");
4102#endif
4103//    return r;
4104  }
4105  ring res=rCopy0(r, FALSE, FALSE);
4106  int i=rBlocks(r);
4107  int j;
4108
4109  res->order=(int *)omAlloc((i+1)*sizeof(int));
4110  res->block0=(int *)omAlloc0((i+1)*sizeof(int));
4111  res->block1=(int *)omAlloc0((i+1)*sizeof(int));
4112  int ** wvhdl =(int **)omAlloc0((i+1)*sizeof(int**));
4113  for(j=i;j>0;j--)
4114  {
4115    res->order[j]=r->order[j-1];
4116    res->block0[j]=r->block0[j-1];
4117    res->block1[j]=r->block1[j-1];
4118    if (r->wvhdl[j-1] != NULL)
4119    {
4120      wvhdl[j] = (int*) omMemDup(r->wvhdl[j-1]);
4121    }
4122  }
4123  res->order[0]=ringorder_s;
4124
4125  res->wvhdl = wvhdl;
4126
4127  if (complete)
4128  {
4129    rComplete(res, 1);
4130
4131#ifdef HAVE_PLURAL
4132    if (rIsPluralRing(r))
4133    {
4134      if ( nc_rComplete(r, res, false) ) // no qideal!
4135      {
4136#ifndef NDEBUG
4137        WarnS("error in nc_rComplete");      // cleanup?//      rDelete(res);//      return r;      // just go on..
4138#endif
4139      }
4140    }
4141    assume(rIsPluralRing(r) == rIsPluralRing(res));
4142#endif
4143
4144
4145#ifdef HAVE_PLURAL
4146    ring old_ring = r;
4147#endif
4148
4149    if (r->qideal!=NULL)
4150    {
4151      res->qideal= idrCopyR_NoSort(r->qideal, r, res);
4152
4153      assume(id_RankFreeModule(res->qideal, res) == 0);
4154
4155#ifdef HAVE_PLURAL
4156      if( rIsPluralRing(res) )
4157        if( nc_SetupQuotient(res, r, true) )
4158        {
4159//          WarnS("error in nc_SetupQuotient"); // cleanup?      rDelete(res);       return r;  // just go on...?
4160        }
4161
4162#endif
4163      assume(id_RankFreeModule(res->qideal, res) == 0);
4164    }
4165
4166#ifdef HAVE_PLURAL
4167    assume((res->qideal==NULL) == (old_ring->qideal==NULL));
4168    assume(rIsPluralRing(res) == rIsPluralRing(old_ring));
4169    assume(rIsSCA(res) == rIsSCA(old_ring));
4170    assume(ncRingType(res) == ncRingType(old_ring));
4171#endif
4172  }
4173
4174  return res;
4175}
4176
4177ring rAssure_TDeg(ring r, int start_var, int end_var, int &pos)
4178{
4179  int i;
4180  if (r->typ!=NULL)
4181  {
4182    for(i=r->OrdSize-1;i>=0;i--)
4183    {
4184      if ((r->typ[i].ord_typ==ro_dp)
4185      && (r->typ[i].data.dp.start==start_var)
4186      && (r->typ[i].data.dp.end==end_var))
4187      {
4188        pos=r->typ[i].data.dp.place;
4189        //printf("no change, pos=%d\n",pos);
4190        return r;
4191      }
4192    }
4193  }
4194
4195#ifdef HAVE_PLURAL
4196  nc_struct* save=r->GetNC();
4197  r->GetNC()=NULL;
4198#endif
4199  ring res=rCopy(r);
4200
4201  i=rBlocks(r);
4202  int j;
4203
4204  res->ExpL_Size=r->ExpL_Size+1; // one word more in each monom
4205  res->PolyBin=omGetSpecBin(POLYSIZE + (res->ExpL_Size)*sizeof(long));
4206  omFree((ADDRESS)res->ordsgn);
4207  res->ordsgn=(long *)omAlloc0(res->ExpL_Size*sizeof(long));
4208  for(j=0;j<r->CmpL_Size;j++)
4209  {
4210    res->ordsgn[j] = r->ordsgn[j];
4211  }
4212  res->OrdSize=r->OrdSize+1;   // one block more for pSetm
4213  if (r->typ!=NULL)
4214    omFree((ADDRESS)res->typ);
4215  res->typ=(sro_ord*)omAlloc0(res->OrdSize*sizeof(sro_ord));
4216  if (r->typ!=NULL)
4217    memcpy(res->typ,r->typ,r->OrdSize*sizeof(sro_ord));
4218  // the additional block for pSetm: total degree at the last word
4219  // but not included in the compare part
4220  res->typ[res->OrdSize-1].ord_typ=ro_dp;
4221  res->typ[res->OrdSize-1].data.dp.start=start_var;
4222  res->typ[res->OrdSize-1].data.dp.end=end_var;
4223  res->typ[res->OrdSize-1].data.dp.place=res->ExpL_Size-1;
4224  pos=res->ExpL_Size-1;
4225  //if ((start_var==1) && (end_var==res->N)) res->pOrdIndex=pos;
4226  extern void p_Setm_General(poly p, ring r);
4227  res->p_Setm=p_Setm_General;
4228  // ----------------------------
4229  omFree((ADDRESS)res->p_Procs);
4230  res->p_Procs = (p_Procs_s*)omAlloc(sizeof(p_Procs_s));
4231
4232  p_ProcsSet(res, res->p_Procs);
4233  if (res->qideal!=NULL) id_Delete(&res->qideal,res);
4234#ifdef HAVE_PLURAL
4235  r->GetNC()=save;
4236  if (rIsPluralRing(r))
4237  {
4238    if ( nc_rComplete(r, res, false) ) // no qideal!
4239    {
4240#ifndef NDEBUG
4241      WarnS("error in nc_rComplete");
4242#endif
4243      // just go on..
4244    }
4245  }
4246#endif
4247  if (r->qideal!=NULL)
4248  {
4249     res->qideal=idrCopyR_NoSort(r->qideal,r, res);
4250#ifdef HAVE_PLURAL
4251     if (rIsPluralRing(res))
4252     {
4253//       nc_SetupQuotient(res, currRing);
4254       nc_SetupQuotient(res, r); // ?
4255     }
4256     assume((res->qideal==NULL) == (r->qideal==NULL));
4257#endif
4258  }
4259
4260#ifdef HAVE_PLURAL
4261  assume(rIsPluralRing(res) == rIsPluralRing(r));
4262  assume(rIsSCA(res) == rIsSCA(r));
4263  assume(ncRingType(res) == ncRingType(r));
4264#endif
4265
4266  return res;
4267}
4268
4269ring rAssure_HasComp(const ring r)
4270{
4271  int last_block;
4272  int i=0;
4273  do
4274  {
4275     if (r->order[i] == ringorder_c ||
4276        r->order[i] == ringorder_C) return r;
4277     if (r->order[i] == 0)
4278        break;
4279     i++;
4280  } while (1);
4281  //WarnS("re-creating ring with comps");
4282  last_block=i-1;
4283
4284  ring new_r = rCopy0(r, FALSE, FALSE);
4285  i+=2;
4286  new_r->wvhdl=(int **)omAlloc0(i * sizeof(int *));
4287  new_r->order   = (int *) omAlloc0(i * sizeof(int));
4288  new_r->block0   = (int *) omAlloc0(i * sizeof(int));
4289  new_r->block1   = (int *) omAlloc0(i * sizeof(int));
4290  memcpy(new_r->order,r->order,(i-1) * sizeof(int));
4291  memcpy(new_r->block0,r->block0,(i-1) * sizeof(int));
4292  memcpy(new_r->block1,r->block1,(i-1) * sizeof(int));
4293  for (int j=0; j<=last_block; j++)
4294  {
4295    if (r->wvhdl[j]!=NULL)
4296    {
4297      new_r->wvhdl[j] = (int*) omMemDup(r->wvhdl[j]);
4298    }
4299  }
4300  last_block++;
4301  new_r->order[last_block]=ringorder_C;
4302  //new_r->block0[last_block]=0;
4303  //new_r->block1[last_block]=0;
4304  //new_r->wvhdl[last_block]=NULL;
4305
4306  rComplete(new_r, 1);
4307
4308#ifdef HAVE_PLURAL
4309  if (rIsPluralRing(r))
4310  {
4311    if ( nc_rComplete(r, new_r, false) ) // no qideal!
4312    {
4313#ifndef NDEBUG
4314      WarnS("error in nc_rComplete");      // cleanup?//      rDelete(res);//      return r;      // just go on..
4315#endif
4316    }
4317  }
4318  assume(rIsPluralRing(r) == rIsPluralRing(new_r));
4319#endif
4320
4321  return new_r;
4322}
4323
4324static ring rAssure_CompLastBlock(ring r, BOOLEAN complete = TRUE)
4325{
4326  int last_block = rBlocks(r) - 2;
4327  if (r->order[last_block] != ringorder_c &&
4328      r->order[last_block] != ringorder_C)
4329  {
4330    int c_pos = 0;
4331    int i;
4332
4333    for (i=0; i< last_block; i++)
4334    {
4335      if (r->order[i] == ringorder_c || r->order[i] == ringorder_C)
4336      {
4337        c_pos = i;
4338        break;
4339      }
4340    }
4341    if (c_pos != -1)
4342    {
4343      ring new_r = rCopy0(r, FALSE, TRUE);
4344      for (i=c_pos+1; i<=last_block; i++)
4345      {
4346        new_r->order[i-1] = new_r->order[i];
4347        new_r->block0[i-1] = new_r->block0[i];
4348        new_r->block1[i-1] = new_r->block1[i];
4349        new_r->wvhdl[i-1] = new_r->wvhdl[i];
4350      }
4351      new_r->order[last_block] = r->order[c_pos];
4352      new_r->block0[last_block] = r->block0[c_pos];
4353      new_r->block1[last_block] = r->block1[c_pos];
4354      new_r->wvhdl[last_block] = r->wvhdl[c_pos];
4355      if (complete)
4356      {
4357        rComplete(new_r, 1);
4358
4359#ifdef HAVE_PLURAL
4360        if (rIsPluralRing(r))
4361        {
4362          if ( nc_rComplete(r, new_r, false) ) // no qideal!
4363          {
4364#ifndef NDEBUG
4365            WarnS("error in nc_rComplete");   // cleanup?//      rDelete(res);//      return r;      // just go on..
4366#endif
4367          }
4368        }
4369        assume(rIsPluralRing(r) == rIsPluralRing(new_r));
4370#endif
4371      }
4372      return new_r;
4373    }
4374  }
4375  return r;
4376}
4377
4378// Moves _c or _C ordering to the last place AND adds _s on the 1st place
4379ring rAssure_SyzComp_CompLastBlock(const ring r)
4380{
4381  ring new_r_1 = rAssure_CompLastBlock(r, FALSE); // due to this FALSE - no completion!
4382  ring new_r = rAssure_SyzComp(new_r_1, FALSE); // new_r_1 is used only here!!!
4383
4384  if (new_r != r)
4385  {
4386    ring old_r = r;
4387    if (new_r_1 != new_r && new_r_1 != old_r) rDelete(new_r_1);
4388    rComplete(new_r, 1);
4389#ifdef HAVE_PLURAL
4390    if (rIsPluralRing(old_r))
4391    {
4392      if ( nc_rComplete(old_r, new_r, false) ) // no qideal!
4393      {
4394#ifndef NDEBUG
4395        WarnS("error in nc_rComplete"); // cleanup?      rDelete(res);       return r;  // just go on...?
4396#endif
4397        }
4398    }
4399    assume(rIsPluralRing(new_r) == rIsPluralRing(old_r));
4400#endif
4401///?    rChangeCurrRing(new_r);
4402    if (old_r->qideal != NULL)
4403    {
4404      new_r->qideal = idrCopyR(old_r->qideal, old_r, new_r);
4405      //currQuotient = new_r->qideal;
4406
4407#ifdef HAVE_PLURAL
4408      if( rIsPluralRing(old_r) )
4409        if( nc_SetupQuotient(new_r, old_r, true) )
4410        {
4411#ifndef NDEBUG
4412          WarnS("error in nc_SetupQuotient"); // cleanup?      rDelete(res);       return r;  // just go on...?
4413#endif
4414        }
4415#endif
4416    }
4417
4418#ifdef HAVE_PLURAL
4419    assume((new_r->qideal==NULL) == (old_r->qideal==NULL));
4420    assume(rIsPluralRing(new_r) == rIsPluralRing(old_r));
4421    assume(rIsSCA(new_r) == rIsSCA(old_r));
4422    assume(ncRingType(new_r) == ncRingType(old_r));
4423#endif
4424
4425    rTest(new_r);
4426    rTest(old_r);
4427  }
4428  return new_r;
4429}
4430
4431// use this for global orderings consisting of two blocks
4432static ring rAssure_Global(rRingOrder_t b1, rRingOrder_t b2, const ring r)
4433{
4434  int r_blocks = rBlocks(r);
4435
4436  assume(b1 == ringorder_c || b1 == ringorder_C ||
4437         b2 == ringorder_c || b2 == ringorder_C ||
4438         b2 == ringorder_S);
4439  if ((r_blocks == 3) &&
4440      (r->order[0] == b1) &&
4441      (r->order[1] == b2) &&
4442      (r->order[2] == 0))
4443    return r;
4444  ring res = rCopy0(r, TRUE, FALSE);
4445  res->order = (int*)omAlloc0(3*sizeof(int));
4446  res->block0 = (int*)omAlloc0(3*sizeof(int));
4447  res->block1 = (int*)omAlloc0(3*sizeof(int));
4448  res->wvhdl = (int**)omAlloc0(3*sizeof(int*));
4449  res->order[0] = b1;
4450  res->order[1] = b2;
4451  if (b1 == ringorder_c || b1 == ringorder_C)
4452  {
4453    res->block0[1] = 1;
4454    res->block1[1] = r->N;
4455  }
4456  else
4457  {
4458    res->block0[0] = 1;
4459    res->block1[0] = r->N;
4460  }
4461  // HANNES: This sould be set in rComplete
4462  res->OrdSgn = 1;
4463  rComplete(res, 1);
4464#ifdef HAVE_PLURAL
4465  if (rIsPluralRing(r))
4466  {
4467    if ( nc_rComplete(r, res, false) ) // no qideal!
4468    {
4469#ifndef NDEBUG
4470      WarnS("error in nc_rComplete");
4471#endif
4472    }
4473  }
4474#endif
4475//  rChangeCurrRing(res);
4476  return res;
4477}
4478
4479ring rAssure_InducedSchreyerOrdering(const ring r, BOOLEAN complete = TRUE, int sgn = 1)
4480{ // TODO: ???? Add leading Syz-comp ordering here...????
4481
4482#if MYTEST
4483    Print("rAssure_InducedSchreyerOrdering(r, complete = %d, sgn = %d): r: \n", complete, sgn);
4484    rWrite(r);
4485#ifdef RDEBUG
4486    rDebugPrint(r);
4487#endif
4488    PrintLn();
4489#endif
4490  assume((sgn == 1) || (sgn == -1));
4491
4492  ring res=rCopy0(r, FALSE, FALSE); // No qideal & ordering copy.
4493
4494  int n = rBlocks(r); // Including trailing zero!
4495
4496  // Create 2 more blocks for prefix/suffix:
4497  res->order=(int *)omAlloc0((n+2)*sizeof(int)); // 0  ..  n+1
4498  res->block0=(int *)omAlloc0((n+2)*sizeof(int));
4499  res->block1=(int *)omAlloc0((n+2)*sizeof(int));
4500  int ** wvhdl =(int **)omAlloc0((n+2)*sizeof(int**));
4501
4502  // Encapsulate all existing blocks between induced Schreyer ordering markers: prefix and suffix!
4503  // Note that prefix and suffix have the same ringorder marker and only differ in block[] parameters!
4504
4505  // new 1st block
4506  int j = 0;
4507  res->order[j] = ringorder_IS; // Prefix
4508  res->block0[j] = res->block1[j] = 0;
4509  // wvhdl[j] = NULL;
4510  j++;
4511
4512  for(int i = 0; (i <= n) && (r->order[i] != 0); i++, j++) // i = [0 .. n-1] <- non-zero old blocks
4513  {
4514    res->order [j] = r->order [i];
4515    res->block0[j] = r->block0[i];
4516    res->block1[j] = r->block1[i];
4517
4518    if (r->wvhdl[i] != NULL)
4519    {
4520      wvhdl[j] = (int*) omMemDup(r->wvhdl[i]);
4521    } // else wvhdl[j] = NULL;
4522  }
4523
4524  // new last block
4525  res->order [j] = ringorder_IS; // Suffix
4526  res->block0[j] = res->block1[j] = sgn; // Sign of v[o]: 1 for C, -1 for c
4527  // wvhdl[j] = NULL;
4528  j++;
4529
4530  // res->order [j] = 0; // The End!
4531  res->wvhdl = wvhdl;
4532
4533  // j == the last zero block now!
4534  assume(j == (n+1));
4535  assume(res->order[0]==ringorder_IS);
4536  assume(res->order[j-1]==ringorder_IS);
4537  assume(res->order[j]==0);
4538
4539
4540  if (complete)
4541  {
4542    rComplete(res, 1);
4543
4544#ifdef HAVE_PLURAL
4545    if (rIsPluralRing(r))
4546    {
4547      if ( nc_rComplete(r, res, false) ) // no qideal!
4548      {
4549#ifndef NDEBUG
4550        WarnS("error in nc_rComplete");      // cleanup?//      rDelete(res);//      return r;      // just go on..
4551#endif
4552      }
4553    }
4554    assume(rIsPluralRing(r) == rIsPluralRing(res));
4555#endif
4556
4557
4558#ifdef HAVE_PLURAL
4559    ring old_ring = r;
4560#endif
4561
4562    if (r->qideal!=NULL)
4563    {
4564      res->qideal= idrCopyR_NoSort(r->qideal, r, res);
4565
4566      assume(id_RankFreeModule(res->qideal, res) == 0);
4567
4568#ifdef HAVE_PLURAL
4569      if( rIsPluralRing(res) )
4570        if( nc_SetupQuotient(res, r, true) )
4571        {
4572//          WarnS("error in nc_SetupQuotient"); // cleanup?      rDelete(res);       return r;  // just go on...?
4573        }
4574
4575#endif
4576      assume(id_RankFreeModule(res->qideal, res) == 0);
4577    }
4578
4579#ifdef HAVE_PLURAL
4580    assume((res->qideal==NULL) == (old_ring->qideal==NULL));
4581    assume(rIsPluralRing(res) == rIsPluralRing(old_ring));
4582    assume(rIsSCA(res) == rIsSCA(old_ring));
4583    assume(ncRingType(res) == ncRingType(old_ring));
4584#endif
4585  }
4586
4587  return res;
4588}
4589
4590ring rAssure_dp_S(const ring r)
4591{
4592  return rAssure_Global(ringorder_dp, ringorder_S,r);
4593}
4594
4595ring rAssure_dp_C(const ring r)
4596{
4597  return rAssure_Global(ringorder_dp, ringorder_C,r);
4598}
4599
4600ring rAssure_C_dp(const ring r)
4601{
4602  return rAssure_Global(ringorder_C, ringorder_dp,r);
4603}
4604
4605
4606
4607/// Finds p^th IS ordering, and returns its position in r->typ[]
4608/// returns -1 if something went wrong!
4609int rGetISPos(const int p, const ring r)
4610{
4611  // Put the reference set F into the ring -ordering -recor
4612#if MYTEST
4613  Print("rIsIS(p: %d)\nF:", p);
4614  PrintLn();
4615#endif
4616
4617  if (r->typ==NULL)
4618  {
4619    dReportError("'rIsIS:' Error: wrong ring! (typ == NULL)");
4620    return -1;
4621  }
4622
4623  int j = p; // Which IS record to use...
4624  for( int pos = 0; pos < r->OrdSize; pos++ )
4625    if( r->typ[pos].ord_typ == ro_is)
4626      if( j-- == 0 )
4627      {
4628        return pos;
4629      }
4630
4631  return -1;
4632}
4633
4634
4635
4636
4637
4638
4639/// Changes r by setting induced ordering parameters: limit and reference leading terms
4640/// F belong to r, we will DO a copy! (same to componentWeights)
4641/// We will use it AS IS!
4642/// returns true is everything was allright!
4643BOOLEAN rSetISReference(const ring r, const ideal F, const int i, const int p, const intvec * componentWeights)
4644{
4645  // Put the reference set F into the ring -ordering -recor
4646
4647  // TEST THAT THERE ARE DEGs!!!
4648  // assume( componentWeights == NULL  ); // ???
4649  if( componentWeights != NULL )
4650  {
4651//    assure that the ring r has degrees!!!
4652//    Add weights to degrees of F[i]
4653  }
4654
4655  if (r->typ==NULL)
4656  {
4657    dReportError("Error: WRONG USE of rSetISReference: wrong ring! (typ == NULL)");
4658    return FALSE;
4659  }
4660
4661
4662  int pos = rGetISPos(p, r);
4663
4664  if( pos == -1 )
4665  {
4666    dReportError("Error: WRONG USE of rSetISReference: specified ordering block was not found!!!" );
4667    return FALSE;
4668  }
4669
4670#if MYTEST
4671  if( i != r->typ[pos].data.is.limit )
4672    Print("Changing record on pos: %d\nOld limit: %d --->> New Limit: %d\n", pos, r->typ[pos].data.is.limit, i);
4673#endif
4674
4675  const ideal FF = id_Copy(F, r); // idrHeadR(F, r, r);
4676
4677
4678  if( r->typ[pos].data.is.F != NULL)
4679  {
4680#if MYTEST
4681    PrintS("Deleting old reference set F... \n");        // idShow(r->typ[pos].data.is.F, r);         PrintLn();
4682#endif
4683    id_Delete(&r->typ[pos].data.is.F, r);
4684    r->typ[pos].data.is.F = NULL;
4685  }
4686
4687  assume(r->typ[pos].data.is.F == NULL);
4688
4689  r->typ[pos].data.is.F = FF; // F is owened by ring now! TODO: delete at the end!
4690
4691  if(r->typ[pos].data.is.componentWeights != NULL)
4692  {
4693#if MYTEST
4694    PrintS("Deleting old componentWeights: "); r->typ[pos].data.is.componentWeights->show(); PrintLn();
4695#endif
4696    delete r->typ[pos].data.is.componentWeights;
4697    r->typ[pos].data.is.componentWeights = NULL;
4698  }
4699
4700
4701  assume(r->typ[pos].data.is.componentWeights == NULL);
4702
4703  if( componentWeights != NULL )
4704    componentWeights = ivCopy(componentWeights); // componentWeights is owened by ring now! TODO: delete at the end!
4705
4706  r->typ[pos].data.is.componentWeights = componentWeights;
4707
4708  r->typ[pos].data.is.limit = i; // First induced component
4709
4710#if MYTEST
4711  PrintS("New reference set FF : \n");        idShow(FF, r, r, 1);         PrintLn();
4712#endif
4713
4714  return TRUE;
4715}
4716
4717
4718void rSetSyzComp(int k, const ring r)
4719{
4720  if(k < 0)
4721  {
4722    dReportError("rSetSyzComp with negative limit!");
4723    return;
4724  }
4725
4726  assume( k >= 0 );
4727  if (TEST_OPT_PROT) Print("{%d}", k);
4728  if ((r->typ!=NULL) && (r->typ[0].ord_typ==ro_syz))
4729  {
4730    if( k == r->typ[0].data.syz.limit )
4731      return; // nothing to do
4732
4733    int i;
4734    if (r->typ[0].data.syz.limit == 0)
4735    {
4736      r->typ[0].data.syz.syz_index = (int*) omAlloc0((k+1)*sizeof(int));
4737      r->typ[0].data.syz.syz_index[0] = 0;
4738      r->typ[0].data.syz.curr_index = 1;
4739    }
4740    else
4741    {
4742      r->typ[0].data.syz.syz_index = (int*)
4743        omReallocSize(r->typ[0].data.syz.syz_index,
4744                (r->typ[0].data.syz.limit+1)*sizeof(int),
4745                (k+1)*sizeof(int));
4746    }
4747    for (i=r->typ[0].data.syz.limit + 1; i<= k; i++)
4748    {
4749      r->typ[0].data.syz.syz_index[i] =
4750        r->typ[0].data.syz.curr_index;
4751    }
4752    if(k < r->typ[0].data.syz.limit) // ?
4753    {
4754#ifndef NDEBUG
4755      Warn("rSetSyzComp called with smaller limit (%d) as before (%d)", k, r->typ[0].data.syz.limit);
4756#endif
4757      r->typ[0].data.syz.curr_index = 1 + r->typ[0].data.syz.syz_index[k];
4758    }
4759
4760
4761    r->typ[0].data.syz.limit = k;
4762    r->typ[0].data.syz.curr_index++;
4763  }
4764  else if(
4765            (r->typ!=NULL) &&
4766            (r->typ[0].ord_typ==ro_isTemp)
4767           )
4768  {
4769//      (r->typ[currRing->typ[0].data.isTemp.suffixpos].data.is.limit == k)
4770#ifndef NDEBUG
4771    Warn("rSetSyzComp(%d) in an IS ring! Be careful!", k);
4772#endif
4773  }
4774  else
4775  if ((r->order[0]!=ringorder_c) && (k!=0)) // ???
4776  {
4777    dReportError("syzcomp in incompatible ring");
4778  }
4779#ifdef PDEBUG
4780  extern int pDBsyzComp;
4781  pDBsyzComp=k;
4782#endif
4783}
4784
4785// return the max-comonent wchich has syzIndex i
4786int rGetMaxSyzComp(int i, const ring r)
4787{
4788  if ((r->typ!=NULL) && (r->typ[0].ord_typ==ro_syz) &&
4789      r->typ[0].data.syz.limit > 0 && i > 0)
4790  {
4791    assume(i <= r->typ[0].data.syz.limit);
4792    int j;
4793    for (j=0; j<r->typ[0].data.syz.limit; j++)
4794    {
4795      if (r->typ[0].data.syz.syz_index[j] == i  &&
4796          r->typ[0].data.syz.syz_index[j+1] != i)
4797      {
4798        assume(r->typ[0].data.syz.syz_index[j+1] == i+1);
4799        return j;
4800      }
4801    }
4802    return r->typ[0].data.syz.limit;
4803  }
4804  else
4805  {
4806    return 0;
4807  }
4808}
4809
4810BOOLEAN rRing_is_Homog(ring r)
4811{
4812  if (r == NULL) return FALSE;
4813  int i, j, nb = rBlocks(r);
4814  for (i=0; i<nb; i++)
4815  {
4816    if (r->wvhdl[i] != NULL)
4817    {
4818      int length = r->block1[i] - r->block0[i];
4819      int* wvhdl = r->wvhdl[i];
4820      if (r->order[i] == ringorder_M) length *= length;
4821      assume(omSizeOfAddr(wvhdl) >= length*sizeof(int));
4822
4823      for (j=0; j< length; j++)
4824      {
4825        if (wvhdl[j] != 0 && wvhdl[j] != 1) return FALSE;
4826      }
4827    }
4828  }
4829  return TRUE;
4830}
4831
4832BOOLEAN rRing_has_CompLastBlock(ring r)
4833{
4834  assume(r != NULL);
4835  int lb = rBlocks(r) - 2;
4836  return (r->order[lb] == ringorder_c || r->order[lb] == ringorder_C);
4837}
4838
4839n_coeffType rFieldType(ring r)
4840{
4841  return (r->cf->type);
4842  if (rField_is_Zp(r))     return n_Zp;
4843  if (rField_is_Q(r))      return n_Q;
4844  if (rField_is_R(r))      return n_R;
4845  if (rField_is_GF(r))     return n_GF;
4846  if (rField_is_long_R(r)) return n_long_R;
4847  if (rField_is_Zp_a(r))   return getCoeffType(r->cf);
4848  if (rField_is_Q_a(r))    return getCoeffType(r->cf);
4849  if (rField_is_long_C(r)) return n_long_C;
4850  #ifdef HAVE_RINGS
4851   if (rField_is_Ring_Z(r)) return n_Z;
4852   if (rField_is_Ring_ModN(r)) return n_Zn;
4853   if (rField_is_Ring_PtoM(r)) return n_Zpn;
4854   if (rField_is_Ring_2toM(r)) return  n_Z2m;
4855  #endif
4856
4857  return n_unknown;
4858}
4859
4860int64 * rGetWeightVec(ring r)
4861{
4862  assume(r!=NULL);
4863  assume(r->OrdSize>0);
4864  int i=0;
4865  while((r->typ[i].ord_typ!=ro_wp64) && (r->typ[i].ord_typ>0)) i++;
4866  assume(r->typ[i].ord_typ==ro_wp64);
4867  return (int64*)(r->typ[i].data.wp64.weights64);
4868}
4869
4870void rSetWeightVec(ring r, int64 *wv)
4871{
4872  assume(r!=NULL);
4873  assume(r->OrdSize>0);
4874  assume(r->typ[0].ord_typ==ro_wp64);
4875  memcpy(r->typ[0].data.wp64.weights64,wv,r->N*sizeof(int64));
4876}
4877
4878#include <ctype.h>
4879
4880static int rRealloc1(ring r, int size, int pos)
4881{
4882  r->order=(int*)omReallocSize(r->order, size*sizeof(int), (size+1)*sizeof(int));
4883  r->block0=(int*)omReallocSize(r->block0, size*sizeof(int), (size+1)*sizeof(int));
4884  r->block1=(int*)omReallocSize(r->block1, size*sizeof(int), (size+1)*sizeof(int));
4885  r->wvhdl=(int **)omReallocSize(r->wvhdl,size*sizeof(int *), (size+1)*sizeof(int *));
4886  for(int k=size; k>pos; k--) r->wvhdl[k]=r->wvhdl[k-1];
4887  r->order[size]=0;
4888  size++;
4889  return size;
4890}
4891#if 0 // currently unused
4892static int rReallocM1(ring r, int size, int pos)
4893{
4894  r->order=(int*)omReallocSize(r->order, size*sizeof(int), (size-1)*sizeof(int));
4895  r->block0=(int*)omReallocSize(r->block0, size*sizeof(int), (size-1)*sizeof(int));
4896  r->block1=(int*)omReallocSize(r->block1, size*sizeof(int), (size-1)*sizeof(int));
4897  r->wvhdl=(int **)omReallocSize(r->wvhdl,size*sizeof(int *), (size-1)*sizeof(int *));
4898  for(int k=pos+1; k<size; k++) r->wvhdl[k]=r->wvhdl[k+1];
4899  size--;
4900  return size;
4901}
4902#endif
4903static void rOppWeight(int *w, int l)
4904{
4905  int i2=(l+1)/2;
4906  for(int j=0; j<=i2; j++)
4907  {
4908    int t=w[j];
4909    w[j]=w[l-j];
4910    w[l-j]=t;
4911  }
4912}
4913
4914#define rOppVar(R,I) (rVar(R)+1-I)
4915
4916ring rOpposite(ring src)
4917  /* creates an opposite algebra of R */
4918  /* that is R^opp, where f (*^opp) g = g*f  */
4919  /* treats the case of qring */
4920{
4921  if (src == NULL) return(NULL);
4922
4923#ifdef RDEBUG
4924  rTest(src);
4925#endif
4926
4927  //rChangeCurrRing(src);
4928
4929#ifdef RDEBUG
4930  rTest(src);
4931//  rWrite(src);
4932//  rDebugPrint(src);
4933#endif
4934
4935
4936  ring r = rCopy0(src,FALSE); /* qideal will be deleted later on!!! */
4937
4938  // change vars v1..vN -> vN..v1
4939  int i;
4940  int i2 = (rVar(r)-1)/2;
4941  for(i=i2; i>=0; i--)
4942  {
4943    // index: 0..N-1
4944    //Print("ex var names: %d <-> %d\n",i,rOppVar(r,i));
4945    // exchange names
4946    char *p;
4947    p = r->names[rVar(r)-1-i];
4948    r->names[rVar(r)-1-i] = r->names[i];
4949    r->names[i] = p;
4950  }
4951//  i2=(rVar(r)+1)/2;
4952//  for(int i=i2; i>0; i--)
4953//  {
4954//    // index: 1..N
4955//    //Print("ex var places: %d <-> %d\n",i,rVar(r)+1-i);
4956//    // exchange VarOffset
4957//    int t;
4958//    t=r->VarOffset[i];
4959//    r->VarOffset[i]=r->VarOffset[rOppVar(r,i)];
4960//    r->VarOffset[rOppVar(r,i)]=t;
4961//  }
4962  // change names:
4963  for (i=rVar(r)-1; i>=0; i--)
4964  {
4965    char *p=r->names[i];
4966    if(isupper(*p)) *p = tolower(*p);
4967    else            *p = toupper(*p);
4968  }
4969  // change ordering: listing
4970  // change ordering: compare
4971//  for(i=0; i<r->OrdSize; i++)
4972//  {
4973//    int t,tt;
4974//    switch(r->typ[i].ord_typ)
4975//    {
4976//      case ro_dp:
4977//      //
4978//        t=r->typ[i].data.dp.start;
4979//        r->typ[i].data.dp.start=rOppVar(r,r->typ[i].data.dp.end);
4980//        r->typ[i].data.dp.end=rOppVar(r,t);
4981//        break;
4982//      case ro_wp:
4983//      case ro_wp_neg:
4984//      {
4985//        t=r->typ[i].data.wp.start;
4986//        r->typ[i].data.wp.start=rOppVar(r,r->typ[i].data.wp.end);
4987//        r->typ[i].data.wp.end=rOppVar(r,t);
4988//        // invert r->typ[i].data.wp.weights
4989//        rOppWeight(r->typ[i].data.wp.weights,
4990//                   r->typ[i].data.wp.end-r->typ[i].data.wp.start);
4991//        break;
4992//      }
4993//      //case ro_wp64:
4994//      case ro_syzcomp:
4995//      case ro_syz:
4996//         WerrorS("not implemented in rOpposite");
4997//         // should not happen
4998//         break;
4999//
5000//      case ro_cp:
5001//        t=r->typ[i].data.cp.start;
5002//        r->typ[i].data.cp.start=rOppVar(r,r->typ[i].data.cp.end);
5003//        r->typ[i].data.cp.end=rOppVar(r,t);
5004//        break;
5005//      case ro_none:
5006//      default:
5007//       Werror("unknown type in rOpposite(%d)",r->typ[i].ord_typ);
5008//       break;
5009//    }
5010//  }
5011  // Change order/block structures (needed for rPrint, rAdd etc.)
5012  int j=0;
5013  int l=rBlocks(src);
5014  for(i=0; src->order[i]!=0; i++)
5015  {
5016    switch (src->order[i])
5017    {
5018      case ringorder_c: /* c-> c */
5019      case ringorder_C: /* C-> C */
5020      case ringorder_no /*=0*/: /* end-of-block */
5021        r->order[j]=src->order[i];
5022        j++; break;
5023      case ringorder_lp: /* lp -> rp */
5024        r->order[j]=ringorder_rp;
5025        r->block0[j]=rOppVar(r, src->block1[i]);
5026        r->block1[j]=rOppVar(r, src->block0[i]);
5027        break;
5028      case ringorder_rp: /* rp -> lp */
5029        r->order[j]=ringorder_lp;
5030        r->block0[j]=rOppVar(r, src->block1[i]);
5031        r->block1[j]=rOppVar(r, src->block0[i]);
5032        break;
5033      case ringorder_dp: /* dp -> a(1..1),ls */
5034      {
5035        l=rRealloc1(r,l,j);
5036        r->order[j]=ringorder_a;
5037        r->block0[j]=rOppVar(r, src->block1[i]);
5038        r->block1[j]=rOppVar(r, src->block0[i]);
5039        r->wvhdl[j]=(int*)omAlloc((r->block1[j]-r->block0[j]+1)*sizeof(int));
5040        for(int k=r->block0[j]; k<=r->block1[j]; k++)
5041          r->wvhdl[j][k-r->block0[j]]=1;
5042        j++;
5043        r->order[j]=ringorder_ls;
5044        r->block0[j]=rOppVar(r, src->block1[i]);
5045        r->block1[j]=rOppVar(r, src->block0[i]);
5046        j++;
5047        break;
5048      }
5049      case ringorder_Dp: /* Dp -> a(1..1),rp */
5050      {
5051        l=rRealloc1(r,l,j);
5052        r->order[j]=ringorder_a;
5053        r->block0[j]=rOppVar(r, src->block1[i]);
5054        r->block1[j]=rOppVar(r, src->block0[i]);
5055        r->wvhdl[j]=(int*)omAlloc((r->block1[j]-r->block0[j]+1)*sizeof(int));
5056        for(int k=r->block0[j]; k<=r->block1[j]; k++)
5057          r->wvhdl[j][k-r->block0[j]]=1;
5058        j++;
5059        r->order[j]=ringorder_rp;
5060        r->block0[j]=rOppVar(r, src->block1[i]);
5061        r->block1[j]=rOppVar(r, src->block0[i]);
5062        j++;
5063        break;
5064      }
5065      case ringorder_wp: /* wp -> a(...),ls */
5066      {
5067        l=rRealloc1(r,l,j);
5068        r->order[j]=ringorder_a;
5069        r->block0[j]=rOppVar(r, src->block1[i]);
5070        r->block1[j]=rOppVar(r, src->block0[i]);
5071        r->wvhdl[j]=r->wvhdl[j+1]; r->wvhdl[j+1]=NULL;
5072        rOppWeight(r->wvhdl[j], r->block1[j]-r->block0[j]);
5073        j++;
5074        r->order[j]=ringorder_ls;
5075        r->block0[j]=rOppVar(r, src->block1[i]);
5076        r->block1[j]=rOppVar(r, src->block0[i]);
5077        j++;
5078        break;
5079      }
5080      case ringorder_Wp: /* Wp -> a(...),rp */
5081      {
5082        l=rRealloc1(r,l,j);
5083        r->order[j]=ringorder_a;
5084        r->block0[j]=rOppVar(r, src->block1[i]);
5085        r->block1[j]=rOppVar(r, src->block0[i]);
5086        r->wvhdl[j]=r->wvhdl[j+1]; r->wvhdl[j+1]=NULL;
5087        rOppWeight(r->wvhdl[j], r->block1[j]-r->block0[j]);
5088        j++;
5089        r->order[j]=ringorder_rp;
5090        r->block0[j]=rOppVar(r, src->block1[i]);
5091        r->block1[j]=rOppVar(r, src->block0[i]);
5092        j++;
5093        break;
5094      }
5095      case ringorder_M: /* M -> M */
5096      {
5097        r->order[j]=ringorder_M;
5098        r->block0[j]=rOppVar(r, src->block1[i]);
5099        r->block1[j]=rOppVar(r, src->block0[i]);
5100        int n=r->block1[j]-r->block0[j];
5101        /* M is a (n+1)x(n+1) matrix */
5102        for (int nn=0; nn<=n; nn++)
5103        {
5104          rOppWeight(&(r->wvhdl[j][nn*(n+1)]), n /*r->block1[j]-r->block0[j]*/);
5105        }
5106        j++;
5107        break;
5108      }
5109      case ringorder_a: /*  a(...),ls -> wp/dp */
5110      {
5111        r->block0[j]=rOppVar(r, src->block1[i]);
5112        r->block1[j]=rOppVar(r, src->block0[i]);
5113        rOppWeight(r->wvhdl[j], r->block1[j]-r->block0[j]);
5114        if (src->order[i+1]==ringorder_ls)
5115        {
5116          r->order[j]=ringorder_wp;
5117          i++;
5118          //l=rReallocM1(r,l,j);
5119        }
5120        else
5121        {
5122          r->order[j]=ringorder_a;
5123        }
5124        j++;
5125        break;
5126      }
5127      // not yet done:
5128      case ringorder_ls:
5129      case ringorder_rs:
5130      case ringorder_ds:
5131      case ringorder_Ds:
5132      case ringorder_ws:
5133      case ringorder_Ws:
5134      // should not occur:
5135      case ringorder_S:
5136      case ringorder_IS:
5137      case ringorder_s:
5138      case ringorder_aa:
5139      case ringorder_L:
5140      case ringorder_unspec:
5141        Werror("order %s not (yet) supported", rSimpleOrdStr(src->order[i]));
5142        break;
5143    }
5144  }
5145  rComplete(r);
5146
5147
5148#ifdef RDEBUG
5149  rTest(r);
5150#endif
5151
5152  //rChangeCurrRing(r);
5153
5154#ifdef RDEBUG
5155  rTest(r);
5156//  rWrite(r);
5157//  rDebugPrint(r);
5158#endif
5159
5160
5161#ifdef HAVE_PLURAL
5162  // now, we initialize a non-comm structure on r
5163  if (rIsPluralRing(src))
5164  {
5165//    assume( currRing == r);
5166
5167    int *perm       = (int *)omAlloc0((rVar(r)+1)*sizeof(int));
5168    int *par_perm   = NULL;
5169    nMapFunc nMap   = n_SetMap(src->cf,r->cf);
5170    int ni,nj;
5171    for(i=1; i<=r->N; i++)
5172    {
5173      perm[i] = rOppVar(r,i);
5174    }
5175
5176    matrix C = mpNew(rVar(r),rVar(r));
5177    matrix D = mpNew(rVar(r),rVar(r));
5178
5179    for (i=1; i< rVar(r); i++)
5180    {
5181      for (j=i+1; j<=rVar(r); j++)
5182      {
5183        ni = r->N +1 - i;
5184        nj = r->N +1 - j; /* i<j ==>   nj < ni */
5185
5186        assume(MATELEM(src->GetNC()->C,i,j) != NULL);
5187        MATELEM(C,nj,ni) = p_PermPoly(MATELEM(src->GetNC()->C,i,j),perm,src,r, nMap,par_perm,rPar(src));
5188
5189        if(MATELEM(src->GetNC()->D,i,j) != NULL)
5190          MATELEM(D,nj,ni) = p_PermPoly(MATELEM(src->GetNC()->D,i,j),perm,src,r, nMap,par_perm,rPar(src));
5191      }
5192    }
5193
5194    id_Test((ideal)C, r);
5195    id_Test((ideal)D, r);
5196
5197    if (nc_CallPlural(C, D, NULL, NULL, r, false, false, true, r)) // no qring setup!
5198      WarnS("Error initializing non-commutative multiplication!");
5199
5200#ifdef RDEBUG
5201    rTest(r);
5202//    rWrite(r);
5203//    rDebugPrint(r);
5204#endif
5205
5206    assume( r->GetNC()->IsSkewConstant == src->GetNC()->IsSkewConstant);
5207
5208    omFreeSize((ADDRESS)perm,(rVar(r)+1)*sizeof(int));
5209  }
5210#endif /* HAVE_PLURAL */
5211
5212  /* now oppose the qideal for qrings */
5213  if (src->qideal != NULL)
5214  {
5215    id_Delete(&(r->qideal), r);
5216
5217#ifdef HAVE_PLURAL
5218    r->qideal = idOppose(src, src->qideal, r); // into the currRing: r
5219#else
5220    r->qideal = id_Copy(src->qideal, r); // ?
5221#endif
5222
5223#ifdef HAVE_PLURAL
5224    if( rIsPluralRing(r) )
5225    {
5226      nc_SetupQuotient(r);
5227#ifdef RDEBUG
5228      rTest(r);
5229//      rWrite(r);
5230//      rDebugPrint(r);
5231#endif
5232    }
5233#endif
5234  }
5235#ifdef HAVE_PLURAL
5236  if( rIsPluralRing(r) )
5237    assume( ncRingType(r) == ncRingType(src) );
5238#endif
5239  rTest(r);
5240
5241  return r;
5242}
5243
5244ring rEnvelope(ring R)
5245  /* creates an enveloping algebra of R */
5246  /* that is R^e = R \tensor_K R^opp */
5247{
5248  ring Ropp = rOpposite(R);
5249  ring Renv = NULL;
5250  int stat = rSum(R, Ropp, Renv); /* takes care of qideals */
5251  if ( stat <=0 )
5252    WarnS("Error in rEnvelope at rSum");
5253  rTest(Renv);
5254  return Renv;
5255}
5256
5257#ifdef HAVE_PLURAL
5258BOOLEAN nc_rComplete(const ring src, ring dest, bool bSetupQuotient)
5259/* returns TRUE is there were errors */
5260/* dest is actualy equals src with the different ordering */
5261/* we map src->nc correctly to dest->src */
5262/* to be executed after rComplete, before rChangeCurrRing */
5263{
5264// NOTE: Originally used only by idElimination to transfer NC structure to dest
5265// ring created by dirty hack (without nc_CallPlural)
5266  rTest(src);
5267
5268  assume(!rIsPluralRing(dest)); // destination must be a newly constructed commutative ring
5269
5270  if (!rIsPluralRing(src))
5271  {
5272    return FALSE;
5273  }
5274
5275  const int N = dest->N;
5276
5277  assume(src->N == N);
5278
5279//  ring save = currRing;
5280
5281//  if (dest != save)
5282//    rChangeCurrRing(dest);
5283
5284  const ring srcBase = src;
5285
5286  assume( n_SetMap(srcBase->cf,dest->cf) == n_SetMap(dest->cf,dest->cf) ); // currRing is important here!
5287
5288  matrix C = mpNew(N,N); // ring independent
5289  matrix D = mpNew(N,N);
5290
5291  matrix C0 = src->GetNC()->C;
5292  matrix D0 = src->GetNC()->D;
5293
5294  // map C and D into dest
5295  for (int i = 1; i < N; i++)
5296  {
5297    for (int j = i + 1; j <= N; j++)
5298    {
5299      const number n = n_Copy(p_GetCoeff(MATELEM(C0,i,j), srcBase), srcBase->cf); // src, mapping for coeffs into currRing = dest!
5300      const poly   p = p_NSet(n, dest);
5301      MATELEM(C,i,j) = p;
5302      if (MATELEM(D0,i,j) != NULL)
5303        MATELEM(D,i,j) = prCopyR(MATELEM(D0,i,j), srcBase, dest); // ?
5304    }
5305  }
5306  /* One must test C and D _only_ in r->GetNC()->basering!!! not in r!!! */
5307
5308  id_Test((ideal)C, dest);
5309  id_Test((ideal)D, dest);
5310
5311  if (nc_CallPlural(C, D, NULL, NULL, dest, bSetupQuotient, false, true, dest)) // also takes care about quotient ideal
5312  {
5313    //WarnS("Error transferring non-commutative structure");
5314    // error message should be in the interpreter interface
5315
5316    mp_Delete(&C, dest);
5317    mp_Delete(&D, dest);
5318
5319//    if (currRing != save)
5320//       rChangeCurrRing(save);
5321
5322    return TRUE;
5323  }
5324
5325//  mp_Delete(&C, dest); // used by nc_CallPlural!
5326//  mp_Delete(&D, dest);
5327
5328//  if (dest != save)
5329//    rChangeCurrRing(save);
5330
5331  assume(rIsPluralRing(dest));
5332  return FALSE;
5333}
5334#endif
5335
5336void rModify_a_to_A(ring r)
5337// to be called BEFORE rComplete:
5338// changes every Block with a(...) to A(...)
5339{
5340   int i=0;
5341   int j;
5342   while(r->order[i]!=0)
5343   {
5344      if (r->order[i]==ringorder_a)
5345      {
5346        r->order[i]=ringorder_a64;
5347        int *w=r->wvhdl[i];
5348        int64 *w64=(int64 *)omAlloc((r->block1[i]-r->block0[i]+1)*sizeof(int64));
5349        for(j=r->block1[i]-r->block0[i];j>=0;j--)
5350                w64[j]=(int64)w[j];
5351        r->wvhdl[i]=(int*)w64;
5352        omFreeSize(w,(r->block1[i]-r->block0[i]+1)*sizeof(int));
5353      }
5354      i++;
5355   }
5356}
5357
5358
5359BOOLEAN rMinpolyIsNULL(const ring r)
5360{
5361  assume(r != NULL);
5362  const coeffs C = r->cf;
5363  assume(C != NULL);
5364
5365  if( rField_is_Extension(r) )
5366  {
5367    const ring R = C->extRing;
5368    assume( R != NULL );
5369    return idIs0(R->minideal);
5370  }
5371  return TRUE;
5372}
5373
5374poly rGetVar(const int varIndex, const ring r)
5375{
5376    poly p = p_ISet(1, r);
5377    p_SetExp(p, varIndex, 1, r);
5378    p_Setm(p, r);
5379    return p;
5380}
5381
Note: See TracBrowser for help on using the repository browser.