source: git/libpolys/polys/monomials/ring.cc @ 977ebb

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