source: git/libpolys/polys/monomials/ring.cc @ 4c1b5d

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