source: git/kernel/ring.cc @ b43de70

spielwiese
Last change on this file since b43de70 was b43de70, checked in by Hans Schönemann <hannes@…>, 16 years ago
*hannes: M(..) test git-svn-id: file:///usr/local/Singular/svn/trunk@10170 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 102.7 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: ring.cc,v 1.63 2007-07-05 09:40:41 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 1;
1455
1456  if (r1 == NULL || r2 == NULL) return 0;
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 0;
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 0;
1471    }
1472    else if ((r1->names[i] != NULL) ^ (r2->names[i] != NULL))
1473    {
1474      return 0;
1475    }
1476  }
1477
1478  i=0;
1479  while (r1->order[i] != 0)
1480  {
1481    if (r2->order[i] == 0) return 0;
1482    if ((r1->order[i] != r2->order[i])
1483    || (r1->block0[i] != r2->block0[i])
1484    || (r2->block0[i] != r1->block0[i]))
1485      return 0;
1486    if (r1->wvhdl[i] != NULL)
1487    {
1488      if (r2->wvhdl[i] == NULL)
1489        return 0;
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 0;
1493    }
1494    else if (r2->wvhdl[i] != NULL) return 0;
1495    i++;
1496  }
1497
1498  for (i=0; i<rPar(r1);i++)
1499  {
1500      if (strcmp(r1->parameter[i], r2->parameter[i])!=0)
1501        return 0;
1502  }
1503
1504  if (r1->minpoly != NULL)
1505  {
1506    if (r2->minpoly == NULL) return 0;
1507    if (currRing == r1 || currRing == r2)
1508    {
1509      if (! nEqual(r1->minpoly, r2->minpoly)) return 0;
1510    }
1511  }
1512  else if (r2->minpoly != NULL) return 0;
1513
1514  if (qr)
1515  {
1516    if (r1->qideal != NULL)
1517    {
1518      ideal id1 = r1->qideal, id2 = r2->qideal;
1519      int i, n;
1520      poly *m1, *m2;
1521
1522      if (id2 == NULL) return 0;
1523      if ((n = IDELEMS(id1)) != IDELEMS(id2)) return 0;
1524
1525      if (currRing == r1 || currRing == r2)
1526      {
1527        m1 = id1->m;
1528        m2 = id2->m;
1529        for (i=0; i<n; i++)
1530          if (! pEqualPolys(m1[i],m2[i])) return 0;
1531      }
1532    }
1533    else if (r2->qideal != NULL) return 0;
1534  }
1535
1536  return 1;
1537}
1538
1539rOrderType_t rGetOrderType(ring r)
1540{
1541  // check for simple ordering
1542  if (rHasSimpleOrder(r))
1543  {
1544    if ((r->order[1] == ringorder_c)
1545    || (r->order[1] == ringorder_C))
1546    {
1547      switch(r->order[0])
1548      {
1549          case ringorder_dp:
1550          case ringorder_wp:
1551          case ringorder_ds:
1552          case ringorder_ws:
1553          case ringorder_ls:
1554          case ringorder_unspec:
1555            if (r->order[1] == ringorder_C
1556            ||  r->order[0] == ringorder_unspec)
1557              return rOrderType_ExpComp;
1558            return rOrderType_Exp;
1559
1560          default:
1561            assume(r->order[0] == ringorder_lp ||
1562                   r->order[0] == ringorder_rs ||
1563                   r->order[0] == ringorder_Dp ||
1564                   r->order[0] == ringorder_Wp ||
1565                   r->order[0] == ringorder_Ds ||
1566                   r->order[0] == ringorder_Ws);
1567
1568            if (r->order[1] == ringorder_c) return rOrderType_ExpComp;
1569            return rOrderType_Exp;
1570      }
1571    }
1572    else
1573    {
1574      assume((r->order[0]==ringorder_c)||(r->order[0]==ringorder_C));
1575      return rOrderType_CompExp;
1576    }
1577  }
1578  else
1579    return rOrderType_General;
1580}
1581
1582BOOLEAN rHasSimpleOrder(ring r)
1583{
1584  if (r->order[0] == ringorder_unspec) return TRUE;
1585  int blocks = rBlocks(r) - 1;
1586  assume(blocks >= 1);
1587  if (blocks == 1) return TRUE;
1588  if (blocks > 2)  return FALSE;
1589  if ((r->order[0] != ringorder_c)
1590  && (r->order[0] != ringorder_C)
1591  && (r->order[1] != ringorder_c)
1592  && (r->order[1] != ringorder_C))
1593    return FALSE;
1594  if ((r->order[1] == ringorder_M)
1595  || (r->order[0] == ringorder_M))
1596    return FALSE;
1597  return TRUE;
1598}
1599
1600// returns TRUE, if simple lp or ls ordering
1601BOOLEAN rHasSimpleLexOrder(ring r)
1602{
1603  return rHasSimpleOrder(r) &&
1604    (r->order[0] == ringorder_ls ||
1605     r->order[0] == ringorder_lp ||
1606     r->order[1] == ringorder_ls ||
1607     r->order[1] == ringorder_lp);
1608}
1609
1610BOOLEAN rOrder_is_DegOrdering(rRingOrder_t order)
1611{
1612  switch(order)
1613  {
1614      case ringorder_dp:
1615      case ringorder_Dp:
1616      case ringorder_ds:
1617      case ringorder_Ds:
1618      case ringorder_Ws:
1619      case ringorder_Wp:
1620      case ringorder_ws:
1621      case ringorder_wp:
1622        return TRUE;
1623
1624      default:
1625        return FALSE;
1626  }
1627}
1628
1629BOOLEAN rOrder_is_WeightedOrdering(rRingOrder_t order)
1630{
1631  switch(order)
1632  {
1633      case ringorder_Ws:
1634      case ringorder_Wp:
1635      case ringorder_ws:
1636      case ringorder_wp:
1637        return TRUE;
1638
1639      default:
1640        return FALSE;
1641  }
1642}
1643
1644BOOLEAN rHasSimpleOrderAA(ring r)
1645{
1646  int blocks = rBlocks(r) - 1;
1647  if ((blocks > 3) || (blocks < 2)) return FALSE;
1648  if (blocks == 3)
1649  {
1650    return (((r->order[0] == ringorder_aa) && (r->order[1] != ringorder_M) &&
1651             ((r->order[2] == ringorder_c) || (r->order[2] == ringorder_C))) ||
1652            (((r->order[0] == ringorder_c) || (r->order[0] == ringorder_C)) &&
1653             (r->order[1] == ringorder_aa) && (r->order[2] != ringorder_M)));
1654  }
1655  else
1656  {
1657    return ((r->order[0] == ringorder_aa) && (r->order[1] != ringorder_M));
1658  }
1659}
1660
1661// return TRUE if p_SetComp requires p_Setm
1662BOOLEAN rOrd_SetCompRequiresSetm(ring r)
1663{
1664  if (r->typ != NULL)
1665  {
1666    int pos;
1667    for (pos=0;pos<r->OrdSize;pos++)
1668    {
1669      sro_ord* o=&(r->typ[pos]);
1670      if ((o->ord_typ == ro_syzcomp) || (o->ord_typ == ro_syz)) return TRUE;
1671    }
1672  }
1673  return FALSE;
1674}
1675
1676// return TRUE if p->exp[r->pOrdIndex] holds total degree of p */
1677BOOLEAN rOrd_is_Totaldegree_Ordering(ring r)
1678{
1679  // Hmm.... what about Syz orderings?
1680  return (rVar(r) > 1 &&
1681          ((rHasSimpleOrder(r) &&
1682           (rOrder_is_DegOrdering((rRingOrder_t)r->order[0]) ||
1683            rOrder_is_DegOrdering(( rRingOrder_t)r->order[1]))) ||
1684           (rHasSimpleOrderAA(r) &&
1685            (rOrder_is_DegOrdering((rRingOrder_t)r->order[1]) ||
1686             rOrder_is_DegOrdering((rRingOrder_t)r->order[2])))));
1687}
1688
1689// return TRUE if p->exp[r->pOrdIndex] holds a weighted degree of p */
1690BOOLEAN rOrd_is_WeightedDegree_Ordering(ring r =currRing)
1691{
1692  // Hmm.... what about Syz orderings?
1693  return ((rVar(r) > 1) &&
1694          rHasSimpleOrder(r) &&
1695          (rOrder_is_WeightedOrdering((rRingOrder_t)r->order[0]) ||
1696           rOrder_is_WeightedOrdering(( rRingOrder_t)r->order[1])));
1697}
1698
1699BOOLEAN rIsPolyVar(int v, ring r)
1700{
1701  int  i=0;
1702  while(r->order[i]!=0)
1703  {
1704    if((r->block0[i]<=v)
1705    && (r->block1[i]>=v))
1706    {
1707      switch(r->order[i])
1708      {
1709        case ringorder_a:
1710          return (r->wvhdl[i][v-r->block0[i]]>0);
1711        case ringorder_M:
1712          return 2; /*don't know*/
1713        case ringorder_a64: /* assume: all weight are non-negative!*/
1714        case ringorder_lp:
1715        case ringorder_rs:
1716        case ringorder_dp:
1717        case ringorder_Dp:
1718        case ringorder_wp:
1719        case ringorder_Wp:
1720          return TRUE;
1721        case ringorder_ls:
1722        case ringorder_ds:
1723        case ringorder_Ds:
1724        case ringorder_ws:
1725        case ringorder_Ws:
1726          return FALSE;
1727        default:
1728          break;
1729      }
1730    }
1731    i++;
1732  }
1733  return 3; /* could not find var v*/
1734}
1735
1736#ifdef RDEBUG
1737// This should eventually become a full-fledge ring check, like pTest
1738BOOLEAN rDBTest(ring r, char* fn, int l)
1739{
1740  int i,j;
1741
1742  if (r == NULL)
1743  {
1744    dReportError("Null ring in %s:%d", fn, l);
1745    return FALSE;
1746  }
1747
1748
1749  if (r->N == 0) return TRUE;
1750
1751//  omCheckAddrSize(r,sizeof(ip_sring));
1752#if OM_CHECK > 0
1753  i=rBlocks(r);
1754  omCheckAddrSize(r->order,i*sizeof(int));
1755  omCheckAddrSize(r->block0,i*sizeof(int));
1756  omCheckAddrSize(r->block1,i*sizeof(int));
1757  omCheckAddrSize(r->wvhdl,i*sizeof(int *));
1758  for (j=0;j<i; j++)
1759  {
1760    if (r->wvhdl[j] != NULL) omCheckAddr(r->wvhdl[j]);
1761  }
1762#endif
1763  if (r->VarOffset == NULL)
1764  {
1765    dReportError("Null ring VarOffset -- no rComplete (?) in n %s:%d", fn, l);
1766    return FALSE;
1767  }
1768  omCheckAddrSize(r->VarOffset,(r->N+1)*sizeof(int));
1769
1770  if ((r->OrdSize==0)!=(r->typ==NULL))
1771  {
1772    dReportError("mismatch OrdSize and typ-pointer in %s:%d");
1773    return FALSE;
1774  }
1775  omcheckAddrSize(r->typ,r->OrdSize*sizeof(*(r->typ)));
1776  omCheckAddrSize(r->VarOffset,(r->N+1)*sizeof(*(r->VarOffset)));
1777  // test assumptions:
1778  for(i=0;i<=r->N;i++)
1779  {
1780    if(r->typ!=NULL)
1781    {
1782      for(j=0;j<r->OrdSize;j++)
1783      {
1784        if (r->typ[j].ord_typ==ro_cp)
1785        {
1786          if(((short)r->VarOffset[i]) == r->typ[j].data.cp.place)
1787            dReportError("ordrec %d conflicts with var %d",j,i);
1788        }
1789        else
1790          if ((r->typ[j].ord_typ!=ro_syzcomp)
1791          && (r->VarOffset[i] == r->typ[j].data.dp.place))
1792            dReportError("ordrec %d conflicts with var %d",j,i);
1793      }
1794    }
1795    int tmp;
1796      tmp=r->VarOffset[i] & 0xffffff;
1797      #if SIZEOF_LONG == 8
1798        if ((r->VarOffset[i] >> 24) >63)
1799      #else
1800        if ((r->VarOffset[i] >> 24) >31)
1801      #endif
1802          dReportError("bit_start out of range:%d",r->VarOffset[i] >> 24);
1803      if (i > 0 && ((tmp<0) ||(tmp>r->ExpL_Size-1)))
1804      {
1805        dReportError("varoffset out of range for var %d: %d",i,tmp);
1806      }
1807  }
1808  if(r->typ!=NULL)
1809  {
1810    for(j=0;j<r->OrdSize;j++)
1811    {
1812      if ((r->typ[j].ord_typ==ro_dp)
1813      || (r->typ[j].ord_typ==ro_wp)
1814      || (r->typ[j].ord_typ==ro_wp_neg))
1815      {
1816        if (r->typ[j].data.dp.start > r->typ[j].data.dp.end)
1817          dReportError("in ordrec %d: start(%d) > end(%d)",j,
1818            r->typ[j].data.dp.start, r->typ[j].data.dp.end);
1819        if ((r->typ[j].data.dp.start < 1)
1820        || (r->typ[j].data.dp.end > r->N))
1821          dReportError("in ordrec %d: start(%d)<1 or end(%d)>vars(%d)",j,
1822            r->typ[j].data.dp.start, r->typ[j].data.dp.end,r->N);
1823      }
1824    }
1825  }
1826  if (r->minpoly!=NULL)
1827  {
1828    omCheckAddr(r->minpoly);
1829  }
1830  //assume(r->cf!=NULL);
1831
1832  return TRUE;
1833}
1834#endif
1835
1836static void rO_Align(int &place, int &bitplace)
1837{
1838  // increment place to the next aligned one
1839  // (count as Exponent_t,align as longs)
1840  if (bitplace!=BITS_PER_LONG)
1841  {
1842    place++;
1843    bitplace=BITS_PER_LONG;
1844  }
1845}
1846
1847static void rO_TDegree(int &place, int &bitplace, int start, int end,
1848    long *o, sro_ord &ord_struct)
1849{
1850  // degree (aligned) of variables v_start..v_end, ordsgn 1
1851  rO_Align(place,bitplace);
1852  ord_struct.ord_typ=ro_dp;
1853  ord_struct.data.dp.start=start;
1854  ord_struct.data.dp.end=end;
1855  ord_struct.data.dp.place=place;
1856  o[place]=1;
1857  place++;
1858  rO_Align(place,bitplace);
1859}
1860
1861static void rO_TDegree_neg(int &place, int &bitplace, int start, int end,
1862    long *o, sro_ord &ord_struct)
1863{
1864  // degree (aligned) of variables v_start..v_end, ordsgn -1
1865  rO_Align(place,bitplace);
1866  ord_struct.ord_typ=ro_dp;
1867  ord_struct.data.dp.start=start;
1868  ord_struct.data.dp.end=end;
1869  ord_struct.data.dp.place=place;
1870  o[place]=-1;
1871  place++;
1872  rO_Align(place,bitplace);
1873}
1874
1875static void rO_WDegree(int &place, int &bitplace, int start, int end,
1876    long *o, sro_ord &ord_struct, int *weights)
1877{
1878  // weighted degree (aligned) of variables v_start..v_end, ordsgn 1
1879  while((start<end) && (weights[0]==0)) { start++; weights++; }
1880  while((start<end) && (weights[end-start]==0)) { end--; }
1881  int i;
1882  int pure_tdeg=1;
1883  for(i=start;i<=end;i++)
1884  {
1885    if(weights[i-start]!=1)
1886    {
1887      pure_tdeg=0;
1888      break;
1889    }
1890  }
1891  if (pure_tdeg)
1892  {
1893    rO_TDegree(place,bitplace,start,end,o,ord_struct);
1894    return;
1895  }
1896  rO_Align(place,bitplace);
1897  ord_struct.ord_typ=ro_wp;
1898  ord_struct.data.wp.start=start;
1899  ord_struct.data.wp.end=end;
1900  ord_struct.data.wp.place=place;
1901  ord_struct.data.wp.weights=weights;
1902  o[place]=1;
1903  place++;
1904  rO_Align(place,bitplace);
1905  for(i=start;i<=end;i++)
1906  {
1907    if(weights[i-start]<0)
1908    {
1909      ord_struct.ord_typ=ro_wp_neg;
1910      break;
1911    }
1912  }
1913}
1914
1915static void rO_WDegree64(int &place, int &bitplace, int start, int end,
1916    long *o, sro_ord &ord_struct, int64 *weights)
1917{
1918  // weighted degree (aligned) of variables v_start..v_end, ordsgn 1,
1919  // reserved 2 places
1920  rO_Align(place,bitplace);
1921  ord_struct.ord_typ=ro_wp64;
1922  ord_struct.data.wp64.start=start;
1923  ord_struct.data.wp64.end=end;
1924  ord_struct.data.wp64.place=place;
1925  ord_struct.data.wp64.weights64=weights;
1926  o[place]=1;
1927  place++;
1928  o[place]=1;
1929  place++;
1930  rO_Align(place,bitplace);
1931  int i;
1932}
1933
1934static void rO_WDegree_neg(int &place, int &bitplace, int start, int end,
1935    long *o, sro_ord &ord_struct, int *weights)
1936{
1937  // weighted degree (aligned) of variables v_start..v_end, ordsgn -1
1938  while((start<end) && (weights[0]==0)) { start++; weights++; }
1939  while((start<end) && (weights[end-start]==0)) { end--; }
1940  rO_Align(place,bitplace);
1941  ord_struct.ord_typ=ro_wp;
1942  ord_struct.data.wp.start=start;
1943  ord_struct.data.wp.end=end;
1944  ord_struct.data.wp.place=place;
1945  ord_struct.data.wp.weights=weights;
1946  o[place]=-1;
1947  place++;
1948  rO_Align(place,bitplace);
1949  int i;
1950  for(i=start;i<=end;i++)
1951  {
1952    if(weights[i-start]<0)
1953    {
1954      ord_struct.ord_typ=ro_wp_neg;
1955      break;
1956    }
1957  }
1958}
1959
1960static void rO_LexVars(int &place, int &bitplace, int start, int end,
1961  int &prev_ord, long *o,int *v, int bits, int opt_var)
1962{
1963  // a block of variables v_start..v_end with lex order, ordsgn 1
1964  int k;
1965  int incr=1;
1966  if(prev_ord==-1) rO_Align(place,bitplace);
1967
1968  if (start>end)
1969  {
1970    incr=-1;
1971  }
1972  for(k=start;;k+=incr)
1973  {
1974    bitplace-=bits;
1975    if (bitplace < 0) { bitplace=BITS_PER_LONG-bits; place++; }
1976    o[place]=1;
1977    v[k]= place | (bitplace << 24);
1978    if (k==end) break;
1979  }
1980  prev_ord=1;
1981  if (opt_var!= -1)
1982  {
1983    assume((opt_var == end+1) ||(opt_var == end-1));
1984    if((opt_var != end+1) &&(opt_var != end-1)) WarnS("hier-2");
1985    int save_bitplace=bitplace;
1986    bitplace-=bits;
1987    if (bitplace < 0)
1988    {
1989      bitplace=save_bitplace;
1990      return;
1991    }
1992    // there is enough space for the optional var
1993    v[opt_var]=place | (bitplace << 24);
1994  }
1995}
1996
1997static void rO_LexVars_neg(int &place, int &bitplace, int start, int end,
1998  int &prev_ord, long *o,int *v, int bits, int opt_var)
1999{
2000  // a block of variables v_start..v_end with lex order, ordsgn -1
2001  int k;
2002  int incr=1;
2003  if(prev_ord==1) rO_Align(place,bitplace);
2004
2005  if (start>end)
2006  {
2007    incr=-1;
2008  }
2009  for(k=start;;k+=incr)
2010  {
2011    bitplace-=bits;
2012    if (bitplace < 0) { bitplace=BITS_PER_LONG-bits; place++; }
2013    o[place]=-1;
2014    v[k]=place | (bitplace << 24);
2015    if (k==end) break;
2016  }
2017  prev_ord=-1;
2018//  #if 0
2019  if (opt_var!= -1)
2020  {
2021    assume((opt_var == end+1) ||(opt_var == end-1));
2022    if((opt_var != end+1) &&(opt_var != end-1)) WarnS("hier-1");
2023    int save_bitplace=bitplace;
2024    bitplace-=bits;
2025    if (bitplace < 0)
2026    {
2027      bitplace=save_bitplace;
2028      return;
2029    }
2030    // there is enough space for the optional var
2031    v[opt_var]=place | (bitplace << 24);
2032  }
2033//  #endif
2034}
2035
2036static void rO_Syzcomp(int &place, int &bitplace, int &prev_ord,
2037    long *o, sro_ord &ord_struct)
2038{
2039  // ordering is derived from component number
2040  rO_Align(place,bitplace);
2041  ord_struct.ord_typ=ro_syzcomp;
2042  ord_struct.data.syzcomp.place=place;
2043  ord_struct.data.syzcomp.Components=NULL;
2044  ord_struct.data.syzcomp.ShiftedComponents=NULL;
2045  o[place]=1;
2046  prev_ord=1;
2047  place++;
2048  rO_Align(place,bitplace);
2049}
2050
2051static void rO_Syz(int &place, int &bitplace, int &prev_ord,
2052    long *o, sro_ord &ord_struct)
2053{
2054  // ordering is derived from component number
2055  // let's reserve one Exponent_t for it
2056  if ((prev_ord== 1) || (bitplace!=BITS_PER_LONG))
2057    rO_Align(place,bitplace);
2058  ord_struct.ord_typ=ro_syz;
2059  ord_struct.data.syz.place=place;
2060  ord_struct.data.syz.limit=0;
2061  ord_struct.data.syz.syz_index = NULL;
2062  ord_struct.data.syz.curr_index = 1;
2063  o[place]= -1;
2064  prev_ord=-1;
2065  place++;
2066}
2067
2068static unsigned long rGetExpSize(unsigned long bitmask, int & bits)
2069{
2070  if (bitmask == 0)
2071  {
2072    bits=16; bitmask=0xffff;
2073  }
2074  else if (bitmask <= 1)
2075  {
2076    bits=1; bitmask = 1;
2077  }
2078  else if (bitmask <= 3)
2079  {
2080    bits=2; bitmask = 3;
2081  }
2082  else if (bitmask <= 7)
2083  {
2084    bits=3; bitmask=7;
2085  }
2086  else if (bitmask <= 0xf)
2087  {
2088    bits=4; bitmask=0xf;
2089  }
2090  else if (bitmask <= 0x1f)
2091  {
2092    bits=5; bitmask=0x1f;
2093  }
2094  else if (bitmask <= 0x3f)
2095  {
2096    bits=6; bitmask=0x3f;
2097  }
2098#if SIZEOF_LONG == 8
2099  else if (bitmask <= 0x7f)
2100  {
2101    bits=7; bitmask=0x7f; /* 64 bit longs only */
2102  }
2103#endif
2104  else if (bitmask <= 0xff)
2105  {
2106    bits=8; bitmask=0xff;
2107  }
2108#if SIZEOF_LONG == 8
2109  else if (bitmask <= 0x1ff)
2110  {
2111    bits=9; bitmask=0x1ff; /* 64 bit longs only */
2112  }
2113#endif
2114  else if (bitmask <= 0x3ff)
2115  {
2116    bits=10; bitmask=0x3ff;
2117  }
2118#if SIZEOF_LONG == 8
2119  else if (bitmask <= 0xfff)
2120  {
2121    bits=12; bitmask=0xfff; /* 64 bit longs only */
2122  }
2123#endif
2124  else if (bitmask <= 0xffff)
2125  {
2126    bits=16; bitmask=0xffff;
2127  }
2128#if SIZEOF_LONG == 8
2129  else if (bitmask <= 0xfffff)
2130  {
2131    bits=20; bitmask=0xfffff; /* 64 bit longs only */
2132  }
2133  else if (bitmask <= 0xffffffff)
2134  {
2135    bits=32; bitmask=0xffffffff;
2136  }
2137  else
2138  {
2139    bits=64; bitmask=0xffffffffffffffff;
2140  }
2141#else
2142  else
2143  {
2144    bits=32; bitmask=0xffffffff;
2145  }
2146#endif
2147  return bitmask;
2148}
2149
2150/*2
2151* optimize rGetExpSize for a block of N variables, exp <=bitmask
2152*/
2153static unsigned long rGetExpSize(unsigned long bitmask, int & bits, int N)
2154{
2155  bitmask =rGetExpSize(bitmask, bits);
2156  int vars_per_long=BIT_SIZEOF_LONG/bits;
2157  int bits1;
2158  loop
2159  {
2160    if (bits == BIT_SIZEOF_LONG)
2161    {
2162      bits =  BIT_SIZEOF_LONG - 1;
2163      return LONG_MAX;
2164    }
2165    unsigned long bitmask1 =rGetExpSize(bitmask+1, bits1);
2166    int vars_per_long1=BIT_SIZEOF_LONG/bits1;
2167    if ((((N+vars_per_long-1)/vars_per_long) ==
2168         ((N+vars_per_long1-1)/vars_per_long1)))
2169    {
2170      vars_per_long=vars_per_long1;
2171      bits=bits1;
2172      bitmask=bitmask1;
2173    }
2174    else
2175    {
2176      return bitmask; /* and bits */
2177    }
2178  }
2179}
2180
2181/*2
2182 * create a copy of the ring r, which must be equivalent to currRing
2183 * used for std computations
2184 * may share data structures with currRing
2185 * DOES CALL rComplete
2186 */
2187ring rModifyRing(ring r, BOOLEAN omit_degree,
2188                         BOOLEAN omit_comp,
2189                         unsigned long exp_limit)
2190{
2191  assume (r != NULL );
2192  assume (exp_limit > 1);
2193  BOOLEAN need_other_ring;
2194  BOOLEAN omitted_degree = FALSE;
2195  int bits;
2196
2197  exp_limit=rGetExpSize(exp_limit, bits, r->N);
2198  need_other_ring = (exp_limit != r->bitmask);
2199
2200  int nblocks=rBlocks(r);
2201  int *order=(int*)omAlloc0((nblocks+1)*sizeof(int));
2202  int *block0=(int*)omAlloc0((nblocks+1)*sizeof(int));
2203  int *block1=(int*)omAlloc0((nblocks+1)*sizeof(int));
2204  int **wvhdl=(int**)omAlloc0((nblocks+1)*sizeof(int_ptr));
2205
2206  int i=0;
2207  int j=0; /*  i index in r, j index in res */
2208  loop
2209  {
2210    BOOLEAN copy_block_index=TRUE;
2211    int r_ord=r->order[i];
2212    if (r->block0[i]==r->block1[i])
2213    {
2214      switch(r_ord)
2215      {
2216        case ringorder_wp:
2217        case ringorder_dp:
2218        case ringorder_Wp:
2219        case ringorder_Dp:
2220          r_ord=ringorder_lp;
2221          break;
2222        case ringorder_Ws:
2223        case ringorder_Ds:
2224        case ringorder_ws:
2225        case ringorder_ds:
2226          r_ord=ringorder_ls;
2227          break;
2228        default:
2229          break;
2230      }
2231    }
2232    switch(r_ord)
2233    {
2234      case ringorder_C:
2235      case ringorder_c:
2236        if (!omit_comp)
2237        {
2238          order[j]=r_ord; /*r->order[i]*/;
2239        }
2240        else
2241        {
2242          j--;
2243          need_other_ring=TRUE;
2244          omit_comp=FALSE;
2245          copy_block_index=FALSE;
2246        }
2247        break;
2248      case ringorder_wp:
2249      case ringorder_dp:
2250      case ringorder_ws:
2251      case ringorder_ds:
2252        if(!omit_degree)
2253        {
2254          order[j]=r_ord; /*r->order[i]*/;
2255        }
2256        else
2257        {
2258          order[j]=ringorder_rs;
2259          need_other_ring=TRUE;
2260          omit_degree=FALSE;
2261          omitted_degree = TRUE;
2262        }
2263        break;
2264      case ringorder_Wp:
2265      case ringorder_Dp:
2266      case ringorder_Ws:
2267      case ringorder_Ds:
2268        if(!omit_degree)
2269        {
2270          order[j]=r_ord; /*r->order[i];*/
2271        }
2272        else
2273        {
2274          order[j]=ringorder_lp;
2275          need_other_ring=TRUE;
2276          omit_degree=FALSE;
2277          omitted_degree = TRUE;
2278        }
2279        break;
2280      default:
2281        order[j]=r_ord; /*r->order[i];*/
2282        break;
2283    }
2284    if (copy_block_index)
2285    {
2286      block0[j]=r->block0[i];
2287      block1[j]=r->block1[i];
2288      wvhdl[j]=r->wvhdl[i];
2289    }
2290    i++;j++;
2291    // order[j]=ringorder_no; //  done by omAlloc0
2292    if (i==nblocks) break;
2293  }
2294  if(!need_other_ring)
2295  {
2296    omFreeSize(order,(nblocks+1)*sizeof(int));
2297    omFreeSize(block0,(nblocks+1)*sizeof(int));
2298    omFreeSize(block1,(nblocks+1)*sizeof(int));
2299    omFreeSize(wvhdl,(nblocks+1)*sizeof(int_ptr));
2300    return r;
2301  }
2302  ring res=(ring)omAlloc0Bin(ip_sring_bin);
2303  *res = *r;
2304  // res->qideal, res->idroot ???
2305  res->wvhdl=wvhdl;
2306  res->order=order;
2307  res->block0=block0;
2308  res->block1=block1;
2309  res->bitmask=exp_limit;
2310  int tmpref=r->cf->ref;
2311  rComplete(res, 1);
2312  r->cf->ref=tmpref;
2313
2314  // adjust res->pFDeg: if it was changed globally, then
2315  // it must also be changed for new ring
2316  if (r->pFDegOrig != res->pFDegOrig &&
2317           rOrd_is_WeightedDegree_Ordering(r))
2318  {
2319    // still might need adjustment for weighted orderings
2320    // and omit_degree
2321    res->firstwv = r->firstwv;
2322    res->firstBlockEnds = r->firstBlockEnds;
2323    res->pFDeg = res->pFDegOrig = pWFirstTotalDegree;
2324  }
2325  if (omitted_degree)
2326    res->pLDeg = res->pLDegOrig = r->pLDegOrig;
2327
2328  rOptimizeLDeg(res);
2329
2330  // set syzcomp
2331  if (res->typ != NULL && res->typ[0].ord_typ == ro_syz)
2332  {
2333    res->typ[0] = r->typ[0];
2334    if (r->typ[0].data.syz.limit > 0)
2335    {
2336      res->typ[0].data.syz.syz_index
2337        = (int*) omAlloc((r->typ[0].data.syz.limit +1)*sizeof(int));
2338      memcpy(res->typ[0].data.syz.syz_index, r->typ[0].data.syz.syz_index,
2339             (r->typ[0].data.syz.limit +1)*sizeof(int));
2340    }
2341  }
2342  // the special case: homog (omit_degree) and 1 block rs: that is global:
2343  // it comes from dp
2344  res->OrdSgn=r->OrdSgn;
2345  return res;
2346}
2347
2348// construct Wp,C ring
2349ring rModifyRing_Wp(ring r, int* weights)
2350{
2351  ring res=(ring)omAlloc0Bin(ip_sring_bin);
2352  *res = *r;
2353  /*weights: entries for 3 blocks: NULL*/
2354  res->wvhdl = (int **)omAlloc0(3 * sizeof(int_ptr));
2355  /*order: Wp,C,0*/
2356  res->order = (int *) omAlloc(3 * sizeof(int *));
2357  res->block0 = (int *)omAlloc0(3 * sizeof(int *));
2358  res->block1 = (int *)omAlloc0(3 * sizeof(int *));
2359  /* ringorder Wp for the first block: var 1..r->N */
2360  res->order[0]  = ringorder_Wp;
2361  res->block0[0] = 1;
2362  res->block1[0] = r->N;
2363  res->wvhdl[0] = weights;
2364  /* ringorder C for the second block: no vars */
2365  res->order[1]  = ringorder_C;
2366  /* the last block: everything is 0 */
2367  res->order[2]  = 0;
2368  /*polynomial ring*/
2369  res->OrdSgn    = 1;
2370
2371  int tmpref=r->cf->ref;
2372  rComplete(res, 1);
2373  r->cf->ref=tmpref;
2374  return res;
2375}
2376
2377// construct lp ring with r->N variables, r->names vars....
2378ring rModifyRing_Simple(ring r, BOOLEAN ommit_degree, BOOLEAN ommit_comp, unsigned long exp_limit, BOOLEAN &simple)
2379{
2380  simple=TRUE;
2381  if (!rHasSimpleOrder(r))
2382  {
2383    simple=FALSE; // sorting needed
2384    assume (r != NULL );
2385    assume (exp_limit > 1);
2386    BOOLEAN omitted_degree = FALSE;
2387    int bits;
2388
2389    exp_limit=rGetExpSize(exp_limit, bits, r->N);
2390
2391    int nblocks=1+(ommit_comp!=0);
2392    int *order=(int*)omAlloc0((nblocks+1)*sizeof(int));
2393    int *block0=(int*)omAlloc0((nblocks+1)*sizeof(int));
2394    int *block1=(int*)omAlloc0((nblocks+1)*sizeof(int));
2395    int **wvhdl=(int**)omAlloc0((nblocks+1)*sizeof(int_ptr));
2396
2397    order[0]=ringorder_lp;
2398    block0[0]=1;
2399    block1[0]=r->N;
2400    if (!ommit_comp)
2401    {
2402      order[1]=ringorder_C;
2403    }
2404    ring res=(ring)omAlloc0Bin(ip_sring_bin);
2405    *res = *r;
2406    // res->qideal, res->idroot ???
2407    res->wvhdl=wvhdl;
2408    res->order=order;
2409    res->block0=block0;
2410    res->block1=block1;
2411    res->bitmask=exp_limit;
2412    int tmpref=r->cf->ref;
2413    rComplete(res, 1);
2414    r->cf->ref=tmpref;
2415
2416    rOptimizeLDeg(res);
2417
2418    return res;
2419  }
2420  return rModifyRing(r, ommit_degree, ommit_comp, exp_limit);
2421}
2422
2423void rKillModifiedRing_Simple(ring r)
2424{
2425  rKillModifiedRing(r);
2426}
2427
2428
2429void rKillModifiedRing(ring r)
2430{
2431  rUnComplete(r);
2432  omFree(r->order);
2433  omFree(r->block0);
2434  omFree(r->block1);
2435  omFree(r->wvhdl);
2436  omFreeBin(r,ip_sring_bin);
2437}
2438
2439void rKillModified_Wp_Ring(ring r)
2440{
2441  rUnComplete(r);
2442  omFree(r->order);
2443  omFree(r->block0);
2444  omFree(r->block1);
2445  omFree(r->wvhdl[0]);
2446  omFree(r->wvhdl);
2447  omFreeBin(r,ip_sring_bin);
2448}
2449
2450static void rSetOutParams(ring r)
2451{
2452  r->VectorOut = (r->order[0] == ringorder_c);
2453  r->ShortOut = TRUE;
2454#ifdef HAVE_TCL
2455  if (tcllmode)
2456  {
2457    r->ShortOut = FALSE;
2458  }
2459  else
2460#endif
2461  {
2462    int i;
2463    if ((r->parameter!=NULL) && (r->ch<2))
2464    {
2465      for (i=0;i<rPar(r);i++)
2466      {
2467        if(strlen(r->parameter[i])>1)
2468        {
2469          r->ShortOut=FALSE;
2470          break;
2471        }
2472      }
2473    }
2474    if (r->ShortOut)
2475    {
2476      // Hmm... sometimes (e.g., from maGetPreimage) new variables
2477      // are intorduced, but their names are never set
2478      // hence, we do the following awkward trick
2479      int N = omSizeWOfAddr(r->names);
2480      if (r->N < N) N = r->N;
2481
2482      for (i=(N-1);i>=0;i--)
2483      {
2484        if(r->names[i] != NULL && strlen(r->names[i])>1)
2485        {
2486          r->ShortOut=FALSE;
2487          break;
2488        }
2489      }
2490    }
2491  }
2492  r->CanShortOut = r->ShortOut;
2493}
2494
2495/*2
2496* sets pMixedOrder and pComponentOrder for orderings with more than one block
2497* block of variables (ip is the block number, o_r the number of the ordering)
2498* o is the position of the orderingering in r
2499*/
2500static void rHighSet(ring r, int o_r, int o)
2501{
2502  switch(o_r)
2503  {
2504    case ringorder_lp:
2505    case ringorder_dp:
2506    case ringorder_Dp:
2507    case ringorder_wp:
2508    case ringorder_Wp:
2509    case ringorder_rp:
2510    case ringorder_a:
2511    case ringorder_aa:
2512    case ringorder_a64:
2513      if (r->OrdSgn==-1) r->MixedOrder=TRUE;
2514      break;
2515    case ringorder_ls:
2516    case ringorder_rs:
2517    case ringorder_ds:
2518    case ringorder_Ds:
2519    case ringorder_s:
2520      break;
2521    case ringorder_ws:
2522    case ringorder_Ws:
2523      if (r->wvhdl[o]!=NULL)
2524      {
2525        int i;
2526        for(i=r->block1[o]-r->block0[o];i>=0;i--)
2527          if (r->wvhdl[o][i]<0) { r->MixedOrder=TRUE; break; }
2528      }
2529      break;
2530    case ringorder_c:
2531      r->ComponentOrder=1;
2532      break;
2533    case ringorder_C:
2534    case ringorder_S:
2535      r->ComponentOrder=-1;
2536      break;
2537    case ringorder_M:
2538      r->MixedOrder=TRUE;
2539      break;
2540    default:
2541      dReportError("wrong internal ordering:%d at %s, l:%d\n",o_r,__FILE__,__LINE__);
2542  }
2543}
2544
2545static void rSetFirstWv(ring r, int i, int* order, int* block1, int** wvhdl)
2546{
2547  // cheat for ringorder_aa
2548  if (order[i] == ringorder_aa)
2549    i++;
2550  if(block1[i]!=r->N) r->LexOrder=TRUE;
2551  r->firstBlockEnds=block1[i];
2552  r->firstwv = wvhdl[i];
2553  if ((order[i]== ringorder_ws)
2554  || (order[i]==ringorder_Ws)
2555  || (order[i]== ringorder_wp) 
2556  || (order[i]==ringorder_Wp)
2557  || (order[i]== ringorder_a)
2558   /*|| (order[i]==ringorder_A)*/)
2559  {
2560    int j;
2561    for(j=block1[i]-r->block0[i];j>=0;j--)
2562    {
2563      if (r->firstwv[j]<0) r->MixedOrder=TRUE;
2564      if (r->firstwv[j]==0) r->LexOrder=TRUE;
2565    }
2566  }
2567  else if (order[i]==ringorder_a64)
2568  {
2569    int j;
2570    int64 *w=rGetWeightVec(r);
2571    for(j=block1[i]-r->block0[i];j>=0;j--)
2572    {
2573      if (w[j]==0) r->LexOrder=TRUE;
2574    }
2575  }
2576}
2577
2578static void rOptimizeLDeg(ring r)
2579{
2580  if (r->pFDeg == pDeg)
2581  {
2582    if (r->pLDeg == pLDeg1)
2583      r->pLDeg = pLDeg1_Deg;
2584    if (r->pLDeg == pLDeg1c)
2585      r->pLDeg = pLDeg1c_Deg;
2586  }
2587  else if (r->pFDeg == pTotaldegree)
2588  {
2589    if (r->pLDeg == pLDeg1)
2590      r->pLDeg = pLDeg1_Totaldegree;
2591    if (r->pLDeg == pLDeg1c)
2592      r->pLDeg = pLDeg1c_Totaldegree;
2593  }
2594  else if (r->pFDeg == pWFirstTotalDegree)
2595  {
2596    if (r->pLDeg == pLDeg1)
2597      r->pLDeg = pLDeg1_WFirstTotalDegree;
2598    if (r->pLDeg == pLDeg1c)
2599      r->pLDeg = pLDeg1c_WFirstTotalDegree;
2600  }
2601}
2602
2603// set pFDeg, pLDeg, MixOrder, ComponentOrder, etc
2604static void rSetDegStuff(ring r)
2605{
2606  int* order = r->order;
2607  int* block0 = r->block0;
2608  int* block1 = r->block1;
2609  int** wvhdl = r->wvhdl;
2610
2611  if (order[0]==ringorder_S ||order[0]==ringorder_s)
2612  {
2613    order++;
2614    block0++;
2615    block1++;
2616    wvhdl++;
2617  }
2618  r->LexOrder = FALSE;
2619  r->MixedOrder = FALSE;
2620  r->ComponentOrder = 1;
2621  r->pFDeg = pTotaldegree;
2622  r->pLDeg = (r->OrdSgn == 1 ? pLDegb : pLDeg0);
2623
2624  /*======== ordering type is (_,c) =========================*/
2625  if ((order[0]==ringorder_unspec) || (order[1] == 0)
2626      ||(
2627    ((order[1]==ringorder_c)||(order[1]==ringorder_C)
2628     ||(order[1]==ringorder_S)
2629     ||(order[1]==ringorder_s))
2630    && (order[0]!=ringorder_M)
2631    && (order[2]==0))
2632    )
2633  {
2634    if ((order[0]!=ringorder_unspec)
2635    && ((order[1]==ringorder_C)||(order[1]==ringorder_S)||
2636        (order[1]==ringorder_s)))
2637      r->ComponentOrder=-1;
2638    if (r->OrdSgn == -1) r->pLDeg = pLDeg0c;
2639    if ((order[0] == ringorder_lp)
2640    || (order[0] == ringorder_ls)
2641    || (order[0] == ringorder_rp)
2642    || (order[0] == ringorder_rs))
2643    {
2644      r->LexOrder=TRUE;
2645      r->pLDeg = pLDeg1c;
2646      r->pFDeg = pTotaldegree;
2647    }
2648    if ((order[0] == ringorder_a) 
2649    || (order[0] == ringorder_wp) 
2650    || (order[0] == ringorder_Wp) 
2651    || (order[0] == ringorder_ws) 
2652    || (order[0] == ringorder_Ws))
2653      r->pFDeg = pWFirstTotalDegree;
2654    r->firstBlockEnds=block1[0];
2655    r->firstwv = wvhdl[0];
2656  }
2657  /*======== ordering type is (c,_) =========================*/
2658  else if (((order[0]==ringorder_c)
2659            ||(order[0]==ringorder_C)
2660            ||(order[0]==ringorder_S)
2661            ||(order[0]==ringorder_s))
2662  && (order[1]!=ringorder_M)
2663  &&  (order[2]==0))
2664  {
2665    if ((order[0]==ringorder_C)||(order[0]==ringorder_S)||
2666        order[0]==ringorder_s)
2667      r->ComponentOrder=-1;
2668    if ((order[1] == ringorder_lp)
2669    || (order[1] == ringorder_ls)
2670    || (order[1] == ringorder_rp)
2671    || order[1] == ringorder_rs)
2672    {
2673      r->LexOrder=TRUE;
2674      r->pLDeg = pLDeg1c;
2675      r->pFDeg = pTotaldegree;
2676    }
2677    r->firstBlockEnds=block1[1];
2678    r->firstwv = wvhdl[1];
2679    if ((order[1] == ringorder_a) 
2680    || (order[1] == ringorder_wp) 
2681    || (order[1] == ringorder_Wp) 
2682    || (order[1] == ringorder_ws) 
2683    || (order[1] == ringorder_Ws))
2684      r->pFDeg = pWFirstTotalDegree;
2685  }
2686  /*------- more than one block ----------------------*/
2687  else
2688  {
2689    if ((r->VectorOut)||(order[0]==ringorder_C)||(order[0]==ringorder_S)||(order[0]==ringorder_s))
2690    {
2691      rSetFirstWv(r, 1, order, block1, wvhdl);
2692    }
2693    else
2694      rSetFirstWv(r, 0, order, block1, wvhdl);
2695
2696    /*the number of orderings:*/
2697    int i = 0;
2698    while (order[++i] != 0);
2699    do
2700    {
2701      i--;
2702      rHighSet(r, order[i],i);
2703    }
2704    while (i != 0);
2705
2706    if ((order[0]!=ringorder_c)
2707        && (order[0]!=ringorder_C)
2708        && (order[0]!=ringorder_S)
2709        && (order[0]!=ringorder_s))
2710    {
2711      r->pLDeg = pLDeg1c;
2712    }
2713    else
2714    {
2715      r->pLDeg = pLDeg1;
2716    }
2717    r->pFDeg = pWTotaldegree; // may be improved: pTotaldegree for lp/dp/ls/.. blocks
2718  }
2719  if (rOrd_is_Totaldegree_Ordering(r) || rOrd_is_WeightedDegree_Ordering(r))
2720    r->pFDeg = pDeg;
2721
2722  r->pFDegOrig = r->pFDeg;
2723  r->pLDegOrig = r->pLDeg;
2724  rOptimizeLDeg(r);
2725}
2726
2727/*2
2728* set NegWeightL_Size, NegWeightL_Offset
2729*/
2730static void rSetNegWeight(ring r)
2731{
2732  int i,l;
2733  if (r->typ!=NULL)
2734  {
2735    l=0;
2736    for(i=0;i<r->OrdSize;i++)
2737    {
2738      if(r->typ[i].ord_typ==ro_wp_neg) l++;
2739    }
2740    if (l>0)
2741    {
2742      r->NegWeightL_Size=l;
2743      r->NegWeightL_Offset=(int *) omAlloc(l*sizeof(int));
2744      l=0;
2745      for(i=0;i<r->OrdSize;i++)
2746      {
2747        if(r->typ[i].ord_typ==ro_wp_neg)
2748        {
2749          r->NegWeightL_Offset[l]=r->typ[i].data.wp.place;
2750          l++;
2751        }
2752      }
2753      return;
2754    }
2755  }
2756  r->NegWeightL_Size = 0;
2757  r->NegWeightL_Offset = NULL;
2758}
2759
2760static void rSetOption(ring r)
2761{
2762  // set redthrough
2763  if (!TEST_OPT_OLDSTD && r->OrdSgn == 1 && ! r->LexOrder)
2764    r->options |= Sy_bit(OPT_REDTHROUGH);
2765  else
2766    r->options &= ~Sy_bit(OPT_REDTHROUGH);
2767
2768  // set intStrategy
2769#ifdef HAVE_RINGS
2770  if (rField_is_Extension(r) || rField_is_Q(r) || rField_is_Ring(r))
2771#else
2772  if (rField_is_Extension(r) || rField_is_Q(r))
2773#endif
2774    r->options |= Sy_bit(OPT_INTSTRATEGY);
2775  else
2776    r->options &= ~Sy_bit(OPT_INTSTRATEGY);
2777
2778  // set redTail
2779  if (r->LexOrder || r->OrdSgn == -1 || rField_is_Extension(r))
2780    r->options &= ~Sy_bit(OPT_REDTAIL);
2781  else
2782    r->options |= Sy_bit(OPT_REDTAIL);
2783}
2784
2785BOOLEAN rComplete(ring r, int force)
2786{
2787  if (r->VarOffset!=NULL && force == 0) return FALSE;
2788  nInitChar(r);
2789  rSetOutParams(r);
2790  int n=rBlocks(r)-1;
2791  int i;
2792  int bits;
2793  r->bitmask=rGetExpSize(r->bitmask,bits,r->N);
2794  r->BitsPerExp = bits;
2795  r->ExpPerLong = BIT_SIZEOF_LONG / bits;
2796  r->divmask=rGetDivMask(bits);
2797
2798  // will be used for ordsgn:
2799  long *tmp_ordsgn=(long *)omAlloc0(2*(n+r->N)*sizeof(long));
2800  // will be used for VarOffset:
2801  int *v=(int *)omAlloc((r->N+1)*sizeof(int));
2802  for(i=r->N; i>=0 ; i--)
2803  {
2804    v[i]=-1;
2805  }
2806  sro_ord *tmp_typ=(sro_ord *)omAlloc0(2*(n+r->N)*sizeof(sro_ord));
2807  int typ_i=0;
2808  int prev_ordsgn=0;
2809
2810  // fill in v, tmp_typ, tmp_ordsgn, determine typ_i (== ordSize)
2811  int j=0;
2812  int j_bits=BITS_PER_LONG;
2813  BOOLEAN need_to_add_comp=FALSE;
2814  for(i=0;i<n;i++)
2815  {
2816    tmp_typ[typ_i].order_index=i;
2817    switch (r->order[i])
2818    {
2819      case ringorder_a:
2820      case ringorder_aa:
2821        rO_WDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,tmp_typ[typ_i],
2822                   r->wvhdl[i]);
2823        typ_i++;
2824        break;
2825
2826      case ringorder_a64:
2827        rO_WDegree64(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
2828                     tmp_typ[typ_i], (int64 *)(r->wvhdl[i]));
2829        typ_i++;
2830        break;
2831
2832      case ringorder_c:
2833        rO_Align(j, j_bits);
2834        rO_LexVars_neg(j, j_bits, 0,0, prev_ordsgn,tmp_ordsgn,v,BITS_PER_LONG, -1);
2835        break;
2836
2837      case ringorder_C:
2838        rO_Align(j, j_bits);
2839        rO_LexVars(j, j_bits, 0,0, prev_ordsgn,tmp_ordsgn,v,BITS_PER_LONG, -1);
2840        break;
2841
2842      case ringorder_M:
2843        {
2844          int k,l;
2845          k=r->block1[i]-r->block0[i]+1; // number of vars
2846          for(l=0;l<k;l++)
2847          {
2848            rO_WDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
2849                       tmp_typ[typ_i],
2850                       r->wvhdl[i]+(r->block1[i]-r->block0[i]+1)*l);
2851            typ_i++;
2852          }
2853          break;
2854        }
2855
2856      case ringorder_lp:
2857        rO_LexVars(j, j_bits, r->block0[i],r->block1[i], prev_ordsgn,
2858                   tmp_ordsgn,v,bits, -1);
2859        break;
2860
2861      case ringorder_ls:
2862        rO_LexVars_neg(j, j_bits, r->block0[i],r->block1[i], prev_ordsgn,
2863                       tmp_ordsgn,v, bits, -1);
2864        break;
2865
2866      case ringorder_rs:
2867        rO_LexVars_neg(j, j_bits, r->block1[i],r->block0[i], prev_ordsgn,
2868                       tmp_ordsgn,v, bits, -1);
2869        break;
2870
2871      case ringorder_rp:
2872        rO_LexVars_neg(j, j_bits, r->block1[i],r->block0[i], prev_ordsgn,
2873                       tmp_ordsgn,v, bits, -1);
2874        break;
2875
2876      case ringorder_dp:
2877        if (r->block0[i]==r->block1[i])
2878        {
2879          rO_LexVars(j, j_bits, r->block0[i],r->block0[i], prev_ordsgn,
2880                     tmp_ordsgn,v, bits, -1);
2881        }
2882        else
2883        {
2884          rO_TDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
2885                     tmp_typ[typ_i]);
2886          typ_i++;
2887          rO_LexVars_neg(j, j_bits, r->block1[i],r->block0[i]+1,
2888                         prev_ordsgn,tmp_ordsgn,v,bits, r->block0[i]);
2889        }
2890        break;
2891
2892      case ringorder_Dp:
2893        if (r->block0[i]==r->block1[i])
2894        {
2895          rO_LexVars(j, j_bits, r->block0[i],r->block0[i], prev_ordsgn,
2896                     tmp_ordsgn,v, bits, -1);
2897        }
2898        else
2899        {
2900          rO_TDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
2901                     tmp_typ[typ_i]);
2902          typ_i++;
2903          rO_LexVars(j, j_bits, r->block0[i],r->block1[i]-1, prev_ordsgn,
2904                     tmp_ordsgn,v, bits, r->block1[i]);
2905        }
2906        break;
2907
2908      case ringorder_ds:
2909        if (r->block0[i]==r->block1[i])
2910        {
2911          rO_LexVars_neg(j, j_bits,r->block0[i],r->block1[i],prev_ordsgn,
2912                         tmp_ordsgn,v,bits, -1);
2913        }
2914        else
2915        {
2916          rO_TDegree_neg(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
2917                         tmp_typ[typ_i]);
2918          typ_i++;
2919          rO_LexVars_neg(j, j_bits, r->block1[i],r->block0[i]+1,
2920                         prev_ordsgn,tmp_ordsgn,v,bits, r->block0[i]);
2921        }
2922        break;
2923
2924      case ringorder_Ds:
2925        if (r->block0[i]==r->block1[i])
2926        {
2927          rO_LexVars_neg(j, j_bits, r->block0[i],r->block0[i],prev_ordsgn,
2928                         tmp_ordsgn,v, bits, -1);
2929        }
2930        else
2931        {
2932          rO_TDegree_neg(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
2933                         tmp_typ[typ_i]);
2934          typ_i++;
2935          rO_LexVars(j, j_bits, r->block0[i],r->block1[i]-1, prev_ordsgn,
2936                     tmp_ordsgn,v, bits, r->block1[i]);
2937        }
2938        break;
2939
2940      case ringorder_wp:
2941        rO_WDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
2942                   tmp_typ[typ_i], r->wvhdl[i]);
2943        typ_i++;
2944        if (r->block1[i]!=r->block0[i])
2945        {
2946          rO_LexVars_neg(j, j_bits,r->block1[i],r->block0[i]+1, prev_ordsgn,
2947                         tmp_ordsgn, v,bits, r->block0[i]);
2948        }
2949        break;
2950
2951      case ringorder_Wp:
2952        rO_WDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
2953                   tmp_typ[typ_i], r->wvhdl[i]);
2954        typ_i++;
2955        if (r->block1[i]!=r->block0[i])
2956        {
2957          rO_LexVars(j, j_bits,r->block0[i],r->block1[i]-1, prev_ordsgn,
2958                     tmp_ordsgn,v, bits, r->block1[i]);
2959        }
2960        break;
2961
2962      case ringorder_ws:
2963        rO_WDegree_neg(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
2964                       tmp_typ[typ_i], r->wvhdl[i]);
2965        typ_i++;
2966        if (r->block1[i]!=r->block0[i])
2967        {
2968          rO_LexVars_neg(j, j_bits,r->block1[i],r->block0[i]+1, prev_ordsgn,
2969                         tmp_ordsgn, v,bits, r->block0[i]);
2970        }
2971        break;
2972
2973      case ringorder_Ws:
2974        rO_WDegree_neg(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
2975                       tmp_typ[typ_i], r->wvhdl[i]);
2976        typ_i++;
2977        if (r->block1[i]!=r->block0[i])
2978        {
2979          rO_LexVars(j, j_bits,r->block0[i],r->block1[i]-1, prev_ordsgn,
2980                     tmp_ordsgn,v, bits, r->block1[i]);
2981        }
2982        break;
2983
2984      case ringorder_S:
2985        rO_Syzcomp(j, j_bits,prev_ordsgn, tmp_ordsgn,tmp_typ[typ_i]);
2986        need_to_add_comp=TRUE;
2987        typ_i++;
2988        break;
2989
2990      case ringorder_s:
2991        rO_Syz(j, j_bits,prev_ordsgn, tmp_ordsgn,tmp_typ[typ_i]);
2992        need_to_add_comp=TRUE;
2993        typ_i++;
2994        break;
2995
2996      case ringorder_unspec:
2997      case ringorder_no:
2998      default:
2999        dReportError("undef. ringorder used\n");
3000        break;
3001    }
3002  }
3003
3004  int j0=j; // save j
3005  int j_bits0=j_bits; // save jbits
3006  rO_Align(j,j_bits);
3007  r->CmpL_Size = j;
3008
3009  j_bits=j_bits0; j=j0;
3010
3011  // fill in some empty slots with variables not already covered
3012  // v0 is special, is therefore normally already covered
3013  // now we do have rings without comp...
3014  if((need_to_add_comp) && (v[0]== -1))
3015  {
3016    if (prev_ordsgn==1)
3017    {
3018      rO_Align(j, j_bits);
3019      rO_LexVars(j, j_bits, 0,0, prev_ordsgn,tmp_ordsgn,v,BITS_PER_LONG, -1);
3020    }
3021    else
3022    {
3023      rO_Align(j, j_bits);
3024      rO_LexVars_neg(j, j_bits, 0,0, prev_ordsgn,tmp_ordsgn,v,BITS_PER_LONG, -1);
3025    }
3026  }
3027  // the variables
3028  for(i=1 ; i<r->N+1 ; i++)
3029  {
3030    if(v[i]==(-1))
3031    {
3032      if (prev_ordsgn==1)
3033      {
3034        rO_LexVars(j, j_bits, i,i, prev_ordsgn,tmp_ordsgn,v,bits, -1);
3035      }
3036      else
3037      {
3038        rO_LexVars_neg(j,j_bits,i,i, prev_ordsgn,tmp_ordsgn,v,bits, -1);
3039      }
3040    }
3041  }
3042
3043  rO_Align(j,j_bits);
3044  // ----------------------------
3045  // finished with constructing the monomial, computing sizes:
3046
3047  r->ExpL_Size=j;
3048  r->PolyBin = omGetSpecBin(POLYSIZE + (r->ExpL_Size)*sizeof(long));
3049  assume(r->PolyBin != NULL);
3050
3051  // ----------------------------
3052  // indices and ordsgn vector for comparison
3053  //
3054  // r->pCompHighIndex already set
3055  r->ordsgn=(long *)omAlloc0(r->ExpL_Size*sizeof(long));
3056
3057  for(j=0;j<r->CmpL_Size;j++)
3058  {
3059    r->ordsgn[j] = tmp_ordsgn[j];
3060  }
3061
3062  omFreeSize((ADDRESS)tmp_ordsgn,(2*(n+r->N)*sizeof(long)));
3063
3064  // ----------------------------
3065  // description of orderings for setm:
3066  //
3067  r->OrdSize=typ_i;
3068  if (typ_i==0) r->typ=NULL;
3069  else
3070  {
3071    r->typ=(sro_ord*)omAlloc(typ_i*sizeof(sro_ord));
3072    memcpy(r->typ,tmp_typ,typ_i*sizeof(sro_ord));
3073  }
3074  omFreeSize((ADDRESS)tmp_typ,(2*(n+r->N)*sizeof(sro_ord)));
3075
3076  // ----------------------------
3077  // indices for (first copy of ) variable entries in exp.e vector (VarOffset):
3078  r->VarOffset=v;
3079
3080  // ----------------------------
3081  // other indicies
3082  r->pCompIndex=(r->VarOffset[0] & 0xffff); //r->VarOffset[0];
3083  i=0; // position
3084  j=0; // index in r->typ
3085  if (i==r->pCompIndex) i++;
3086  while ((j < r->OrdSize)
3087         && ((r->typ[j].ord_typ==ro_syzcomp) ||
3088             (r->typ[j].ord_typ==ro_syz) ||
3089             (r->order[r->typ[j].order_index] == ringorder_aa)))
3090  {
3091    i++; j++;
3092  }
3093  if (i==r->pCompIndex) i++;
3094  r->pOrdIndex=i;
3095
3096  // ----------------------------
3097  rSetDegStuff(r);
3098  rSetOption(r);
3099  // ----------------------------
3100  // r->p_Setm
3101  r->p_Setm = p_GetSetmProc(r);
3102
3103  // ----------------------------
3104  // set VarL_*
3105  rSetVarL(r);
3106
3107  //  ----------------------------
3108  // right-adjust VarOffset
3109  rRightAdjustVarOffset(r);
3110
3111  // ----------------------------
3112  // set NegWeightL*
3113  rSetNegWeight(r);
3114
3115  // ----------------------------
3116  // p_Procs: call AFTER NegWeightL
3117  r->p_Procs = (p_Procs_s*)omAlloc(sizeof(p_Procs_s));
3118  p_ProcsSet(r, r->p_Procs);
3119
3120  return FALSE;
3121}
3122
3123void rUnComplete(ring r)
3124{
3125  if (r == NULL) return;
3126  if (r->VarOffset != NULL)
3127  {
3128    if (r->PolyBin != NULL)
3129      omUnGetSpecBin(&(r->PolyBin));
3130
3131    omFreeSize((ADDRESS)r->VarOffset, (r->N +1)*sizeof(int));
3132    if (r->order != NULL)
3133    {
3134      if (r->order[0] == ringorder_s && r->typ[0].data.syz.limit > 0)
3135      {
3136        omFreeSize(r->typ[0].data.syz.syz_index,
3137             (r->typ[0].data.syz.limit +1)*sizeof(int));
3138      }
3139    }
3140    if (r->OrdSize!=0 && r->typ != NULL)
3141    {
3142      omFreeSize((ADDRESS)r->typ,r->OrdSize*sizeof(sro_ord));
3143    }
3144    if (r->ordsgn != NULL && r->CmpL_Size != 0)
3145      omFreeSize((ADDRESS)r->ordsgn,r->ExpL_Size*sizeof(long));
3146    if (r->p_Procs != NULL)
3147      omFreeSize(r->p_Procs, sizeof(p_Procs_s));
3148    omfreeSize(r->VarL_Offset, r->VarL_Size*sizeof(int));
3149  }
3150  if (r->NegWeightL_Offset!=NULL)
3151  {
3152    omFreeSize(r->NegWeightL_Offset, r->NegWeightL_Size*sizeof(int));
3153    r->NegWeightL_Offset=NULL;
3154  }
3155}
3156
3157// set r->VarL_Size, r->VarL_Offset, r->VarL_LowIndex
3158static void rSetVarL(ring r)
3159{
3160  int  min = INT_MAX, min_j = -1;
3161  int* VarL_Number = (int*) omAlloc0(r->ExpL_Size*sizeof(int));
3162
3163  int i,j;
3164
3165  // count how often a var long is occupied by an exponent
3166  for (i=1; i<=r->N; i++)
3167  {
3168    VarL_Number[r->VarOffset[i] & 0xffffff]++;
3169  }
3170
3171  // determine how many and min
3172  for (i=0, j=0; i<r->ExpL_Size; i++)
3173  {
3174    if (VarL_Number[i] != 0)
3175    {
3176      if (min > VarL_Number[i])
3177      {
3178        min = VarL_Number[i];
3179        min_j = j;
3180      }
3181      j++;
3182    }
3183  }
3184
3185  r->VarL_Size = j;
3186  r->VarL_Offset = (int*) omAlloc(r->VarL_Size*sizeof(int));
3187  r->VarL_LowIndex = 0;
3188
3189  // set VarL_Offset
3190  for (i=0, j=0; i<r->ExpL_Size; i++)
3191  {
3192    if (VarL_Number[i] != 0)
3193    {
3194      r->VarL_Offset[j] = i;
3195      if (j > 0 && r->VarL_Offset[j-1] != r->VarL_Offset[j] - 1)
3196        r->VarL_LowIndex = -1;
3197      j++;
3198    }
3199  }
3200  if (r->VarL_LowIndex >= 0)
3201    r->VarL_LowIndex = r->VarL_Offset[0];
3202
3203  r->MinExpPerLong = min;
3204  if (min_j != 0)
3205  {
3206    j = r->VarL_Offset[min_j];
3207    r->VarL_Offset[min_j] = r->VarL_Offset[0];
3208    r->VarL_Offset[0] = j;
3209  }
3210  omFree(VarL_Number);
3211}
3212
3213static void rRightAdjustVarOffset(ring r)
3214{
3215  int* shifts = (int*) omAlloc(r->ExpL_Size*sizeof(int));
3216  int i;
3217  // initialize shifts
3218  for (i=0;i<r->ExpL_Size;i++)
3219    shifts[i] = BIT_SIZEOF_LONG;
3220
3221  // find minimal bit in each long var
3222  for (i=1;i<=r->N;i++)
3223  {
3224    if (shifts[r->VarOffset[i] & 0xffffff] > r->VarOffset[i] >> 24)
3225      shifts[r->VarOffset[i] & 0xffffff] = r->VarOffset[i] >> 24;
3226  }
3227  // reset r->VarOffset
3228  for (i=1;i<=r->N;i++)
3229  {
3230    if (shifts[r->VarOffset[i] & 0xffffff] != 0)
3231      r->VarOffset[i]
3232        = (r->VarOffset[i] & 0xffffff) |
3233        (((r->VarOffset[i] >> 24) - shifts[r->VarOffset[i] & 0xffffff]) << 24);
3234  }
3235  omFree(shifts);
3236}
3237
3238// get r->divmask depending on bits per exponent
3239static unsigned long rGetDivMask(int bits)
3240{
3241  unsigned long divmask = 1;
3242  int i = bits;
3243
3244  while (i < BIT_SIZEOF_LONG)
3245  {
3246    divmask |= (((unsigned long) 1) << (unsigned long) i);
3247    i += bits;
3248  }
3249  return divmask;
3250}
3251
3252#ifdef RDEBUG
3253void rDebugPrint(ring r)
3254{
3255  if (r==NULL)
3256  {
3257    PrintS("NULL ?\n");
3258    return;
3259  }
3260  char *TYP[]={"ro_dp","ro_wp","ro_wp64","ro_wp_neg","ro_cp",
3261               "ro_syzcomp", "ro_syz", "ro_none"};
3262  int i,j;
3263
3264  Print("ExpL_Size:%d ",r->ExpL_Size);
3265  Print("CmpL_Size:%d ",r->CmpL_Size);
3266  Print("VarL_Size:%d\n",r->VarL_Size);
3267  Print("bitmask=0x%x (expbound=%d) \n",r->bitmask, r->bitmask);
3268  Print("BitsPerExp=%d ExpPerLong=%d MinExpPerLong=%d at L[%d]\n", r->BitsPerExp, r->ExpPerLong, r->MinExpPerLong, r->VarL_Offset[0]);
3269  PrintS("varoffset:\n");
3270  if (r->VarOffset==NULL) PrintS(" NULL\n");
3271  else
3272    for(j=0;j<=r->N;j++)
3273      Print("  v%d at e-pos %d, bit %d\n",
3274            j,r->VarOffset[j] & 0xffffff, r->VarOffset[j] >>24);
3275  Print("divmask=%p\n", r->divmask);
3276  PrintS("ordsgn:\n");
3277  for(j=0;j<r->CmpL_Size;j++)
3278    Print("  ordsgn %d at pos %d\n",r->ordsgn[j],j);
3279  Print("OrdSgn:%d\n",r->OrdSgn);
3280  PrintS("ordrec:\n");
3281  for(j=0;j<r->OrdSize;j++)
3282  {
3283    Print("  typ %s",TYP[r->typ[j].ord_typ]);
3284    Print("  place %d",r->typ[j].data.dp.place);
3285    if (r->typ[j].ord_typ!=ro_syzcomp)
3286    {
3287      Print("  start %d",r->typ[j].data.dp.start);
3288      Print("  end %d",r->typ[j].data.dp.end);
3289      if ((r->typ[j].ord_typ==ro_wp)
3290      || (r->typ[j].ord_typ==ro_wp_neg))
3291      {
3292        Print(" w:");
3293        int l;
3294        for(l=r->typ[j].data.wp.start;l<=r->typ[j].data.wp.end;l++)
3295          Print(" %d",r->typ[j].data.wp.weights[l-r->typ[j].data.wp.start]);
3296      }
3297      else if (r->typ[j].ord_typ==ro_wp64)
3298      {
3299        Print(" w64:");
3300        int l;
3301        for(l=r->typ[j].data.wp64.start;l<=r->typ[j].data.wp64.end;l++)
3302          Print(" %l",(long)(((int64*)r->typ[j].data.wp64.weights64)+l-r->typ[j].data.wp64.start));
3303      }
3304    }
3305    PrintLn();
3306  }
3307  Print("pOrdIndex:%d pCompIndex:%d\n", r->pOrdIndex, r->pCompIndex);
3308  Print("OrdSize:%d\n",r->OrdSize);
3309  PrintS("--------------------\n");
3310  for(j=0;j<r->ExpL_Size;j++)
3311  {
3312    Print("L[%d]: ",j);
3313    if (j< r->CmpL_Size)
3314      Print("ordsgn %d ", r->ordsgn[j]);
3315    else
3316      PrintS("no comp ");
3317    i=1;
3318    for(;i<=r->N;i++)
3319    {
3320      if( (r->VarOffset[i] & 0xffffff) == j )
3321      {  Print("v%d at e[%d], bit %d; ", i,r->VarOffset[i] & 0xffffff,
3322                                         r->VarOffset[i] >>24 ); }
3323    }
3324    if( r->pCompIndex==j ) PrintS("v0; ");
3325    for(i=0;i<r->OrdSize;i++)
3326    {
3327      if (r->typ[i].data.dp.place == j)
3328      {
3329        Print("ordrec:%s (start:%d, end:%d) ",TYP[r->typ[i].ord_typ],
3330          r->typ[i].data.dp.start, r->typ[i].data.dp.end);
3331      }
3332    }
3333
3334    if (j==r->pOrdIndex)
3335      PrintS("pOrdIndex\n");
3336    else
3337      PrintLn();
3338  }
3339
3340  // p_Procs stuff
3341  p_Procs_s proc_names;
3342  char* field;
3343  char* length;
3344  char* ord;
3345  p_Debug_GetProcNames(r, &proc_names);
3346  p_Debug_GetSpecNames(r, field, length, ord);
3347
3348  Print("p_Spec  : %s, %s, %s\n", field, length, ord);
3349  PrintS("p_Procs :\n");
3350  for (i=0; i<(int) (sizeof(p_Procs_s)/sizeof(void*)); i++)
3351  {
3352    Print(" %s,\n", ((char**) &proc_names)[i]);
3353  }
3354}
3355
3356void pDebugPrintR(poly p, const ring r)
3357{
3358  int i,j;
3359  p_Write(p,r);
3360  j=2;
3361  while(p!=NULL)
3362  {
3363    Print("\nexp[0..%d]\n",r->ExpL_Size-1);
3364    for(i=0;i<r->ExpL_Size;i++)
3365      Print("%ld ",p->exp[i]);
3366    PrintLn();
3367    Print("v0:%d ",p_GetComp(p, r));
3368    for(i=1;i<=r->N;i++) Print(" v%d:%d",i,p_GetExp(p,i, r));
3369    PrintLn();
3370    pIter(p);
3371    j--;
3372    if (j==0) { PrintS("...\n"); break; }
3373  }
3374}
3375
3376void pDebugPrint(poly p)
3377{
3378  pDebugPrintR(p, currRing);
3379}
3380#endif // RDEBUG
3381
3382
3383/*2
3384* asssume that rComplete was called with r
3385* assume that the first block ist ringorder_S
3386* change the block to reflect the sequence given by appending v
3387*/
3388
3389#ifdef PDEBUG
3390void rDBChangeSComps(int* currComponents,
3391                     long* currShiftedComponents,
3392                     int length,
3393                     ring r)
3394{
3395  r->typ[1].data.syzcomp.length = length;
3396  rNChangeSComps( currComponents, currShiftedComponents, r);
3397}
3398void rDBGetSComps(int** currComponents,
3399                 long** currShiftedComponents,
3400                 int *length,
3401                 ring r)
3402{
3403  *length = r->typ[1].data.syzcomp.length;
3404  rNGetSComps( currComponents, currShiftedComponents, r);
3405}
3406#endif
3407
3408void rNChangeSComps(int* currComponents, long* currShiftedComponents, ring r)
3409{
3410  assume(r->order[1]==ringorder_S);
3411
3412  r->typ[1].data.syzcomp.ShiftedComponents = currShiftedComponents;
3413  r->typ[1].data.syzcomp.Components = currComponents;
3414}
3415
3416void rNGetSComps(int** currComponents, long** currShiftedComponents, ring r)
3417{
3418  assume(r->order[1]==ringorder_S);
3419
3420  *currShiftedComponents = r->typ[1].data.syzcomp.ShiftedComponents;
3421  *currComponents =   r->typ[1].data.syzcomp.Components;
3422}
3423
3424/////////////////////////////////////////////////////////////////////////////
3425//
3426// The following routines all take as input a ring r, and return R
3427// where R has a certain property. R might be equal r in which case r
3428// had already this property
3429//
3430// Without argument, these functions work on currRing and change it,
3431// if necessary
3432
3433// for the time being, this is still here
3434static ring rAssure_SyzComp(ring r, BOOLEAN complete = TRUE);
3435
3436ring rCurrRingAssure_SyzComp()
3437{
3438  ring r = rAssure_SyzComp(currRing);
3439  if (r != currRing)
3440  {
3441    ring old_ring = currRing;
3442    rChangeCurrRing(r);
3443    if (old_ring->qideal != NULL)
3444    {
3445      r->qideal = idrCopyR_NoSort(old_ring->qideal, old_ring);
3446      assume(idRankFreeModule(r->qideal) == 0);
3447      currQuotient = r->qideal;
3448    }
3449  }
3450  return r;
3451}
3452
3453static ring rAssure_SyzComp(ring r, BOOLEAN complete)
3454{
3455  if (r->order[0] == ringorder_s) return r;
3456  ring res=rCopy0(r, FALSE, FALSE);
3457  int i=rBlocks(r);
3458  int j;
3459
3460  res->order=(int *)omAlloc0((i+1)*sizeof(int));
3461  for(j=i;j>0;j--) res->order[j]=r->order[j-1];
3462  res->order[0]=ringorder_s;
3463
3464  res->block0=(int *)omAlloc0((i+1)*sizeof(int));
3465  for(j=i;j>0;j--) res->block0[j]=r->block0[j-1];
3466
3467  res->block1=(int *)omAlloc0((i+1)*sizeof(int));
3468  for(j=i;j>0;j--) res->block1[j]=r->block1[j-1];
3469
3470  int ** wvhdl =(int **)omAlloc0((i+1)*sizeof(int**));
3471  for(j=i;j>0;j--)
3472  {
3473    if (r->wvhdl[j-1] != NULL)
3474    {
3475      wvhdl[j] = (int*) omMemDup(r->wvhdl[j-1]);
3476    }
3477  }
3478  res->wvhdl = wvhdl;
3479
3480  if (complete) rComplete(res, 1);
3481  return res;
3482}
3483
3484static ring rAssure_CompLastBlock(ring r, BOOLEAN complete = TRUE)
3485{
3486  int last_block = rBlocks(r) - 2;
3487  if (r->order[last_block] != ringorder_c &&
3488      r->order[last_block] != ringorder_C)
3489  {
3490    int c_pos = 0;
3491    int i;
3492
3493    for (i=0; i< last_block; i++)
3494    {
3495      if (r->order[i] == ringorder_c || r->order[i] == ringorder_C)
3496      {
3497        c_pos = i;
3498        break;
3499      }
3500    }
3501    if (c_pos != -1)
3502    {
3503      ring new_r = rCopy0(r, FALSE, TRUE);
3504      for (i=c_pos+1; i<=last_block; i++)
3505      {
3506        new_r->order[i-1] = new_r->order[i];
3507        new_r->block0[i-1] = new_r->block0[i];
3508        new_r->block1[i-1] = new_r->block1[i];
3509        new_r->wvhdl[i-1] = new_r->wvhdl[i];
3510      }
3511      new_r->order[last_block] = r->order[c_pos];
3512      new_r->block0[last_block] = r->block0[c_pos];
3513      new_r->block1[last_block] = r->block1[c_pos];
3514      new_r->wvhdl[last_block] = r->wvhdl[c_pos];
3515      if (complete) rComplete(new_r, 1);
3516      return new_r;
3517    }
3518  }
3519  return r;
3520}
3521
3522ring rCurrRingAssure_CompLastBlock()
3523{
3524  ring new_r = rAssure_CompLastBlock(currRing);
3525  if (currRing != new_r)
3526  {
3527    ring old_r = currRing;
3528    rChangeCurrRing(new_r);
3529    if (old_r->qideal != NULL)
3530    {
3531      new_r->qideal = idrCopyR(old_r->qideal, old_r);
3532      currQuotient = new_r->qideal;
3533    }
3534  }
3535  return new_r;
3536}
3537
3538ring rCurrRingAssure_SyzComp_CompLastBlock()
3539{
3540  ring new_r_1 = rAssure_CompLastBlock(currRing, FALSE);
3541  ring new_r = rAssure_SyzComp(new_r_1, FALSE);
3542
3543  if (new_r != currRing)
3544  {
3545    ring old_r = currRing;
3546    if (new_r_1 != new_r && new_r_1 != old_r) rDelete(new_r_1);
3547    rComplete(new_r, 1);
3548    rChangeCurrRing(new_r);
3549    if (old_r->qideal != NULL)
3550    {
3551      new_r->qideal = idrCopyR(old_r->qideal, old_r);
3552      currQuotient = new_r->qideal;
3553    }
3554    rTest(new_r);
3555    rTest(old_r);
3556  }
3557  return new_r;
3558}
3559
3560// use this for global orderings consisting of two blocks
3561static ring rCurrRingAssure_Global(rRingOrder_t b1, rRingOrder_t b2)
3562{
3563  int r_blocks = rBlocks(currRing);
3564  int i;
3565
3566  assume(b1 == ringorder_c || b1 == ringorder_C ||
3567         b2 == ringorder_c || b2 == ringorder_C ||
3568         b2 == ringorder_S);
3569  if ((r_blocks == 3) &&
3570      (currRing->order[0] == b1) &&
3571      (currRing->order[1] == b2) &&
3572      (currRing->order[2] == 0))
3573    return currRing;
3574  ring res = rCopy0(currRing, TRUE, FALSE);
3575  res->order = (int*)omAlloc0(3*sizeof(int));
3576  res->block0 = (int*)omAlloc0(3*sizeof(int));
3577  res->block1 = (int*)omAlloc0(3*sizeof(int));
3578  res->wvhdl = (int**)omAlloc0(3*sizeof(int*));
3579  res->order[0] = b1;
3580  res->order[1] = b2;
3581  if (b1 == ringorder_c || b1 == ringorder_C)
3582  {
3583    res->block0[1] = 1;
3584    res->block1[1] = currRing->N;
3585  }
3586  else
3587  {
3588    res->block0[0] = 1;
3589    res->block1[0] = currRing->N;
3590  }
3591  // HANNES: This sould be set in rComplete
3592  res->OrdSgn = 1;
3593  rComplete(res, 1);
3594  rChangeCurrRing(res);
3595  return res;
3596}
3597
3598
3599ring rCurrRingAssure_dp_S()
3600{
3601  return rCurrRingAssure_Global(ringorder_dp, ringorder_S);
3602}
3603
3604ring rCurrRingAssure_dp_C()
3605{
3606  return rCurrRingAssure_Global(ringorder_dp, ringorder_C);
3607}
3608
3609ring rCurrRingAssure_C_dp()
3610{
3611  return rCurrRingAssure_Global(ringorder_C, ringorder_dp);
3612}
3613
3614
3615void rSetSyzComp(int k)
3616{
3617  if (TEST_OPT_PROT) Print("{%d}", k);
3618  if ((currRing->typ!=NULL) && (currRing->typ[0].ord_typ==ro_syz))
3619  {
3620    assume(k > currRing->typ[0].data.syz.limit);
3621    int i;
3622    if (currRing->typ[0].data.syz.limit == 0)
3623    {
3624      currRing->typ[0].data.syz.syz_index = (int*) omAlloc0((k+1)*sizeof(int));
3625      currRing->typ[0].data.syz.syz_index[0] = 0;
3626      currRing->typ[0].data.syz.curr_index = 1;
3627    }
3628    else
3629    {
3630      currRing->typ[0].data.syz.syz_index = (int*)
3631        omReallocSize(currRing->typ[0].data.syz.syz_index,
3632                (currRing->typ[0].data.syz.limit+1)*sizeof(int),
3633                (k+1)*sizeof(int));
3634    }
3635    for (i=currRing->typ[0].data.syz.limit + 1; i<= k; i++)
3636    {
3637      currRing->typ[0].data.syz.syz_index[i] =
3638        currRing->typ[0].data.syz.curr_index;
3639    }
3640    currRing->typ[0].data.syz.limit = k;
3641    currRing->typ[0].data.syz.curr_index++;
3642  }
3643  else if ((currRing->order[0]!=ringorder_c) && (k!=0))
3644  {
3645    dReportError("syzcomp in incompatible ring");
3646  }
3647#ifdef PDEBUG
3648  extern int pDBsyzComp;
3649  pDBsyzComp=k;
3650#endif
3651}
3652
3653// return the max-comonent wchich has syzIndex i
3654int rGetMaxSyzComp(int i)
3655{
3656  if ((currRing->typ!=NULL) && (currRing->typ[0].ord_typ==ro_syz) &&
3657      currRing->typ[0].data.syz.limit > 0 && i > 0)
3658  {
3659    assume(i <= currRing->typ[0].data.syz.limit);
3660    int j;
3661    for (j=0; j<currRing->typ[0].data.syz.limit; j++)
3662    {
3663      if (currRing->typ[0].data.syz.syz_index[j] == i  &&
3664          currRing->typ[0].data.syz.syz_index[j+1] != i)
3665      {
3666        assume(currRing->typ[0].data.syz.syz_index[j+1] == i+1);
3667        return j;
3668      }
3669    }
3670    return currRing->typ[0].data.syz.limit;
3671  }
3672  else
3673  {
3674    return 0;
3675  }
3676}
3677
3678BOOLEAN rRing_is_Homog(ring r)
3679{
3680  if (r == NULL) return FALSE;
3681  int i, j, nb = rBlocks(r);
3682  for (i=0; i<nb; i++)
3683  {
3684    if (r->wvhdl[i] != NULL)
3685    {
3686      int length = r->block1[i] - r->block0[i];
3687      int* wvhdl = r->wvhdl[i];
3688      if (r->order[i] == ringorder_M) length *= length;
3689      assume(omSizeOfAddr(wvhdl) >= length*sizeof(int));
3690
3691      for (j=0; j< length; j++)
3692      {
3693        if (wvhdl[j] != 0 && wvhdl[j] != 1) return FALSE;
3694      }
3695    }
3696  }
3697  return TRUE;
3698}
3699
3700BOOLEAN rRing_has_CompLastBlock(ring r)
3701{
3702  assume(r != NULL);
3703  int lb = rBlocks(r) - 2;
3704  return (r->order[lb] == ringorder_c || r->order[lb] == ringorder_C);
3705}
3706
3707n_coeffType rFieldType(ring r)
3708{
3709  if (rField_is_Zp(r))     return n_Zp;
3710  if (rField_is_Q(r))      return n_Q;
3711  if (rField_is_R(r))      return n_R;
3712  if (rField_is_GF(r))     return n_GF;
3713  if (rField_is_long_R(r)) return n_long_R;
3714  if (rField_is_Zp_a(r))   return n_Zp_a;
3715  if (rField_is_Q_a(r))    return n_Q_a;
3716  if (rField_is_long_C(r)) return n_long_C;
3717  return n_unknown;
3718}
3719
3720int64 * rGetWeightVec(ring r)
3721{
3722  assume(r!=NULL);
3723  assume(r->OrdSize>0);
3724  int i=0;
3725  while((r->typ[i].ord_typ!=ro_wp64) && (r->typ[i].ord_typ>0)) i++;
3726  assume(r->typ[i].ord_typ==ro_wp64);
3727  return (int64*)(r->typ[i].data.wp64.weights64);
3728}
3729
3730void rSetWeightVec(ring r, int64 *wv)
3731{
3732  assume(r!=NULL);
3733  assume(r->OrdSize>0);
3734  assume(r->typ[0].ord_typ==ro_wp64);
3735  memcpy(r->typ[0].data.wp64.weights64,wv,r->N*sizeof(int64));
3736}
3737
3738#include <ctype.h>
3739
3740static int rRealloc1(ring r, ring src, int size, int pos)
3741{
3742  r->order=(int*)omReallocSize(r->order, size*sizeof(int), (size+1)*sizeof(int));
3743  r->block0=(int*)omReallocSize(r->block0, size*sizeof(int), (size+1)*sizeof(int));
3744  r->block1=(int*)omReallocSize(r->block1, size*sizeof(int), (size+1)*sizeof(int));
3745  r->wvhdl=(int_ptr*)omReallocSize(r->wvhdl,size*sizeof(int_ptr), (size+1)*sizeof(int_ptr));
3746  for(int k=size; k>pos; k--) r->wvhdl[k]=r->wvhdl[k-1];
3747  r->order[size]=0;
3748  size++;
3749  return size;
3750}
3751static int rReallocM1(ring r, ring src, int size, int pos)
3752{
3753  r->order=(int*)omReallocSize(r->order, size*sizeof(int), (size-1)*sizeof(int));
3754  r->block0=(int*)omReallocSize(r->block0, size*sizeof(int), (size-1)*sizeof(int));
3755  r->block1=(int*)omReallocSize(r->block1, size*sizeof(int), (size-1)*sizeof(int));
3756  r->wvhdl=(int_ptr*)omReallocSize(r->wvhdl,size*sizeof(int_ptr), (size-1)*sizeof(int_ptr));
3757  for(int k=pos+1; k<size; k++) r->wvhdl[k]=r->wvhdl[k+1];
3758  size--;
3759  return size;
3760}
3761static void rOppWeight(int *w, int l)
3762{
3763  int i2=(l+1)/2;
3764  for(int j=0; j<=i2; j++)
3765  {
3766    int t=w[j];
3767    w[j]=w[l-j];
3768    w[l-j]=t;
3769  }
3770}
3771
3772#define rOppVar(R,I) (rVar(R)+1-I)
3773
3774ring rOpposite(ring src)
3775  /* creates an opposite algebra of R */
3776  /* that is R^opp, where f (*^opp) g = g*f  */
3777  /* treats the case of qring */
3778{
3779  if (src == NULL) return(NULL);
3780  ring save = currRing;
3781  rChangeCurrRing(src);
3782  ring r = rCopy0(src,TRUE); /* TRUE for copy the qideal */
3783  /*  rChangeCurrRing(r); */
3784  // change vars v1..vN -> vN..v1
3785  int i;
3786  int i2 = (rVar(r)-1)/2;
3787  for(i=i2; i>=0; i--)
3788  {
3789    // index: 0..N-1
3790    //Print("ex var names: %d <-> %d\n",i,rOppVar(r,i));
3791    // exchange names
3792    char *p;
3793    p = r->names[rVar(r)-1-i];
3794    r->names[rVar(r)-1-i] = r->names[i];
3795    r->names[i] = p;
3796  }
3797//  i2=(rVar(r)+1)/2;
3798//  for(int i=i2; i>0; i--)
3799//  {
3800//    // index: 1..N
3801//    //Print("ex var places: %d <-> %d\n",i,rVar(r)+1-i);
3802//    // exchange VarOffset
3803//    int t;
3804//    t=r->VarOffset[i];
3805//    r->VarOffset[i]=r->VarOffset[rOppVar(r,i)];
3806//    r->VarOffset[rOppVar(r,i)]=t;
3807//  }
3808  // change names:
3809  for (i=rVar(r)-1; i>=0; i--)
3810  {
3811    char *p=r->names[i];
3812    if(isupper(*p)) *p = tolower(*p);
3813    else            *p = toupper(*p);
3814  }
3815  // change ordering: listing
3816  // change ordering: compare
3817//  for(i=0; i<r->OrdSize; i++)
3818//  {
3819//    int t,tt;
3820//    switch(r->typ[i].ord_typ)
3821//    {
3822//      case ro_dp:
3823//      //
3824//        t=r->typ[i].data.dp.start;
3825//        r->typ[i].data.dp.start=rOppVar(r,r->typ[i].data.dp.end);
3826//        r->typ[i].data.dp.end=rOppVar(r,t);
3827//        break;
3828//      case ro_wp:
3829//      case ro_wp_neg:
3830//      {
3831//        t=r->typ[i].data.wp.start;
3832//        r->typ[i].data.wp.start=rOppVar(r,r->typ[i].data.wp.end);
3833//        r->typ[i].data.wp.end=rOppVar(r,t);
3834//        // invert r->typ[i].data.wp.weights
3835//        rOppWeight(r->typ[i].data.wp.weights,
3836//                   r->typ[i].data.wp.end-r->typ[i].data.wp.start);
3837//        break;
3838//      }
3839//      //case ro_wp64:
3840//      case ro_syzcomp:
3841//      case ro_syz:
3842//         WerrorS("not implemented in rOpposite");
3843//         // should not happen
3844//         break;
3845//
3846//      case ro_cp:
3847//        t=r->typ[i].data.cp.start;
3848//        r->typ[i].data.cp.start=rOppVar(r,r->typ[i].data.cp.end);
3849//        r->typ[i].data.cp.end=rOppVar(r,t);
3850//        break;
3851//      case ro_none:
3852//      default:
3853//       Werror("unknown type in rOpposite(%d)",r->typ[i].ord_typ);
3854//       break;
3855//    }
3856//  }
3857  // Change order/block structures (needed for rPrint, rAdd etc.)
3858  int j=0;
3859  int l=rBlocks(src);
3860  for(i=0; src->order[i]!=0; i++)
3861  {
3862    switch (src->order[i])
3863    {
3864      case ringorder_c: /* c-> c */
3865      case ringorder_C: /* C-> C */
3866      case ringorder_no /*=0*/: /* end-of-block */
3867        r->order[j]=src->order[i];
3868        j++; break;
3869      case ringorder_lp: /* lp -> rp */
3870        r->order[j]=ringorder_rp;
3871        r->block0[j]=rOppVar(r, src->block1[i]);
3872        r->block1[j]=rOppVar(r, src->block0[i]);
3873        break;
3874      case ringorder_rp: /* rp -> lp */
3875        r->order[j]=ringorder_lp;
3876        r->block0[j]=rOppVar(r, src->block1[i]);
3877        r->block1[j]=rOppVar(r, src->block0[i]);
3878        break;
3879      case ringorder_dp: /* dp -> a(1..1),ls */
3880      {
3881        l=rRealloc1(r,src,l,j);
3882        r->order[j]=ringorder_a;
3883        r->block0[j]=rOppVar(r, src->block1[i]);
3884        r->block1[j]=rOppVar(r, src->block0[i]);
3885        r->wvhdl[j]=(int*)omAlloc((r->block1[j]-r->block0[j]+1)*sizeof(int));
3886        for(int k=r->block0[j]; k<=r->block1[j]; k++)
3887          r->wvhdl[j][k-r->block0[j]]=1;
3888        j++;
3889        r->order[j]=ringorder_ls;
3890        r->block0[j]=rOppVar(r, src->block1[i]);
3891        r->block1[j]=rOppVar(r, src->block0[i]);
3892        j++;
3893        break;
3894      }
3895      case ringorder_Dp: /* Dp -> a(1..1),rp */
3896      {
3897        l=rRealloc1(r,src,l,j);
3898        r->order[j]=ringorder_a;
3899        r->block0[j]=rOppVar(r, src->block1[i]);
3900        r->block1[j]=rOppVar(r, src->block0[i]);
3901        r->wvhdl[j]=(int*)omAlloc((r->block1[j]-r->block0[j]+1)*sizeof(int));
3902        for(int k=r->block0[j]; k<=r->block1[j]; k++)
3903          r->wvhdl[j][k-r->block0[j]]=1;
3904        j++;
3905        r->order[j]=ringorder_rp;
3906        r->block0[j]=rOppVar(r, src->block1[i]);
3907        r->block1[j]=rOppVar(r, src->block0[i]);
3908        j++;
3909        break;
3910      }
3911      case ringorder_wp: /* wp -> a(...),ls */
3912      {
3913        l=rRealloc1(r,src,l,j);
3914        r->order[j]=ringorder_a;
3915        r->block0[j]=rOppVar(r, src->block1[i]);
3916        r->block1[j]=rOppVar(r, src->block0[i]);
3917        r->wvhdl[j]=r->wvhdl[j+1]; r->wvhdl[j+1]=r->wvhdl[j+1]=NULL;
3918        rOppWeight(r->wvhdl[j], r->block1[j]-r->block0[j]);
3919        j++;
3920        r->order[j]=ringorder_ls;
3921        r->block0[j]=rOppVar(r, src->block1[i]);
3922        r->block1[j]=rOppVar(r, src->block0[i]);
3923        j++;
3924        break;
3925      }
3926      case ringorder_Wp: /* Wp -> a(...),rp */
3927      {
3928        l=rRealloc1(r,src,l,j);
3929        r->order[j]=ringorder_a;
3930        r->block0[j]=rOppVar(r, src->block1[i]);
3931        r->block1[j]=rOppVar(r, src->block0[i]);
3932        r->wvhdl[j]=r->wvhdl[j+1]; r->wvhdl[j+1]=r->wvhdl[j+1]=NULL;
3933        rOppWeight(r->wvhdl[j], r->block1[j]-r->block0[j]);
3934        j++;
3935        r->order[j]=ringorder_rp;
3936        r->block0[j]=rOppVar(r, src->block1[i]);
3937        r->block1[j]=rOppVar(r, src->block0[i]);
3938        j++;
3939        break;
3940      }
3941      case ringorder_M: /* M -> M */
3942      {
3943        r->order[j]=ringorder_M;
3944        r->block0[j]=rOppVar(r, src->block1[i]);
3945        r->block1[j]=rOppVar(r, src->block0[i]);
3946        int n=r->block1[j]-r->block0[j];
3947        /* M is a (n+1)x(n+1) matrix */
3948        for (int nn=0; nn<=n; nn++)
3949        {
3950          rOppWeight(&(r->wvhdl[j][nn*(n+1)]), n /*r->block1[j]-r->block0[j]*/);
3951        }
3952        j++;
3953        break;
3954      }
3955      case ringorder_a: /*  a(...),ls -> wp/dp */
3956      {
3957        r->block0[j]=rOppVar(r, src->block1[i]);
3958        r->block1[j]=rOppVar(r, src->block0[i]);
3959        rOppWeight(r->wvhdl[j], r->block1[j]-r->block0[j]);
3960        if (src->order[i+1]==ringorder_ls)
3961        {
3962          r->order[j]=ringorder_wp;
3963          i++;
3964          //l=rReallocM1(r,src,l,j);
3965        }
3966        else
3967        {
3968          r->order[j]=ringorder_a;
3969        }
3970        j++;
3971        break;
3972      }
3973      // not yet done:
3974      case ringorder_ls:
3975      case ringorder_rs:
3976      case ringorder_ds:
3977      case ringorder_Ds:
3978      case ringorder_ws:
3979      case ringorder_Ws:
3980      // should not occur:
3981      case ringorder_S:
3982      case ringorder_s:
3983      case ringorder_aa:
3984      case ringorder_L:
3985      case ringorder_unspec:
3986        Werror("order %s not (yet) supported", rSimpleOrdStr(src->order[i]));
3987        break;
3988    }
3989  }
3990  rComplete(r);
3991#ifdef RDEBUG
3992  //   rDebugPrint(r);
3993#endif
3994  rTest(r);
3995#ifdef HAVE_PLURAL
3996  /* now, we initialize a non-comm structure on r */
3997  if (!rIsPluralRing(src))
3998  {
3999    return r;
4000  }
4001  rChangeCurrRing(r);  /* we were not in r */
4002  /* basic nc constructions  */
4003  r->nc           = (nc_struct *)omAlloc0(sizeof(nc_struct));
4004  r->nc->ref      = 1; /* in spite of rCopy(src)? */
4005  r->nc->basering = r;
4006  ncRingType(r, ncRingType(src));
4007  int *perm       = (int *)omAlloc0((rVar(r)+1)*sizeof(int));
4008  int *par_perm   = NULL;
4009  nMapFunc nMap   = nSetMap(src);
4010  int ni,nj;
4011  for(i=1; i<=r->N; i++)
4012  {
4013    perm[i] = rOppVar(r,i);
4014  }
4015  matrix C = mpNew(rVar(r),rVar(r));
4016  matrix D = mpNew(rVar(r),rVar(r));
4017  r->nc->C = C;
4018  r->nc->D = D;
4019  if (nc_InitMultiplication(r))
4020    WarnS("Error initializing multiplication!");
4021  for (i=1; i< rVar(r); i++)
4022  {
4023    for (j=i+1; j<=rVar(r); j++)
4024    {
4025      ni = r->N +1 - i;
4026      nj = r->N +1 - j; /* i<j ==>   nj < ni */
4027      MATELEM(C,nj,ni) = pPermPoly(MATELEM(src->nc->C,i,j),perm,src,nMap,par_perm,src->P);
4028      MATELEM(D,nj,ni) = pPermPoly(MATELEM(src->nc->D,i,j),perm,src,nMap,par_perm,src->P);
4029    }
4030  }
4031  idTest((ideal)C);
4032  idTest((ideal)D);
4033  if (nc_InitMultiplication(r))
4034    WarnS("Error initializing multiplication!");
4035  r->nc->IsSkewConstant =   src->nc->IsSkewConstant;
4036  omFreeSize((ADDRESS)perm,(rVar(r)+1)*sizeof(int));
4037  /* now oppose the qideal for qrings */
4038  if (src->qideal != NULL)
4039  {
4040    idDelete(&(r->qideal));
4041    r->qideal = idOppose(src, src->qideal);
4042  }
4043  rTest(r);
4044  rChangeCurrRing(save);
4045#endif /* HAVE_PLURAL */
4046  return r;
4047}
4048
4049ring rEnvelope(ring R)
4050  /* creates an enveloping algebra of R */
4051  /* that is R^e = R \tensor_K R^opp */
4052{
4053  ring Ropp = rOpposite(R);
4054  ring Renv = NULL;
4055  int stat = rSum(R, Ropp, Renv); /* takes care of qideals */
4056  if ( stat <=0 )
4057    WarnS("Error in rEnvelope at rSum");
4058  rTest(Renv);
4059  return Renv;
4060}
4061#ifdef HAVE_PLURAL
4062BOOLEAN nc_rComplete(ring src, ring dest)
4063/* returns TRUE is there were errors */
4064/* dest is actualy equals src with the different ordering */
4065/* we map src->nc correctly to dest->src */
4066/* to be executed after rComplete, before rChangeCurrRing */
4067
4068{
4069  if (!rIsPluralRing(src))
4070    return FALSE;
4071  int i,j;
4072  int N = dest->N;
4073  if (src->N != N)
4074  {
4075    /* should not happen */
4076    WarnS("wrong nc_rComplete call");
4077    return TRUE;
4078  }
4079  ring save = currRing;
4080  int WeChangeRing = 0;
4081  if (dest != currRing)
4082  {
4083    WeChangeRing = 1;
4084    rChangeCurrRing(dest);
4085  }
4086  matrix C = mpNew(N,N);
4087  matrix D = mpNew(N,N);
4088  matrix C0 = src->nc->C;
4089  matrix D0 = src->nc->D;
4090  poly p = NULL;
4091  number n = NULL;
4092  for (i=1; i< N; i++)
4093  {
4094    for (j= i+1; j<= N; j++)
4095    {
4096      n = n_Copy(p_GetCoeff(MATELEM(C0,i,j), src),src);
4097      p = p_ISet(1,dest);
4098      p_SetCoeff(p,n,dest);
4099      MATELEM(C,i,j) = p;
4100      p = NULL;
4101      if (MATELEM(D0,i,j) != NULL)
4102      {
4103        p = prCopyR(MATELEM(D0,i,j), src->nc->basering, dest);
4104        MATELEM(D,i,j) = nc_p_CopyPut(p, dest);
4105        p_Delete(&p, dest);
4106        p = NULL;
4107      }
4108    }
4109  }
4110  /* One must test C and D _only_ in r->nc->basering!!! not in r!!! */
4111  //  idTest((ideal)C);
4112  //  idTest((ideal)D);
4113  id_Delete((ideal *)&(dest->nc->C),dest->nc->basering);
4114  id_Delete((ideal *)&(dest->nc->D),dest->nc->basering);
4115  dest->nc->C = C;
4116  dest->nc->D = D;
4117  if ( WeChangeRing )
4118    rChangeCurrRing(save);
4119  if (nc_InitMultiplication(dest))
4120  {
4121    WarnS("Error initializing multiplication!");
4122    return TRUE;
4123  }
4124  return FALSE;
4125}
4126#endif
4127
4128void rModify_a_to_A(ring r)
4129// to be called BEFORE rComplete:
4130// changes every Block with a(...) to A(...)
4131{
4132   int i=0;
4133   int j;
4134   while(r->order[i]!=0)
4135   {
4136      if (r->order[i]==ringorder_a)
4137      {
4138        r->order[i]=ringorder_a64;
4139        int *w=r->wvhdl[i];
4140        int64 *w64=(int64 *)omAlloc((r->block1[i]-r->block0[i]+1)*sizeof(int64));
4141        for(j=r->block1[i]-r->block0[i];j>=0;j--)
4142                w64[j]=(int64)w[j];
4143        r->wvhdl[i]=(int*)w64;
4144        omFreeSize(w,(r->block1[i]-r->block0[i]+1)*sizeof(int));
4145      }
4146      i++;
4147   }
4148}
Note: See TracBrowser for help on using the repository browser.