source: git/libpolys/polys/monomials/ring.cc @ 868d77d

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