source: git/Singular/ring.cc @ 367e88

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