source: git/libpolys/polys/monomials/ring.cc @ 4470c0e

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