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

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