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

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