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

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