source: git/libpolys/polys/monomials/ring.cc @ 90934a

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