source: git/libpolys/polys/monomials/ring.cc @ 55567a

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