source: git/kernel/ring.cc @ 899741

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