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

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