source: git/kernel/ring.cc @ e5fc4d4

spielwiese
Last change on this file since e5fc4d4 was e5fc4d4, checked in by Viktor Levandovskyy <levandov@…>, 19 years ago
*levandov: big update - in idElimination, the ordering check introduced, minor fixes throughout the code git-svn-id: file:///usr/local/Singular/svn/trunk@7888 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 98.2 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: ring.cc,v 1.33 2005-04-22 18:09:42 levandov Exp $ */
5
6/*
7* ABSTRACT - the interpreter related ring operations
8*/
9
10/* includes */
11#include <math.h>
12#include "mod2.h"
13#include "structs.h"
14#include "omalloc.h"
15#include "polys.h"
16#include "numbers.h"
17#include "febase.h"
18#include "ipconv.h"
19#include "intvec.h"
20#include "longalg.h"
21#include "ffields.h"
22#include "ideals.h"
23#include "ring.h"
24#include "prCopy.h"
25#include "../Singular/ipshell.h"
26#include "p_Procs.h"
27#ifdef HAVE_PLURAL
28#include "gring.h"
29#include "matpol.h"
30#include "maps.h"
31#endif
32
33#define BITS_PER_LONG 8*SIZEOF_LONG
34
35static const char * const ringorder_name[] =
36{
37  " ?", //ringorder_no = 0,
38  "a", //ringorder_a,
39  "c", //ringorder_c,
40  "C", //ringorder_C,
41  "M", //ringorder_M,
42  "S", //ringorder_S,
43  "s", //ringorder_s,
44  "lp", //ringorder_lp,
45  "dp", //ringorder_dp,
46  "rp", //ringorder_rp,
47  "Dp", //ringorder_Dp,
48  "wp", //ringorder_wp,
49  "Wp", //ringorder_Wp,
50  "ls", //ringorder_ls,
51  "ds", //ringorder_ds,
52  "Ds", //ringorder_Ds,
53  "ws", //ringorder_ws,
54  "Ws", //ringorder_Ws,
55  "L", //ringorder_L,
56  "aa", //ringorder_aa
57  " _" //ringorder_unspec
58};
59
60const char * rSimpleOrdStr(int ord)
61{
62  return ringorder_name[ord];
63}
64
65// unconditionally deletes fields in r
66void rDelete(ring r);
67// set r->VarL_Size, r->VarL_Offset, r->VarL_LowIndex
68static void rSetVarL(ring r);
69// get r->divmask depending on bits per exponent
70static unsigned long rGetDivMask(int bits);
71// right-adjust r->VarOffset
72static void rRightAdjustVarOffset(ring r);
73static void rOptimizeLDeg(ring r);
74
75/*0 implementation*/
76//BOOLEAN rField_is_R(ring r=currRing)
77//{
78//  if (r->ch== -1)
79//  {
80//    if (r->float_len==(short)0) return TRUE;
81//  }
82//  return FALSE;
83//}
84
85// internally changes the gloabl ring and resets the relevant
86// global variables:
87// complete == FALSE : only delete operations are enabled
88// complete == TRUE  : full reset of all variables
89void rChangeCurrRing(ring r)
90{
91  /*------------ set global ring vars --------------------------------*/
92  currRing = r;
93  currQuotient=NULL;
94  if (r != NULL)
95  {
96    rTest(r);
97    /*------------ set global ring vars --------------------------------*/
98    currQuotient=r->qideal;
99
100    /*------------ global variables related to coefficients ------------*/
101    nSetChar(r);
102
103    /*------------ global variables related to polys -------------------*/
104    pSetGlobals(r);
105  }
106}
107
108
109ring rDefault(int ch, int N, char **n)
110{
111  ring r=(ring) omAlloc0Bin(sip_sring_bin);
112  r->ch    = ch;
113  r->N     = N;
114  /*r->P     = 0; Alloc0 */
115  /*names*/
116  r->names = (char **) omAlloc0(N * sizeof(char_ptr));
117  int i;
118  for(i=0;i<N;i++)
119  {
120    r->names[i]  = omStrDup(n[i]);
121  }
122  /*weights: entries for 2 blocks: NULL*/
123  r->wvhdl = (int **)omAlloc0(2 * sizeof(int_ptr));
124  /*order: lp,0*/
125  r->order = (int *) omAlloc(2* sizeof(int *));
126  r->block0 = (int *)omAlloc0(2 * sizeof(int *));
127  r->block1 = (int *)omAlloc0(2 * sizeof(int *));
128  /* ringorder dp for the first block: var 1..N */
129  r->order[0]  = ringorder_lp;
130  r->block0[0] = 1;
131  r->block1[0] = N;
132  /* the last block: everything is 0 */
133  r->order[1]  = 0;
134  /*polynomial ring*/
135  r->OrdSgn    = 1;
136
137  /* complete ring intializations */
138  rComplete(r);
139  return r;
140}
141
142///////////////////////////////////////////////////////////////////////////
143//
144// rInit: define a new ring from sleftv's
145//
146
147/////////////////////////////
148// Auxillary functions
149//
150
151// check intvec, describing the ordering
152BOOLEAN rCheckIV(intvec *iv)
153{
154  if ((iv->length()!=2)&&(iv->length()!=3))
155  {
156    WerrorS("weights only for orderings wp,ws,Wp,Ws,a,M");
157    return TRUE;
158  }
159  return FALSE;
160}
161
162int rTypeOfMatrixOrder(intvec * order)
163{
164  int i=0,j,typ=1;
165  int sz = (int)sqrt((double)(order->length()-2));
166
167  while ((i<sz) && (typ==1))
168  {
169    j=0;
170    while ((j<sz) && ((*order)[j*sz+i+2]==0)) j++;
171    if (j>=sz)
172    {
173      typ = 0;
174      WerrorS("Matrix order not complete");
175    }
176    else if ((*order)[j*sz+i+2]<0)
177      typ = -1;
178    else
179      i++;
180  }
181  return typ;
182}
183
184// set R->order, R->block, R->wvhdl, r->OrdSgn from sleftv
185BOOLEAN rSleftvOrdering2Ordering(sleftv *ord, ring R);
186
187// get array of strings from list of sleftv's
188BOOLEAN rSleftvList2StringArray(sleftv* sl, char** p);
189
190
191/*2
192 * set a new ring from the data:
193 s: name, chr: ch, varnames: rv, ordering: ord, typ: typ
194 */
195
196int r_IsRingVar(char *n, ring r)
197{
198  if ((r!=NULL) && (r->names!=NULL))
199  {
200    for (int i=0; i<r->N; i++)
201    {
202      if (r->names[i]==NULL) return -1;
203      if (strcmp(n,r->names[i]) == 0) return (int)i;
204    }
205  }
206  return -1;
207}
208
209
210void rWrite(ring r)
211{
212  if ((r==NULL)||(r->order==NULL))
213    return; /*to avoid printing after errors....*/
214
215  int nblocks=rBlocks(r);
216
217  // omCheckAddrSize(r,sizeof(ip_sring));
218  omCheckAddrSize(r->order,nblocks*sizeof(int));
219  omCheckAddrSize(r->block0,nblocks*sizeof(int));
220  omCheckAddrSize(r->block1,nblocks*sizeof(int));
221  omCheckAddrSize(r->wvhdl,nblocks*sizeof(int_ptr));
222  omCheckAddrSize(r->names,r->N*sizeof(char_ptr));
223
224  nblocks--;
225
226
227  if (rField_is_GF(r))
228  {
229    Print("//   # ground field : %d\n",rInternalChar(r));
230    Print("//   primitive element : %s\n", r->parameter[0]);
231    if (r==currRing)
232    {
233      StringSetS("//   minpoly        : ");
234      nfShowMipo();PrintS(StringAppendS("\n"));
235    }
236  }
237  else
238  {
239    PrintS("//   characteristic : ");
240    if ( rField_is_R(r) )             PrintS("0 (real)\n");  /* R */
241    else if ( rField_is_long_R(r) )
242      Print("0 (real:%d digits, additional %d digits)\n",
243             r->float_len,r->float_len2);  /* long R */
244    else if ( rField_is_long_C(r) )
245      Print("0 (complex:%d digits, additional %d digits)\n",
246             r->float_len, r->float_len2);  /* long C */
247    else
248      Print ("%d\n",rChar(r)); /* Fp(a) */
249    if (r->parameter!=NULL)
250    {
251      Print ("//   %d parameter    : ",rPar(r));
252      char **sp=r->parameter;
253      int nop=0;
254      while (nop<rPar(r))
255      {
256        PrintS(*sp);
257        PrintS(" ");
258        sp++; nop++;
259      }
260      PrintS("\n//   minpoly        : ");
261      if ( rField_is_long_C(r) )
262      {
263        // i^2+1:
264        Print("(%s^2+1)\n",r->parameter[0]);
265      }
266      else if (r->minpoly==NULL)
267      {
268        PrintS("0\n");
269      }
270      else if (r==currRing)
271      {
272        StringSetS(""); nWrite(r->minpoly); PrintS(StringAppendS("\n"));
273      }
274      else
275      {
276        PrintS("...\n");
277      }
278    }
279  }
280  Print("//   number of vars : %d",r->N);
281
282  //for (nblocks=0; r->order[nblocks]; nblocks++);
283  nblocks=rBlocks(r)-1;
284
285  for (int l=0, nlen=0 ; l<nblocks; l++)
286  {
287    int i;
288    Print("\n//        block %3d : ",l+1);
289
290    Print("ordering %s", rSimpleOrdStr(r->order[l]));
291
292    if ((r->order[l] >= ringorder_lp)
293    ||(r->order[l] == ringorder_M)
294    ||(r->order[l] == ringorder_a)
295    ||(r->order[l] == ringorder_aa))
296    {
297      PrintS("\n//                  : names    ");
298      for (i = r->block0[l]-1; i<r->block1[l]; i++)
299      {
300        nlen = strlen(r->names[i]);
301        Print("%s ",r->names[i]);
302      }
303    }
304#ifndef NDEBUG
305    else if (r->order[l] == ringorder_s)
306    {
307      Print("  syzcomp at %d",r->typ[l].data.syz.limit);
308    }
309#endif
310
311    if (r->wvhdl[l]!=NULL)
312    {
313      for (int j= 0;
314           j<(r->block1[l]-r->block0[l]+1)*(r->block1[l]-r->block0[l]+1);
315           j+=i)
316      {
317        PrintS("\n//                  : weights  ");
318        for (i = 0; i<=r->block1[l]-r->block0[l]; i++)
319        {
320          Print("%*d " ,nlen,r->wvhdl[l][i+j],i+j);
321        }
322        if (r->order[l]!=ringorder_M) break;
323      }
324    }
325  }
326#ifdef HAVE_PLURAL
327  if ((r->nc!=NULL) && (r==currRing))
328  {
329    poly pl=NULL;
330    int nl;
331    PrintS("\n//   noncommutative relations:");
332    //    Print("\n//   noncommutative relations (type %d):",(int)r->nc->type);
333    for (int i = 1; i<r->N; i++)
334    {
335      for (int j = i+1; j<=r->N; j++)
336      {
337        nl=nIsOne(p_GetCoeff(MATELEM(r->nc->C,i,j),r));
338        if ((MATELEM(r->nc->D,i,j)!=NULL)||(!nl))
339        {
340          Print("\n//    %s%s=",r->names[j-1],r->names[i-1]);
341          pl=MATELEM(r->nc->MT[UPMATELEM(i,j,r->N)],1,1);
342          pWrite0(pl);
343        }
344      }
345    }
346#ifdef PDEBUG
347    Print("\n//   noncommutative type:%d",r->nc->type);
348    Print("\n//   is skew constant:%d",r->nc->IsSkewConstant);
349    Print("\n//   ref:%d",r->nc->ref);
350#endif
351  }
352#endif
353  if (r->qideal!=NULL)
354  {
355    PrintS("\n// quotient ring from ideal");
356    if (r==currRing)
357    {
358      PrintLn();
359      iiWriteMatrix((matrix)r->qideal,"_",1);
360    }
361    else PrintS(" ...");
362  }
363}
364
365void rDelete(ring r)
366{
367  int i, j;
368
369  if (r == NULL) return;
370
371#ifdef HAVE_PLURAL
372  if (r->nc != NULL)
373  {
374    if (r->nc->ref>1) /* in use by somebody else */
375    {
376      r->nc->ref--;
377    }
378    else
379    {
380      ncKill(r);
381    }
382  }
383#endif
384  nKillChar(r);
385  rUnComplete(r);
386  // delete order stuff
387  if (r->order != NULL)
388  {
389    i=rBlocks(r);
390    assume(r->block0 != NULL && r->block1 != NULL && r->wvhdl != NULL);
391    // delete order
392    omFreeSize((ADDRESS)r->order,i*sizeof(int));
393    omFreeSize((ADDRESS)r->block0,i*sizeof(int));
394    omFreeSize((ADDRESS)r->block1,i*sizeof(int));
395    // delete weights
396    for (j=0; j<i; j++)
397    {
398      if (r->wvhdl[j]!=NULL)
399        omFree(r->wvhdl[j]);
400    }
401    omFreeSize((ADDRESS)r->wvhdl,i*sizeof(int *));
402  }
403  else
404  {
405    assume(r->block0 == NULL && r->block1 == NULL && r->wvhdl == NULL);
406  }
407
408  // delete varnames
409  if(r->names!=NULL)
410  {
411    for (i=0; i<r->N; i++)
412    {
413      if (r->names[i] != NULL) omFree((ADDRESS)r->names[i]);
414    }
415    omFreeSize((ADDRESS)r->names,r->N*sizeof(char_ptr));
416  }
417
418  // delete parameter
419  if (r->parameter!=NULL)
420  {
421    char **s=r->parameter;
422    j = 0;
423    while (j < rPar(r))
424    {
425      if (*s != NULL) omFree((ADDRESS)*s);
426      s++;
427      j++;
428    }
429    omFreeSize((ADDRESS)r->parameter,rPar(r)*sizeof(char_ptr));
430  }
431  omFreeBin(r, ip_sring_bin);
432}
433
434int rOrderName(char * ordername)
435{
436  int order=ringorder_unspec;
437  while (order!= 0)
438  {
439    if (strcmp(ordername,rSimpleOrdStr(order))==0)
440      break;
441    order--;
442  }
443  if (order==0) Werror("wrong ring order `%s`",ordername);
444  omFree((ADDRESS)ordername);
445  return order;
446}
447
448char * rOrdStr(ring r)
449{
450  int nblocks,l,i;
451
452  for (nblocks=0; r->order[nblocks]; nblocks++);
453  nblocks--;
454
455  StringSetS("");
456  for (l=0; ; l++)
457  {
458    StringAppend((char *)rSimpleOrdStr(r->order[l]));
459    if ((r->order[l] != ringorder_c) && (r->order[l] != ringorder_C))
460    {
461      if (r->wvhdl[l]!=NULL)
462      {
463        StringAppendS("(");
464        for (int j= 0;
465             j<(r->block1[l]-r->block0[l]+1)*(r->block1[l]-r->block0[l]+1);
466             j+=i+1)
467        {
468          char c=',';
469          for (i = 0; i<r->block1[l]-r->block0[l]; i++)
470          {
471            StringAppend("%d," ,r->wvhdl[l][i+j]);
472          }
473          if (r->order[l]!=ringorder_M)
474          {
475            StringAppend("%d)" ,r->wvhdl[l][i+j]);
476            break;
477          }
478          if (j+i+1==(r->block1[l]-r->block0[l]+1)*(r->block1[l]-r->block0[l]+1))
479            c=')';
480          StringAppend("%d%c" ,r->wvhdl[l][i+j],c);
481        }
482      }
483      else
484        StringAppend("(%d)",r->block1[l]-r->block0[l]+1);
485    }
486    if (l==nblocks) return omStrDup(StringAppendS(""));
487    StringAppendS(",");
488  }
489}
490
491char * rVarStr(ring r)
492{
493  int i;
494  int l=2;
495  char *s;
496
497  for (i=0; i<r->N; i++)
498  {
499    l+=strlen(r->names[i])+1;
500  }
501  s=(char *)omAlloc(l);
502  s[0]='\0';
503  for (i=0; i<r->N-1; i++)
504  {
505    strcat(s,r->names[i]);
506    strcat(s,",");
507  }
508  strcat(s,r->names[i]);
509  return s;
510}
511
512char * rCharStr(ring r)
513{
514  char *s;
515  int i;
516
517  if (r->parameter==NULL)
518  {
519    i=r->ch;
520    if(i==-1)
521      s=omStrDup("real");                    /* R */
522    else
523    {
524      s=(char *)omAlloc(6);
525      sprintf(s,"%d",i);                   /* Q, Z/p */
526    }
527    return s;
528  }
529  if (rField_is_long_C(r))
530  {
531    s=(char *)omAlloc(21+strlen(r->parameter[0]));
532    sprintf(s,"complex,%d,%s",r->float_len,r->parameter[0]);   /* C */
533    return s;
534  }
535  int l=0;
536  for(i=0; i<rPar(r);i++)
537  {
538    l+=(strlen(r->parameter[i])+1);
539  }
540  s=(char *)omAlloc(l+6);
541  s[0]='\0';
542  if (r->ch<0)       sprintf(s,"%d",-r->ch); /* Fp(a) */
543  else if (r->ch==1) sprintf(s,"0");         /* Q(a)  */
544  else
545  {
546    sprintf(s,"%d,%s",r->ch,r->parameter[0]); /* Fq  */
547    return s;
548  }
549  char tt[2];
550  tt[0]=',';
551  tt[1]='\0';
552  for(i=0; i<rPar(r);i++)
553  {
554    strcat(s,tt);
555    strcat(s,r->parameter[i]);
556  }
557  return s;
558}
559
560char * rParStr(ring r)
561{
562  if (r->parameter==NULL) return omStrDup("");
563
564  int i;
565  int l=2;
566
567  for (i=0; i<rPar(r); i++)
568  {
569    l+=strlen(r->parameter[i])+1;
570  }
571  char *s=(char *)omAlloc(l);
572  s[0]='\0';
573  for (i=0; i<rPar(r)-1; i++)
574  {
575    strcat(s,r->parameter[i]);
576    strcat(s,",");
577  }
578  strcat(s,r->parameter[i]);
579  return s;
580}
581
582char * rString(ring r)
583{
584  char *ch=rCharStr(r);
585  char *var=rVarStr(r);
586  char *ord=rOrdStr(r);
587  char *res=(char *)omAlloc(strlen(ch)+strlen(var)+strlen(ord)+9);
588  sprintf(res,"(%s),(%s),(%s)",ch,var,ord);
589  omFree((ADDRESS)ch);
590  omFree((ADDRESS)var);
591  omFree((ADDRESS)ord);
592  return res;
593}
594
595int  rIsExtension(ring r)
596{
597  return (r->parameter!=NULL); /* R, Q, Fp: FALSE */
598}
599
600int  rIsExtension()
601{
602  return rIsExtension( currRing );
603}
604
605int rChar(ring r)
606{
607  if (rField_is_numeric(r))
608    return 0;
609  if (!rIsExtension(r)) /* Q, Fp */
610    return r->ch;
611  if (rField_is_Zp_a(r))  /* Fp(a)  */
612    return -r->ch;
613  if (rField_is_Q_a(r))   /* Q(a)  */
614    return 0;
615  /*else*/               /* GF(p,n) */
616  {
617    if ((r->ch & 1)==0) return 2;
618    int i=3;
619    while ((r->ch % i)!=0) i+=2;
620    return i;
621  }
622}
623
624/*2
625 *returns -1 for not compatible, (sum is undefined)
626 *         0 for equal, (and sum)
627 *         1 for compatible (and sum)
628 */
629int rSum(ring r1, ring r2, ring &sum)
630{
631  if (r1==r2)
632  {
633    sum=r1;
634    r1->ref++;
635    return 0;
636  }
637  ring save=currRing;
638  ip_sring tmpR;
639  memset(&tmpR,0,sizeof(tmpR));
640  /* check coeff. field =====================================================*/
641  if (rInternalChar(r1)==rInternalChar(r2))
642  {
643    tmpR.ch=rInternalChar(r1);
644    if (rField_is_Q(r1)||rField_is_Zp(r1)||rField_is_GF(r1)) /*Q, Z/p, GF(p,n)*/
645    {
646      if (r1->parameter!=NULL)
647      {
648        if (strcmp(r1->parameter[0],r2->parameter[0])==0) /* 1 char */
649        {
650          tmpR.parameter=(char **)omAllocBin(char_ptr_bin);
651          tmpR.parameter[0]=omStrDup(r1->parameter[0]);
652          tmpR.P=1;
653        }
654        else
655        {
656          WerrorS("GF(p,n)+GF(p,n)");
657          return -1;
658        }
659      }
660    }
661    else if ((r1->ch==1)||(r1->ch<-1)) /* Q(a),Z/p(a) */
662    {
663      if (r1->minpoly!=NULL)
664      {
665        if (r2->minpoly!=NULL)
666        {
667          // HANNES: TODO: delete nSetChar
668          rChangeCurrRing(r1);
669          if ((strcmp(r1->parameter[0],r2->parameter[0])==0) /* 1 char */
670              && n_Equal(r1->minpoly,r2->minpoly, r1))
671          {
672            tmpR.parameter=(char **)omAllocBin(char_ptr_bin);
673            tmpR.parameter[0]=omStrDup(r1->parameter[0]);
674            tmpR.minpoly=n_Copy(r1->minpoly, r1);
675            tmpR.P=1;
676            // HANNES: TODO: delete nSetChar
677            rChangeCurrRing(save);
678          }
679          else
680          {
681            // HANNES: TODO: delete nSetChar
682            rChangeCurrRing(save);
683            WerrorS("different minpolys");
684            return -1;
685          }
686        }
687        else
688        {
689          if ((strcmp(r1->parameter[0],r2->parameter[0])==0) /* 1 char */
690              && (rPar(r2)==1))
691          {
692            tmpR.parameter=(char **)omAllocBin(char_ptr_bin);
693            tmpR.parameter[0]=omStrDup(r1->parameter[0]);
694            tmpR.P=1;
695            tmpR.minpoly=n_Copy(r1->minpoly, r1);
696          }
697          else
698          {
699            WerrorS("different parameters and minpoly!=0");
700            return -1;
701          }
702        }
703      }
704      else /* r1->minpoly==NULL */
705      {
706        if (r2->minpoly!=NULL)
707        {
708          if ((strcmp(r1->parameter[0],r2->parameter[0])==0) /* 1 char */
709              && (rPar(r1)==1))
710          {
711            tmpR.parameter=(char **)omAllocBin(char_ptr_bin);
712            tmpR.parameter[0]=omStrDup(r1->parameter[0]);
713            tmpR.P=1;
714            tmpR.minpoly=n_Copy(r2->minpoly, r2);
715          }
716          else
717          {
718            WerrorS("different parameters and minpoly!=0");
719            return -1;
720          }
721        }
722        else
723        {
724          int len=rPar(r1)+rPar(r2);
725          tmpR.parameter=(char **)omAlloc0(len*sizeof(char_ptr));
726          int i;
727          for (i=0;i<rPar(r1);i++)
728          {
729            tmpR.parameter[i]=omStrDup(r1->parameter[i]);
730          }
731          int j,l;
732          for(j=0;j<rPar(r2);j++)
733          {
734            for(l=0;l<i;l++)
735            {
736              if(strcmp(tmpR.parameter[l],r2->parameter[j])==0)
737                break;
738            }
739            if (l==i)
740            {
741              tmpR.parameter[i]=omStrDup(r2->parameter[j]);
742              i++;
743            }
744          }
745          if (i!=len)
746          {
747            tmpR.parameter=(char**)omReallocSize(tmpR.parameter,len*sizeof(char_ptr),i*sizeof(char_ptr));
748          }
749          tmpR.P=i;
750        }
751      }
752    }
753  }
754  else /* r1->ch!=r2->ch */
755  {
756    if (r1->ch<-1) /* Z/p(a) */
757    {
758      if ((r2->ch==0) /* Q */
759          || (r2->ch==-r1->ch)) /* Z/p */
760      {
761        tmpR.ch=rInternalChar(r1);
762        tmpR.P=rPar(r1);
763        tmpR.parameter=(char **)omAlloc(rPar(r1)*sizeof(char_ptr));
764        int i;
765        for (i=0;i<rPar(r1);i++)
766        {
767          tmpR.parameter[i]=omStrDup(r1->parameter[i]);
768        }
769        if (r1->minpoly!=NULL)
770        {
771          tmpR.minpoly=n_Copy(r1->minpoly, r1);
772        }
773      }
774      else  /* R, Q(a),Z/q,Z/p(a),GF(p,n) */
775      {
776        WerrorS("Z/p(a)+(R,Q(a),Z/q(a),GF(q,n))");
777        return -1;
778      }
779    }
780    else if (r1->ch==-1) /* R */
781    {
782      WerrorS("R+..");
783      return -1;
784    }
785    else if (r1->ch==0) /* Q */
786    {
787      if ((r2->ch<-1)||(r2->ch==1)) /* Z/p(a),Q(a) */
788      {
789        tmpR.ch=rInternalChar(r2);
790        tmpR.P=rPar(r2);
791        tmpR.parameter=(char **)omAlloc(rPar(r2)*sizeof(char_ptr));
792        int i;
793        for (i=0;i<rPar(r2);i++)
794        {
795          tmpR.parameter[i]=omStrDup(r2->parameter[i]);
796        }
797        if (r2->minpoly!=NULL)
798        {
799          tmpR.minpoly=n_Copy(r2->minpoly, r2);
800        }
801      }
802      else if (r2->ch>1) /* Z/p,GF(p,n) */
803      {
804        tmpR.ch=r2->ch;
805        if (r2->parameter!=NULL)
806        {
807          tmpR.parameter=(char **)omAllocBin(char_ptr_bin);
808          tmpR.P=1;
809          tmpR.parameter[0]=omStrDup(r2->parameter[0]);
810        }
811      }
812      else
813      {
814        WerrorS("Q+R");
815        return -1; /* R */
816      }
817    }
818    else if (r1->ch==1) /* Q(a) */
819    {
820      if (r2->ch==0) /* Q */
821      {
822        tmpR.ch=rInternalChar(r1);
823        tmpR.P=rPar(r1);
824        tmpR.parameter=(char **)omAlloc(rPar(r1)*sizeof(char_ptr));
825        int i;
826        for(i=0;i<rPar(r1);i++)
827        {
828          tmpR.parameter[i]=omStrDup(r1->parameter[i]);
829        }
830        if (r1->minpoly!=NULL)
831        {
832          tmpR.minpoly=n_Copy(r1->minpoly, r1);
833        }
834      }
835      else  /* R, Z/p,GF(p,n) */
836      {
837        WerrorS("Q(a)+(R,Z/p,GF(p,n))");
838        return -1;
839      }
840    }
841    else /* r1->ch >=2 , Z/p */
842    {
843      if (r2->ch==0) /* Q */
844      {
845        tmpR.ch=r1->ch;
846      }
847      else if (r2->ch==-r1->ch) /* Z/p(a) */
848      {
849        tmpR.ch=rInternalChar(r2);
850        tmpR.P=rPar(r2);
851        tmpR.parameter=(char **)omAlloc(rPar(r2)*sizeof(char_ptr));
852        int i;
853        for(i=0;i<rPar(r2);i++)
854        {
855          tmpR.parameter[i]=omStrDup(r2->parameter[i]);
856        }
857        if (r2->minpoly!=NULL)
858        {
859          tmpR.minpoly=n_Copy(r2->minpoly, r2);
860        }
861      }
862      else
863      {
864        WerrorS("Z/p+(GF(q,n),Z/q(a),R,Q(a))");
865        return -1; /* GF(p,n),Z/q(a),R,Q(a) */
866      }
867    }
868  }
869  /* variable names ========================================================*/
870  int i,j,k;
871  int l=r1->N+r2->N;
872  char **names=(char **)omAlloc0(l*sizeof(char_ptr));
873  k=0;
874
875  // collect all varnames from r1, except those which are parameters
876  // of r2, or those which are the empty string
877  for (i=0;i<r1->N;i++)
878  {
879    BOOLEAN b=TRUE;
880
881    if (*(r1->names[i]) == '\0')
882      b = FALSE;
883    else if ((r2->parameter!=NULL) && (strlen(r1->names[i])==1))
884    {
885      for(j=0;j<rPar(r2);j++)
886      {
887        if (strcmp(r1->names[i],r2->parameter[j])==0)
888        {
889          b=FALSE;
890          break;
891        }
892      }
893    }
894
895    if (b)
896    {
897      //Print("name : %d: %s\n",k,r1->names[i]);
898      names[k]=omStrDup(r1->names[i]);
899      k++;
900    }
901    //else
902    //  Print("no name (par1) %s\n",r1->names[i]);
903  }
904  // Add variables from r2, except those which are parameters of r1
905  // those which are empty strings, and those which equal a var of r1
906  for(i=0;i<r2->N;i++)
907  {
908    BOOLEAN b=TRUE;
909
910    if (*(r2->names[i]) == '\0')
911      b = FALSE;
912    else if ((r1->parameter!=NULL) && (strlen(r2->names[i])==1))
913    {
914      for(j=0;j<rPar(r1);j++)
915      {
916        if (strcmp(r2->names[i],r1->parameter[j])==0)
917        {
918          b=FALSE;
919          break;
920        }
921      }
922    }
923
924    if (b)
925    {
926      for(j=0;j<r1->N;j++)
927      {
928        if (strcmp(r1->names[j],r2->names[i])==0)
929        {
930          b=FALSE;
931          break;
932        }
933      }
934      if (b)
935      {
936        //Print("name : %d : %s\n",k,r2->names[i]);
937        names[k]=omStrDup(r2->names[i]);
938        k++;
939      }
940      //else
941      //  Print("no name (var): %s\n",r2->names[i]);
942    }
943    //else
944    //  Print("no name (par): %s\n",r2->names[i]);
945  }
946  // check whether we found any vars at all
947  if (k == 0)
948  {
949    names[k]=omStrDup("");
950    k=1;
951  }
952  tmpR.N=k;
953  tmpR.names=names;
954  /* ordering *======================================================== */
955  tmpR.OrdSgn=1;
956  if ((r1->order[0]==ringorder_unspec)
957      && (r2->order[0]==ringorder_unspec))
958  {
959    tmpR.order=(int*)omAlloc(3*sizeof(int));
960    tmpR.block0=(int*)omAlloc(3*sizeof(int));
961    tmpR.block1=(int*)omAlloc(3*sizeof(int));
962    tmpR.wvhdl=(int**)omAlloc0(3*sizeof(int_ptr));
963    tmpR.order[0]=ringorder_unspec;
964    tmpR.order[1]=ringorder_C;
965    tmpR.order[2]=0;
966    tmpR.block0[0]=1;
967    tmpR.block1[0]=tmpR.N;
968  }
969  else if (l==k) /* r3=r1+r2 */
970  {
971    int b;
972    ring rb;
973    if (r1->order[0]==ringorder_unspec)
974    {
975      /* extend order of r2 to r3 */
976      b=rBlocks(r2);
977      rb=r2;
978      tmpR.OrdSgn=r2->OrdSgn;
979    }
980    else if (r2->order[0]==ringorder_unspec)
981    {
982      /* extend order of r1 to r3 */
983      b=rBlocks(r1);
984      rb=r1;
985      tmpR.OrdSgn=r1->OrdSgn;
986    }
987    else
988    {
989      b=rBlocks(r1)+rBlocks(r2)-2; /* for only one order C, only one 0 */
990      rb=NULL;
991    }
992    tmpR.order=(int*)omAlloc0(b*sizeof(int));
993    tmpR.block0=(int*)omAlloc0(b*sizeof(int));
994    tmpR.block1=(int*)omAlloc0(b*sizeof(int));
995    tmpR.wvhdl=(int**)omAlloc0(b*sizeof(int_ptr));
996    /* weights not implemented yet ...*/
997    if (rb!=NULL)
998    {
999      for (i=0;i<b;i++)
1000      {
1001        tmpR.order[i]=rb->order[i];
1002        tmpR.block0[i]=rb->block0[i];
1003        tmpR.block1[i]=rb->block1[i];
1004        if (rb->wvhdl[i]!=NULL)
1005          WarnS("rSum: weights not implemented");
1006      }
1007      tmpR.block0[0]=1;
1008    }
1009    else /* ring sum for complete rings */
1010    {
1011      for (i=0;r1->order[i]!=0;i++)
1012      {
1013        tmpR.order[i]=r1->order[i];
1014        tmpR.block0[i]=r1->block0[i];
1015        tmpR.block1[i]=r1->block1[i];
1016        if (r1->wvhdl[i]!=NULL)
1017          tmpR.wvhdl[i] = (int*) omMemDup(r1->wvhdl[i]);
1018      }
1019      j=i;
1020      i--;
1021      if ((r1->order[i]==ringorder_c)
1022          ||(r1->order[i]==ringorder_C))
1023      {
1024        j--;
1025        tmpR.order[b-2]=r1->order[i];
1026      }
1027      for (i=0;r2->order[i]!=0;i++)
1028      {
1029        if ((r2->order[i]!=ringorder_c)
1030            &&(r2->order[i]!=ringorder_C))
1031        {
1032          tmpR.order[j]=r2->order[i];
1033          tmpR.block0[j]=r2->block0[i]+r1->N;
1034          tmpR.block1[j]=r2->block1[i]+r1->N;
1035          if (r2->wvhdl[i]!=NULL)
1036          {
1037            tmpR.wvhdl[j] = (int*) omMemDup(r2->wvhdl[i]);
1038          }
1039          j++;
1040        }
1041      }
1042      if((r1->OrdSgn==-1)||(r2->OrdSgn==-1))
1043        tmpR.OrdSgn=-1;
1044    }
1045  }
1046  else if ((k==r1->N) && (k==r2->N)) /* r1 and r2 are "quite" the same ring */
1047    /* copy r1, because we have the variables from r1 */
1048  {
1049    int b=rBlocks(r1);
1050
1051    tmpR.order=(int*)omAlloc0(b*sizeof(int));
1052    tmpR.block0=(int*)omAlloc0(b*sizeof(int));
1053    tmpR.block1=(int*)omAlloc0(b*sizeof(int));
1054    tmpR.wvhdl=(int**)omAlloc0(b*sizeof(int_ptr));
1055    /* weights not implemented yet ...*/
1056    for (i=0;i<b;i++)
1057    {
1058      tmpR.order[i]=r1->order[i];
1059      tmpR.block0[i]=r1->block0[i];
1060      tmpR.block1[i]=r1->block1[i];
1061      if (r1->wvhdl[i]!=NULL)
1062      {
1063        tmpR.wvhdl[i] = (int*) omMemDup(r1->wvhdl[i]);
1064      }
1065    }
1066    tmpR.OrdSgn=r1->OrdSgn;
1067  }
1068  else
1069  {
1070    for(i=0;i<k;i++) omFree((ADDRESS)tmpR.names[i]);
1071    omFreeSize((ADDRESS)names,tmpR.N*sizeof(char_ptr));
1072    Werror("difficulties with variables: %d,%d -> %d",rVar(r1),rVar(r2),k);
1073    return -1;
1074  }
1075  sum=(ring)omAllocBin(ip_sring_bin);
1076  memcpy(sum,&tmpR,sizeof(ip_sring));
1077  rComplete(sum);
1078//#ifdef RDEBUG
1079//  rDebugPrint(sum);
1080//#endif
1081#ifdef HAVE_PLURAL
1082  ring old_ring = currRing;
1083  rChangeCurrRing(sum);
1084  BOOLEAN R1_is_nc = rIsPluralRing(r1);
1085  BOOLEAN R2_is_nc = rIsPluralRing(r2);
1086  if ( (R1_is_nc) || (R2_is_nc))
1087  {
1088    rChangeCurrRing(r1); /* since rCopy works well only in currRing */
1089    ring R1 = rCopy(r1);
1090    rChangeCurrRing(r2);
1091    ring R2 = rCopy(r2);
1092    rChangeCurrRing(sum);
1093    /* basic nc constructions  */
1094    sum->nc = (nc_struct *)omAlloc0(sizeof(nc_struct));
1095    sum->nc->ref = 1;
1096    sum->nc->basering = sum;
1097    if ( !R1_is_nc ) nc_rCreateNCcomm(R1);
1098    if ( !R2_is_nc ) nc_rCreateNCcomm(R2);
1099    /* nc types */
1100    sum->nc->type = nc_undef;
1101    nc_type t1 = R1->nc->type, t2 = R2->nc->type;
1102    if ( t1==t2) sum->nc->type = t1;
1103    else
1104    {
1105      if ( (t1==nc_general) || (t2==nc_general) ) sum->nc->type = nc_general;
1106    }
1107    if (sum->nc->type == nc_undef) /* not yet done */
1108    {
1109      switch (t1) 
1110      {
1111        case nc_comm:
1112          sum->nc->type = t2;
1113          break;
1114        case nc_lie:
1115          switch(t2)
1116          {
1117            case nc_skew:
1118              sum->nc->type = nc_general;  break;
1119            case nc_comm:
1120              sum->nc->type = nc_lie;  break;
1121            default:
1122              /*sum->nc->type = nc_undef;*/  break;
1123          }
1124          break;
1125        case nc_skew:
1126          switch(t2)
1127          {
1128            case nc_lie:
1129              sum->nc->type = nc_lie;  break;
1130            case nc_comm:
1131              sum->nc->type = nc_skew;  break;
1132            default:
1133              /*sum->nc->type = nc_undef;*/  break;
1134          }
1135        default:
1136          /*sum->nc->type = nc_undef;*/
1137          break;
1138      }
1139    }
1140    if (sum->nc->type == nc_undef)
1141      WarnS("Error on recognizing nc types");
1142    /* multiplication matrices */
1143//     ring old_ring = currRing;
1144//     rChangeCurrRing(sum);
1145/* find permutations of vars and pars */
1146    int *perm1 = (int *)omAlloc0((rVar(R1)+1)*sizeof(int));
1147    int *par_perm1 = NULL;
1148    if (rPar(R1)!=0) par_perm1=(int *)omAlloc0((rPar(R1)+1)*sizeof(int));
1149    int *perm2 = (int *)omAlloc0((rVar(R2)+1)*sizeof(int));
1150    int *par_perm2 = NULL;
1151    if (rPar(R2)!=0) par_perm2=(int *)omAlloc0((rPar(R2)+1)*sizeof(int));
1152    //    maFindPerm(char **preim_names, int preim_n, char **preim_par, int preim_p,
1153    //                char **names,       int n,       char **par,       int nop,
1154    //                int * perm, int *par_perm, int ch);
1155    maFindPerm(R1->names,  rVar(R1),  R1->parameter,  rPar(R1),
1156               sum->names, rVar(sum), sum->parameter, rPar(sum),
1157               perm1, par_perm1, sum->ch);
1158    maFindPerm(R2->names,  rVar(R2),  R2->parameter,  rPar(R2),
1159               sum->names, rVar(sum), sum->parameter, rPar(sum),
1160               perm2, par_perm2, sum->ch);
1161    nMapFunc nMap1 = nSetMap(R1);
1162    nMapFunc nMap2 = nSetMap(R2);
1163    matrix C1 = R1->nc->C, C2 = R2->nc->C;
1164    matrix D1 = R1->nc->D, D2 = R2->nc->D;
1165    int l = rVar(R1) + rVar(R2);
1166    matrix C  = mpNew(l,l);
1167    matrix D  = mpNew(l,l);
1168    int param_shift = 0;
1169    for (i=1; i<= rVar(R1) + rVar(R2); i++)
1170    {
1171      for (j= i+1; j<= rVar(R1) + rVar(R2); j++)
1172      {
1173        MATELEM(C,i,j) = pOne();
1174      }
1175    }
1176    for (i=1; i< rVar(R1); i++)
1177    {
1178      for (j=i+1; j<=rVar(R1); j++)
1179      {
1180
1181        MATELEM(C,i,j) = pPermPoly(MATELEM(C1,i,j),perm1,R1,nMap1,par_perm1,rPar(R1));
1182        if (MATELEM(D1,i,j) != NULL)
1183        {
1184          MATELEM(D,i,j) = pPermPoly(MATELEM(D1,i,j),perm1,R1,nMap1,par_perm1,rPar(R1));
1185        }
1186      }
1187    }
1188    for (i=1; i< rVar(R2); i++)
1189    {
1190      for (j=i+1; j<=rVar(R2); j++)
1191      {
1192        MATELEM(C,rVar(R1)+i,rVar(R1)+j) = pPermPoly(MATELEM(C2,i,j),perm2,R2,nMap2,par_perm2,rPar(R2));
1193        if (MATELEM(D2,i,j) != NULL)
1194        {
1195          MATELEM(D,rVar(R1)+i,rVar(R1)+j) = pPermPoly(MATELEM(D2,i,j),perm2,R2,nMap2,par_perm2,rPar(R2));
1196        }
1197      }
1198    }
1199    idTest((ideal)C);
1200    idTest((ideal)D);
1201    sum->nc->C = C;
1202    sum->nc->D = D;
1203    if (nc_InitMultiplication(sum))
1204      WarnS("Error initializing multiplication!");
1205    sum->nc->IsSkewConstant =(int)((R1->nc->IsSkewConstant) && (R2->nc->IsSkewConstant));
1206    /* delete R1, R2*/
1207    rDelete(R1);
1208    rDelete(R2);
1209    /* TODO: delete perm arrays */
1210    if (perm1!=NULL) omFree((ADDRESS)perm1);
1211    if (perm2!=NULL) omFree((ADDRESS)perm2);
1212    if (par_perm1!=NULL) omFree((ADDRESS)par_perm1);
1213    if (par_perm2!=NULL) omFree((ADDRESS)par_perm2);
1214    if ( old_ring != NULL)
1215      rChangeCurrRing(old_ring);
1216  }
1217#endif
1218  ring old_ring2 = currRing;
1219  rChangeCurrRing(sum);
1220  ideal Q=NULL;
1221  ideal Q1, Q2;
1222  if (r1->qideal!=NULL)
1223  {
1224//     if (r2->qideal!=NULL)
1225//     {
1226//       WerrorS("todo: qring+qring");
1227//       return -1;
1228//     }
1229//     else
1230//     {}
1231    /* these were defined in the Plural Part above... */
1232    int *perm1 = (int *)omAlloc0((rVar(r1)+1)*sizeof(int));
1233    int *par_perm1 = NULL;
1234    if (rPar(r1)!=0) par_perm1=(int *)omAlloc0((rPar(r1)+1)*sizeof(int));
1235    maFindPerm(r1->names,  rVar(r1),  r1->parameter,  rPar(r1),
1236               sum->names, rVar(sum), sum->parameter, rPar(sum),
1237               perm1, par_perm1, sum->ch);
1238    nMapFunc nMap1 = nSetMap(r1);
1239    Q1 = idInit(IDELEMS(r1->qideal),1);
1240    for (int i=0;i<IDELEMS(r1->qideal);i++)
1241      Q1->m[i] = pPermPoly(r1->qideal->m[i],perm1,r1,nMap1,par_perm1,rPar(r1));
1242    omFree((ADDRESS)perm1);
1243  }
1244  else
1245  {
1246    Q1 = NULL;
1247  }
1248
1249  if (r2->qideal!=NULL)
1250  {
1251    int *perm2 = (int *)omAlloc0((rVar(r2)+1)*sizeof(int));
1252    int *par_perm2 = NULL;
1253    if (rPar(r2)!=0) par_perm2=(int *)omAlloc0((rPar(r2)+1)*sizeof(int));
1254    maFindPerm(r2->names,  rVar(r2),  r2->parameter,  rPar(r2),
1255               sum->names, rVar(sum), sum->parameter, rPar(sum),
1256               perm2, par_perm2, sum->ch);
1257    nMapFunc nMap2 = nSetMap(r2);
1258    Q2 = idInit(IDELEMS(r2->qideal),1);
1259    for (int i=0;i<IDELEMS(r2->qideal);i++)
1260      Q2->m[i] = pPermPoly(r2->qideal->m[i],perm2,r2,nMap2,par_perm2,rPar(r2));
1261    omFree((ADDRESS)perm2);
1262  }
1263  else
1264  {
1265    Q2 = NULL;
1266  }
1267  if ( (Q1!=NULL) || ( Q2!=NULL))
1268    Q = idSimpleAdd(Q1,Q2);
1269  sum->qideal = Q; 
1270  if ( old_ring2 != NULL)
1271    rChangeCurrRing(old_ring2);
1272  return 1;
1273}
1274/*2
1275 * create a copy of the ring r, which must be equivalent to currRing
1276 * used for qring definition,..
1277 * (i.e.: normal rings: same nCopy as currRing;
1278 *        qring:        same nCopy, same idCopy as currRing)
1279 * DOES NOT CALL rComplete
1280 */
1281static ring rCopy0(ring r, BOOLEAN copy_qideal = TRUE,
1282                   BOOLEAN copy_ordering = TRUE)
1283{
1284  if (r == NULL) return NULL;
1285  int i,j;
1286  ring res=(ring)omAllocBin(ip_sring_bin);
1287
1288  memcpy4(res,r,sizeof(ip_sring));
1289  res->VarOffset = NULL;
1290  res->ref=0;
1291  if (r->algring!=NULL)
1292    r->algring->ref++;
1293  if (r->parameter!=NULL)
1294  {
1295    res->minpoly=nCopy(r->minpoly);
1296    int l=rPar(r);
1297    res->parameter=(char **)omAlloc(l*sizeof(char_ptr));
1298    int i;
1299    for(i=0;i<rPar(r);i++)
1300    {
1301      res->parameter[i]=omStrDup(r->parameter[i]);
1302    }
1303    if (r->minideal!=NULL)
1304    {
1305      res->minideal-id_Copy(r->minideal,r->algring);
1306    }
1307  }
1308  if (copy_ordering == TRUE)
1309  {
1310    i=rBlocks(r);
1311    res->wvhdl   = (int **)omAlloc(i * sizeof(int_ptr));
1312    res->order   = (int *) omAlloc(i * sizeof(int));
1313    res->block0  = (int *) omAlloc(i * sizeof(int));
1314    res->block1  = (int *) omAlloc(i * sizeof(int));
1315    for (j=0; j<i; j++)
1316    {
1317      if (r->wvhdl[j]!=NULL)
1318      {
1319        res->wvhdl[j] = (int*) omMemDup(r->wvhdl[j]);
1320      }
1321      else
1322        res->wvhdl[j]=NULL;
1323    }
1324    memcpy4(res->order,r->order,i * sizeof(int));
1325    memcpy4(res->block0,r->block0,i * sizeof(int));
1326    memcpy4(res->block1,r->block1,i * sizeof(int));
1327  }
1328  else
1329  {
1330    res->wvhdl = NULL;
1331    res->order = NULL;
1332    res->block0 = NULL;
1333    res->block1 = NULL;
1334  }
1335
1336  res->names   = (char **)omAlloc0(rVar(r) * sizeof(char_ptr));
1337  for (i=0; i<res->N; i++)
1338  {
1339    res->names[i] = omStrDup(r->names[i]);
1340  }
1341  res->idroot = NULL;
1342  if (r->qideal!=NULL)
1343  {
1344    if (copy_qideal) res->qideal= idrCopyR_NoSort(r->qideal, r);
1345    else res->qideal = NULL;
1346  }
1347#ifdef HAVE_PLURAL
1348  if (rIsPluralRing(r))
1349  {
1350    res->nc=r->nc;
1351    res->nc->ref++;
1352  }
1353#endif
1354  return res;
1355}
1356
1357/*2
1358 * create a copy of the ring r, which must be equivalent to currRing
1359 * used for qring definition,..
1360 * (i.e.: normal rings: same nCopy as currRing;
1361 *        qring:        same nCopy, same idCopy as currRing)
1362 */
1363ring rCopy(ring r)
1364{
1365  if (r == NULL) return NULL;
1366  ring res=rCopy0(r);
1367  rComplete(res, 1);
1368  return res;
1369}
1370
1371// returns TRUE, if r1 equals r2 FALSE, otherwise Equality is
1372// determined componentwise, if qr == 1, then qrideal equality is
1373// tested, as well
1374BOOLEAN rEqual(ring r1, ring r2, BOOLEAN qr)
1375{
1376  int i, j;
1377
1378  if (r1 == r2) return 1;
1379
1380  if (r1 == NULL || r2 == NULL) return 0;
1381
1382  if ((rInternalChar(r1) != rInternalChar(r2))
1383  || (r1->float_len != r2->float_len)
1384  || (r1->float_len2 != r2->float_len2)
1385  || (rVar(r1) != rVar(r2))
1386  || (r1->OrdSgn != r2->OrdSgn)
1387  || (rPar(r1) != rPar(r2)))
1388    return 0;
1389
1390  for (i=0; i<rVar(r1); i++)
1391  {
1392    if (r1->names[i] != NULL && r2->names[i] != NULL)
1393    {
1394      if (strcmp(r1->names[i], r2->names[i])) return 0;
1395    }
1396    else if ((r1->names[i] != NULL) ^ (r2->names[i] != NULL))
1397    {
1398      return 0;
1399    }
1400  }
1401
1402  i=0;
1403  while (r1->order[i] != 0)
1404  {
1405    if (r2->order[i] == 0) return 0;
1406    if ((r1->order[i] != r2->order[i]) ||
1407        (r1->block0[i] != r2->block0[i]) || (r2->block0[i] != r1->block0[i]))
1408      return 0;
1409    if (r1->wvhdl[i] != NULL)
1410    {
1411      if (r2->wvhdl[i] == NULL)
1412        return 0;
1413      for (j=0; j<r1->block1[i]-r1->block0[i]+1; j++)
1414        if (r2->wvhdl[i][j] != r1->wvhdl[i][j])
1415          return 0;
1416    }
1417    else if (r2->wvhdl[i] != NULL) return 0;
1418    i++;
1419  }
1420
1421  for (i=0; i<rPar(r1);i++)
1422  {
1423      if (strcmp(r1->parameter[i], r2->parameter[i])!=0)
1424        return 0;
1425  }
1426
1427  if (r1->minpoly != NULL)
1428  {
1429    if (r2->minpoly == NULL) return 0;
1430    if (currRing == r1 || currRing == r2)
1431    {
1432      if (! nEqual(r1->minpoly, r2->minpoly)) return 0;
1433    }
1434  }
1435  else if (r2->minpoly != NULL) return 0;
1436
1437  if (qr)
1438  {
1439    if (r1->qideal != NULL)
1440    {
1441      ideal id1 = r1->qideal, id2 = r2->qideal;
1442      int i, n;
1443      poly *m1, *m2;
1444
1445      if (id2 == NULL) return 0;
1446      if ((n = IDELEMS(id1)) != IDELEMS(id2)) return 0;
1447
1448      if (currRing == r1 || currRing == r2)
1449      {
1450        m1 = id1->m;
1451        m2 = id2->m;
1452        for (i=0; i<n; i++)
1453          if (! pEqualPolys(m1[i],m2[i])) return 0;
1454      }
1455    }
1456    else if (r2->qideal != NULL) return 0;
1457  }
1458
1459  return 1;
1460}
1461
1462rOrderType_t rGetOrderType(ring r)
1463{
1464  // check for simple ordering
1465  if (rHasSimpleOrder(r))
1466  {
1467    if ((r->order[1] == ringorder_c) || (r->order[1] == ringorder_C))
1468    {
1469      switch(r->order[0])
1470      {
1471          case ringorder_dp:
1472          case ringorder_wp:
1473          case ringorder_ds:
1474          case ringorder_ws:
1475          case ringorder_ls:
1476          case ringorder_unspec:
1477            if (r->order[1] == ringorder_C ||  r->order[0] == ringorder_unspec)
1478              return rOrderType_ExpComp;
1479            return rOrderType_Exp;
1480
1481          default:
1482            assume(r->order[0] == ringorder_lp ||
1483                   r->order[0] == ringorder_rp ||
1484                   r->order[0] == ringorder_Dp ||
1485                   r->order[0] == ringorder_Wp ||
1486                   r->order[0] == ringorder_Ds ||
1487                   r->order[0] == ringorder_Ws);
1488
1489            if (r->order[1] == ringorder_c) return rOrderType_ExpComp;
1490            return rOrderType_Exp;
1491      }
1492    }
1493    else
1494    {
1495      assume((r->order[0]==ringorder_c)||(r->order[0]==ringorder_C));
1496      return rOrderType_CompExp;
1497    }
1498  }
1499  else
1500    return rOrderType_General;
1501}
1502
1503BOOLEAN rHasSimpleOrder(ring r)
1504{
1505  if (r->order[0] == ringorder_unspec) return TRUE;
1506  int blocks = rBlocks(r) - 1;
1507  assume(blocks >= 1);
1508  if (blocks == 1) return TRUE;
1509  if (blocks > 2)  return FALSE;
1510  if (r->order[0] != ringorder_c && r->order[0] != ringorder_C &&
1511      r->order[1] != ringorder_c && r->order[1] != ringorder_C)
1512    return FALSE;
1513  if (r->order[1] == ringorder_M || r->order[0] == ringorder_M)
1514    return FALSE;
1515  return TRUE;
1516}
1517
1518// returns TRUE, if simple lp or ls ordering
1519BOOLEAN rHasSimpleLexOrder(ring r)
1520{
1521  return rHasSimpleOrder(r) &&
1522    (r->order[0] == ringorder_ls ||
1523     r->order[0] == ringorder_lp ||
1524     r->order[1] == ringorder_ls ||
1525     r->order[1] == ringorder_lp);
1526}
1527
1528BOOLEAN rOrder_is_DegOrdering(rRingOrder_t order)
1529{
1530  switch(order)
1531  {
1532      case ringorder_dp:
1533      case ringorder_Dp:
1534      case ringorder_ds:
1535      case ringorder_Ds:
1536      case ringorder_Ws:
1537      case ringorder_Wp:
1538      case ringorder_ws:
1539      case ringorder_wp:
1540        return TRUE;
1541
1542      default:
1543        return FALSE;
1544  }
1545}
1546
1547BOOLEAN rOrder_is_WeightedOrdering(rRingOrder_t order)
1548{
1549  switch(order)
1550  {
1551      case ringorder_Ws:
1552      case ringorder_Wp:
1553      case ringorder_ws:
1554      case ringorder_wp:
1555        return TRUE;
1556
1557      default:
1558        return FALSE;
1559  }
1560}
1561
1562BOOLEAN rHasSimpleOrderAA(ring r)
1563{
1564  int blocks = rBlocks(r) - 1;
1565  if (blocks > 3 || blocks < 2) return FALSE;
1566  if (blocks == 3)
1567  {
1568    return ((r->order[0] == ringorder_aa && r->order[1] != ringorder_M &&
1569             (r->order[2] == ringorder_c || r->order[2] == ringorder_C)) ||
1570            ((r->order[0] == ringorder_c || r->order[0] == ringorder_C) &&
1571             r->order[1] == ringorder_aa && r->order[2] != ringorder_M));
1572  }
1573  else
1574  {
1575    return (r->order[0] == ringorder_aa && r->order[1] != ringorder_M);
1576  }
1577}
1578
1579// return TRUE if p_SetComp requires p_Setm
1580BOOLEAN rOrd_SetCompRequiresSetm(ring r)
1581{
1582  if (r->typ != NULL)
1583  {
1584    int pos;
1585    for (pos=0;pos<r->OrdSize;pos++)
1586    {
1587      sro_ord* o=&(r->typ[pos]);
1588      if (o->ord_typ == ro_syzcomp || o->ord_typ == ro_syz) return TRUE;
1589    }
1590  }
1591  return FALSE;
1592}
1593
1594// return TRUE if p->exp[r->pOrdIndex] holds total degree of p */
1595BOOLEAN rOrd_is_Totaldegree_Ordering(ring r)
1596{
1597  // Hmm.... what about Syz orderings?
1598  return (rVar(r) > 1 &&
1599          ((rHasSimpleOrder(r) &&
1600           (rOrder_is_DegOrdering((rRingOrder_t)r->order[0]) ||
1601            rOrder_is_DegOrdering(( rRingOrder_t)r->order[1]))) ||
1602           (rHasSimpleOrderAA(r) &&
1603            (rOrder_is_DegOrdering((rRingOrder_t)r->order[1]) ||
1604             rOrder_is_DegOrdering((rRingOrder_t)r->order[2])))));
1605}
1606
1607// return TRUE if p->exp[r->pOrdIndex] holds a weighted degree of p */
1608BOOLEAN rOrd_is_WeightedDegree_Ordering(ring r =currRing)
1609{
1610  // Hmm.... what about Syz orderings?
1611  return ((rVar(r) > 1) &&
1612          rHasSimpleOrder(r) &&
1613          (rOrder_is_WeightedOrdering((rRingOrder_t)r->order[0]) ||
1614           rOrder_is_WeightedOrdering(( rRingOrder_t)r->order[1])));
1615}
1616
1617BOOLEAN rIsPolyVar(int v, ring r)
1618{
1619  int  i=0;
1620  while(r->order[i]!=0)
1621  {
1622    if((r->block0[i]<=v)
1623    && (r->block1[i]>=v))
1624    {
1625      switch(r->order[i])
1626      {
1627        case ringorder_a:
1628          return (r->wvhdl[i][v-r->block0[i]]>0);
1629        case ringorder_M:
1630          return 2; /*don't know*/
1631        case ringorder_lp:
1632        case ringorder_rp:
1633        case ringorder_dp:
1634        case ringorder_Dp:
1635        case ringorder_wp:
1636        case ringorder_Wp:
1637          return TRUE;
1638        case ringorder_ls:
1639        case ringorder_ds:
1640        case ringorder_Ds:
1641        case ringorder_ws:
1642        case ringorder_Ws:
1643          return FALSE;
1644        default:
1645          break;
1646      }
1647    }
1648    i++;
1649  }
1650  return 3; /* could not find var v*/
1651}
1652
1653#ifdef RDEBUG
1654// This should eventually become a full-fledge ring check, like pTest
1655BOOLEAN rDBTest(ring r, char* fn, int l)
1656{
1657  int i,j;
1658
1659  if (r == NULL)
1660  {
1661    dReportError("Null ring in %s:%d", fn, l);
1662    return FALSE;
1663  }
1664
1665
1666  if (r->N == 0) return TRUE;
1667
1668//  omCheckAddrSize(r,sizeof(ip_sring));
1669#if OM_CHECK > 0
1670  i=rBlocks(r);
1671  omCheckAddrSize(r->order,i*sizeof(int));
1672  omCheckAddrSize(r->block0,i*sizeof(int));
1673  omCheckAddrSize(r->block1,i*sizeof(int));
1674  omCheckAddrSize(r->wvhdl,i*sizeof(int *));
1675  for (j=0;j<i; j++)
1676  {
1677    if (r->wvhdl[j] != NULL) omCheckAddr(r->wvhdl[j]);
1678  }
1679#endif
1680  if (r->VarOffset == NULL)
1681  {
1682    dReportError("Null ring VarOffset -- no rComplete (?) in n %s:%d", fn, l);
1683    return FALSE;
1684  }
1685  omCheckAddrSize(r->VarOffset,(r->N+1)*sizeof(int));
1686
1687  if ((r->OrdSize==0)!=(r->typ==NULL))
1688  {
1689    dReportError("mismatch OrdSize and typ-pointer in %s:%d");
1690    return FALSE;
1691  }
1692  omcheckAddrSize(r->typ,r->OrdSize*sizeof(*(r->typ)));
1693  omCheckAddrSize(r->VarOffset,(r->N+1)*sizeof(*(r->VarOffset)));
1694  // test assumptions:
1695  for(i=0;i<=r->N;i++)
1696  {
1697    if(r->typ!=NULL)
1698    {
1699      for(j=0;j<r->OrdSize;j++)
1700      {
1701        if (r->typ[j].ord_typ==ro_cp)
1702        {
1703          if(((short)r->VarOffset[i]) == r->typ[j].data.cp.place)
1704            dReportError("ordrec %d conflicts with var %d",j,i);
1705        }
1706        else
1707          if ((r->typ[j].ord_typ!=ro_syzcomp)
1708          && (r->VarOffset[i] == r->typ[j].data.dp.place))
1709            dReportError("ordrec %d conflicts with var %d",j,i);
1710      }
1711    }
1712    int tmp;
1713      tmp=r->VarOffset[i] & 0xffffff;
1714      #if SIZEOF_LONG == 8
1715        if ((r->VarOffset[i] >> 24) >63)
1716      #else
1717        if ((r->VarOffset[i] >> 24) >31)
1718      #endif
1719          dReportError("bit_start out of range:%d",r->VarOffset[i] >> 24);
1720      if (i > 0 && ((tmp<0) ||(tmp>r->ExpL_Size-1)))
1721      {
1722        dReportError("varoffset out of range for var %d: %d",i,tmp);
1723      }
1724  }
1725  if(r->typ!=NULL)
1726  {
1727    for(j=0;j<r->OrdSize;j++)
1728    {
1729      if ((r->typ[j].ord_typ==ro_dp)
1730      || (r->typ[j].ord_typ==ro_wp)
1731      || (r->typ[j].ord_typ==ro_wp_neg))
1732      {
1733        if (r->typ[j].data.dp.start > r->typ[j].data.dp.end)
1734          dReportError("in ordrec %d: start(%d) > end(%d)",j,
1735            r->typ[j].data.dp.start, r->typ[j].data.dp.end);
1736        if ((r->typ[j].data.dp.start < 1)
1737        || (r->typ[j].data.dp.end > r->N))
1738          dReportError("in ordrec %d: start(%d)<1 or end(%d)>vars(%d)",j,
1739            r->typ[j].data.dp.start, r->typ[j].data.dp.end,r->N);
1740      }
1741    }
1742  }
1743  //assume(r->cf!=NULL);
1744
1745  return TRUE;
1746}
1747#endif
1748
1749static void rO_Align(int &place, int &bitplace)
1750{
1751  // increment place to the next aligned one
1752  // (count as Exponent_t,align as longs)
1753  if (bitplace!=BITS_PER_LONG)
1754  {
1755    place++;
1756    bitplace=BITS_PER_LONG;
1757  }
1758}
1759
1760static void rO_TDegree(int &place, int &bitplace, int start, int end,
1761    long *o, sro_ord &ord_struct)
1762{
1763  // degree (aligned) of variables v_start..v_end, ordsgn 1
1764  rO_Align(place,bitplace);
1765  ord_struct.ord_typ=ro_dp;
1766  ord_struct.data.dp.start=start;
1767  ord_struct.data.dp.end=end;
1768  ord_struct.data.dp.place=place;
1769  o[place]=1;
1770  place++;
1771  rO_Align(place,bitplace);
1772}
1773
1774static void rO_TDegree_neg(int &place, int &bitplace, int start, int end,
1775    long *o, sro_ord &ord_struct)
1776{
1777  // degree (aligned) of variables v_start..v_end, ordsgn -1
1778  rO_Align(place,bitplace);
1779  ord_struct.ord_typ=ro_dp;
1780  ord_struct.data.dp.start=start;
1781  ord_struct.data.dp.end=end;
1782  ord_struct.data.dp.place=place;
1783  o[place]=-1;
1784  place++;
1785  rO_Align(place,bitplace);
1786}
1787
1788static void rO_WDegree(int &place, int &bitplace, int start, int end,
1789    long *o, sro_ord &ord_struct, int *weights)
1790{
1791  // weighted degree (aligned) of variables v_start..v_end, ordsgn 1
1792  while((start<end) && (weights[0]==0)) { start++; weights++; }
1793  while((start<end) && (weights[end-start]==0)) { end--; }
1794  int i;
1795  int pure_tdeg=1;
1796  for(i=start;i<=end;i++)
1797  {
1798    if(weights[i-start]!=1)
1799    {
1800      pure_tdeg=0;
1801      break;
1802    }
1803  }
1804  if (pure_tdeg)
1805  {
1806    rO_TDegree(place,bitplace,start,end,o,ord_struct);
1807    return;
1808  }
1809  rO_Align(place,bitplace);
1810  ord_struct.ord_typ=ro_wp;
1811  ord_struct.data.wp.start=start;
1812  ord_struct.data.wp.end=end;
1813  ord_struct.data.wp.place=place;
1814  ord_struct.data.wp.weights=weights;
1815  o[place]=1;
1816  place++;
1817  rO_Align(place,bitplace);
1818  for(i=start;i<=end;i++)
1819  {
1820    if(weights[i-start]<0)
1821    {
1822      ord_struct.ord_typ=ro_wp_neg;
1823      break;
1824    }
1825  }
1826}
1827
1828static void rO_WDegree_neg(int &place, int &bitplace, int start, int end,
1829    long *o, sro_ord &ord_struct, int *weights)
1830{
1831  // weighted degree (aligned) of variables v_start..v_end, ordsgn -1
1832  while((start<end) && (weights[0]==0)) { start++; weights++; }
1833  while((start<end) && (weights[end-start]==0)) { end--; }
1834  rO_Align(place,bitplace);
1835  ord_struct.ord_typ=ro_wp;
1836  ord_struct.data.wp.start=start;
1837  ord_struct.data.wp.end=end;
1838  ord_struct.data.wp.place=place;
1839  ord_struct.data.wp.weights=weights;
1840  o[place]=-1;
1841  place++;
1842  rO_Align(place,bitplace);
1843  int i;
1844  for(i=start;i<=end;i++)
1845  {
1846    if(weights[i-start]<0)
1847    {
1848      ord_struct.ord_typ=ro_wp_neg;
1849      break;
1850    }
1851  }
1852}
1853
1854static void rO_LexVars(int &place, int &bitplace, int start, int end,
1855  int &prev_ord, long *o,int *v, int bits, int opt_var)
1856{
1857  // a block of variables v_start..v_end with lex order, ordsgn 1
1858  int k;
1859  int incr=1;
1860  if(prev_ord==-1) rO_Align(place,bitplace);
1861
1862  if (start>end)
1863  {
1864    incr=-1;
1865  }
1866  for(k=start;;k+=incr)
1867  {
1868    bitplace-=bits;
1869    if (bitplace < 0) { bitplace=BITS_PER_LONG-bits; place++; }
1870    o[place]=1;
1871    v[k]= place | (bitplace << 24);
1872    if (k==end) break;
1873  }
1874  prev_ord=1;
1875  if (opt_var!= -1)
1876  {
1877    assume((opt_var == end+1) ||(opt_var == end-1));
1878    if((opt_var != end+1) &&(opt_var != end-1)) WarnS("hier-2");
1879    int save_bitplace=bitplace;
1880    bitplace-=bits;
1881    if (bitplace < 0)
1882    {
1883      bitplace=save_bitplace;
1884      return;
1885    }
1886    // there is enough space for the optional var
1887    v[opt_var]=place | (bitplace << 24);
1888  }
1889}
1890
1891static void rO_LexVars_neg(int &place, int &bitplace, int start, int end,
1892  int &prev_ord, long *o,int *v, int bits, int opt_var)
1893{
1894  // a block of variables v_start..v_end with lex order, ordsgn -1
1895  int k;
1896  int incr=1;
1897  if(prev_ord==1) rO_Align(place,bitplace);
1898
1899  if (start>end)
1900  {
1901    incr=-1;
1902  }
1903  for(k=start;;k+=incr)
1904  {
1905    bitplace-=bits;
1906    if (bitplace < 0) { bitplace=BITS_PER_LONG-bits; place++; }
1907    o[place]=-1;
1908    v[k]=place | (bitplace << 24);
1909    if (k==end) break;
1910  }
1911  prev_ord=-1;
1912//  #if 0
1913  if (opt_var!= -1)
1914  {
1915    assume((opt_var == end+1) ||(opt_var == end-1));
1916    if((opt_var != end+1) &&(opt_var != end-1)) WarnS("hier-1");
1917    int save_bitplace=bitplace;
1918    bitplace-=bits;
1919    if (bitplace < 0)
1920    {
1921      bitplace=save_bitplace;
1922      return;
1923    }
1924    // there is enough space for the optional var
1925    v[opt_var]=place | (bitplace << 24);
1926  }
1927//  #endif
1928}
1929
1930static void rO_Syzcomp(int &place, int &bitplace, int &prev_ord,
1931    long *o, sro_ord &ord_struct)
1932{
1933  // ordering is derived from component number
1934  rO_Align(place,bitplace);
1935  ord_struct.ord_typ=ro_syzcomp;
1936  ord_struct.data.syzcomp.place=place;
1937  ord_struct.data.syzcomp.Components=NULL;
1938  ord_struct.data.syzcomp.ShiftedComponents=NULL;
1939  o[place]=1;
1940  prev_ord=1;
1941  place++;
1942  rO_Align(place,bitplace);
1943}
1944
1945static void rO_Syz(int &place, int &bitplace, int &prev_ord,
1946    long *o, sro_ord &ord_struct)
1947{
1948  // ordering is derived from component number
1949  // let's reserve one Exponent_t for it
1950  if ((prev_ord== 1) || (bitplace!=BITS_PER_LONG))
1951    rO_Align(place,bitplace);
1952  ord_struct.ord_typ=ro_syz;
1953  ord_struct.data.syz.place=place;
1954  ord_struct.data.syz.limit=0;
1955  ord_struct.data.syz.syz_index = NULL;
1956  ord_struct.data.syz.curr_index = 1;
1957  o[place]= -1;
1958  prev_ord=-1;
1959  place++;
1960}
1961
1962static unsigned long rGetExpSize(unsigned long bitmask, int & bits)
1963{
1964  if (bitmask == 0)
1965  {
1966    bits=16; bitmask=0xffff;
1967  }
1968  else if (bitmask <= 1)
1969  {
1970    bits=1; bitmask = 1;
1971  }
1972  else if (bitmask <= 3)
1973  {
1974    bits=2; bitmask = 3;
1975  }
1976  else if (bitmask <= 7)
1977  {
1978    bits=3; bitmask=7;
1979  }
1980  else if (bitmask <= 0xf)
1981  {
1982    bits=4; bitmask=0xf;
1983  }
1984  else if (bitmask <= 0x1f)
1985  {
1986    bits=5; bitmask=0x1f;
1987  }
1988  else if (bitmask <= 0x3f)
1989  {
1990    bits=6; bitmask=0x3f;
1991  }
1992#if SIZEOF_LONG == 8
1993  else if (bitmask <= 0x7f)
1994  {
1995    bits=7; bitmask=0x7f; /* 64 bit longs only */
1996  }
1997#endif
1998  else if (bitmask <= 0xff)
1999  {
2000    bits=8; bitmask=0xff;
2001  }
2002#if SIZEOF_LONG == 8
2003  else if (bitmask <= 0x1ff)
2004  {
2005    bits=9; bitmask=0x1ff; /* 64 bit longs only */
2006  }
2007#endif
2008  else if (bitmask <= 0x3ff)
2009  {
2010    bits=10; bitmask=0x3ff;
2011  }
2012#if SIZEOF_LONG == 8
2013  else if (bitmask <= 0xfff)
2014  {
2015    bits=12; bitmask=0xfff; /* 64 bit longs only */
2016  }
2017#endif
2018  else if (bitmask <= 0xffff)
2019  {
2020    bits=16; bitmask=0xffff;
2021  }
2022#if SIZEOF_LONG == 8
2023  else if (bitmask <= 0xfffff)
2024  {
2025    bits=20; bitmask=0xfffff; /* 64 bit longs only */
2026  }
2027  else if (bitmask <= 0xffffffff)
2028  {
2029    bits=32; bitmask=0xffffffff;
2030  }
2031  else
2032  {
2033    bits=64; bitmask=0xffffffffffffffff;
2034  }
2035#else
2036  else
2037  {
2038    bits=32; bitmask=0xffffffff;
2039  }
2040#endif
2041  return bitmask;
2042}
2043
2044/*2
2045* optimize rGetExpSize for a block of N variables, exp <=bitmask
2046*/
2047static unsigned long rGetExpSize(unsigned long bitmask, int & bits, int N)
2048{
2049  bitmask =rGetExpSize(bitmask, bits);
2050  int vars_per_long=BIT_SIZEOF_LONG/bits;
2051  int bits1;
2052  loop
2053  {
2054    if (bits == BIT_SIZEOF_LONG)
2055    {
2056      bits =  BIT_SIZEOF_LONG - 1;
2057      return LONG_MAX;
2058    }
2059    unsigned long bitmask1 =rGetExpSize(bitmask+1, bits1);
2060    int vars_per_long1=BIT_SIZEOF_LONG/bits1;
2061    if ((((N+vars_per_long-1)/vars_per_long) ==
2062         ((N+vars_per_long1-1)/vars_per_long1)))
2063    {
2064      vars_per_long=vars_per_long1;
2065      bits=bits1;
2066      bitmask=bitmask1;
2067    }
2068    else
2069    {
2070      return bitmask; /* and bits */
2071    }
2072  }
2073}
2074
2075/*2
2076 * create a copy of the ring r, which must be equivalent to currRing
2077 * used for std computations
2078 * may share data structures with currRing
2079 * DOES CALL rComplete
2080 */
2081ring rModifyRing(ring r, BOOLEAN omit_degree,
2082                         BOOLEAN omit_comp,
2083                         unsigned long exp_limit)
2084{
2085  assume (r != NULL );
2086  assume (exp_limit > 1);
2087  BOOLEAN need_other_ring;
2088  BOOLEAN omitted_degree = FALSE;
2089  int bits;
2090
2091  exp_limit=rGetExpSize(exp_limit, bits, r->N);
2092  need_other_ring = (exp_limit != r->bitmask);
2093
2094  int nblocks=rBlocks(r);
2095  int *order=(int*)omAlloc0((nblocks+1)*sizeof(int));
2096  int *block0=(int*)omAlloc0((nblocks+1)*sizeof(int));
2097  int *block1=(int*)omAlloc0((nblocks+1)*sizeof(int));
2098  int **wvhdl=(int**)omAlloc0((nblocks+1)*sizeof(int_ptr));
2099
2100  int i=0;
2101  int j=0; /*  i index in r, j index in res */
2102  loop
2103  {
2104    BOOLEAN copy_block_index=TRUE;
2105    int r_ord=r->order[i];
2106    if (r->block0[i]==r->block1[i])
2107    {
2108      switch(r_ord)
2109      {
2110        case ringorder_wp:
2111        case ringorder_dp:
2112        case ringorder_Wp:
2113        case ringorder_Dp:
2114          r_ord=ringorder_lp;
2115          break;
2116        case ringorder_Ws:
2117        case ringorder_Ds:
2118        case ringorder_ws:
2119        case ringorder_ds:
2120          r_ord=ringorder_ls;
2121          break;
2122        default:
2123          break;
2124      }
2125    }
2126    switch(r_ord)
2127    {
2128      case ringorder_C:
2129      case ringorder_c:
2130        if (!omit_comp)
2131        {
2132          order[j]=r_ord; /*r->order[i]*/;
2133        }
2134        else
2135        {
2136          j--;
2137          need_other_ring=TRUE;
2138          omit_comp=FALSE;
2139          copy_block_index=FALSE;
2140        }
2141        break;
2142      case ringorder_wp:
2143      case ringorder_dp:
2144      case ringorder_ws:
2145      case ringorder_ds:
2146        if(!omit_degree)
2147        {
2148          order[j]=r_ord; /*r->order[i]*/;
2149        }
2150        else
2151        {
2152          order[j]=ringorder_rp;
2153          need_other_ring=TRUE;
2154          omit_degree=FALSE;
2155          omitted_degree = TRUE;
2156        }
2157        break;
2158      case ringorder_Wp:
2159      case ringorder_Dp:
2160      case ringorder_Ws:
2161      case ringorder_Ds:
2162        if(!omit_degree)
2163        {
2164          order[j]=r_ord; /*r->order[i];*/
2165        }
2166        else
2167        {
2168          order[j]=ringorder_lp;
2169          need_other_ring=TRUE;
2170          omit_degree=FALSE;
2171          omitted_degree = TRUE;
2172        }
2173        break;
2174      default:
2175        order[j]=r_ord; /*r->order[i];*/
2176        break;
2177    }
2178    if (copy_block_index)
2179    {
2180      block0[j]=r->block0[i];
2181      block1[j]=r->block1[i];
2182      wvhdl[j]=r->wvhdl[i];
2183    }
2184    i++;j++;
2185    // order[j]=ringorder_no; //  done by omAlloc0
2186    if (i==nblocks) break;
2187  }
2188  if(!need_other_ring)
2189  {
2190    omFreeSize(order,(nblocks+1)*sizeof(int));
2191    omFreeSize(block0,(nblocks+1)*sizeof(int));
2192    omFreeSize(block1,(nblocks+1)*sizeof(int));
2193    omFreeSize(wvhdl,(nblocks+1)*sizeof(int_ptr));
2194    return r;
2195  }
2196  ring res=(ring)omAlloc0Bin(ip_sring_bin);
2197  *res = *r;
2198  // res->qideal, res->idroot ???
2199  res->wvhdl=wvhdl;
2200  res->order=order;
2201  res->block0=block0;
2202  res->block1=block1;
2203  res->bitmask=exp_limit;
2204  int tmpref=r->cf->ref;
2205  rComplete(res, 1);
2206  r->cf->ref=tmpref;
2207
2208  // adjust res->pFDeg: if it was changed globally, then
2209  // it must also be changed for new ring
2210  if (r->pFDegOrig != res->pFDegOrig &&
2211           rOrd_is_WeightedDegree_Ordering(r))
2212  {
2213    // still might need adjustment for weighted orderings
2214    // and omit_degree
2215    res->firstwv = r->firstwv;
2216    res->firstBlockEnds = r->firstBlockEnds;
2217    res->pFDeg = res->pFDegOrig = pWFirstTotalDegree;
2218  }
2219  if (omitted_degree)
2220    res->pLDeg = res->pLDegOrig = r->pLDegOrig;
2221
2222  rOptimizeLDeg(res);
2223
2224  // set syzcomp
2225  if (res->typ != NULL && res->typ[0].ord_typ == ro_syz)
2226  {
2227    res->typ[0] = r->typ[0];
2228    if (r->typ[0].data.syz.limit > 0)
2229    {
2230      res->typ[0].data.syz.syz_index
2231        = (int*) omAlloc((r->typ[0].data.syz.limit +1)*sizeof(int));
2232      memcpy(res->typ[0].data.syz.syz_index, r->typ[0].data.syz.syz_index,
2233             (r->typ[0].data.syz.limit +1)*sizeof(int));
2234    }
2235  }
2236  return res;
2237}
2238
2239// construct Wp,C ring
2240ring rModifyRing_Wp(ring r, int* weights)
2241{
2242  ring res=(ring)omAlloc0Bin(ip_sring_bin);
2243  *res = *r;
2244  /*weights: entries for 3 blocks: NULL*/
2245  res->wvhdl = (int **)omAlloc0(3 * sizeof(int_ptr));
2246  /*order: dp,C,0*/
2247  res->order = (int *) omAlloc(3 * sizeof(int *));
2248  res->block0 = (int *)omAlloc0(3 * sizeof(int *));
2249  res->block1 = (int *)omAlloc0(3 * sizeof(int *));
2250  /* ringorder dp for the first block: var 1..3 */
2251  res->order[0]  = ringorder_Wp;
2252  res->block0[0] = 1;
2253  res->block1[0] = r->N;
2254  res->wvhdl[0] = weights;
2255  /* ringorder C for the second block: no vars */
2256  res->order[1]  = ringorder_C;
2257  /* the last block: everything is 0 */
2258  res->order[2]  = 0;
2259  /*polynomial ring*/
2260  res->OrdSgn    = 1;
2261
2262  int tmpref=r->cf->ref;
2263  rComplete(res, 1);
2264  r->cf->ref=tmpref;
2265  return res;
2266}
2267
2268// construct lp ring with r->N variables, r->names vars....
2269ring rModifyRing_Simple(ring r, BOOLEAN ommit_degree, BOOLEAN ommit_comp, unsigned long exp_limit, BOOLEAN &simple)
2270{
2271  simple=TRUE;
2272  if (!rHasSimpleOrder(r))
2273  {
2274    simple=FALSE; // sorting needed
2275    assume (r != NULL );
2276    assume (exp_limit > 1);
2277    BOOLEAN omitted_degree = FALSE;
2278    int bits;
2279
2280    exp_limit=rGetExpSize(exp_limit, bits, r->N);
2281
2282    int nblocks=1+(ommit_comp!=0);
2283    int *order=(int*)omAlloc0((nblocks+1)*sizeof(int));
2284    int *block0=(int*)omAlloc0((nblocks+1)*sizeof(int));
2285    int *block1=(int*)omAlloc0((nblocks+1)*sizeof(int));
2286    int **wvhdl=(int**)omAlloc0((nblocks+1)*sizeof(int_ptr));
2287
2288    order[0]=ringorder_lp;
2289    block0[0]=1;
2290    block1[0]=r->N;
2291    if (!ommit_comp)
2292    {
2293      order[1]=ringorder_C;
2294    }
2295    ring res=(ring)omAlloc0Bin(ip_sring_bin);
2296    *res = *r;
2297    // res->qideal, res->idroot ???
2298    res->wvhdl=wvhdl;
2299    res->order=order;
2300    res->block0=block0;
2301    res->block1=block1;
2302    res->bitmask=exp_limit;
2303    int tmpref=r->cf->ref;
2304    rComplete(res, 1);
2305    r->cf->ref=tmpref;
2306
2307    rOptimizeLDeg(res);
2308
2309    return res;
2310  }
2311  return rModifyRing(r, ommit_degree, ommit_comp, exp_limit);
2312}
2313
2314void rKillModifiedRing_Simple(ring r)
2315{
2316  rKillModifiedRing(r);
2317}
2318
2319
2320void rKillModifiedRing(ring r)
2321{
2322  rUnComplete(r);
2323  omFree(r->order);
2324  omFree(r->block0);
2325  omFree(r->block1);
2326  omFree(r->wvhdl);
2327  omFreeBin(r,ip_sring_bin);
2328}
2329
2330void rKillModified_Wp_Ring(ring r)
2331{
2332  rUnComplete(r);
2333  omFree(r->order);
2334  omFree(r->block0);
2335  omFree(r->block1);
2336  omFree(r->wvhdl[0]);
2337  omFree(r->wvhdl);
2338  omFreeBin(r,ip_sring_bin);
2339}
2340
2341static void rSetOutParams(ring r)
2342{
2343  r->VectorOut = (r->order[0] == ringorder_c);
2344  r->ShortOut = TRUE;
2345#ifdef HAVE_TCL
2346  if (tcllmode)
2347  {
2348    r->ShortOut = FALSE;
2349  }
2350  else
2351#endif
2352  {
2353    int i;
2354    if ((r->parameter!=NULL) && (r->ch<2))
2355    {
2356      for (i=0;i<rPar(r);i++)
2357      {
2358        if(strlen(r->parameter[i])>1)
2359        {
2360          r->ShortOut=FALSE;
2361          break;
2362        }
2363      }
2364    }
2365    if (r->ShortOut)
2366    {
2367      // Hmm... sometimes (e.g., from maGetPreimage) new variables
2368      // are intorduced, but their names are never set
2369      // hence, we do the following awkward trick
2370      int N = omSizeWOfAddr(r->names);
2371      if (r->N < N) N = r->N;
2372
2373      for (i=(N-1);i>=0;i--)
2374      {
2375        if(r->names[i] != NULL && strlen(r->names[i])>1)
2376        {
2377          r->ShortOut=FALSE;
2378          break;
2379        }
2380      }
2381    }
2382  }
2383  r->CanShortOut = r->ShortOut;
2384}
2385
2386/*2
2387* sets pMixedOrder and pComponentOrder for orderings with more than one block
2388* block of variables (ip is the block number, o_r the number of the ordering)
2389* o is the position of the orderingering in r
2390*/
2391static void rHighSet(ring r, int o_r, int o)
2392{
2393  switch(o_r)
2394  {
2395    case ringorder_lp:
2396    case ringorder_dp:
2397    case ringorder_Dp:
2398    case ringorder_wp:
2399    case ringorder_Wp:
2400    case ringorder_rp:
2401    case ringorder_a:
2402    case ringorder_aa:
2403      if (r->OrdSgn==-1) r->MixedOrder=TRUE;
2404      break;
2405    case ringorder_ls:
2406    case ringorder_ds:
2407    case ringorder_Ds:
2408    case ringorder_s:
2409      break;
2410    case ringorder_ws:
2411    case ringorder_Ws:
2412      if (r->wvhdl[o]!=NULL)
2413      {
2414        int i;
2415        for(i=r->block1[o]-r->block0[o];i>=0;i--)
2416          if (r->wvhdl[o][i]<0) { r->MixedOrder=TRUE; break; }
2417      }
2418      break;
2419    case ringorder_c:
2420      r->ComponentOrder=1;
2421      break;
2422    case ringorder_C:
2423    case ringorder_S:
2424      r->ComponentOrder=-1;
2425      break;
2426    case ringorder_M:
2427      r->MixedOrder=TRUE;
2428      break;
2429    default:
2430      dReportError("wrong internal ordering:%d at %s, l:%d\n",o_r,__FILE__,__LINE__);
2431  }
2432}
2433
2434static void rSetFirstWv(ring r, int i, int* order, int* block1, int** wvhdl)
2435{
2436  // cheat for ringorder_aa
2437  if (order[i] == ringorder_aa)
2438    i++;
2439  if(block1[i]!=r->N) r->LexOrder=TRUE;
2440  r->firstBlockEnds=block1[i];
2441  r->firstwv = wvhdl[i];
2442  if ((order[i]== ringorder_ws) || (order[i]==ringorder_Ws))
2443  {
2444    int j;
2445    for(j=block1[i]-r->block0[i];j>=0;j--)
2446      if (r->firstwv[j]<0) { r->MixedOrder=TRUE; break; }
2447  }
2448}
2449
2450static void rOptimizeLDeg(ring r)
2451{
2452  if (r->pFDeg == pDeg)
2453  {
2454    if (r->pLDeg == pLDeg1)
2455      r->pLDeg = pLDeg1_Deg;
2456    if (r->pLDeg == pLDeg1c)
2457      r->pLDeg = pLDeg1c_Deg;
2458  }
2459  else if (r->pFDeg == pTotaldegree)
2460  {
2461    if (r->pLDeg == pLDeg1)
2462      r->pLDeg = pLDeg1_Totaldegree;
2463    if (r->pLDeg == pLDeg1c)
2464      r->pLDeg = pLDeg1c_Totaldegree;
2465  }
2466  else if (r->pFDeg == pWFirstTotalDegree)
2467  {
2468    if (r->pLDeg == pLDeg1)
2469      r->pLDeg = pLDeg1_WFirstTotalDegree;
2470    if (r->pLDeg == pLDeg1c)
2471      r->pLDeg = pLDeg1c_WFirstTotalDegree;
2472  }
2473}
2474
2475// set pFDeg, pLDeg, MixOrder, ComponentOrder, etc
2476static void rSetDegStuff(ring r)
2477{
2478  int* order = r->order;
2479  int* block0 = r->block0;
2480  int* block1 = r->block1;
2481  int** wvhdl = r->wvhdl;
2482
2483  if (order[0]==ringorder_S ||order[0]==ringorder_s)
2484  {
2485    order++;
2486    block0++;
2487    block1++;
2488    wvhdl++;
2489  }
2490  r->LexOrder = FALSE;
2491  r->MixedOrder = FALSE;
2492  r->ComponentOrder = 1;
2493  r->pFDeg = pTotaldegree;
2494  r->pLDeg = (r->OrdSgn == 1 ? pLDegb : pLDeg0);
2495
2496  /*======== ordering type is (_,c) =========================*/
2497  if ((order[0]==ringorder_unspec) || (order[1] == 0)
2498      ||(
2499    ((order[1]==ringorder_c)||(order[1]==ringorder_C)
2500     ||(order[1]==ringorder_S)
2501     ||(order[1]==ringorder_s))
2502    && (order[0]!=ringorder_M)
2503    && (order[2]==0))
2504    )
2505  {
2506    if ((order[0]!=ringorder_unspec)
2507    && ((order[1]==ringorder_C)||(order[1]==ringorder_S)||
2508        (order[1]==ringorder_s)))
2509      r->ComponentOrder=-1;
2510    if (r->OrdSgn == -1) r->pLDeg = pLDeg0c;
2511    if ((order[0] == ringorder_lp) || (order[0] == ringorder_ls) || order[0] == ringorder_rp)
2512    {
2513      r->LexOrder=TRUE;
2514      r->pLDeg = pLDeg1c;
2515    }
2516    if (order[0] == ringorder_wp || order[0] == ringorder_Wp ||
2517        order[0] == ringorder_ws || order[0] == ringorder_Ws)
2518      r->pFDeg = pWFirstTotalDegree;
2519    r->firstBlockEnds=block1[0];
2520    r->firstwv = wvhdl[0];
2521  }
2522  /*======== ordering type is (c,_) =========================*/
2523  else if (((order[0]==ringorder_c)
2524            ||(order[0]==ringorder_C)
2525            ||(order[0]==ringorder_S)
2526            ||(order[0]==ringorder_s))
2527  && (order[1]!=ringorder_M)
2528  &&  (order[2]==0))
2529  {
2530    if ((order[0]==ringorder_C)||(order[0]==ringorder_S)||
2531        order[0]==ringorder_s)
2532      r->ComponentOrder=-1;
2533    if ((order[1] == ringorder_lp) || (order[1] == ringorder_ls) || order[1] == ringorder_rp)
2534    {
2535      r->LexOrder=TRUE;
2536      r->pLDeg = pLDeg1c;
2537    }
2538    r->firstBlockEnds=block1[1];
2539    r->firstwv = wvhdl[1];
2540    if (order[1] == ringorder_wp || order[1] == ringorder_Wp ||
2541        order[1] == ringorder_ws || order[1] == ringorder_Ws)
2542      r->pFDeg = pWFirstTotalDegree;
2543  }
2544  /*------- more than one block ----------------------*/
2545  else
2546  {
2547    if ((r->VectorOut)||(order[0]==ringorder_C)||(order[0]==ringorder_S)||(order[0]==ringorder_s))
2548    {
2549      rSetFirstWv(r, 1, order, block1, wvhdl);
2550    }
2551    else
2552      rSetFirstWv(r, 0, order, block1, wvhdl);
2553
2554    /*the number of orderings:*/
2555    int i = 0;
2556    while (order[++i] != 0);
2557    do
2558    {
2559      i--;
2560      rHighSet(r, order[i],i);
2561    }
2562    while (i != 0);
2563
2564    if ((order[0]!=ringorder_c)
2565        && (order[0]!=ringorder_C)
2566        && (order[0]!=ringorder_S)
2567        && (order[0]!=ringorder_s))
2568    {
2569      r->pLDeg = pLDeg1c;
2570    }
2571    else
2572    {
2573      r->pLDeg = pLDeg1;
2574    }
2575    r->pFDeg = pWTotaldegree; // may be improved: pTotaldegree for lp/dp/ls/.. blocks
2576  }
2577  if (rOrd_is_Totaldegree_Ordering(r) || rOrd_is_WeightedDegree_Ordering(r))
2578    r->pFDeg = pDeg;
2579
2580  r->pFDegOrig = r->pFDeg;
2581  r->pLDegOrig = r->pLDeg;
2582  rOptimizeLDeg(r);
2583}
2584
2585/*2
2586* set NegWeightL_Size, NegWeightL_Offset
2587*/
2588static void rSetNegWeight(ring r)
2589{
2590  int i,l;
2591  if (r->typ!=NULL)
2592  {
2593    l=0;
2594    for(i=0;i<r->OrdSize;i++)
2595    {
2596      if(r->typ[i].ord_typ==ro_wp_neg) l++;
2597    }
2598    if (l>0)
2599    {
2600      r->NegWeightL_Size=l;
2601      r->NegWeightL_Offset=(int *) omAlloc(l*sizeof(int));
2602      l=0;
2603      for(i=0;i<r->OrdSize;i++)
2604      {
2605        if(r->typ[i].ord_typ==ro_wp_neg)
2606        {
2607          r->NegWeightL_Offset[l]=r->typ[i].data.wp.place;
2608          l++;
2609        }
2610      }
2611      return;
2612    }
2613  }
2614  r->NegWeightL_Size = 0;
2615  r->NegWeightL_Offset = NULL;
2616}
2617
2618static void rSetOption(ring r)
2619{
2620  // set redthrough
2621  if (!TEST_OPT_OLDSTD && r->OrdSgn == 1 && ! r->LexOrder)
2622    r->options |= Sy_bit(OPT_REDTHROUGH);
2623  else
2624    r->options &= ~Sy_bit(OPT_REDTHROUGH);
2625
2626  // set intStrategy
2627  if (rField_is_Extension(r) || rField_is_Q(r))
2628    r->options |= Sy_bit(OPT_INTSTRATEGY);
2629  else
2630    r->options &= ~Sy_bit(OPT_INTSTRATEGY);
2631
2632  // set redTail
2633  if (r->LexOrder || r->OrdSgn == -1 || rField_is_Extension(r))
2634    r->options &= ~Sy_bit(OPT_REDTAIL);
2635  else
2636    r->options |= Sy_bit(OPT_REDTAIL);
2637}
2638
2639BOOLEAN rComplete(ring r, int force)
2640{
2641  if (r->VarOffset!=NULL && force == 0) return FALSE;
2642  nInitChar(r);
2643  rSetOutParams(r);
2644  int n=rBlocks(r)-1;
2645  int i;
2646  int bits;
2647  r->bitmask=rGetExpSize(r->bitmask,bits,r->N);
2648  r->BitsPerExp = bits;
2649  r->ExpPerLong = BIT_SIZEOF_LONG / bits;
2650  r->divmask=rGetDivMask(bits);
2651
2652  // will be used for ordsgn:
2653  long *tmp_ordsgn=(long *)omAlloc0(2*(n+r->N)*sizeof(long));
2654  // will be used for VarOffset:
2655  int *v=(int *)omAlloc((r->N+1)*sizeof(int));
2656  for(i=r->N; i>=0 ; i--)
2657  {
2658    v[i]=-1;
2659  }
2660  sro_ord *tmp_typ=(sro_ord *)omAlloc0(2*(n+r->N)*sizeof(sro_ord));
2661  int typ_i=0;
2662  int prev_ordsgn=0;
2663
2664  // fill in v, tmp_typ, tmp_ordsgn, determine typ_i (== ordSize)
2665  int j=0;
2666  int j_bits=BITS_PER_LONG;
2667  BOOLEAN need_to_add_comp=FALSE;
2668  for(i=0;i<n;i++)
2669  {
2670    tmp_typ[typ_i].order_index=i;
2671    switch (r->order[i])
2672    {
2673      case ringorder_a:
2674      case ringorder_aa:
2675        rO_WDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,tmp_typ[typ_i],
2676                   r->wvhdl[i]);
2677        typ_i++;
2678        break;
2679
2680      case ringorder_c:
2681        rO_Align(j, j_bits);
2682        rO_LexVars_neg(j, j_bits, 0,0, prev_ordsgn,tmp_ordsgn,v,BITS_PER_LONG, -1);
2683        break;
2684
2685      case ringorder_C:
2686        rO_Align(j, j_bits);
2687        rO_LexVars(j, j_bits, 0,0, prev_ordsgn,tmp_ordsgn,v,BITS_PER_LONG, -1);
2688        break;
2689
2690      case ringorder_M:
2691        {
2692          int k,l;
2693          k=r->block1[i]-r->block0[i]+1; // number of vars
2694          for(l=0;l<k;l++)
2695          {
2696            rO_WDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
2697                       tmp_typ[typ_i],
2698                       r->wvhdl[i]+(r->block1[i]-r->block0[i]+1)*l);
2699            typ_i++;
2700          }
2701          break;
2702        }
2703
2704      case ringorder_lp:
2705        rO_LexVars(j, j_bits, r->block0[i],r->block1[i], prev_ordsgn,
2706                   tmp_ordsgn,v,bits, -1);
2707        break;
2708
2709      case ringorder_ls:
2710        rO_LexVars_neg(j, j_bits, r->block0[i],r->block1[i], prev_ordsgn,
2711                       tmp_ordsgn,v, bits, -1);
2712        break;
2713
2714      case ringorder_rp:
2715        rO_LexVars_neg(j, j_bits, r->block1[i],r->block0[i], prev_ordsgn,
2716                       tmp_ordsgn,v, bits, -1);
2717        break;
2718
2719      case ringorder_dp:
2720        if (r->block0[i]==r->block1[i])
2721        {
2722          rO_LexVars(j, j_bits, r->block0[i],r->block0[i], prev_ordsgn,
2723                     tmp_ordsgn,v, bits, -1);
2724        }
2725        else
2726        {
2727          rO_TDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
2728                     tmp_typ[typ_i]);
2729          typ_i++;
2730          rO_LexVars_neg(j, j_bits, r->block1[i],r->block0[i]+1,
2731                         prev_ordsgn,tmp_ordsgn,v,bits, r->block0[i]);
2732        }
2733        break;
2734
2735      case ringorder_Dp:
2736        if (r->block0[i]==r->block1[i])
2737        {
2738          rO_LexVars(j, j_bits, r->block0[i],r->block0[i], prev_ordsgn,
2739                     tmp_ordsgn,v, bits, -1);
2740        }
2741        else
2742        {
2743          rO_TDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
2744                     tmp_typ[typ_i]);
2745          typ_i++;
2746          rO_LexVars(j, j_bits, r->block0[i],r->block1[i]-1, prev_ordsgn,
2747                     tmp_ordsgn,v, bits, r->block1[i]);
2748        }
2749        break;
2750
2751      case ringorder_ds:
2752        if (r->block0[i]==r->block1[i])
2753        {
2754          rO_LexVars_neg(j, j_bits,r->block0[i],r->block1[i],prev_ordsgn,
2755                         tmp_ordsgn,v,bits, -1);
2756        }
2757        else
2758        {
2759          rO_TDegree_neg(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
2760                         tmp_typ[typ_i]);
2761          typ_i++;
2762          rO_LexVars_neg(j, j_bits, r->block1[i],r->block0[i]+1,
2763                         prev_ordsgn,tmp_ordsgn,v,bits, r->block0[i]);
2764        }
2765        break;
2766
2767      case ringorder_Ds:
2768        if (r->block0[i]==r->block1[i])
2769        {
2770          rO_LexVars_neg(j, j_bits, r->block0[i],r->block0[i],prev_ordsgn,
2771                         tmp_ordsgn,v, bits, -1);
2772        }
2773        else
2774        {
2775          rO_TDegree_neg(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
2776                         tmp_typ[typ_i]);
2777          typ_i++;
2778          rO_LexVars(j, j_bits, r->block0[i],r->block1[i]-1, prev_ordsgn,
2779                     tmp_ordsgn,v, bits, r->block1[i]);
2780        }
2781        break;
2782
2783      case ringorder_wp:
2784        rO_WDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
2785                   tmp_typ[typ_i], r->wvhdl[i]);
2786        typ_i++;
2787        if (r->block1[i]!=r->block0[i])
2788        {
2789          rO_LexVars_neg(j, j_bits,r->block1[i],r->block0[i]+1, prev_ordsgn,
2790                         tmp_ordsgn, v,bits, r->block0[i]);
2791        }
2792        break;
2793
2794      case ringorder_Wp:
2795        rO_WDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
2796                   tmp_typ[typ_i], r->wvhdl[i]);
2797        typ_i++;
2798        if (r->block1[i]!=r->block0[i])
2799        {
2800          rO_LexVars(j, j_bits,r->block0[i],r->block1[i]-1, prev_ordsgn,
2801                     tmp_ordsgn,v, bits, r->block1[i]);
2802        }
2803        break;
2804
2805      case ringorder_ws:
2806        rO_WDegree_neg(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
2807                       tmp_typ[typ_i], r->wvhdl[i]);
2808        typ_i++;
2809        if (r->block1[i]!=r->block0[i])
2810        {
2811          rO_LexVars_neg(j, j_bits,r->block1[i],r->block0[i]+1, prev_ordsgn,
2812                         tmp_ordsgn, v,bits, r->block0[i]);
2813        }
2814        break;
2815
2816      case ringorder_Ws:
2817        rO_WDegree_neg(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
2818                       tmp_typ[typ_i], r->wvhdl[i]);
2819        typ_i++;
2820        if (r->block1[i]!=r->block0[i])
2821        {
2822          rO_LexVars(j, j_bits,r->block0[i],r->block1[i]-1, prev_ordsgn,
2823                     tmp_ordsgn,v, bits, r->block1[i]);
2824        }
2825        break;
2826
2827      case ringorder_S:
2828        rO_Syzcomp(j, j_bits,prev_ordsgn, tmp_ordsgn,tmp_typ[typ_i]);
2829        need_to_add_comp=TRUE;
2830        typ_i++;
2831        break;
2832
2833      case ringorder_s:
2834        rO_Syz(j, j_bits,prev_ordsgn, tmp_ordsgn,tmp_typ[typ_i]);
2835        need_to_add_comp=TRUE;
2836        typ_i++;
2837        break;
2838
2839      case ringorder_unspec:
2840      case ringorder_no:
2841      default:
2842        dReportError("undef. ringorder used\n");
2843        break;
2844    }
2845  }
2846
2847  int j0=j; // save j
2848  int j_bits0=j_bits; // save jbits
2849  rO_Align(j,j_bits);
2850  r->CmpL_Size = j;
2851
2852  j_bits=j_bits0; j=j0;
2853
2854  // fill in some empty slots with variables not already covered
2855  // v0 is special, is therefore normally already covered
2856  // now we do have rings without comp...
2857  if((need_to_add_comp) && (v[0]== -1))
2858  {
2859    if (prev_ordsgn==1)
2860    {
2861      rO_Align(j, j_bits);
2862      rO_LexVars(j, j_bits, 0,0, prev_ordsgn,tmp_ordsgn,v,BITS_PER_LONG, -1);
2863    }
2864    else
2865    {
2866      rO_Align(j, j_bits);
2867      rO_LexVars_neg(j, j_bits, 0,0, prev_ordsgn,tmp_ordsgn,v,BITS_PER_LONG, -1);
2868    }
2869  }
2870  // the variables
2871  for(i=1 ; i<r->N+1 ; i++)
2872  {
2873    if(v[i]==(-1))
2874    {
2875      if (prev_ordsgn==1)
2876      {
2877        rO_LexVars(j, j_bits, i,i, prev_ordsgn,tmp_ordsgn,v,bits, -1);
2878      }
2879      else
2880      {
2881        rO_LexVars_neg(j,j_bits,i,i, prev_ordsgn,tmp_ordsgn,v,bits, -1);
2882      }
2883    }
2884  }
2885
2886  rO_Align(j,j_bits);
2887  // ----------------------------
2888  // finished with constructing the monomial, computing sizes:
2889
2890  r->ExpL_Size=j;
2891  r->PolyBin = omGetSpecBin(POLYSIZE + (r->ExpL_Size)*sizeof(long));
2892  assume(r->PolyBin != NULL);
2893
2894  // ----------------------------
2895  // indices and ordsgn vector for comparison
2896  //
2897  // r->pCompHighIndex already set
2898  r->ordsgn=(long *)omAlloc0(r->ExpL_Size*sizeof(long));
2899
2900  for(j=0;j<r->CmpL_Size;j++)
2901  {
2902    r->ordsgn[j] = tmp_ordsgn[j];
2903  }
2904
2905  omFreeSize((ADDRESS)tmp_ordsgn,(2*(n+r->N)*sizeof(long)));
2906
2907  // ----------------------------
2908  // description of orderings for setm:
2909  //
2910  r->OrdSize=typ_i;
2911  if (typ_i==0) r->typ=NULL;
2912  else
2913  {
2914    r->typ=(sro_ord*)omAlloc(typ_i*sizeof(sro_ord));
2915    memcpy(r->typ,tmp_typ,typ_i*sizeof(sro_ord));
2916  }
2917  omFreeSize((ADDRESS)tmp_typ,(2*(n+r->N)*sizeof(sro_ord)));
2918
2919  // ----------------------------
2920  // indices for (first copy of ) variable entries in exp.e vector (VarOffset):
2921  r->VarOffset=v;
2922
2923  // ----------------------------
2924  // other indicies
2925  r->pCompIndex=(r->VarOffset[0] & 0xffff); //r->VarOffset[0];
2926  i=0; // position
2927  j=0; // index in r->typ
2928  if (i==r->pCompIndex) i++;
2929  while ((j < r->OrdSize)
2930         && ((r->typ[j].ord_typ==ro_syzcomp) ||
2931             (r->typ[j].ord_typ==ro_syz) ||
2932             (r->order[r->typ[j].order_index] == ringorder_aa)))
2933  {
2934    i++; j++;
2935  }
2936  if (i==r->pCompIndex) i++;
2937  r->pOrdIndex=i;
2938
2939  // ----------------------------
2940  rSetDegStuff(r);
2941  rSetOption(r);
2942  // ----------------------------
2943  // r->p_Setm
2944  r->p_Setm = p_GetSetmProc(r);
2945
2946  // ----------------------------
2947  // set VarL_*
2948  rSetVarL(r);
2949
2950  //  ----------------------------
2951  // right-adjust VarOffset
2952  rRightAdjustVarOffset(r);
2953
2954  // ----------------------------
2955  // set NegWeightL*
2956  rSetNegWeight(r);
2957
2958  // ----------------------------
2959  // p_Procs: call AFTER NegWeightL
2960  r->p_Procs = (p_Procs_s*)omAlloc(sizeof(p_Procs_s));
2961  p_ProcsSet(r, r->p_Procs);
2962
2963  return FALSE;
2964}
2965
2966void rUnComplete(ring r)
2967{
2968  if (r == NULL) return;
2969  if (r->VarOffset != NULL)
2970  {
2971    if (r->PolyBin != NULL)
2972      omUnGetSpecBin(&(r->PolyBin));
2973
2974    omFreeSize((ADDRESS)r->VarOffset, (r->N +1)*sizeof(int));
2975    if (r->order != NULL)
2976    {
2977      if (r->order[0] == ringorder_s && r->typ[0].data.syz.limit > 0)
2978      {
2979        omFreeSize(r->typ[0].data.syz.syz_index,
2980             (r->typ[0].data.syz.limit +1)*sizeof(int));
2981      }
2982    }
2983    if (r->OrdSize!=0 && r->typ != NULL)
2984    {
2985      omFreeSize((ADDRESS)r->typ,r->OrdSize*sizeof(sro_ord));
2986    }
2987    if (r->ordsgn != NULL && r->CmpL_Size != 0)
2988      omFreeSize((ADDRESS)r->ordsgn,r->ExpL_Size*sizeof(long));
2989    if (r->p_Procs != NULL)
2990      omFreeSize(r->p_Procs, sizeof(p_Procs_s));
2991    omfreeSize(r->VarL_Offset, r->VarL_Size*sizeof(int));
2992  }
2993  if (r->NegWeightL_Offset!=NULL)
2994  {
2995    omFreeSize(r->NegWeightL_Offset, r->NegWeightL_Size*sizeof(int));
2996    r->NegWeightL_Offset=NULL;
2997  }
2998}
2999
3000// set r->VarL_Size, r->VarL_Offset, r->VarL_LowIndex
3001static void rSetVarL(ring r)
3002{
3003  int  min = INT_MAX, min_j = -1;
3004  int* VarL_Number = (int*) omAlloc0(r->ExpL_Size*sizeof(int));
3005
3006  int i,j;
3007
3008  // count how often a var long is occupied by an exponent
3009  for (i=1; i<=r->N; i++)
3010  {
3011    VarL_Number[r->VarOffset[i] & 0xffffff]++;
3012  }
3013
3014  // determine how many and min
3015  for (i=0, j=0; i<r->ExpL_Size; i++)
3016  {
3017    if (VarL_Number[i] != 0)
3018    {
3019      if (min > VarL_Number[i])
3020      {
3021        min = VarL_Number[i];
3022        min_j = j;
3023      }
3024      j++;
3025    }
3026  }
3027
3028  r->VarL_Size = j;
3029  r->VarL_Offset = (int*) omAlloc(r->VarL_Size*sizeof(int));
3030  r->VarL_LowIndex = 0;
3031
3032  // set VarL_Offset
3033  for (i=0, j=0; i<r->ExpL_Size; i++)
3034  {
3035    if (VarL_Number[i] != 0)
3036    {
3037      r->VarL_Offset[j] = i;
3038      if (j > 0 && r->VarL_Offset[j-1] != r->VarL_Offset[j] - 1)
3039        r->VarL_LowIndex = -1;
3040      j++;
3041    }
3042  }
3043  if (r->VarL_LowIndex >= 0)
3044    r->VarL_LowIndex = r->VarL_Offset[0];
3045
3046  r->MinExpPerLong = min;
3047  if (min_j != 0)
3048  {
3049    j = r->VarL_Offset[min_j];
3050    r->VarL_Offset[min_j] = r->VarL_Offset[0];
3051    r->VarL_Offset[0] = j;
3052  }
3053  omFree(VarL_Number);
3054}
3055
3056static void rRightAdjustVarOffset(ring r)
3057{
3058  int* shifts = (int*) omAlloc(r->ExpL_Size*sizeof(int));
3059  int i;
3060  // initialize shifts
3061  for (i=0;i<r->ExpL_Size;i++)
3062    shifts[i] = BIT_SIZEOF_LONG;
3063
3064  // find minimal bit in each long var
3065  for (i=1;i<=r->N;i++)
3066  {
3067    if (shifts[r->VarOffset[i] & 0xffffff] > r->VarOffset[i] >> 24)
3068      shifts[r->VarOffset[i] & 0xffffff] = r->VarOffset[i] >> 24;
3069  }
3070  // reset r->VarOffset
3071  for (i=1;i<=r->N;i++)
3072  {
3073    if (shifts[r->VarOffset[i] & 0xffffff] != 0)
3074      r->VarOffset[i]
3075        = (r->VarOffset[i] & 0xffffff) |
3076        (((r->VarOffset[i] >> 24) - shifts[r->VarOffset[i] & 0xffffff]) << 24);
3077  }
3078  omFree(shifts);
3079}
3080
3081// get r->divmask depending on bits per exponent
3082static unsigned long rGetDivMask(int bits)
3083{
3084  unsigned long divmask = 1;
3085  int i = bits;
3086
3087  while (i < BIT_SIZEOF_LONG)
3088  {
3089    divmask |= (((unsigned long) 1) << (unsigned long) i);
3090    i += bits;
3091  }
3092  return divmask;
3093}
3094
3095#ifdef RDEBUG
3096void rDebugPrint(ring r)
3097{
3098  if (r==NULL)
3099  {
3100    PrintS("NULL ?\n");
3101    return;
3102  }
3103  char *TYP[]={"ro_dp","ro_wp","ro_wp_neg","ro_cp",
3104               "ro_syzcomp", "ro_syz", "ro_none"};
3105  int i,j;
3106
3107  Print("ExpL_Size:%d ",r->ExpL_Size);
3108  Print("CmpL_Size:%d ",r->CmpL_Size);
3109  Print("VarL_Size:%d\n",r->VarL_Size);
3110  Print("bitmask=0x%x (expbound=%d) \n",r->bitmask, r->bitmask);
3111  Print("BitsPerExp=%d ExpPerLong=%d MinExpPerLong=%d at L[%d]\n", r->BitsPerExp, r->ExpPerLong, r->MinExpPerLong, r->VarL_Offset[0]);
3112  PrintS("varoffset:\n");
3113  if (r->VarOffset==NULL) PrintS(" NULL\n");
3114  else
3115    for(j=0;j<=r->N;j++)
3116      Print("  v%d at e-pos %d, bit %d\n",
3117            j,r->VarOffset[j] & 0xffffff, r->VarOffset[j] >>24);
3118  Print("divmask=%p\n", r->divmask);
3119  PrintS("ordsgn:\n");
3120  for(j=0;j<r->CmpL_Size;j++)
3121    Print("  ordsgn %d at pos %d\n",r->ordsgn[j],j);
3122  Print("OrdSgn:%d\n",r->OrdSgn);
3123  PrintS("ordrec:\n");
3124  for(j=0;j<r->OrdSize;j++)
3125  {
3126    Print("  typ %s",TYP[r->typ[j].ord_typ]);
3127    Print("  place %d",r->typ[j].data.dp.place);
3128    if (r->typ[j].ord_typ!=ro_syzcomp)
3129    {
3130      Print("  start %d",r->typ[j].data.dp.start);
3131      Print("  end %d",r->typ[j].data.dp.end);
3132      if ((r->typ[j].ord_typ==ro_wp)
3133      || (r->typ[j].ord_typ==ro_wp_neg))
3134      {
3135        Print(" w:");
3136        int l;
3137        for(l=r->typ[j].data.wp.start;l<=r->typ[j].data.wp.end;l++)
3138          Print(" %d",r->typ[j].data.wp.weights[l-r->typ[j].data.wp.start]);
3139      }
3140    }
3141    PrintLn();
3142  }
3143  Print("pOrdIndex:%d pCompIndex:%d\n", r->pOrdIndex, r->pCompIndex);
3144  Print("OrdSize:%d\n",r->OrdSize);
3145  PrintS("--------------------\n");
3146  for(j=0;j<r->ExpL_Size;j++)
3147  {
3148    Print("L[%d]: ",j);
3149    if (j< r->CmpL_Size)
3150      Print("ordsgn %d ", r->ordsgn[j]);
3151    else
3152      PrintS("no comp ");
3153    i=1;
3154    for(;i<=r->N;i++)
3155    {
3156      if( (r->VarOffset[i] & 0xffffff) == j )
3157      {  Print("v%d at e[%d], bit %d; ", i,r->VarOffset[i] & 0xffffff,
3158                                         r->VarOffset[i] >>24 ); }
3159    }
3160    if( r->pCompIndex==j ) PrintS("v0; ");
3161    for(i=0;i<r->OrdSize;i++)
3162    {
3163      if (r->typ[i].data.dp.place == j)
3164      {
3165        Print("ordrec:%s (start:%d, end:%d) ",TYP[r->typ[i].ord_typ],
3166          r->typ[i].data.dp.start, r->typ[i].data.dp.end);
3167      }
3168    }
3169
3170    if (j==r->pOrdIndex)
3171      PrintS("pOrdIndex\n");
3172    else
3173      PrintLn();
3174  }
3175
3176  // p_Procs stuff
3177  p_Procs_s proc_names;
3178  char* field;
3179  char* length;
3180  char* ord;
3181  p_Debug_GetProcNames(r, &proc_names);
3182  p_Debug_GetSpecNames(r, field, length, ord);
3183
3184  Print("p_Spec  : %s, %s, %s\n", field, length, ord);
3185  PrintS("p_Procs :\n");
3186  for (i=0; i<(int) (sizeof(p_Procs_s)/sizeof(void*)); i++)
3187  {
3188    Print(" %s,\n", ((char**) &proc_names)[i]);
3189  }
3190}
3191
3192void pDebugPrintR(poly p, ring r)
3193{
3194  int i,j;
3195  pWrite(p);
3196  j=2;
3197  while(p!=NULL)
3198  {
3199    Print("\nexp[0..%d]\n",r->ExpL_Size-1);
3200    for(i=0;i<r->ExpL_Size;i++)
3201      Print("%d ",p->exp[i]);
3202    PrintLn();
3203    Print("v0:%d ",p_GetComp(p, r));
3204    for(i=1;i<=r->N;i++) Print(" v%d:%d",i,p_GetExp(p,i, r));
3205    PrintLn();
3206    pIter(p);
3207    j--;
3208    if (j==0) { PrintS("...\n"); break; }
3209  }
3210}
3211
3212void pDebugPrint(poly p)
3213{
3214  pDebugPrintR(p, currRing);
3215}
3216#endif // RDEBUG
3217
3218
3219/*2
3220* asssume that rComplete was called with r
3221* assume that the first block ist ringorder_S
3222* change the block to reflect the sequence given by appending v
3223*/
3224
3225#ifdef PDEBUG
3226void rDBChangeSComps(int* currComponents,
3227                     long* currShiftedComponents,
3228                     int length,
3229                     ring r)
3230{
3231  r->typ[1].data.syzcomp.length = length;
3232  rNChangeSComps( currComponents, currShiftedComponents, r);
3233}
3234void rDBGetSComps(int** currComponents,
3235                 long** currShiftedComponents,
3236                 int *length,
3237                 ring r)
3238{
3239  *length = r->typ[1].data.syzcomp.length;
3240  rNGetSComps( currComponents, currShiftedComponents, r);
3241}
3242#endif
3243
3244void rNChangeSComps(int* currComponents, long* currShiftedComponents, ring r)
3245{
3246  assume(r->order[1]==ringorder_S);
3247
3248  r->typ[1].data.syzcomp.ShiftedComponents = currShiftedComponents;
3249  r->typ[1].data.syzcomp.Components = currComponents;
3250}
3251
3252void rNGetSComps(int** currComponents, long** currShiftedComponents, ring r)
3253{
3254  assume(r->order[1]==ringorder_S);
3255
3256  *currShiftedComponents = r->typ[1].data.syzcomp.ShiftedComponents;
3257  *currComponents =   r->typ[1].data.syzcomp.Components;
3258}
3259
3260/////////////////////////////////////////////////////////////////////////////
3261//
3262// The following routines all take as input a ring r, and return R
3263// where R has a certain property. P might be equal r in which case r
3264// had already this property
3265//
3266// Without argument, these functions work on currRing and change it,
3267// if necessary
3268
3269// for the time being, this is still here
3270static ring rAssure_SyzComp(ring r, BOOLEAN complete = TRUE);
3271
3272ring rCurrRingAssure_SyzComp()
3273{
3274  ring r = rAssure_SyzComp(currRing);
3275  if (r != currRing)
3276  {
3277    ring old_ring = currRing;
3278    rChangeCurrRing(r);
3279    if (old_ring->qideal != NULL)
3280    {
3281      r->qideal = idrCopyR_NoSort(old_ring->qideal, old_ring);
3282      assume(idRankFreeModule(r->qideal) == 0);
3283      currQuotient = r->qideal;
3284    }
3285  }
3286  return r;
3287}
3288
3289static ring rAssure_SyzComp(ring r, BOOLEAN complete)
3290{
3291  if (r->order[0] == ringorder_s) return r;
3292  ring res=rCopy0(r, FALSE, FALSE);
3293  int i=rBlocks(r);
3294  int j;
3295
3296  res->order=(int *)omAlloc0((i+1)*sizeof(int));
3297  for(j=i;j>0;j--) res->order[j]=r->order[j-1];
3298  res->order[0]=ringorder_s;
3299
3300  res->block0=(int *)omAlloc0((i+1)*sizeof(int));
3301  for(j=i;j>0;j--) res->block0[j]=r->block0[j-1];
3302
3303  res->block1=(int *)omAlloc0((i+1)*sizeof(int));
3304  for(j=i;j>0;j--) res->block1[j]=r->block1[j-1];
3305
3306  int ** wvhdl =(int **)omAlloc0((i+1)*sizeof(int**));
3307  for(j=i;j>0;j--)
3308  {
3309    if (r->wvhdl[j-1] != NULL)
3310    {
3311      wvhdl[j] = (int*) omMemDup(r->wvhdl[j-1]);
3312    }
3313  }
3314  res->wvhdl = wvhdl;
3315
3316  if (complete) rComplete(res, 1);
3317  return res;
3318}
3319
3320static ring rAssure_CompLastBlock(ring r, BOOLEAN complete = TRUE)
3321{
3322  int last_block = rBlocks(r) - 2;
3323  if (r->order[last_block] != ringorder_c &&
3324      r->order[last_block] != ringorder_C)
3325  {
3326    int c_pos = 0;
3327    int i;
3328
3329    for (i=0; i< last_block; i++)
3330    {
3331      if (r->order[i] == ringorder_c || r->order[i] == ringorder_C)
3332      {
3333        c_pos = i;
3334        break;
3335      }
3336    }
3337    if (c_pos != -1)
3338    {
3339      ring new_r = rCopy0(r, FALSE, TRUE);
3340      for (i=c_pos+1; i<=last_block; i++)
3341      {
3342        new_r->order[i-1] = new_r->order[i];
3343        new_r->block0[i-1] = new_r->block0[i];
3344        new_r->block1[i-1] = new_r->block1[i];
3345        new_r->wvhdl[i-1] = new_r->wvhdl[i];
3346      }
3347      new_r->order[last_block] = r->order[c_pos];
3348      new_r->block0[last_block] = r->block0[c_pos];
3349      new_r->block1[last_block] = r->block1[c_pos];
3350      new_r->wvhdl[last_block] = r->wvhdl[c_pos];
3351      if (complete) rComplete(new_r, 1);
3352      return new_r;
3353    }
3354  }
3355  return r;
3356}
3357
3358ring rCurrRingAssure_CompLastBlock()
3359{
3360  ring new_r = rAssure_CompLastBlock(currRing);
3361  if (currRing != new_r)
3362  {
3363    ring old_r = currRing;
3364    rChangeCurrRing(new_r);
3365    if (old_r->qideal != NULL)
3366    {
3367      new_r->qideal = idrCopyR(old_r->qideal, old_r);
3368      currQuotient = new_r->qideal;
3369    }
3370  }
3371  return new_r;
3372}
3373
3374ring rCurrRingAssure_SyzComp_CompLastBlock()
3375{
3376  ring new_r_1 = rAssure_CompLastBlock(currRing, FALSE);
3377  ring new_r = rAssure_SyzComp(new_r_1, FALSE);
3378
3379  if (new_r != currRing)
3380  {
3381    ring old_r = currRing;
3382    if (new_r_1 != new_r && new_r_1 != old_r) rDelete(new_r_1);
3383    rComplete(new_r, 1);
3384    rChangeCurrRing(new_r);
3385    if (old_r->qideal != NULL)
3386    {
3387      new_r->qideal = idrCopyR(old_r->qideal, old_r);
3388      currQuotient = new_r->qideal;
3389    }
3390    rTest(new_r);
3391    rTest(old_r);
3392  }
3393  return new_r;
3394}
3395
3396// use this for global orderings consisting of two blocks
3397static ring rCurrRingAssure_Global(rRingOrder_t b1, rRingOrder_t b2)
3398{
3399  int r_blocks = rBlocks(currRing);
3400  int i;
3401
3402  assume(b1 == ringorder_c || b1 == ringorder_C ||
3403         b2 == ringorder_c || b2 == ringorder_C ||
3404         b2 == ringorder_S);
3405  if ((r_blocks == 3) &&
3406      (currRing->order[0] == b1) &&
3407      (currRing->order[1] == b2) &&
3408      (currRing->order[2] == 0))
3409    return currRing;
3410  ring res = rCopy0(currRing, TRUE, FALSE);
3411  res->order = (int*)omAlloc0(3*sizeof(int));
3412  res->block0 = (int*)omAlloc0(3*sizeof(int));
3413  res->block1 = (int*)omAlloc0(3*sizeof(int));
3414  res->wvhdl = (int**)omAlloc0(3*sizeof(int*));
3415  res->order[0] = b1;
3416  res->order[1] = b2;
3417  if (b1 == ringorder_c || b1 == ringorder_C)
3418  {
3419    res->block0[1] = 1;
3420    res->block1[1] = currRing->N;
3421  }
3422  else
3423  {
3424    res->block0[0] = 1;
3425    res->block1[0] = currRing->N;
3426  }
3427  // HANNES: This sould be set in rComplete
3428  res->OrdSgn = 1;
3429  rComplete(res, 1);
3430  rChangeCurrRing(res);
3431  return res;
3432}
3433
3434
3435ring rCurrRingAssure_dp_S()
3436{
3437  return rCurrRingAssure_Global(ringorder_dp, ringorder_S);
3438}
3439
3440ring rCurrRingAssure_dp_C()
3441{
3442  return rCurrRingAssure_Global(ringorder_dp, ringorder_C);
3443}
3444
3445ring rCurrRingAssure_C_dp()
3446{
3447  return rCurrRingAssure_Global(ringorder_C, ringorder_dp);
3448}
3449
3450
3451void rSetSyzComp(int k)
3452{
3453  if (TEST_OPT_PROT) Print("{%d}", k);
3454  if ((currRing->typ!=NULL) && (currRing->typ[0].ord_typ==ro_syz))
3455  {
3456    assume(k > currRing->typ[0].data.syz.limit);
3457    int i;
3458    if (currRing->typ[0].data.syz.limit == 0)
3459    {
3460      currRing->typ[0].data.syz.syz_index = (int*) omAlloc0((k+1)*sizeof(int));
3461      currRing->typ[0].data.syz.syz_index[0] = 0;
3462      currRing->typ[0].data.syz.curr_index = 1;
3463    }
3464    else
3465    {
3466      currRing->typ[0].data.syz.syz_index = (int*)
3467        omReallocSize(currRing->typ[0].data.syz.syz_index,
3468                (currRing->typ[0].data.syz.limit+1)*sizeof(int),
3469                (k+1)*sizeof(int));
3470    }
3471    for (i=currRing->typ[0].data.syz.limit + 1; i<= k; i++)
3472    {
3473      currRing->typ[0].data.syz.syz_index[i] =
3474        currRing->typ[0].data.syz.curr_index;
3475    }
3476    currRing->typ[0].data.syz.limit = k;
3477    currRing->typ[0].data.syz.curr_index++;
3478  }
3479  else if ((currRing->order[0]!=ringorder_c) && (k!=0))
3480  {
3481    dReportError("syzcomp in incompatible ring");
3482  }
3483#ifdef PDEBUG
3484  extern int pDBsyzComp;
3485  pDBsyzComp=k;
3486#endif
3487}
3488
3489// return the max-comonent wchich has syzIndex i
3490int rGetMaxSyzComp(int i)
3491{
3492  if ((currRing->typ!=NULL) && (currRing->typ[0].ord_typ==ro_syz) &&
3493      currRing->typ[0].data.syz.limit > 0 && i > 0)
3494  {
3495    assume(i <= currRing->typ[0].data.syz.limit);
3496    int j;
3497    for (j=0; j<currRing->typ[0].data.syz.limit; j++)
3498    {
3499      if (currRing->typ[0].data.syz.syz_index[j] == i  &&
3500          currRing->typ[0].data.syz.syz_index[j+1] != i)
3501      {
3502        assume(currRing->typ[0].data.syz.syz_index[j+1] == i+1);
3503        return j;
3504      }
3505    }
3506    return currRing->typ[0].data.syz.limit;
3507  }
3508  else
3509  {
3510    return 0;
3511  }
3512}
3513
3514BOOLEAN rRing_is_Homog(ring r)
3515{
3516  if (r == NULL) return FALSE;
3517  int i, j, nb = rBlocks(r);
3518  for (i=0; i<nb; i++)
3519  {
3520    if (r->wvhdl[i] != NULL)
3521    {
3522      int length = r->block1[i] - r->block0[i];
3523      int* wvhdl = r->wvhdl[i];
3524      if (r->order[i] == ringorder_M) length *= length;
3525      assume(omSizeOfAddr(wvhdl) >= length*sizeof(int));
3526
3527      for (j=0; j< length; j++)
3528      {
3529        if (wvhdl[j] != 0 && wvhdl[j] != 1) return FALSE;
3530      }
3531    }
3532  }
3533  return TRUE;
3534}
3535
3536BOOLEAN rRing_has_CompLastBlock(ring r)
3537{
3538  assume(r != NULL);
3539  int lb = rBlocks(r) - 2;
3540  return (r->order[lb] == ringorder_c || r->order[lb] == ringorder_C);
3541}
3542
3543n_coeffType rFieldType(ring r)
3544{
3545  if (rField_is_Zp(r))     return n_Zp;
3546  if (rField_is_Q(r))      return n_Q;
3547  if (rField_is_R(r))      return n_R;
3548  if (rField_is_GF(r))     return n_GF;
3549  if (rField_is_long_R(r)) return n_long_R;
3550  if (rField_is_Zp_a(r))   return n_Zp_a;
3551  if (rField_is_Q_a(r))    return n_Q_a;
3552  if (rField_is_long_C(r)) return n_long_C;
3553  return n_unknown;
3554}
3555
3556int * rGetWeightVec(ring r)
3557{
3558  assume(r!=NULL);
3559  assume(r->OrdSize>0);
3560  assume(r->typ[0].ord_typ==ro_wp);
3561  return (r->typ[0].data.wp.weights);
3562}
3563
3564void rSetWeightVec(ring r, int *wv)
3565{
3566  assume(r!=NULL);
3567  assume(r->OrdSize>0);
3568  assume(r->typ[0].ord_typ==ro_wp);
3569  memcpy(r->typ[0].data.wp.weights,wv,r->N*sizeof(int));
3570}
3571
3572#include <ctype.h>
3573
3574static int rRealloc1(ring r, ring src, int size, int pos)
3575{
3576  r->order=(int*)omReallocSize(r->order, size*sizeof(int), (size+1)*sizeof(int));
3577  r->block0=(int*)omReallocSize(r->block0, size*sizeof(int), (size+1)*sizeof(int));
3578  r->block1=(int*)omReallocSize(r->block1, size*sizeof(int), (size+1)*sizeof(int));
3579  r->wvhdl=(int_ptr*)omReallocSize(r->wvhdl,size*sizeof(int_ptr), (size+1)*sizeof(int_ptr));
3580  for(int k=size; k>pos; k--) r->wvhdl[k]=r->wvhdl[k-1];
3581  r->order[size]=0;
3582  size++;
3583  return size;
3584}
3585static int rReallocM1(ring r, ring src, int size, int pos)
3586{
3587  r->order=(int*)omReallocSize(r->order, size*sizeof(int), (size-1)*sizeof(int));
3588  r->block0=(int*)omReallocSize(r->block0, size*sizeof(int), (size-1)*sizeof(int));
3589  r->block1=(int*)omReallocSize(r->block1, size*sizeof(int), (size-1)*sizeof(int));
3590  r->wvhdl=(int_ptr*)omReallocSize(r->wvhdl,size*sizeof(int_ptr), (size-1)*sizeof(int_ptr));
3591  for(int k=pos+1; k<size; k++) r->wvhdl[k]=r->wvhdl[k+1];
3592  size--;
3593  return size;
3594}
3595static void rOppWeight(int *w, int l)
3596{
3597  int i2=(l+1)/2;
3598  for(int j=0; j<=i2; j++)
3599  {
3600    int t=w[j];
3601    w[j]=w[l-j]; 
3602    w[l-j]=t; 
3603  }
3604}
3605
3606#define rOppVar(R,I) (rVar(R)+1-I)
3607
3608ring rOpposite(ring src)
3609  /* creates an opposite algebra of R */
3610  /* that is R^opp, where f (*^opp) g = g*f  */
3611  /* treats the case of qring */
3612{
3613  if (src == NULL) return(NULL);
3614  ring save = currRing;
3615  rChangeCurrRing(src);
3616  ring r = rCopy0(src,TRUE); /* TRUE for copy the qideal */
3617  /*  rChangeCurrRing(r); */
3618  // change vars v1..vN -> vN..v1
3619  int i;
3620  int i2 = (rVar(r)-1)/2;
3621  for(i=i2; i>=0; i--)
3622  {
3623    // index: 0..N-1
3624    //Print("ex var names: %d <-> %d\n",i,rOppVar(r,i));
3625    // exchange names
3626    char *p;
3627    p = r->names[rVar(r)-1-i];
3628    r->names[rVar(r)-1-i] = r->names[i];
3629    r->names[i] = p;
3630  }
3631//  i2=(rVar(r)+1)/2;
3632//  for(int i=i2; i>0; i--)
3633//  {
3634//    // index: 1..N
3635//    //Print("ex var places: %d <-> %d\n",i,rVar(r)+1-i);
3636//    // exchange VarOffset
3637//    int t;
3638//    t=r->VarOffset[i];
3639//    r->VarOffset[i]=r->VarOffset[rOppVar(r,i)];
3640//    r->VarOffset[rOppVar(r,i)]=t;
3641//  }
3642  // change names:
3643  for (i=rVar(r)-1; i>=0; i--)
3644  {
3645    char *p=r->names[i];
3646    if(isupper(*p)) *p = tolower(*p);
3647    else            *p = toupper(*p);
3648  }
3649  // change ordering: listing
3650  // change ordering: compare
3651//  for(i=0; i<r->OrdSize; i++)
3652//  {
3653//    int t,tt;
3654//    switch(r->typ[i].ord_typ)
3655//    {
3656//      case ro_dp:
3657//      //
3658//        t=r->typ[i].data.dp.start;
3659//        r->typ[i].data.dp.start=rOppVar(r,r->typ[i].data.dp.end);
3660//        r->typ[i].data.dp.end=rOppVar(r,t);
3661//        break;
3662//      case ro_wp:
3663//      case ro_wp_neg:
3664//      {
3665//        t=r->typ[i].data.wp.start;
3666//        r->typ[i].data.wp.start=rOppVar(r,r->typ[i].data.wp.end);
3667//        r->typ[i].data.wp.end=rOppVar(r,t);
3668//        // invert r->typ[i].data.wp.weights
3669//        rOppWeight(r->typ[i].data.wp.weights,
3670//                   r->typ[i].data.wp.end-r->typ[i].data.wp.start);
3671//        break;
3672//      }
3673//      //case ro_wp64:
3674//      case ro_syzcomp:
3675//      case ro_syz:
3676//         WerrorS("not implemented in rOpposite");
3677//         // should not happen
3678//         break;
3679//
3680//      case ro_cp:
3681//        t=r->typ[i].data.cp.start;
3682//        r->typ[i].data.cp.start=rOppVar(r,r->typ[i].data.cp.end);
3683//        r->typ[i].data.cp.end=rOppVar(r,t);
3684//        break;
3685//      case ro_none:
3686//      default:
3687//       Werror("unknown type in rOpposite(%d)",r->typ[i].ord_typ);
3688//       break;
3689//    }
3690//  }
3691  // Change order/block structures (needed for rPrint, rAdd etc.)
3692  int j=0;
3693  int l=rBlocks(src);
3694  for(i=0; src->order[i]!=0; i++)
3695  {
3696    switch (src->order[i])
3697    {
3698      case ringorder_c: /* c-> c */
3699      case ringorder_C: /* C-> C */
3700      case ringorder_no /*=0*/: /* end-of-block */
3701        r->order[j]=src->order[i];
3702        j++; break;
3703      case ringorder_lp: /* lp -> rp */
3704        r->order[j]=ringorder_rp;
3705        r->block0[j]=rOppVar(r, src->block1[i]);
3706        r->block1[j]=rOppVar(r, src->block0[i]);
3707        break;
3708      case ringorder_rp: /* rp -> lp */
3709        r->order[j]=ringorder_lp;
3710        r->block0[j]=rOppVar(r, src->block1[i]);
3711        r->block1[j]=rOppVar(r, src->block0[i]);
3712        break;
3713      case ringorder_dp: /* dp -> a(1..1),ls */
3714      { 
3715        l=rRealloc1(r,src,l,j);
3716        r->order[j]=ringorder_a;
3717        r->block0[j]=rOppVar(r, src->block1[i]);
3718        r->block1[j]=rOppVar(r, src->block0[i]);
3719        r->wvhdl[j]=(int*)omAlloc((r->block1[j]-r->block0[j]+1)*sizeof(int));
3720        for(int k=r->block0[j]; k<=r->block1[j]; k++)
3721          r->wvhdl[j][k-r->block0[j]]=1;
3722        j++;
3723        r->order[j]=ringorder_ls;
3724        r->block0[j]=rOppVar(r, src->block1[i]);
3725        r->block1[j]=rOppVar(r, src->block0[i]);
3726        j++;
3727        break;
3728      } 
3729      case ringorder_Dp: /* Dp -> a(1..1),rp */
3730      { 
3731        l=rRealloc1(r,src,l,j);
3732        r->order[j]=ringorder_a;
3733        r->block0[j]=rOppVar(r, src->block1[i]);
3734        r->block1[j]=rOppVar(r, src->block0[i]);
3735        r->wvhdl[j]=(int*)omAlloc((r->block1[j]-r->block0[j]+1)*sizeof(int));
3736        for(int k=r->block0[j]; k<=r->block1[j]; k++)
3737          r->wvhdl[j][k-r->block0[j]]=1;
3738        j++;
3739        r->order[j]=ringorder_rp;
3740        r->block0[j]=rOppVar(r, src->block1[i]);
3741        r->block1[j]=rOppVar(r, src->block0[i]);
3742        j++;
3743        break;
3744      } 
3745      case ringorder_wp: /* wp -> a(...),ls */
3746      { 
3747        l=rRealloc1(r,src,l,j);
3748        r->order[j]=ringorder_a;
3749        r->block0[j]=rOppVar(r, src->block1[i]);
3750        r->block1[j]=rOppVar(r, src->block0[i]);
3751        r->wvhdl[j]=r->wvhdl[j+1]; r->wvhdl[j+1]=r->wvhdl[j+1]=NULL;
3752        rOppWeight(r->wvhdl[j], r->block1[j]-r->block0[j]);
3753        j++;
3754        r->order[j]=ringorder_ls;
3755        r->block0[j]=rOppVar(r, src->block1[i]);
3756        r->block1[j]=rOppVar(r, src->block0[i]);
3757        j++;
3758        break;
3759      } 
3760      case ringorder_Wp: /* Wp -> a(...),rp */
3761      { 
3762        l=rRealloc1(r,src,l,j);
3763        r->order[j]=ringorder_a;
3764        r->block0[j]=rOppVar(r, src->block1[i]);
3765        r->block1[j]=rOppVar(r, src->block0[i]);
3766        r->wvhdl[j]=r->wvhdl[j+1]; r->wvhdl[j+1]=r->wvhdl[j+1]=NULL;
3767        rOppWeight(r->wvhdl[j], r->block1[j]-r->block0[j]);
3768        j++;
3769        r->order[j]=ringorder_rp;
3770        r->block0[j]=rOppVar(r, src->block1[i]);
3771        r->block1[j]=rOppVar(r, src->block0[i]);
3772        j++;
3773        break;
3774      } 
3775      case ringorder_M: /* M -> M */
3776      { 
3777        r->order[j]=ringorder_M;
3778        r->block0[j]=rOppVar(r, src->block1[i]);
3779        r->block1[j]=rOppVar(r, src->block0[i]);
3780        int n=r->block1[j]-r->block0[j];
3781        /* M is a (n+1)x(n+1) matrix */
3782        for (int nn=0; nn<=n; nn++)
3783        {
3784          rOppWeight(&(r->wvhdl[j][nn*(n+1)]), n /*r->block1[j]-r->block0[j]*/);
3785        }
3786        j++;
3787        break;
3788      }
3789      case ringorder_a: /*  a(...),ls -> wp/dp */
3790      { 
3791        r->block0[j]=rOppVar(r, src->block1[i]);
3792        r->block1[j]=rOppVar(r, src->block0[i]);
3793        rOppWeight(r->wvhdl[j], r->block1[j]-r->block0[j]);
3794        if (src->order[i+1]==ringorder_ls)
3795        {
3796          r->order[j]=ringorder_wp;
3797          i++;
3798          //l=rReallocM1(r,src,l,j);
3799        }
3800        else
3801        {
3802          r->order[j]=ringorder_a;
3803        }
3804        j++;
3805        break;
3806      }
3807      // not yet done:
3808      case ringorder_ls:
3809      case ringorder_ds:
3810      case ringorder_Ds:
3811      case ringorder_ws:
3812      case ringorder_Ws:
3813      // should not occur:
3814      case ringorder_S:
3815      case ringorder_s:
3816      case ringorder_aa:
3817      case ringorder_L:
3818      case ringorder_unspec:
3819        Werror("order %s not (yet) supported", rSimpleOrdStr(src->order[i]));
3820        break;
3821    }
3822  } 
3823  rComplete(r);
3824#ifdef RDEBUG
3825  //   rDebugPrint(r);
3826#endif
3827  rTest(r);
3828#ifdef HAVE_PLURAL
3829  /* now, we initialize a non-comm structure on r */
3830  if (!rIsPluralRing(src))
3831  {
3832    return r;
3833  }
3834  {
3835  rChangeCurrRing(r);  /* we were not in r */
3836  /* basic nc constructions  */
3837  r->nc           = (nc_struct *)omAlloc0(sizeof(nc_struct));
3838  r->nc->ref      = 1; /* in spite of rCopy(src)? */
3839  r->nc->basering = r;
3840  r->nc->type     =  src->nc->type;
3841  int *perm       = (int *)omAlloc0((rVar(r)+1)*sizeof(int));
3842  int *par_perm   = NULL;
3843  nMapFunc nMap   = nSetMap(src);
3844  int j;
3845  int ni,nj;
3846  for(i=1; i<=r->N; i++)
3847  {
3848    perm[i] = rOppVar(r,i);
3849  }
3850  matrix C = mpNew(rVar(r),rVar(r));
3851  matrix D = mpNew(rVar(r),rVar(r));
3852  for (i=1; i< rVar(r); i++)
3853  {
3854    for (j=i+1; j<=rVar(r); j++)
3855    {
3856      ni = r->N +1 - i;
3857      nj = r->N +1 - j; /* i<j ==>   nj < ni */
3858      MATELEM(C,nj,ni) = pPermPoly(MATELEM(src->nc->C,i,j),perm,src,nMap,par_perm,src->P);
3859      MATELEM(D,nj,ni) = pPermPoly(MATELEM(src->nc->D,i,j),perm,src,nMap,par_perm,src->P);
3860    }
3861  }
3862  idTest((ideal)C);
3863  idTest((ideal)D);
3864  r->nc->C = C;
3865  r->nc->D = D;
3866  if (nc_InitMultiplication(r))
3867    WarnS("Error initializing multiplication!");
3868  r->nc->IsSkewConstant =   src->nc->IsSkewConstant;
3869  omFreeSize((ADDRESS)perm,(rVar(r)+1)*sizeof(int));
3870  /* now oppose the qideal for qrings */
3871  if (src->qideal != NULL)
3872  {
3873    idDelete(&(r->qideal));
3874    r->qideal = idOppose(src, src->qideal);
3875  }
3876  rTest(r);
3877  rChangeCurrRing(save);
3878  }
3879#endif
3880  return r;
3881}
3882
3883ring rEnvelope(ring R)
3884  /* creates an enveloping algebra of R */
3885  /* that is R^e = R \tensor_K R^opp */
3886{
3887  ring Ropp = rOpposite(R);
3888  ring Renv = NULL;
3889  int stat = rSum(R, Ropp, Renv); /* takes care of qideals */
3890  if ( stat <=0 )
3891    WarnS("Error in rEnvelope at rSum");
3892  /* now create the qideal for qrings */
3893//    if (R->qideal != NULL)
3894//    {
3895//      ring save = currRing;
3896//      rChangeCurrRing(Ropp);
3897//      ideal Q = idCopy(R->qideal);
3898//      ideal Qop = idOppose(R,Q);
3899//      rChangeCurrRing(Renv);
3900//      ideal Qenv = idInit(Q->ncols+Qop->ncols,1);
3901//      int i;
3902//      for (i=0; i< Q->ncols; i++)
3903//      {
3904//        Qenv->m[i] = maIMap(R,Q->m[i]);
3905//      }
3906//      for (i=0; i<= Qop->ncols; i++)
3907//      {
3908//        Qenv->m[Q->ncols+i] = maIMap(Ropp,Qop->m[i]);
3909//      }
3910//      /* should we run twostd on the result? */
3911//      Renv->qideal = Qenv;
3912//      rChangeCurrRing(save);
3913//    }
3914  rTest(Renv);
3915  return Renv;
3916}
3917
3918BOOLEAN nc_rComplete(ring src, ring dest)
3919/* returns TRUE is there were errors */
3920/* dest is actualy equals src with the different ordering */
3921/* we map src->nc correctly to dest->src */
3922/* to be executed after rComplete, before rChangeCurrRing */
3923
3924{
3925  if (!rIsPluralRing(src))
3926    return FALSE;
3927  int i,j;
3928  int N = dest->N;
3929  if (src->N != N)
3930  {
3931    /* should not happen */
3932    WarnS("wrong nc_rComplete call");
3933    return TRUE;
3934  }
3935  ring save = currRing;
3936  int WeChangeRing = 0;
3937  if (dest != currRing)
3938  {
3939    WeChangeRing = 1;
3940    rChangeCurrRing(dest);
3941  }
3942  matrix C = mpNew(N,N);
3943  matrix D = mpNew(N,N);
3944  matrix C0 = src->nc->C;
3945  matrix D0 = src->nc->D;
3946  poly p = NULL;
3947  number n = NULL;
3948  for (i=1; i< N; i++)
3949  {
3950    for (j= i+1; j<= N; j++)
3951    {
3952      n = n_Copy(p_GetCoeff(MATELEM(C0,i,j), src),src);
3953      p = p_ISet(1,dest);
3954      p_SetCoeff(p,n,dest);
3955      MATELEM(C,i,j) = p;
3956      p = NULL;
3957      if (MATELEM(D0,i,j) != NULL)
3958      {
3959        p = prCopyR(MATELEM(D0,i,j), src->nc->basering, dest);
3960        MATELEM(D,i,j) = nc_p_CopyPut(p, dest);
3961        p_Delete(&p, dest);
3962        p = NULL;
3963      }
3964    }
3965  }
3966  /* One must test C and D _only_ in r->nc->basering!!! not in r!!! */
3967  //  idTest((ideal)C);
3968  //  idTest((ideal)D);
3969  id_Delete((ideal *)&(dest->nc->C),dest->nc->basering);
3970  id_Delete((ideal *)&(dest->nc->D),dest->nc->basering);
3971  dest->nc->C = C;
3972  dest->nc->D = D;
3973  if ( WeChangeRing )
3974    rChangeCurrRing(save);
3975  if (nc_InitMultiplication(dest))
3976  {
3977    WarnS("Error initializing multiplication!");
3978    return TRUE;
3979  }
3980  return FALSE;
3981}
Note: See TracBrowser for help on using the repository browser.