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

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