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

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