source: git/kernel/ring.cc @ 3b7e97

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