source: git/libpolys/polys/monomials/ring.cc @ cb72d2

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