source: git/kernel/ring.cc @ c985c5

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