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

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