source: git/kernel/ring.cc @ 24fd70

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