source: git/libpolys/polys/monomials/ring.cc @ 417a91a

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