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

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