source: git/kernel/ring.cc @ 9d34f0

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