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

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