source: git/libpolys/polys/monomials/ring.cc @ 4a45a4e

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