source: git/kernel/ring.cc @ 71effd8

spielwiese
Last change on this file since 71effd8 was 71effd8, checked in by Hans Schönemann <hannes@…>, 15 years ago
*hannes: ringflaga git-svn-id: file:///usr/local/Singular/svn/trunk@11285 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 119.7 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: ring.cc,v 1.108 2009-01-07 14:51:11 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  res->idroot=NULL; /* local objects */
1469  //int*       order;  /* array of orderings */
1470  //int*       block0; /* starting pos.*/
1471  //int*       block1; /* ending pos.*/
1472  //char**     parameter; /* names of parameters */
1473  //number     minpoly;
1474  //ideal      minideal;
1475  //int**      wvhdl;  /* array of weight vectors */
1476  //char **    names;  /* array of variable names */
1477
1478  res->options=r->options; /* ring dependent options */
1479
1480  // what follows below here should be set by rComplete, _only_
1481  //long      *ordsgn;  /* array of +/- 1 (or 0) for comparing monomials */
1482                       /*  ExpL_Size entries*/
1483
1484  // is NULL for lp or N == 1, otherwise non-NULL (with OrdSize > 0 entries) */
1485  //sro_ord*   typ;   /* array of orderings + sizes, OrdSize entries */
1486
1487  //ideal      qideal; /* extension to the ring structure: qring */
1488
1489
1490  //int*     VarOffset;
1491  //int*     firstwv;
1492
1493  //struct omBin_s*   PolyBin; /* Bin from where monoms are allocated */
1494  res->ch=r->ch;     /* characteristic */
1495#ifdef HAVE_RINGS
1496  res->ringtype=r->ringtype;  /* cring = 0 => coefficient field, cring = 1 => coeffs from Z/2^m */
1497  if (r->ringflaga!=NULL) 
1498  {
1499    res->ringflaga = (int_number) omAlloc(sizeof(MP_INT));
1500    mpz_set(res->ringflaga,r->ringflaga);
1501  }
1502  res->ringflagb=r->ringflagb;
1503#endif
1504  res->ref=0; /* reference counter to the ring */
1505
1506  res->float_len=r->float_len; /* additional char-flags */
1507  res->float_len2=r->float_len2; /* additional char-flags */
1508
1509  res->N=r->N;      /* number of vars */
1510
1511  res->P=r->P;      /* number of pars */
1512  res->OrdSgn=r->OrdSgn; /* 1 for polynomial rings, -1 otherwise */
1513
1514  res->firstBlockEnds=r->firstBlockEnds;
1515#ifdef HAVE_PLURAL
1516  res->real_var_start=r->real_var_start;
1517  res->real_var_end=r->real_var_end;
1518#endif
1519
1520#ifdef HAVE_SHIFTBBA
1521  res->isLPring=r->isLPring; /* 0 for non-letterplace rings, otherwise the number of LP blocks, at least 1, known also as lV */
1522#endif
1523
1524  res->VectorOut=r->VectorOut;
1525  res->ShortOut=r->ShortOut;
1526  res->CanShortOut=r->CanShortOut;
1527  res->LexOrder=r->LexOrder; // TRUE if the monomial ordering has polynomial and power series blocks
1528  res->MixedOrder=r->MixedOrder; // ?? 1 for lex ordering (except ls), -1 otherwise
1529  res->ComponentOrder=r->ComponentOrder;
1530
1531
1532  // what follows below here should be set by rComplete, _only_
1533  // contains component, but no weight fields in E */
1534  //short      ExpL_Size; // size of exponent vector in long
1535  //short      CmpL_Size; // portions which need to be compared
1536  /* number of long vars in exp vector:
1537     long vars are those longs in the exponent vector which are
1538     occupied by variables, only */
1539  //short     VarL_Size;
1540
1541  //short      BitsPerExp; /* number of bits per exponent */
1542  //short      ExpPerLong; /* maximal number of Exponents per long */
1543
1544  //short      pCompIndex; /* p->exp.e[pCompIndex] is the component */
1545  //short      pOrdIndex; /* p->exp[pOrdIndex] is pGetOrd(p) */
1546
1547  //short      OrdSize; /* size of ord vector (in sro_ord) */
1548
1549
1550  /* if >= 0, long vars in exp vector are consecutive and start there
1551     if <  0, long vars in exp vector are not consecutive */
1552  //short     VarL_LowIndex;
1553  // number of exponents in r->VarL_Offset[0]
1554  // is minimal number of exponents in a long var
1555  //short     MinExpPerLong;
1556
1557  /* if this is > 0, then NegWeightL_Offset[0..size_1] is index of longs in
1558   ExpVector whose values need an offset due to negative weights */
1559  //short     NegWeightL_Size;
1560  /* array of NegWeigtL_Size indicies */
1561  //int*      NegWeightL_Offset;
1562
1563  /* array of size VarL_Size,
1564     VarL_Offset[i] gets i-th long var in exp vector */
1565  //int*      VarL_Offset;
1566
1567  /* mask for getting single exponents */
1568  //unsigned long bitmask;
1569  /* mask used for divisiblity tests */
1570  //unsigned long divmask;
1571
1572  //p_Procs_s*    p_Procs;
1573
1574  /* FDeg and LDeg */
1575  //pFDegProc     pFDeg;
1576  //pLDegProc     pLDeg;
1577
1578  /* as it was determined by rComplete */
1579  //pFDegProc     pFDegOrig;
1580  /* and as it was determined before rOptimizeLDeg */
1581  //pLDegProc     pLDegOrig;
1582
1583  //p_SetmProc    p_Setm;
1584  //n_Procs_s*    cf;
1585  //ring          algring;
1586#ifdef HAVE_PLURAL
1587  //  nc_struct*    _nc; // private
1588#endif
1589
1590  memcpy4(res,r,sizeof(ip_sring));
1591  res->NegWeightL_Offset=NULL;
1592  res->VarL_Offset=NULL;
1593  res->ordsgn=NULL;
1594  res->typ=NULL;
1595  res->order=NULL;
1596  res->VarOffset = NULL;
1597  res->p_Procs=NULL;
1598  res->cf=NULL;
1599  res->PolyBin=NULL;
1600  res->ref=0;
1601  if (r->algring!=NULL)
1602    r->algring->ref++;
1603  if (r->parameter!=NULL)
1604  {
1605    res->minpoly=nCopy(r->minpoly);
1606    int l=rPar(r);
1607    res->parameter=(char **)omAlloc(l*sizeof(char_ptr));
1608    int i;
1609    for(i=0;i<rPar(r);i++)
1610    {
1611      res->parameter[i]=omStrDup(r->parameter[i]);
1612    }
1613    if (r->minideal!=NULL)
1614    {
1615      res->minideal=id_Copy(r->minideal,r->algring);
1616    }
1617  }
1618  if (copy_ordering == TRUE)
1619  {
1620    i=rBlocks(r);
1621    res->wvhdl   = (int **)omAlloc(i * sizeof(int_ptr));
1622    res->order   = (int *) omAlloc(i * sizeof(int));
1623    res->block0  = (int *) omAlloc(i * sizeof(int));
1624    res->block1  = (int *) omAlloc(i * sizeof(int));
1625    for (j=0; j<i; j++)
1626    {
1627      if (r->wvhdl[j]!=NULL)
1628      {
1629        res->wvhdl[j] = (int*) omMemDup(r->wvhdl[j]);
1630      }
1631      else
1632        res->wvhdl[j]=NULL;
1633    }
1634    memcpy4(res->order,r->order,i * sizeof(int));
1635    memcpy4(res->block0,r->block0,i * sizeof(int));
1636    memcpy4(res->block1,r->block1,i * sizeof(int));
1637  }
1638  else
1639  {
1640    res->wvhdl = NULL;
1641    res->order = NULL;
1642    res->block0 = NULL;
1643    res->block1 = NULL;
1644  }
1645
1646  res->names   = (char **)omAlloc0(rVar(r) * sizeof(char_ptr));
1647  for (i=0; i<rVar(res); i++)
1648  {
1649    res->names[i] = omStrDup(r->names[i]);
1650  }
1651  res->idroot = NULL;
1652  if (r->qideal!=NULL)
1653  {
1654    if (copy_qideal) res->qideal= idrCopyR_NoSort(r->qideal, r);
1655    else res->qideal = NULL;
1656  }
1657#ifdef HAVE_PLURAL
1658  res->GetNC() = NULL; // copy is purely commutative!!!
1659//  if (rIsPluralRing(r))
1660//    nc_rCopy0(res, r); // is this correct??? imho: no!
1661#endif
1662  return res;
1663}
1664
1665/*2
1666 * create a copy of the ring r, which must be equivalent to currRing
1667 * used for qring definition,..
1668 * (i.e.: normal rings: same nCopy as currRing;
1669 *        qring:        same nCopy, same idCopy as currRing)
1670 */
1671ring rCopy(ring r)
1672{
1673  if (r == NULL) return NULL;
1674  ring res=rCopy0(r);
1675  rComplete(res, 1); // res is purely commutative so far
1676
1677#ifdef HAVE_PLURAL
1678  if (rIsPluralRing(r))
1679    if( nc_rCopy(res, r, true) );
1680#endif
1681
1682  return res;
1683}
1684
1685// returns TRUE, if r1 equals r2 FALSE, otherwise Equality is
1686// determined componentwise, if qr == 1, then qrideal equality is
1687// tested, as well
1688BOOLEAN rEqual(ring r1, ring r2, BOOLEAN qr)
1689{
1690  int i, j;
1691
1692  if (r1 == r2) return TRUE;
1693
1694  if (r1 == NULL || r2 == NULL) return FALSE;
1695
1696  if ((rInternalChar(r1) != rInternalChar(r2))
1697  || (r1->float_len != r2->float_len)
1698  || (r1->float_len2 != r2->float_len2)
1699  || (rVar(r1) != rVar(r2))
1700  || (r1->OrdSgn != r2->OrdSgn)
1701  || (rPar(r1) != rPar(r2)))
1702    return FALSE;
1703
1704  for (i=0; i<rVar(r1); i++)
1705  {
1706    if (r1->names[i] != NULL && r2->names[i] != NULL)
1707    {
1708      if (strcmp(r1->names[i], r2->names[i])) return FALSE;
1709    }
1710    else if ((r1->names[i] != NULL) ^ (r2->names[i] != NULL))
1711    {
1712      return FALSE;
1713    }
1714  }
1715
1716  i=0;
1717  while (r1->order[i] != 0)
1718  {
1719    if (r2->order[i] == 0) return FALSE;
1720    if ((r1->order[i] != r2->order[i])
1721    || (r1->block0[i] != r2->block0[i])
1722    || (r1->block1[i] != r2->block1[i]))
1723      return FALSE;
1724    if (r1->wvhdl[i] != NULL)
1725    {
1726      if (r2->wvhdl[i] == NULL)
1727        return FALSE;
1728      for (j=0; j<r1->block1[i]-r1->block0[i]+1; j++)
1729        if (r2->wvhdl[i][j] != r1->wvhdl[i][j])
1730          return FALSE;
1731    }
1732    else if (r2->wvhdl[i] != NULL) return FALSE;
1733    i++;
1734  }
1735  if (r2->order[i] != 0) return FALSE;
1736
1737  for (i=0; i<rPar(r1);i++)
1738  {
1739      if (strcmp(r1->parameter[i], r2->parameter[i])!=0)
1740        return FALSE;
1741  }
1742
1743  if (r1->minpoly != NULL)
1744  {
1745    if (r2->minpoly == NULL) return FALSE;
1746    if (currRing == r1 || currRing == r2)
1747    {
1748      if (! nEqual(r1->minpoly, r2->minpoly)) return FALSE;
1749    }
1750  }
1751  else if (r2->minpoly != NULL) return FALSE;
1752
1753  if (qr)
1754  {
1755    if (r1->qideal != NULL)
1756    {
1757      ideal id1 = r1->qideal, id2 = r2->qideal;
1758      int i, n;
1759      poly *m1, *m2;
1760
1761      if (id2 == NULL) return FALSE;
1762      if ((n = IDELEMS(id1)) != IDELEMS(id2)) return FALSE;
1763
1764      if (currRing == r1 || currRing == r2)
1765      {
1766        m1 = id1->m;
1767        m2 = id2->m;
1768        for (i=0; i<n; i++)
1769          if (! pEqualPolys(m1[i],m2[i])) return FALSE;
1770      }
1771    }
1772    else if (r2->qideal != NULL) return FALSE;
1773  }
1774
1775  return TRUE;
1776}
1777
1778// returns TRUE, if r1 and r2 represents the monomials in the same way
1779// FALSE, otherwise
1780// this is an analogue to rEqual but not so strict
1781BOOLEAN rSamePolyRep(ring r1, ring r2)
1782{
1783  int i, j;
1784
1785  if (r1 == r2) return TRUE;
1786
1787  if (r1 == NULL || r2 == NULL) return FALSE;
1788
1789  if ((rInternalChar(r1) != rInternalChar(r2))
1790  || (r1->float_len != r2->float_len)
1791  || (r1->float_len2 != r2->float_len2)
1792  || (rVar(r1) != rVar(r2))
1793  || (r1->OrdSgn != r2->OrdSgn)
1794  || (rPar(r1) != rPar(r2)))
1795    return FALSE;
1796
1797  if (rVar(r1)!=rVar(r2)) return FALSE;
1798  if (rPar(r1)!=rPar(r2)) return FALSE;
1799
1800  i=0;
1801  while (r1->order[i] != 0)
1802  {
1803    if (r2->order[i] == 0) return FALSE;
1804    if ((r1->order[i] != r2->order[i])
1805    || (r1->block0[i] != r2->block0[i])
1806    || (r1->block1[i] != r2->block1[i]))
1807      return FALSE;
1808    if (r1->wvhdl[i] != NULL)
1809    {
1810      if (r2->wvhdl[i] == NULL)
1811        return FALSE;
1812      for (j=0; j<r1->block1[i]-r1->block0[i]+1; j++)
1813        if (r2->wvhdl[i][j] != r1->wvhdl[i][j])
1814          return FALSE;
1815    }
1816    else if (r2->wvhdl[i] != NULL) return FALSE;
1817    i++;
1818  }
1819  if (r2->order[i] != 0) return FALSE;
1820
1821  // we do not check minpoly
1822  // we do not check qideal
1823
1824  return TRUE;
1825}
1826
1827rOrderType_t rGetOrderType(ring r)
1828{
1829  // check for simple ordering
1830  if (rHasSimpleOrder(r))
1831  {
1832    if ((r->order[1] == ringorder_c)
1833    || (r->order[1] == ringorder_C))
1834    {
1835      switch(r->order[0])
1836      {
1837          case ringorder_dp:
1838          case ringorder_wp:
1839          case ringorder_ds:
1840          case ringorder_ws:
1841          case ringorder_ls:
1842          case ringorder_unspec:
1843            if (r->order[1] == ringorder_C
1844            ||  r->order[0] == ringorder_unspec)
1845              return rOrderType_ExpComp;
1846            return rOrderType_Exp;
1847
1848          default:
1849            assume(r->order[0] == ringorder_lp ||
1850                   r->order[0] == ringorder_rs ||
1851                   r->order[0] == ringorder_Dp ||
1852                   r->order[0] == ringorder_Wp ||
1853                   r->order[0] == ringorder_Ds ||
1854                   r->order[0] == ringorder_Ws);
1855
1856            if (r->order[1] == ringorder_c) return rOrderType_ExpComp;
1857            return rOrderType_Exp;
1858      }
1859    }
1860    else
1861    {
1862      assume((r->order[0]==ringorder_c)||(r->order[0]==ringorder_C));
1863      return rOrderType_CompExp;
1864    }
1865  }
1866  else
1867    return rOrderType_General;
1868}
1869
1870BOOLEAN rHasSimpleOrder(const ring r)
1871{
1872  if (r->order[0] == ringorder_unspec) return TRUE;
1873  int blocks = rBlocks(r) - 1;
1874  assume(blocks >= 1);
1875  if (blocks == 1) return TRUE;
1876  if (blocks > 2)  return FALSE;
1877  if ((r->order[0] != ringorder_c)
1878  && (r->order[0] != ringorder_C)
1879  && (r->order[1] != ringorder_c)
1880  && (r->order[1] != ringorder_C))
1881    return FALSE;
1882  if ((r->order[1] == ringorder_M)
1883  || (r->order[0] == ringorder_M))
1884    return FALSE;
1885  return TRUE;
1886}
1887
1888// returns TRUE, if simple lp or ls ordering
1889BOOLEAN rHasSimpleLexOrder(const ring r)
1890{
1891  return rHasSimpleOrder(r) &&
1892    (r->order[0] == ringorder_ls ||
1893     r->order[0] == ringorder_lp ||
1894     r->order[1] == ringorder_ls ||
1895     r->order[1] == ringorder_lp);
1896}
1897
1898BOOLEAN rOrder_is_DegOrdering(const rRingOrder_t order)
1899{
1900  switch(order)
1901  {
1902      case ringorder_dp:
1903      case ringorder_Dp:
1904      case ringorder_ds:
1905      case ringorder_Ds:
1906      case ringorder_Ws:
1907      case ringorder_Wp:
1908      case ringorder_ws:
1909      case ringorder_wp:
1910        return TRUE;
1911
1912      default:
1913        return FALSE;
1914  }
1915}
1916
1917BOOLEAN rOrder_is_WeightedOrdering(rRingOrder_t order)
1918{
1919  switch(order)
1920  {
1921      case ringorder_Ws:
1922      case ringorder_Wp:
1923      case ringorder_ws:
1924      case ringorder_wp:
1925        return TRUE;
1926
1927      default:
1928        return FALSE;
1929  }
1930}
1931
1932BOOLEAN rHasSimpleOrderAA(ring r)
1933{
1934  int blocks = rBlocks(r) - 1;
1935  if ((blocks > 3) || (blocks < 2)) return FALSE;
1936  if (blocks == 3)
1937  {
1938    return (((r->order[0] == ringorder_aa) && (r->order[1] != ringorder_M) &&
1939             ((r->order[2] == ringorder_c) || (r->order[2] == ringorder_C))) ||
1940            (((r->order[0] == ringorder_c) || (r->order[0] == ringorder_C)) &&
1941             (r->order[1] == ringorder_aa) && (r->order[2] != ringorder_M)));
1942  }
1943  else
1944  {
1945    return ((r->order[0] == ringorder_aa) && (r->order[1] != ringorder_M));
1946  }
1947}
1948
1949// return TRUE if p_SetComp requires p_Setm
1950BOOLEAN rOrd_SetCompRequiresSetm(ring r)
1951{
1952  if (r->typ != NULL)
1953  {
1954    int pos;
1955    for (pos=0;pos<r->OrdSize;pos++)
1956    {
1957      sro_ord* o=&(r->typ[pos]);
1958      if ((o->ord_typ == ro_syzcomp) || (o->ord_typ == ro_syz)) return TRUE;
1959    }
1960  }
1961  return FALSE;
1962}
1963
1964// return TRUE if p->exp[r->pOrdIndex] holds total degree of p */
1965BOOLEAN rOrd_is_Totaldegree_Ordering(ring r)
1966{
1967  // Hmm.... what about Syz orderings?
1968  return (rVar(r) > 1 &&
1969          ((rHasSimpleOrder(r) &&
1970           (rOrder_is_DegOrdering((rRingOrder_t)r->order[0]) ||
1971            rOrder_is_DegOrdering(( rRingOrder_t)r->order[1]))) ||
1972           (rHasSimpleOrderAA(r) &&
1973            (rOrder_is_DegOrdering((rRingOrder_t)r->order[1]) ||
1974             rOrder_is_DegOrdering((rRingOrder_t)r->order[2])))));
1975}
1976
1977// return TRUE if p->exp[r->pOrdIndex] holds a weighted degree of p */
1978BOOLEAN rOrd_is_WeightedDegree_Ordering(ring r =currRing)
1979{
1980  // Hmm.... what about Syz orderings?
1981  return ((rVar(r) > 1) &&
1982          rHasSimpleOrder(r) &&
1983          (rOrder_is_WeightedOrdering((rRingOrder_t)r->order[0]) ||
1984           rOrder_is_WeightedOrdering(( rRingOrder_t)r->order[1])));
1985}
1986
1987BOOLEAN rIsPolyVar(int v, ring r)
1988{
1989  int  i=0;
1990  while(r->order[i]!=0)
1991  {
1992    if((r->block0[i]<=v)
1993    && (r->block1[i]>=v))
1994    {
1995      switch(r->order[i])
1996      {
1997        case ringorder_a:
1998          return (r->wvhdl[i][v-r->block0[i]]>0);
1999        case ringorder_M:
2000          return 2; /*don't know*/
2001        case ringorder_a64: /* assume: all weight are non-negative!*/
2002        case ringorder_lp:
2003        case ringorder_rs:
2004        case ringorder_dp:
2005        case ringorder_Dp:
2006        case ringorder_wp:
2007        case ringorder_Wp:
2008          return TRUE;
2009        case ringorder_ls:
2010        case ringorder_ds:
2011        case ringorder_Ds:
2012        case ringorder_ws:
2013        case ringorder_Ws:
2014          return FALSE;
2015        default:
2016          break;
2017      }
2018    }
2019    i++;
2020  }
2021  return 3; /* could not find var v*/
2022}
2023
2024#ifdef RDEBUG
2025// This should eventually become a full-fledge ring check, like pTest
2026BOOLEAN rDBTest(ring r, const char* fn, const int l)
2027{
2028  int i,j;
2029
2030  if (r == NULL)
2031  {
2032    dReportError("Null ring in %s:%d", fn, l);
2033    return FALSE;
2034  }
2035
2036
2037  if (r->N == 0) return TRUE;
2038
2039//  omCheckAddrSize(r,sizeof(ip_sring));
2040#if OM_CHECK > 0
2041  i=rBlocks(r);
2042  omCheckAddrSize(r->order,i*sizeof(int));
2043  omCheckAddrSize(r->block0,i*sizeof(int));
2044  omCheckAddrSize(r->block1,i*sizeof(int));
2045  omCheckAddrSize(r->wvhdl,i*sizeof(int *));
2046  for (j=0;j<i; j++)
2047  {
2048    if (r->wvhdl[j] != NULL) omCheckAddr(r->wvhdl[j]);
2049  }
2050#endif
2051  if (r->VarOffset == NULL)
2052  {
2053    dReportError("Null ring VarOffset -- no rComplete (?) in n %s:%d", fn, l);
2054    return FALSE;
2055  }
2056  omCheckAddrSize(r->VarOffset,(r->N+1)*sizeof(int));
2057
2058  if ((r->OrdSize==0)!=(r->typ==NULL))
2059  {
2060    dReportError("mismatch OrdSize and typ-pointer in %s:%d");
2061    return FALSE;
2062  }
2063  omcheckAddrSize(r->typ,r->OrdSize*sizeof(*(r->typ)));
2064  omCheckAddrSize(r->VarOffset,(r->N+1)*sizeof(*(r->VarOffset)));
2065  // test assumptions:
2066  for(i=0;i<=r->N;i++)
2067  {
2068    if(r->typ!=NULL)
2069    {
2070      for(j=0;j<r->OrdSize;j++)
2071      {
2072        if (r->typ[j].ord_typ==ro_cp)
2073        {
2074          if(((short)r->VarOffset[i]) == r->typ[j].data.cp.place)
2075            dReportError("ordrec %d conflicts with var %d",j,i);
2076        }
2077        else
2078          if ((r->typ[j].ord_typ!=ro_syzcomp)
2079          && (r->VarOffset[i] == r->typ[j].data.dp.place))
2080            dReportError("ordrec %d conflicts with var %d",j,i);
2081      }
2082    }
2083    int tmp;
2084      tmp=r->VarOffset[i] & 0xffffff;
2085      #if SIZEOF_LONG == 8
2086        if ((r->VarOffset[i] >> 24) >63)
2087      #else
2088        if ((r->VarOffset[i] >> 24) >31)
2089      #endif
2090          dReportError("bit_start out of range:%d",r->VarOffset[i] >> 24);
2091      if (i > 0 && ((tmp<0) ||(tmp>r->ExpL_Size-1)))
2092      {
2093        dReportError("varoffset out of range for var %d: %d",i,tmp);
2094      }
2095  }
2096  if(r->typ!=NULL)
2097  {
2098    for(j=0;j<r->OrdSize;j++)
2099    {
2100      if ((r->typ[j].ord_typ==ro_dp)
2101      || (r->typ[j].ord_typ==ro_wp)
2102      || (r->typ[j].ord_typ==ro_wp_neg))
2103      {
2104        if (r->typ[j].data.dp.start > r->typ[j].data.dp.end)
2105          dReportError("in ordrec %d: start(%d) > end(%d)",j,
2106            r->typ[j].data.dp.start, r->typ[j].data.dp.end);
2107        if ((r->typ[j].data.dp.start < 1)
2108        || (r->typ[j].data.dp.end > r->N))
2109          dReportError("in ordrec %d: start(%d)<1 or end(%d)>vars(%d)",j,
2110            r->typ[j].data.dp.start, r->typ[j].data.dp.end,r->N);
2111      }
2112    }
2113  }
2114  if (r->minpoly!=NULL)
2115  {
2116    omCheckAddr(r->minpoly);
2117  }
2118  //assume(r->cf!=NULL);
2119
2120  return TRUE;
2121}
2122#endif
2123
2124static void rO_Align(int &place, int &bitplace)
2125{
2126  // increment place to the next aligned one
2127  // (count as Exponent_t,align as longs)
2128  if (bitplace!=BITS_PER_LONG)
2129  {
2130    place++;
2131    bitplace=BITS_PER_LONG;
2132  }
2133}
2134
2135static void rO_TDegree(int &place, int &bitplace, int start, int end,
2136    long *o, sro_ord &ord_struct)
2137{
2138  // degree (aligned) of variables v_start..v_end, ordsgn 1
2139  rO_Align(place,bitplace);
2140  ord_struct.ord_typ=ro_dp;
2141  ord_struct.data.dp.start=start;
2142  ord_struct.data.dp.end=end;
2143  ord_struct.data.dp.place=place;
2144  o[place]=1;
2145  place++;
2146  rO_Align(place,bitplace);
2147}
2148
2149static void rO_TDegree_neg(int &place, int &bitplace, int start, int end,
2150    long *o, sro_ord &ord_struct)
2151{
2152  // degree (aligned) of variables v_start..v_end, ordsgn -1
2153  rO_Align(place,bitplace);
2154  ord_struct.ord_typ=ro_dp;
2155  ord_struct.data.dp.start=start;
2156  ord_struct.data.dp.end=end;
2157  ord_struct.data.dp.place=place;
2158  o[place]=-1;
2159  place++;
2160  rO_Align(place,bitplace);
2161}
2162
2163static void rO_WDegree(int &place, int &bitplace, int start, int end,
2164    long *o, sro_ord &ord_struct, int *weights)
2165{
2166  // weighted degree (aligned) of variables v_start..v_end, ordsgn 1
2167  while((start<end) && (weights[0]==0)) { start++; weights++; }
2168  while((start<end) && (weights[end-start]==0)) { end--; }
2169  int i;
2170  int pure_tdeg=1;
2171  for(i=start;i<=end;i++)
2172  {
2173    if(weights[i-start]!=1)
2174    {
2175      pure_tdeg=0;
2176      break;
2177    }
2178  }
2179  if (pure_tdeg)
2180  {
2181    rO_TDegree(place,bitplace,start,end,o,ord_struct);
2182    return;
2183  }
2184  rO_Align(place,bitplace);
2185  ord_struct.ord_typ=ro_wp;
2186  ord_struct.data.wp.start=start;
2187  ord_struct.data.wp.end=end;
2188  ord_struct.data.wp.place=place;
2189  ord_struct.data.wp.weights=weights;
2190  o[place]=1;
2191  place++;
2192  rO_Align(place,bitplace);
2193  for(i=start;i<=end;i++)
2194  {
2195    if(weights[i-start]<0)
2196    {
2197      ord_struct.ord_typ=ro_wp_neg;
2198      break;
2199    }
2200  }
2201}
2202
2203static void rO_WDegree64(int &place, int &bitplace, int start, int end,
2204    long *o, sro_ord &ord_struct, int64 *weights)
2205{
2206  // weighted degree (aligned) of variables v_start..v_end, ordsgn 1,
2207  // reserved 2 places
2208  rO_Align(place,bitplace);
2209  ord_struct.ord_typ=ro_wp64;
2210  ord_struct.data.wp64.start=start;
2211  ord_struct.data.wp64.end=end;
2212  ord_struct.data.wp64.place=place;
2213  ord_struct.data.wp64.weights64=weights;
2214  o[place]=1;
2215  place++;
2216  o[place]=1;
2217  place++;
2218  rO_Align(place,bitplace);
2219  int i;
2220}
2221
2222static void rO_WDegree_neg(int &place, int &bitplace, int start, int end,
2223    long *o, sro_ord &ord_struct, int *weights)
2224{
2225  // weighted degree (aligned) of variables v_start..v_end, ordsgn -1
2226  while((start<end) && (weights[0]==0)) { start++; weights++; }
2227  while((start<end) && (weights[end-start]==0)) { end--; }
2228  rO_Align(place,bitplace);
2229  ord_struct.ord_typ=ro_wp;
2230  ord_struct.data.wp.start=start;
2231  ord_struct.data.wp.end=end;
2232  ord_struct.data.wp.place=place;
2233  ord_struct.data.wp.weights=weights;
2234  o[place]=-1;
2235  place++;
2236  rO_Align(place,bitplace);
2237  int i;
2238  for(i=start;i<=end;i++)
2239  {
2240    if(weights[i-start]<0)
2241    {
2242      ord_struct.ord_typ=ro_wp_neg;
2243      break;
2244    }
2245  }
2246}
2247
2248static void rO_LexVars(int &place, int &bitplace, int start, int end,
2249  int &prev_ord, long *o,int *v, int bits, int opt_var)
2250{
2251  // a block of variables v_start..v_end with lex order, ordsgn 1
2252  int k;
2253  int incr=1;
2254  if(prev_ord==-1) rO_Align(place,bitplace);
2255
2256  if (start>end)
2257  {
2258    incr=-1;
2259  }
2260  for(k=start;;k+=incr)
2261  {
2262    bitplace-=bits;
2263    if (bitplace < 0) { bitplace=BITS_PER_LONG-bits; place++; }
2264    o[place]=1;
2265    v[k]= place | (bitplace << 24);
2266    if (k==end) break;
2267  }
2268  prev_ord=1;
2269  if (opt_var!= -1)
2270  {
2271    assume((opt_var == end+1) ||(opt_var == end-1));
2272    if((opt_var != end+1) &&(opt_var != end-1)) WarnS("hier-2");
2273    int save_bitplace=bitplace;
2274    bitplace-=bits;
2275    if (bitplace < 0)
2276    {
2277      bitplace=save_bitplace;
2278      return;
2279    }
2280    // there is enough space for the optional var
2281    v[opt_var]=place | (bitplace << 24);
2282  }
2283}
2284
2285static void rO_LexVars_neg(int &place, int &bitplace, int start, int end,
2286  int &prev_ord, long *o,int *v, int bits, int opt_var)
2287{
2288  // a block of variables v_start..v_end with lex order, ordsgn -1
2289  int k;
2290  int incr=1;
2291  if(prev_ord==1) rO_Align(place,bitplace);
2292
2293  if (start>end)
2294  {
2295    incr=-1;
2296  }
2297  for(k=start;;k+=incr)
2298  {
2299    bitplace-=bits;
2300    if (bitplace < 0) { bitplace=BITS_PER_LONG-bits; place++; }
2301    o[place]=-1;
2302    v[k]=place | (bitplace << 24);
2303    if (k==end) break;
2304  }
2305  prev_ord=-1;
2306//  #if 0
2307  if (opt_var!= -1)
2308  {
2309    assume((opt_var == end+1) ||(opt_var == end-1));
2310    if((opt_var != end+1) &&(opt_var != end-1)) WarnS("hier-1");
2311    int save_bitplace=bitplace;
2312    bitplace-=bits;
2313    if (bitplace < 0)
2314    {
2315      bitplace=save_bitplace;
2316      return;
2317    }
2318    // there is enough space for the optional var
2319    v[opt_var]=place | (bitplace << 24);
2320  }
2321//  #endif
2322}
2323
2324static void rO_Syzcomp(int &place, int &bitplace, int &prev_ord,
2325    long *o, sro_ord &ord_struct)
2326{
2327  // ordering is derived from component number
2328  rO_Align(place,bitplace);
2329  ord_struct.ord_typ=ro_syzcomp;
2330  ord_struct.data.syzcomp.place=place;
2331  ord_struct.data.syzcomp.Components=NULL;
2332  ord_struct.data.syzcomp.ShiftedComponents=NULL;
2333  o[place]=1;
2334  prev_ord=1;
2335  place++;
2336  rO_Align(place,bitplace);
2337}
2338
2339static void rO_Syz(int &place, int &bitplace, int &prev_ord,
2340    long *o, sro_ord &ord_struct)
2341{
2342  // ordering is derived from component number
2343  // let's reserve one Exponent_t for it
2344  if ((prev_ord== 1) || (bitplace!=BITS_PER_LONG))
2345    rO_Align(place,bitplace);
2346  ord_struct.ord_typ=ro_syz;
2347  ord_struct.data.syz.place=place;
2348  ord_struct.data.syz.limit=0;
2349  ord_struct.data.syz.syz_index = NULL;
2350  ord_struct.data.syz.curr_index = 1;
2351  o[place]= -1;
2352  prev_ord=-1;
2353  place++;
2354}
2355
2356static unsigned long rGetExpSize(unsigned long bitmask, int & bits)
2357{
2358  if (bitmask == 0)
2359  {
2360    bits=16; bitmask=0xffff;
2361  }
2362  else if (bitmask <= 1)
2363  {
2364    bits=1; bitmask = 1;
2365  }
2366  else if (bitmask <= 3)
2367  {
2368    bits=2; bitmask = 3;
2369  }
2370  else if (bitmask <= 7)
2371  {
2372    bits=3; bitmask=7;
2373  }
2374  else if (bitmask <= 0xf)
2375  {
2376    bits=4; bitmask=0xf;
2377  }
2378  else if (bitmask <= 0x1f)
2379  {
2380    bits=5; bitmask=0x1f;
2381  }
2382  else if (bitmask <= 0x3f)
2383  {
2384    bits=6; bitmask=0x3f;
2385  }
2386#if SIZEOF_LONG == 8
2387  else if (bitmask <= 0x7f)
2388  {
2389    bits=7; bitmask=0x7f; /* 64 bit longs only */
2390  }
2391#endif
2392  else if (bitmask <= 0xff)
2393  {
2394    bits=8; bitmask=0xff;
2395  }
2396#if SIZEOF_LONG == 8
2397  else if (bitmask <= 0x1ff)
2398  {
2399    bits=9; bitmask=0x1ff; /* 64 bit longs only */
2400  }
2401#endif
2402  else if (bitmask <= 0x3ff)
2403  {
2404    bits=10; bitmask=0x3ff;
2405  }
2406#if SIZEOF_LONG == 8
2407  else if (bitmask <= 0xfff)
2408  {
2409    bits=12; bitmask=0xfff; /* 64 bit longs only */
2410  }
2411#endif
2412  else if (bitmask <= 0xffff)
2413  {
2414    bits=16; bitmask=0xffff;
2415  }
2416#if SIZEOF_LONG == 8
2417  else if (bitmask <= 0xfffff)
2418  {
2419    bits=20; bitmask=0xfffff; /* 64 bit longs only */
2420  }
2421  else if (bitmask <= 0xffffffff)
2422  {
2423    bits=32; bitmask=0xffffffff;
2424  }
2425  else
2426  {
2427    bits=64; bitmask=0xffffffffffffffff;
2428  }
2429#else
2430  else
2431  {
2432    bits=32; bitmask=0xffffffff;
2433  }
2434#endif
2435  return bitmask;
2436}
2437
2438/*2
2439* optimize rGetExpSize for a block of N variables, exp <=bitmask
2440*/
2441static unsigned long rGetExpSize(unsigned long bitmask, int & bits, int N)
2442{
2443  bitmask =rGetExpSize(bitmask, bits);
2444  int vars_per_long=BIT_SIZEOF_LONG/bits;
2445  int bits1;
2446  loop
2447  {
2448    if (bits == BIT_SIZEOF_LONG)
2449    {
2450      bits =  BIT_SIZEOF_LONG - 1;
2451      return LONG_MAX;
2452    }
2453    unsigned long bitmask1 =rGetExpSize(bitmask+1, bits1);
2454    int vars_per_long1=BIT_SIZEOF_LONG/bits1;
2455    if ((((N+vars_per_long-1)/vars_per_long) ==
2456         ((N+vars_per_long1-1)/vars_per_long1)))
2457    {
2458      vars_per_long=vars_per_long1;
2459      bits=bits1;
2460      bitmask=bitmask1;
2461    }
2462    else
2463    {
2464      return bitmask; /* and bits */
2465    }
2466  }
2467}
2468
2469/*2
2470 * create a copy of the ring r, which must be equivalent to currRing
2471 * used for std computations
2472 * may share data structures with currRing
2473 * DOES CALL rComplete
2474 */
2475ring rModifyRing(ring r, BOOLEAN omit_degree,
2476                         BOOLEAN omit_comp,
2477                         unsigned long exp_limit)
2478{
2479  assume (r != NULL );
2480  assume (exp_limit > 1);
2481  BOOLEAN need_other_ring;
2482  BOOLEAN omitted_degree = FALSE;
2483  int bits;
2484
2485  exp_limit=rGetExpSize(exp_limit, bits, r->N);
2486  need_other_ring = (exp_limit != r->bitmask);
2487
2488  int nblocks=rBlocks(r);
2489  int *order=(int*)omAlloc0((nblocks+1)*sizeof(int));
2490  int *block0=(int*)omAlloc0((nblocks+1)*sizeof(int));
2491  int *block1=(int*)omAlloc0((nblocks+1)*sizeof(int));
2492  int **wvhdl=(int**)omAlloc0((nblocks+1)*sizeof(int_ptr));
2493
2494  int i=0;
2495  int j=0; /*  i index in r, j index in res */
2496  loop
2497  {
2498    BOOLEAN copy_block_index=TRUE;
2499    int r_ord=r->order[i];
2500    if (r->block0[i]==r->block1[i])
2501    {
2502      switch(r_ord)
2503      {
2504        case ringorder_wp:
2505        case ringorder_dp:
2506        case ringorder_Wp:
2507        case ringorder_Dp:
2508          r_ord=ringorder_lp;
2509          break;
2510        case ringorder_Ws:
2511        case ringorder_Ds:
2512        case ringorder_ws:
2513        case ringorder_ds:
2514          r_ord=ringorder_ls;
2515          break;
2516        default:
2517          break;
2518      }
2519    }
2520    switch(r_ord)
2521    {
2522      case ringorder_C:
2523      case ringorder_c:
2524        if (!omit_comp)
2525        {
2526          order[j]=r_ord; /*r->order[i]*/;
2527        }
2528        else
2529        {
2530          j--;
2531          need_other_ring=TRUE;
2532          omit_comp=FALSE;
2533          copy_block_index=FALSE;
2534        }
2535        break;
2536      case ringorder_wp:
2537      case ringorder_dp:
2538      case ringorder_ws:
2539      case ringorder_ds:
2540        if(!omit_degree)
2541        {
2542          order[j]=r_ord; /*r->order[i]*/;
2543        }
2544        else
2545        {
2546          order[j]=ringorder_rs;
2547          need_other_ring=TRUE;
2548          omit_degree=FALSE;
2549          omitted_degree = TRUE;
2550        }
2551        break;
2552      case ringorder_Wp:
2553      case ringorder_Dp:
2554      case ringorder_Ws:
2555      case ringorder_Ds:
2556        if(!omit_degree)
2557        {
2558          order[j]=r_ord; /*r->order[i];*/
2559        }
2560        else
2561        {
2562          order[j]=ringorder_lp;
2563          need_other_ring=TRUE;
2564          omit_degree=FALSE;
2565          omitted_degree = TRUE;
2566        }
2567        break;
2568      default:
2569        order[j]=r_ord; /*r->order[i];*/
2570        break;
2571    }
2572    if (copy_block_index)
2573    {
2574      block0[j]=r->block0[i];
2575      block1[j]=r->block1[i];
2576      wvhdl[j]=r->wvhdl[i];
2577    }
2578    i++;j++;
2579    // order[j]=ringorder_no; //  done by omAlloc0
2580    if (i==nblocks) break;
2581  }
2582  if(!need_other_ring)
2583  {
2584    omFreeSize(order,(nblocks+1)*sizeof(int));
2585    omFreeSize(block0,(nblocks+1)*sizeof(int));
2586    omFreeSize(block1,(nblocks+1)*sizeof(int));
2587    omFreeSize(wvhdl,(nblocks+1)*sizeof(int_ptr));
2588    return r;
2589  }
2590  ring res=(ring)omAlloc0Bin(ip_sring_bin);
2591  *res = *r;
2592
2593#ifdef HAVE_PLURAL
2594  res->GetNC() = NULL;
2595#endif
2596
2597  // res->qideal, res->idroot ???
2598  res->wvhdl=wvhdl;
2599  res->order=order;
2600  res->block0=block0;
2601  res->block1=block1;
2602  res->bitmask=exp_limit;
2603  int tmpref=r->cf->ref;
2604  rComplete(res, 1);
2605  r->cf->ref=tmpref;
2606
2607  // adjust res->pFDeg: if it was changed globally, then
2608  // it must also be changed for new ring
2609  if (r->pFDegOrig != res->pFDegOrig &&
2610           rOrd_is_WeightedDegree_Ordering(r))
2611  {
2612    // still might need adjustment for weighted orderings
2613    // and omit_degree
2614    res->firstwv = r->firstwv;
2615    res->firstBlockEnds = r->firstBlockEnds;
2616    res->pFDeg = res->pFDegOrig = pWFirstTotalDegree;
2617  }
2618  if (omitted_degree)
2619    res->pLDeg = res->pLDegOrig = r->pLDegOrig;
2620
2621  rOptimizeLDeg(res);
2622
2623  // set syzcomp
2624  if (res->typ != NULL && res->typ[0].ord_typ == ro_syz)
2625  {
2626    res->typ[0] = r->typ[0];
2627    if (r->typ[0].data.syz.limit > 0)
2628    {
2629      res->typ[0].data.syz.syz_index
2630        = (int*) omAlloc((r->typ[0].data.syz.limit +1)*sizeof(int));
2631      memcpy(res->typ[0].data.syz.syz_index, r->typ[0].data.syz.syz_index,
2632             (r->typ[0].data.syz.limit +1)*sizeof(int));
2633    }
2634  }
2635  // the special case: homog (omit_degree) and 1 block rs: that is global:
2636  // it comes from dp
2637  res->OrdSgn=r->OrdSgn;
2638
2639
2640#ifdef HAVE_PLURAL
2641  if (rIsPluralRing(r))
2642  {
2643    if ( nc_rComplete(r, res, false) ) // no qideal!
2644    {
2645      WarnS("error in nc_rComplete");
2646      // cleanup?
2647
2648//      rDelete(res);
2649//      return r;
2650
2651      // just go on..
2652    }
2653  }
2654#endif
2655
2656  return res;
2657}
2658
2659// construct Wp,C ring
2660ring rModifyRing_Wp(ring r, int* weights)
2661{
2662  ring res=(ring)omAlloc0Bin(ip_sring_bin);
2663  *res = *r;
2664#ifdef HAVE_PLURAL
2665  res->GetNC() = NULL;
2666#endif
2667
2668  /*weights: entries for 3 blocks: NULL*/
2669  res->wvhdl = (int **)omAlloc0(3 * sizeof(int_ptr));
2670  /*order: Wp,C,0*/
2671  res->order = (int *) omAlloc(3 * sizeof(int *));
2672  res->block0 = (int *)omAlloc0(3 * sizeof(int *));
2673  res->block1 = (int *)omAlloc0(3 * sizeof(int *));
2674  /* ringorder Wp for the first block: var 1..r->N */
2675  res->order[0]  = ringorder_Wp;
2676  res->block0[0] = 1;
2677  res->block1[0] = r->N;
2678  res->wvhdl[0] = weights;
2679  /* ringorder C for the second block: no vars */
2680  res->order[1]  = ringorder_C;
2681  /* the last block: everything is 0 */
2682  res->order[2]  = 0;
2683  /*polynomial ring*/
2684  res->OrdSgn    = 1;
2685
2686  int tmpref=r->cf->ref;
2687  rComplete(res, 1);
2688  r->cf->ref=tmpref;
2689#ifdef HAVE_PLURAL
2690  if (rIsPluralRing(r))
2691  {
2692    if ( nc_rComplete(r, res, false) ) // no qideal!
2693    {
2694      WarnS("error in nc_rComplete");
2695      // cleanup?
2696
2697//      rDelete(res);
2698//      return r;
2699
2700      // just go on..
2701    }
2702  }
2703#endif
2704  return res;
2705}
2706
2707// construct lp ring with r->N variables, r->names vars....
2708ring rModifyRing_Simple(ring r, BOOLEAN ommit_degree, BOOLEAN ommit_comp, unsigned long exp_limit, BOOLEAN &simple)
2709{
2710  simple=TRUE;
2711  if (!rHasSimpleOrder(r))
2712  {
2713    simple=FALSE; // sorting needed
2714    assume (r != NULL );
2715    assume (exp_limit > 1);
2716    BOOLEAN omitted_degree = FALSE;
2717    int bits;
2718
2719    exp_limit=rGetExpSize(exp_limit, bits, r->N);
2720
2721    int nblocks=1+(ommit_comp!=0);
2722    int *order=(int*)omAlloc0((nblocks+1)*sizeof(int));
2723    int *block0=(int*)omAlloc0((nblocks+1)*sizeof(int));
2724    int *block1=(int*)omAlloc0((nblocks+1)*sizeof(int));
2725    int **wvhdl=(int**)omAlloc0((nblocks+1)*sizeof(int_ptr));
2726
2727    order[0]=ringorder_lp;
2728    block0[0]=1;
2729    block1[0]=r->N;
2730    if (!ommit_comp)
2731    {
2732      order[1]=ringorder_C;
2733    }
2734    ring res=(ring)omAlloc0Bin(ip_sring_bin);
2735    *res = *r;
2736#ifdef HAVE_PLURAL
2737    res->GetNC() = NULL;
2738#endif
2739    // res->qideal, res->idroot ???
2740    res->wvhdl=wvhdl;
2741    res->order=order;
2742    res->block0=block0;
2743    res->block1=block1;
2744    res->bitmask=exp_limit;
2745    int tmpref=r->cf->ref;
2746    rComplete(res, 1);
2747    r->cf->ref=tmpref;
2748
2749#ifdef HAVE_PLURAL
2750    if (rIsPluralRing(r))
2751    {
2752      if ( nc_rComplete(r, res, false) ) // no qideal!
2753      {
2754        WarnS("error in nc_rComplete");
2755      // cleanup?
2756
2757//      rDelete(res);
2758//      return r;
2759
2760      // just go on..
2761      }
2762    }
2763#endif
2764
2765    rOptimizeLDeg(res);
2766
2767    return res;
2768  }
2769  return rModifyRing(r, ommit_degree, ommit_comp, exp_limit);
2770}
2771
2772void rKillModifiedRing_Simple(ring r)
2773{
2774  rKillModifiedRing(r);
2775}
2776
2777
2778void rKillModifiedRing(ring r)
2779{
2780  rUnComplete(r);
2781  omFree(r->order);
2782  omFree(r->block0);
2783  omFree(r->block1);
2784  omFree(r->wvhdl);
2785  omFreeBin(r,ip_sring_bin);
2786}
2787
2788void rKillModified_Wp_Ring(ring r)
2789{
2790  rUnComplete(r);
2791  omFree(r->order);
2792  omFree(r->block0);
2793  omFree(r->block1);
2794  omFree(r->wvhdl[0]);
2795  omFree(r->wvhdl);
2796  omFreeBin(r,ip_sring_bin);
2797}
2798
2799static void rSetOutParams(ring r)
2800{
2801  r->VectorOut = (r->order[0] == ringorder_c);
2802  r->ShortOut = TRUE;
2803#ifdef HAVE_TCL
2804  if (tcllmode)
2805  {
2806    r->ShortOut = FALSE;
2807  }
2808  else
2809#endif
2810  {
2811    int i;
2812    if ((r->parameter!=NULL) && (r->ch<2))
2813    {
2814      for (i=0;i<rPar(r);i++)
2815      {
2816        if(strlen(r->parameter[i])>1)
2817        {
2818          r->ShortOut=FALSE;
2819          break;
2820        }
2821      }
2822    }
2823    if (r->ShortOut)
2824    {
2825      // Hmm... sometimes (e.g., from maGetPreimage) new variables
2826      // are intorduced, but their names are never set
2827      // hence, we do the following awkward trick
2828      int N = omSizeWOfAddr(r->names);
2829      if (r->N < N) N = r->N;
2830
2831      for (i=(N-1);i>=0;i--)
2832      {
2833        if(r->names[i] != NULL && strlen(r->names[i])>1)
2834        {
2835          r->ShortOut=FALSE;
2836          break;
2837        }
2838      }
2839    }
2840  }
2841  r->CanShortOut = r->ShortOut;
2842}
2843
2844/*2
2845* sets pMixedOrder and pComponentOrder for orderings with more than one block
2846* block of variables (ip is the block number, o_r the number of the ordering)
2847* o is the position of the orderingering in r
2848*/
2849static void rHighSet(ring r, int o_r, int o)
2850{
2851  switch(o_r)
2852  {
2853    case ringorder_lp:
2854    case ringorder_dp:
2855    case ringorder_Dp:
2856    case ringorder_wp:
2857    case ringorder_Wp:
2858    case ringorder_rp:
2859    case ringorder_a:
2860    case ringorder_aa:
2861    case ringorder_a64:
2862      if (r->OrdSgn==-1) r->MixedOrder=TRUE;
2863      break;
2864    case ringorder_ls:
2865    case ringorder_rs:
2866    case ringorder_ds:
2867    case ringorder_Ds:
2868    case ringorder_s:
2869      break;
2870    case ringorder_ws:
2871    case ringorder_Ws:
2872      if (r->wvhdl[o]!=NULL)
2873      {
2874        int i;
2875        for(i=r->block1[o]-r->block0[o];i>=0;i--)
2876          if (r->wvhdl[o][i]<0) { r->MixedOrder=TRUE; break; }
2877      }
2878      break;
2879    case ringorder_c:
2880      r->ComponentOrder=1;
2881      break;
2882    case ringorder_C:
2883    case ringorder_S:
2884      r->ComponentOrder=-1;
2885      break;
2886    case ringorder_M:
2887      r->MixedOrder=TRUE;
2888      break;
2889    default:
2890      dReportError("wrong internal ordering:%d at %s, l:%d\n",o_r,__FILE__,__LINE__);
2891  }
2892}
2893
2894static void rSetFirstWv(ring r, int i, int* order, int* block1, int** wvhdl)
2895{
2896  // cheat for ringorder_aa
2897  if (order[i] == ringorder_aa)
2898    i++;
2899  if(block1[i]!=r->N) r->LexOrder=TRUE;
2900  r->firstBlockEnds=block1[i];
2901  r->firstwv = wvhdl[i];
2902  if ((order[i]== ringorder_ws)
2903  || (order[i]==ringorder_Ws)
2904  || (order[i]== ringorder_wp)
2905  || (order[i]==ringorder_Wp)
2906  || (order[i]== ringorder_a)
2907   /*|| (order[i]==ringorder_A)*/)
2908  {
2909    int j;
2910    for(j=block1[i]-r->block0[i];j>=0;j--)
2911    {
2912      if (r->firstwv[j]<0) r->MixedOrder=TRUE;
2913      if (r->firstwv[j]==0) r->LexOrder=TRUE;
2914    }
2915  }
2916  else if (order[i]==ringorder_a64)
2917  {
2918    int j;
2919    int64 *w=rGetWeightVec(r);
2920    for(j=block1[i]-r->block0[i];j>=0;j--)
2921    {
2922      if (w[j]==0) r->LexOrder=TRUE;
2923    }
2924  }
2925}
2926
2927static void rOptimizeLDeg(ring r)
2928{
2929  if (r->pFDeg == pDeg)
2930  {
2931    if (r->pLDeg == pLDeg1)
2932      r->pLDeg = pLDeg1_Deg;
2933    if (r->pLDeg == pLDeg1c)
2934      r->pLDeg = pLDeg1c_Deg;
2935  }
2936  else if (r->pFDeg == pTotaldegree)
2937  {
2938    if (r->pLDeg == pLDeg1)
2939      r->pLDeg = pLDeg1_Totaldegree;
2940    if (r->pLDeg == pLDeg1c)
2941      r->pLDeg = pLDeg1c_Totaldegree;
2942  }
2943  else if (r->pFDeg == pWFirstTotalDegree)
2944  {
2945    if (r->pLDeg == pLDeg1)
2946      r->pLDeg = pLDeg1_WFirstTotalDegree;
2947    if (r->pLDeg == pLDeg1c)
2948      r->pLDeg = pLDeg1c_WFirstTotalDegree;
2949  }
2950}
2951
2952// set pFDeg, pLDeg, MixOrder, ComponentOrder, etc
2953static void rSetDegStuff(ring r)
2954{
2955  int* order = r->order;
2956  int* block0 = r->block0;
2957  int* block1 = r->block1;
2958  int** wvhdl = r->wvhdl;
2959
2960  if (order[0]==ringorder_S ||order[0]==ringorder_s)
2961  {
2962    order++;
2963    block0++;
2964    block1++;
2965    wvhdl++;
2966  }
2967  r->LexOrder = FALSE;
2968  r->MixedOrder = FALSE;
2969  r->ComponentOrder = 1;
2970  r->pFDeg = pTotaldegree;
2971  r->pLDeg = (r->OrdSgn == 1 ? pLDegb : pLDeg0);
2972
2973  /*======== ordering type is (_,c) =========================*/
2974  if ((order[0]==ringorder_unspec) || (order[1] == 0)
2975      ||(
2976    ((order[1]==ringorder_c)||(order[1]==ringorder_C)
2977     ||(order[1]==ringorder_S)
2978     ||(order[1]==ringorder_s))
2979    && (order[0]!=ringorder_M)
2980    && (order[2]==0))
2981    )
2982  {
2983    if ((order[0]!=ringorder_unspec)
2984    && ((order[1]==ringorder_C)||(order[1]==ringorder_S)||
2985        (order[1]==ringorder_s)))
2986      r->ComponentOrder=-1;
2987    if (r->OrdSgn == -1) r->pLDeg = pLDeg0c;
2988    if ((order[0] == ringorder_lp)
2989    || (order[0] == ringorder_ls)
2990    || (order[0] == ringorder_rp)
2991    || (order[0] == ringorder_rs))
2992    {
2993      r->LexOrder=TRUE;
2994      r->pLDeg = pLDeg1c;
2995      r->pFDeg = pTotaldegree;
2996    }
2997    if ((order[0] == ringorder_a)
2998    || (order[0] == ringorder_wp)
2999    || (order[0] == ringorder_Wp)
3000    || (order[0] == ringorder_ws)
3001    || (order[0] == ringorder_Ws))
3002      r->pFDeg = pWFirstTotalDegree;
3003    r->firstBlockEnds=block1[0];
3004    r->firstwv = wvhdl[0];
3005  }
3006  /*======== ordering type is (c,_) =========================*/
3007  else if (((order[0]==ringorder_c)
3008            ||(order[0]==ringorder_C)
3009            ||(order[0]==ringorder_S)
3010            ||(order[0]==ringorder_s))
3011  && (order[1]!=ringorder_M)
3012  &&  (order[2]==0))
3013  {
3014    if ((order[0]==ringorder_C)||(order[0]==ringorder_S)||
3015        order[0]==ringorder_s)
3016      r->ComponentOrder=-1;
3017    if ((order[1] == ringorder_lp)
3018    || (order[1] == ringorder_ls)
3019    || (order[1] == ringorder_rp)
3020    || order[1] == ringorder_rs)
3021    {
3022      r->LexOrder=TRUE;
3023      r->pLDeg = pLDeg1c;
3024      r->pFDeg = pTotaldegree;
3025    }
3026    r->firstBlockEnds=block1[1];
3027    r->firstwv = wvhdl[1];
3028    if ((order[1] == ringorder_a)
3029    || (order[1] == ringorder_wp)
3030    || (order[1] == ringorder_Wp)
3031    || (order[1] == ringorder_ws)
3032    || (order[1] == ringorder_Ws))
3033      r->pFDeg = pWFirstTotalDegree;
3034  }
3035  /*------- more than one block ----------------------*/
3036  else
3037  {
3038    if ((r->VectorOut)||(order[0]==ringorder_C)||(order[0]==ringorder_S)||(order[0]==ringorder_s))
3039    {
3040      rSetFirstWv(r, 1, order, block1, wvhdl);
3041    }
3042    else
3043      rSetFirstWv(r, 0, order, block1, wvhdl);
3044
3045    /*the number of orderings:*/
3046    int i = 0;
3047    while (order[++i] != 0);
3048    do
3049    {
3050      i--;
3051      rHighSet(r, order[i],i);
3052    }
3053    while (i != 0);
3054
3055    if ((order[0]!=ringorder_c)
3056        && (order[0]!=ringorder_C)
3057        && (order[0]!=ringorder_S)
3058        && (order[0]!=ringorder_s))
3059    {
3060      r->pLDeg = pLDeg1c;
3061    }
3062    else
3063    {
3064      r->pLDeg = pLDeg1;
3065    }
3066    r->pFDeg = pWTotaldegree; // may be improved: pTotaldegree for lp/dp/ls/.. blocks
3067  }
3068  if (rOrd_is_Totaldegree_Ordering(r) || rOrd_is_WeightedDegree_Ordering(r))
3069    r->pFDeg = pDeg;
3070
3071  r->pFDegOrig = r->pFDeg;
3072  r->pLDegOrig = r->pLDeg;
3073  rOptimizeLDeg(r);
3074}
3075
3076/*2
3077* set NegWeightL_Size, NegWeightL_Offset
3078*/
3079static void rSetNegWeight(ring r)
3080{
3081  int i,l;
3082  if (r->typ!=NULL)
3083  {
3084    l=0;
3085    for(i=0;i<r->OrdSize;i++)
3086    {
3087      if(r->typ[i].ord_typ==ro_wp_neg) l++;
3088    }
3089    if (l>0)
3090    {
3091      r->NegWeightL_Size=l;
3092      r->NegWeightL_Offset=(int *) omAlloc(l*sizeof(int));
3093      l=0;
3094      for(i=0;i<r->OrdSize;i++)
3095      {
3096        if(r->typ[i].ord_typ==ro_wp_neg)
3097        {
3098          r->NegWeightL_Offset[l]=r->typ[i].data.wp.place;
3099          l++;
3100        }
3101      }
3102      return;
3103    }
3104  }
3105  r->NegWeightL_Size = 0;
3106  r->NegWeightL_Offset = NULL;
3107}
3108
3109static void rSetOption(ring r)
3110{
3111  // set redthrough
3112  if (!TEST_OPT_OLDSTD && r->OrdSgn == 1 && ! r->LexOrder)
3113    r->options |= Sy_bit(OPT_REDTHROUGH);
3114  else
3115    r->options &= ~Sy_bit(OPT_REDTHROUGH);
3116
3117  // set intStrategy
3118#ifdef HAVE_RINGS
3119  if (rField_is_Extension(r) || rField_is_Q(r) || rField_is_Ring(r))
3120#else
3121  if (rField_is_Extension(r) || rField_is_Q(r))
3122#endif
3123    r->options |= Sy_bit(OPT_INTSTRATEGY);
3124  else
3125    r->options &= ~Sy_bit(OPT_INTSTRATEGY);
3126
3127  // set redTail
3128  if (r->LexOrder || r->OrdSgn == -1 || rField_is_Extension(r))
3129    r->options &= ~Sy_bit(OPT_REDTAIL);
3130  else
3131    r->options |= Sy_bit(OPT_REDTAIL);
3132}
3133
3134BOOLEAN rComplete(ring r, int force)
3135{
3136  if (r->VarOffset!=NULL && force == 0) return FALSE;
3137  nInitChar(r);
3138  rSetOutParams(r);
3139  int n=rBlocks(r)-1;
3140  int i;
3141  int bits;
3142  r->bitmask=rGetExpSize(r->bitmask,bits,r->N);
3143  r->BitsPerExp = bits;
3144  r->ExpPerLong = BIT_SIZEOF_LONG / bits;
3145  r->divmask=rGetDivMask(bits);
3146
3147  // will be used for ordsgn:
3148  long *tmp_ordsgn=(long *)omAlloc0(3*(n+r->N)*sizeof(long));
3149  // will be used for VarOffset:
3150  int *v=(int *)omAlloc((r->N+1)*sizeof(int));
3151  for(i=r->N; i>=0 ; i--)
3152  {
3153    v[i]=-1;
3154  }
3155  sro_ord *tmp_typ=(sro_ord *)omAlloc0(3*(n+r->N)*sizeof(sro_ord));
3156  int typ_i=0;
3157  int prev_ordsgn=0;
3158
3159  // fill in v, tmp_typ, tmp_ordsgn, determine typ_i (== ordSize)
3160  int j=0;
3161  int j_bits=BITS_PER_LONG;
3162  BOOLEAN need_to_add_comp=FALSE;
3163  for(i=0;i<n;i++)
3164  {
3165    tmp_typ[typ_i].order_index=i;
3166    switch (r->order[i])
3167    {
3168      case ringorder_a:
3169      case ringorder_aa:
3170        rO_WDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,tmp_typ[typ_i],
3171                   r->wvhdl[i]);
3172        typ_i++;
3173        break;
3174
3175      case ringorder_a64:
3176        rO_WDegree64(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3177                     tmp_typ[typ_i], (int64 *)(r->wvhdl[i]));
3178        typ_i++;
3179        break;
3180
3181      case ringorder_c:
3182        rO_Align(j, j_bits);
3183        rO_LexVars_neg(j, j_bits, 0,0, prev_ordsgn,tmp_ordsgn,v,BITS_PER_LONG, -1);
3184        break;
3185
3186      case ringorder_C:
3187        rO_Align(j, j_bits);
3188        rO_LexVars(j, j_bits, 0,0, prev_ordsgn,tmp_ordsgn,v,BITS_PER_LONG, -1);
3189        break;
3190
3191      case ringorder_M:
3192        {
3193          int k,l;
3194          k=r->block1[i]-r->block0[i]+1; // number of vars
3195          for(l=0;l<k;l++)
3196          {
3197            rO_WDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3198                       tmp_typ[typ_i],
3199                       r->wvhdl[i]+(r->block1[i]-r->block0[i]+1)*l);
3200            typ_i++;
3201          }
3202          break;
3203        }
3204
3205      case ringorder_lp:
3206        rO_LexVars(j, j_bits, r->block0[i],r->block1[i], prev_ordsgn,
3207                   tmp_ordsgn,v,bits, -1);
3208        break;
3209
3210      case ringorder_ls:
3211        rO_LexVars_neg(j, j_bits, r->block0[i],r->block1[i], prev_ordsgn,
3212                       tmp_ordsgn,v, bits, -1);
3213        break;
3214
3215      case ringorder_rs:
3216        rO_LexVars_neg(j, j_bits, r->block1[i],r->block0[i], prev_ordsgn,
3217                       tmp_ordsgn,v, bits, -1);
3218        break;
3219
3220      case ringorder_rp:
3221        rO_LexVars(j, j_bits, r->block1[i],r->block0[i], prev_ordsgn,
3222                       tmp_ordsgn,v, bits, -1);
3223        break;
3224
3225      case ringorder_dp:
3226        if (r->block0[i]==r->block1[i])
3227        {
3228          rO_LexVars(j, j_bits, r->block0[i],r->block0[i], prev_ordsgn,
3229                     tmp_ordsgn,v, bits, -1);
3230        }
3231        else
3232        {
3233          rO_TDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3234                     tmp_typ[typ_i]);
3235          typ_i++;
3236          rO_LexVars_neg(j, j_bits, r->block1[i],r->block0[i]+1,
3237                         prev_ordsgn,tmp_ordsgn,v,bits, r->block0[i]);
3238        }
3239        break;
3240
3241      case ringorder_Dp:
3242        if (r->block0[i]==r->block1[i])
3243        {
3244          rO_LexVars(j, j_bits, r->block0[i],r->block0[i], prev_ordsgn,
3245                     tmp_ordsgn,v, bits, -1);
3246        }
3247        else
3248        {
3249          rO_TDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3250                     tmp_typ[typ_i]);
3251          typ_i++;
3252          rO_LexVars(j, j_bits, r->block0[i],r->block1[i]-1, prev_ordsgn,
3253                     tmp_ordsgn,v, bits, r->block1[i]);
3254        }
3255        break;
3256
3257      case ringorder_ds:
3258        if (r->block0[i]==r->block1[i])
3259        {
3260          rO_LexVars_neg(j, j_bits,r->block0[i],r->block1[i],prev_ordsgn,
3261                         tmp_ordsgn,v,bits, -1);
3262        }
3263        else
3264        {
3265          rO_TDegree_neg(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3266                         tmp_typ[typ_i]);
3267          typ_i++;
3268          rO_LexVars_neg(j, j_bits, r->block1[i],r->block0[i]+1,
3269                         prev_ordsgn,tmp_ordsgn,v,bits, r->block0[i]);
3270        }
3271        break;
3272
3273      case ringorder_Ds:
3274        if (r->block0[i]==r->block1[i])
3275        {
3276          rO_LexVars_neg(j, j_bits, r->block0[i],r->block0[i],prev_ordsgn,
3277                         tmp_ordsgn,v, bits, -1);
3278        }
3279        else
3280        {
3281          rO_TDegree_neg(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3282                         tmp_typ[typ_i]);
3283          typ_i++;
3284          rO_LexVars(j, j_bits, r->block0[i],r->block1[i]-1, prev_ordsgn,
3285                     tmp_ordsgn,v, bits, r->block1[i]);
3286        }
3287        break;
3288
3289      case ringorder_wp:
3290        rO_WDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3291                   tmp_typ[typ_i], r->wvhdl[i]);
3292        typ_i++;
3293        { // check for weights <=0
3294          int jj;
3295          BOOLEAN have_bad_weights=FALSE;
3296          for(jj=r->block1[i]-r->block0[i];jj>=0; jj--)
3297          {
3298            if (r->wvhdl[i][jj]<=0) have_bad_weights=TRUE;
3299          }
3300          if (have_bad_weights)
3301          {
3302             rO_TDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3303                                     tmp_typ[typ_i]);
3304             typ_i++;
3305          }
3306        }
3307        if (r->block1[i]!=r->block0[i])
3308        {
3309          rO_LexVars_neg(j, j_bits,r->block1[i],r->block0[i]+1, prev_ordsgn,
3310                         tmp_ordsgn, v,bits, r->block0[i]);
3311        }
3312        break;
3313
3314      case ringorder_Wp:
3315        rO_WDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3316                   tmp_typ[typ_i], r->wvhdl[i]);
3317        typ_i++;
3318        { // check for weights <=0
3319          int j;
3320          BOOLEAN have_bad_weights=FALSE;
3321          for(j=r->block1[i]-r->block0[i];j>=0; j--)
3322          {
3323            if (r->wvhdl[i][j]<=0) have_bad_weights=TRUE;
3324          }
3325          if (have_bad_weights)
3326          {
3327             rO_TDegree(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3328                                     tmp_typ[typ_i]);
3329             typ_i++;
3330          }
3331        }
3332        if (r->block1[i]!=r->block0[i])
3333        {
3334          rO_LexVars(j, j_bits,r->block0[i],r->block1[i]-1, prev_ordsgn,
3335                     tmp_ordsgn,v, bits, r->block1[i]);
3336        }
3337        break;
3338
3339      case ringorder_ws:
3340        rO_WDegree_neg(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3341                       tmp_typ[typ_i], r->wvhdl[i]);
3342        typ_i++;
3343        if (r->block1[i]!=r->block0[i])
3344        {
3345          rO_LexVars_neg(j, j_bits,r->block1[i],r->block0[i]+1, prev_ordsgn,
3346                         tmp_ordsgn, v,bits, r->block0[i]);
3347        }
3348        break;
3349
3350      case ringorder_Ws:
3351        rO_WDegree_neg(j,j_bits,r->block0[i],r->block1[i],tmp_ordsgn,
3352                       tmp_typ[typ_i], r->wvhdl[i]);
3353        typ_i++;
3354        if (r->block1[i]!=r->block0[i])
3355        {
3356          rO_LexVars(j, j_bits,r->block0[i],r->block1[i]-1, prev_ordsgn,
3357                     tmp_ordsgn,v, bits, r->block1[i]);
3358        }
3359        break;
3360
3361      case ringorder_S:
3362        rO_Syzcomp(j, j_bits,prev_ordsgn, tmp_ordsgn,tmp_typ[typ_i]);
3363        need_to_add_comp=TRUE;
3364        typ_i++;
3365        break;
3366
3367      case ringorder_s:
3368        rO_Syz(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_unspec:
3374      case ringorder_no:
3375      default:
3376        dReportError("undef. ringorder used\n");
3377        break;
3378    }
3379  }
3380
3381  int j0=j; // save j
3382  int j_bits0=j_bits; // save jbits
3383  rO_Align(j,j_bits);
3384  r->CmpL_Size = j;
3385
3386  j_bits=j_bits0; j=j0;
3387
3388  // fill in some empty slots with variables not already covered
3389  // v0 is special, is therefore normally already covered
3390  // now we do have rings without comp...
3391  if((need_to_add_comp) && (v[0]== -1))
3392  {
3393    if (prev_ordsgn==1)
3394    {
3395      rO_Align(j, j_bits);
3396      rO_LexVars(j, j_bits, 0,0, prev_ordsgn,tmp_ordsgn,v,BITS_PER_LONG, -1);
3397    }
3398    else
3399    {
3400      rO_Align(j, j_bits);
3401      rO_LexVars_neg(j, j_bits, 0,0, prev_ordsgn,tmp_ordsgn,v,BITS_PER_LONG, -1);
3402    }
3403  }
3404  // the variables
3405  for(i=1 ; i<=r->N ; i++)
3406  {
3407    if(v[i]==(-1))
3408    {
3409      if (prev_ordsgn==1)
3410      {
3411        rO_LexVars(j, j_bits, i,i, prev_ordsgn,tmp_ordsgn,v,bits, -1);
3412      }
3413      else
3414      {
3415        rO_LexVars_neg(j,j_bits,i,i, prev_ordsgn,tmp_ordsgn,v,bits, -1);
3416      }
3417    }
3418  }
3419
3420  rO_Align(j,j_bits);
3421  // ----------------------------
3422  // finished with constructing the monomial, computing sizes:
3423
3424  r->ExpL_Size=j;
3425  r->PolyBin = omGetSpecBin(POLYSIZE + (r->ExpL_Size)*sizeof(long));
3426  assume(r->PolyBin != NULL);
3427
3428  // ----------------------------
3429  // indices and ordsgn vector for comparison
3430  //
3431  // r->pCompHighIndex already set
3432  r->ordsgn=(long *)omAlloc0(r->ExpL_Size*sizeof(long));
3433
3434  for(j=0;j<r->CmpL_Size;j++)
3435  {
3436    r->ordsgn[j] = tmp_ordsgn[j];
3437  }
3438
3439  omFreeSize((ADDRESS)tmp_ordsgn,(3*(n+r->N)*sizeof(long)));
3440
3441  // ----------------------------
3442  // description of orderings for setm:
3443  //
3444  r->OrdSize=typ_i;
3445  if (typ_i==0) r->typ=NULL;
3446  else
3447  {
3448    r->typ=(sro_ord*)omAlloc(typ_i*sizeof(sro_ord));
3449    memcpy(r->typ,tmp_typ,typ_i*sizeof(sro_ord));
3450  }
3451  omFreeSize((ADDRESS)tmp_typ,(3*(n+r->N)*sizeof(sro_ord)));
3452
3453  // ----------------------------
3454  // indices for (first copy of ) variable entries in exp.e vector (VarOffset):
3455  r->VarOffset=v;
3456
3457  // ----------------------------
3458  // other indicies
3459  r->pCompIndex=(r->VarOffset[0] & 0xffff); //r->VarOffset[0];
3460  i=0; // position
3461  j=0; // index in r->typ
3462  if (i==r->pCompIndex) i++;
3463  while ((j < r->OrdSize)
3464         && ((r->typ[j].ord_typ==ro_syzcomp) ||
3465             (r->typ[j].ord_typ==ro_syz) ||
3466             (r->order[r->typ[j].order_index] == ringorder_aa)))
3467  {
3468    i++; j++;
3469  }
3470  if (i==r->pCompIndex) i++;
3471  r->pOrdIndex=i;
3472
3473  // ----------------------------
3474  rSetDegStuff(r);
3475  rSetOption(r);
3476  // ----------------------------
3477  // r->p_Setm
3478  r->p_Setm = p_GetSetmProc(r);
3479
3480  // ----------------------------
3481  // set VarL_*
3482  rSetVarL(r);
3483
3484  //  ----------------------------
3485  // right-adjust VarOffset
3486  rRightAdjustVarOffset(r);
3487
3488  // ----------------------------
3489  // set NegWeightL*
3490  rSetNegWeight(r);
3491
3492  // ----------------------------
3493  // p_Procs: call AFTER NegWeightL
3494  r->p_Procs = (p_Procs_s*)omAlloc(sizeof(p_Procs_s));
3495  p_ProcsSet(r, r->p_Procs);
3496  return FALSE;
3497}
3498
3499void rUnComplete(ring r)
3500{
3501  if (r == NULL) return;
3502  if (r->VarOffset != NULL)
3503  {
3504    if (r->PolyBin != NULL)
3505      omUnGetSpecBin(&(r->PolyBin));
3506
3507    omFreeSize((ADDRESS)r->VarOffset, (r->N +1)*sizeof(int));
3508    if (r->order != NULL)
3509    {
3510      if (r->order[0] == ringorder_s && r->typ[0].data.syz.limit > 0)
3511      {
3512        omFreeSize(r->typ[0].data.syz.syz_index,
3513             (r->typ[0].data.syz.limit +1)*sizeof(int));
3514      }
3515    }
3516    if (r->OrdSize!=0 && r->typ != NULL)
3517    {
3518      omFreeSize((ADDRESS)r->typ,r->OrdSize*sizeof(sro_ord));
3519    }
3520    if (r->ordsgn != NULL && r->CmpL_Size != 0)
3521      omFreeSize((ADDRESS)r->ordsgn,r->ExpL_Size*sizeof(long));
3522    if (r->p_Procs != NULL)
3523      omFreeSize(r->p_Procs, sizeof(p_Procs_s));
3524    omfreeSize(r->VarL_Offset, r->VarL_Size*sizeof(int));
3525  }
3526  if (r->NegWeightL_Offset!=NULL)
3527  {
3528    omFreeSize(r->NegWeightL_Offset, r->NegWeightL_Size*sizeof(int));
3529    r->NegWeightL_Offset=NULL;
3530  }
3531}
3532
3533// set r->VarL_Size, r->VarL_Offset, r->VarL_LowIndex
3534static void rSetVarL(ring r)
3535{
3536  int  min = INT_MAX, min_j = -1;
3537  int* VarL_Number = (int*) omAlloc0(r->ExpL_Size*sizeof(int));
3538
3539  int i,j;
3540
3541  // count how often a var long is occupied by an exponent
3542  for (i=1; i<=r->N; i++)
3543  {
3544    VarL_Number[r->VarOffset[i] & 0xffffff]++;
3545  }
3546
3547  // determine how many and min
3548  for (i=0, j=0; i<r->ExpL_Size; i++)
3549  {
3550    if (VarL_Number[i] != 0)
3551    {
3552      if (min > VarL_Number[i])
3553      {
3554        min = VarL_Number[i];
3555        min_j = j;
3556      }
3557      j++;
3558    }
3559  }
3560
3561  r->VarL_Size = j; // number of long with exp. entries in
3562                    //  in p->exp
3563  r->VarL_Offset = (int*) omAlloc(r->VarL_Size*sizeof(int));
3564  r->VarL_LowIndex = 0;
3565
3566  // set VarL_Offset
3567  for (i=0, j=0; i<r->ExpL_Size; i++)
3568  {
3569    if (VarL_Number[i] != 0)
3570    {
3571      r->VarL_Offset[j] = i;
3572      if (j > 0 && r->VarL_Offset[j-1] != r->VarL_Offset[j] - 1)
3573        r->VarL_LowIndex = -1;
3574      j++;
3575    }
3576  }
3577  if (r->VarL_LowIndex >= 0)
3578    r->VarL_LowIndex = r->VarL_Offset[0];
3579
3580  r->MinExpPerLong = min;
3581  if (min_j != 0)
3582  {
3583    j = r->VarL_Offset[min_j];
3584    r->VarL_Offset[min_j] = r->VarL_Offset[0];
3585    r->VarL_Offset[0] = j;
3586  }
3587  omFree(VarL_Number);
3588}
3589
3590static void rRightAdjustVarOffset(ring r)
3591{
3592  int* shifts = (int*) omAlloc(r->ExpL_Size*sizeof(int));
3593  int i;
3594  // initialize shifts
3595  for (i=0;i<r->ExpL_Size;i++)
3596    shifts[i] = BIT_SIZEOF_LONG;
3597
3598  // find minimal bit shift in each long exp entry
3599  for (i=1;i<=r->N;i++)
3600  {
3601    if (shifts[r->VarOffset[i] & 0xffffff] > r->VarOffset[i] >> 24)
3602      shifts[r->VarOffset[i] & 0xffffff] = r->VarOffset[i] >> 24;
3603  }
3604  // reset r->VarOffset: set the minimal shift to 0
3605  for (i=1;i<=r->N;i++)
3606  {
3607    if (shifts[r->VarOffset[i] & 0xffffff] != 0)
3608      r->VarOffset[i]
3609        = (r->VarOffset[i] & 0xffffff) |
3610        (((r->VarOffset[i] >> 24) - shifts[r->VarOffset[i] & 0xffffff]) << 24);
3611  }
3612  omFree(shifts);
3613}
3614
3615// get r->divmask depending on bits per exponent
3616static unsigned long rGetDivMask(int bits)
3617{
3618  unsigned long divmask = 1;
3619  int i = bits;
3620
3621  while (i < BIT_SIZEOF_LONG)
3622  {
3623    divmask |= (((unsigned long) 1) << (unsigned long) i);
3624    i += bits;
3625  }
3626  return divmask;
3627}
3628
3629#ifdef RDEBUG
3630void rDebugPrint(ring r)
3631{
3632  if (r==NULL)
3633  {
3634    PrintS("NULL ?\n");
3635    return;
3636  }
3637  // corresponds to ro_typ from ring.h:
3638  const char *TYP[]={"ro_dp","ro_wp","ro_wp64","ro_wp_neg","ro_cp",
3639                     "ro_syzcomp", "ro_syz", "ro_none"};
3640  int i,j;
3641
3642  Print("ExpL_Size:%d ",r->ExpL_Size);
3643  Print("CmpL_Size:%d ",r->CmpL_Size);
3644  Print("VarL_Size:%d\n",r->VarL_Size);
3645  Print("bitmask=0x%x (expbound=%d) \n",r->bitmask, r->bitmask);
3646  Print("BitsPerExp=%d ExpPerLong=%d MinExpPerLong=%d at L[%d]\n", r->BitsPerExp, r->ExpPerLong, r->MinExpPerLong, r->VarL_Offset[0]);
3647  PrintS("varoffset:\n");
3648  if (r->VarOffset==NULL) PrintS(" NULL\n");
3649  else
3650    for(j=0;j<=r->N;j++)
3651      Print("  v%d at e-pos %d, bit %d\n",
3652            j,r->VarOffset[j] & 0xffffff, r->VarOffset[j] >>24);
3653  Print("divmask=%p\n", r->divmask);
3654  PrintS("ordsgn:\n");
3655  for(j=0;j<r->CmpL_Size;j++)
3656    Print("  ordsgn %d at pos %d\n",r->ordsgn[j],j);
3657  Print("OrdSgn:%d\n",r->OrdSgn);
3658  PrintS("ordrec:\n");
3659  for(j=0;j<r->OrdSize;j++)
3660  {
3661    Print("  typ %s",TYP[r->typ[j].ord_typ]);
3662    Print("  place %d",r->typ[j].data.dp.place);
3663    if (r->typ[j].ord_typ!=ro_syzcomp)
3664    {
3665      Print("  start %d",r->typ[j].data.dp.start);
3666      Print("  end %d",r->typ[j].data.dp.end);
3667      if ((r->typ[j].ord_typ==ro_wp)
3668      || (r->typ[j].ord_typ==ro_wp_neg))
3669      {
3670        Print(" w:");
3671        int l;
3672        for(l=r->typ[j].data.wp.start;l<=r->typ[j].data.wp.end;l++)
3673          Print(" %d",r->typ[j].data.wp.weights[l-r->typ[j].data.wp.start]);
3674      }
3675      else if (r->typ[j].ord_typ==ro_wp64)
3676      {
3677        Print(" w64:");
3678        int l;
3679        for(l=r->typ[j].data.wp64.start;l<=r->typ[j].data.wp64.end;l++)
3680          Print(" %l",(long)(((int64*)r->typ[j].data.wp64.weights64)+l-r->typ[j].data.wp64.start));
3681      }
3682    }
3683    PrintLn();
3684  }
3685  Print("pOrdIndex:%d pCompIndex:%d\n", r->pOrdIndex, r->pCompIndex);
3686  Print("OrdSize:%d\n",r->OrdSize);
3687  PrintS("--------------------\n");
3688  for(j=0;j<r->ExpL_Size;j++)
3689  {
3690    Print("L[%d]: ",j);
3691    if (j< r->CmpL_Size)
3692      Print("ordsgn %d ", r->ordsgn[j]);
3693    else
3694      PrintS("no comp ");
3695    i=1;
3696    for(;i<=r->N;i++)
3697    {
3698      if( (r->VarOffset[i] & 0xffffff) == j )
3699      {  Print("v%d at e[%d], bit %d; ", i,r->VarOffset[i] & 0xffffff,
3700                                         r->VarOffset[i] >>24 ); }
3701    }
3702    if( r->pCompIndex==j ) PrintS("v0; ");
3703    for(i=0;i<r->OrdSize;i++)
3704    {
3705      if (r->typ[i].data.dp.place == j)
3706      {
3707        Print("ordrec:%s (start:%d, end:%d) ",TYP[r->typ[i].ord_typ],
3708          r->typ[i].data.dp.start, r->typ[i].data.dp.end);
3709      }
3710    }
3711
3712    if (j==r->pOrdIndex)
3713      PrintS("pOrdIndex\n");
3714    else
3715      PrintLn();
3716  }
3717
3718  // p_Procs stuff
3719  p_Procs_s proc_names;
3720  const char* field;
3721  const char* length;
3722  const char* ord;
3723  p_Debug_GetProcNames(r, &proc_names); // changes p_Procs!!!
3724  p_Debug_GetSpecNames(r, field, length, ord);
3725
3726  Print("p_Spec  : %s, %s, %s\n", field, length, ord);
3727  PrintS("p_Procs :\n");
3728  for (i=0; i<(int) (sizeof(p_Procs_s)/sizeof(void*)); i++)
3729  {
3730    Print(" %s,\n", ((char**) &proc_names)[i]);
3731  }
3732}
3733
3734void p_DebugPrint(poly p, const ring r)
3735{
3736  int i,j;
3737  p_Write(p,r);
3738  j=2;
3739  while(p!=NULL)
3740  {
3741    Print("\nexp[0..%d]\n",r->ExpL_Size-1);
3742    for(i=0;i<r->ExpL_Size;i++)
3743      Print("%ld ",p->exp[i]);
3744    PrintLn();
3745    Print("v0:%d ",p_GetComp(p, r));
3746    for(i=1;i<=r->N;i++) Print(" v%d:%d",i,p_GetExp(p,i, r));
3747    PrintLn();
3748    pIter(p);
3749    j--;
3750    if (j==0) { PrintS("...\n"); break; }
3751  }
3752}
3753
3754void pDebugPrint(poly p)
3755{
3756  p_DebugPrint(p, currRing);
3757}
3758#endif // RDEBUG
3759
3760
3761/*2
3762* asssume that rComplete was called with r
3763* assume that the first block ist ringorder_S
3764* change the block to reflect the sequence given by appending v
3765*/
3766
3767#ifdef PDEBUG
3768void rDBChangeSComps(int* currComponents,
3769                     long* currShiftedComponents,
3770                     int length,
3771                     ring r)
3772{
3773  r->typ[1].data.syzcomp.length = length;
3774  rNChangeSComps( currComponents, currShiftedComponents, r);
3775}
3776void rDBGetSComps(int** currComponents,
3777                 long** currShiftedComponents,
3778                 int *length,
3779                 ring r)
3780{
3781  *length = r->typ[1].data.syzcomp.length;
3782  rNGetSComps( currComponents, currShiftedComponents, r);
3783}
3784#endif
3785
3786void rNChangeSComps(int* currComponents, long* currShiftedComponents, ring r)
3787{
3788  assume(r->order[1]==ringorder_S);
3789
3790  r->typ[1].data.syzcomp.ShiftedComponents = currShiftedComponents;
3791  r->typ[1].data.syzcomp.Components = currComponents;
3792}
3793
3794void rNGetSComps(int** currComponents, long** currShiftedComponents, ring r)
3795{
3796  assume(r->order[1]==ringorder_S);
3797
3798  *currShiftedComponents = r->typ[1].data.syzcomp.ShiftedComponents;
3799  *currComponents =   r->typ[1].data.syzcomp.Components;
3800}
3801
3802/////////////////////////////////////////////////////////////////////////////
3803//
3804// The following routines all take as input a ring r, and return R
3805// where R has a certain property. R might be equal r in which case r
3806// had already this property
3807//
3808// Without argument, these functions work on currRing and change it,
3809// if necessary
3810
3811// for the time being, this is still here
3812static ring rAssure_SyzComp(ring r, BOOLEAN complete = TRUE);
3813
3814#define MYTEST 0
3815
3816ring rCurrRingAssure_SyzComp()
3817{
3818#ifdef HAVE_PLURAL
3819#if MYTEST
3820  PrintS("rCurrRingAssure_SyzComp(), currRing:  \n");
3821  rWrite(currRing);
3822#ifdef RDEBUG
3823  rDebugPrint(currRing);
3824#endif
3825#endif
3826#endif
3827
3828  ring r = rAssure_SyzComp(currRing);
3829
3830  if (r != currRing)
3831  {
3832    ring old_ring = currRing;
3833    rChangeCurrRing(r);
3834    assume(currRing == r);
3835
3836#ifdef HAVE_PLURAL
3837#if MYTEST
3838    PrintS("rCurrRingAssure_SyzComp(): currRing': ");
3839    rWrite(currRing);
3840#ifdef RDEBUG
3841    rDebugPrint(currRing);
3842#endif
3843#endif
3844#endif
3845
3846
3847    if (old_ring->qideal != NULL)
3848    {
3849      r->qideal = idrCopyR_NoSort(old_ring->qideal, old_ring);
3850      assume(idRankFreeModule(r->qideal) == 0);
3851      currQuotient = r->qideal;
3852
3853#ifdef HAVE_PLURAL
3854      if( rIsPluralRing(r) )
3855        if( nc_SetupQuotient(r, old_ring, true) )
3856        {
3857//          WarnS("error in nc_SetupQuotient"); // cleanup?      rDelete(res);       return r;  // just go on...?
3858        }
3859#endif
3860    }
3861
3862#ifdef HAVE_PLURAL
3863    assume((r->qideal==NULL) == (old_ring->qideal==NULL));
3864    assume(rIsPluralRing(r) == rIsPluralRing(old_ring));
3865    assume(rIsSCA(r) == rIsSCA(old_ring));
3866    assume(ncRingType(r) == ncRingType(old_ring));
3867#endif
3868
3869  }
3870
3871  assume(currRing == r);
3872
3873
3874#ifdef HAVE_PLURAL
3875#if MYTEST
3876  PrintS("\nrCurrRingAssure_SyzComp(): new currRing: \n");
3877  rWrite(currRing);
3878#ifdef RDEBUG
3879  rDebugPrint(currRing);
3880#endif
3881#endif
3882#endif
3883
3884  return r;
3885}
3886
3887static ring rAssure_SyzComp(ring r, BOOLEAN complete)
3888{
3889  if (r->order[0] == ringorder_s) return r;
3890  ring res=rCopy0(r, FALSE, FALSE);
3891  int i=rBlocks(r);
3892  int j;
3893
3894  res->order=(int *)omAlloc((i+1)*sizeof(int));
3895  res->block0=(int *)omAlloc0((i+1)*sizeof(int));
3896  res->block1=(int *)omAlloc0((i+1)*sizeof(int));
3897  int ** wvhdl =(int **)omAlloc0((i+1)*sizeof(int**));
3898  for(j=i;j>0;j--)
3899  {
3900    res->order[j]=r->order[j-1];
3901    res->block0[j]=r->block0[j-1];
3902    res->block1[j]=r->block1[j-1];
3903    if (r->wvhdl[j-1] != NULL)
3904    {
3905      wvhdl[j] = (int*) omMemDup(r->wvhdl[j-1]);
3906    }
3907  }
3908  res->order[0]=ringorder_s;
3909
3910  res->wvhdl = wvhdl;
3911
3912  if (complete)
3913  {
3914    rComplete(res, 1);
3915
3916#ifdef HAVE_PLURAL
3917    if (rIsPluralRing(r))
3918    {
3919      if ( nc_rComplete(r, res, false) ) // no qideal!
3920      {
3921        WarnS("error in nc_rComplete");      // cleanup?//      rDelete(res);//      return r;      // just go on..
3922      }
3923    }
3924    assume(rIsPluralRing(r) == rIsPluralRing(res));
3925#endif
3926  }
3927  return res;
3928}
3929
3930ring rAssure_TDeg(ring r, int start_var, int end_var, int &pos)
3931{
3932  int i;
3933  if (r->typ!=NULL)
3934  {
3935    for(i=r->OrdSize-1;i>=0;i--)
3936    {
3937      if ((r->typ[i].ord_typ==ro_dp)
3938      && (r->typ[i].data.dp.start==start_var)
3939      && (r->typ[i].data.dp.end==end_var))
3940      {
3941        pos=r->typ[i].data.dp.place;
3942        //printf("no change, pos=%d\n",pos);
3943        return r;
3944      }
3945    }
3946  }
3947
3948#ifdef HAVE_PLURAL
3949  nc_struct* save=r->GetNC();
3950  r->GetNC()=NULL;
3951#endif
3952  ring res=rCopy(r);
3953
3954  i=rBlocks(r);
3955  int j;
3956
3957  res->ExpL_Size=r->ExpL_Size+1; // one word more in each monom
3958  res->PolyBin=omGetSpecBin(POLYSIZE + (res->ExpL_Size)*sizeof(long));
3959  omFree((ADDRESS)res->ordsgn);
3960  res->ordsgn=(long *)omAlloc0(res->ExpL_Size*sizeof(long));
3961  for(j=0;j<r->CmpL_Size;j++)
3962  {
3963    res->ordsgn[j] = r->ordsgn[j];
3964  }
3965  res->OrdSize=r->OrdSize+1;   // one block more for pSetm
3966  if (r->typ!=NULL)
3967    omFree((ADDRESS)res->typ);
3968  res->typ=(sro_ord*)omAlloc0(res->OrdSize*sizeof(sro_ord));
3969  if (r->typ!=NULL)
3970    memcpy(res->typ,r->typ,r->OrdSize*sizeof(sro_ord));
3971  // the additional block for pSetm: total degree at the last word
3972  // but not included in the compare part
3973  res->typ[res->OrdSize-1].ord_typ=ro_dp;
3974  res->typ[res->OrdSize-1].data.dp.start=start_var;
3975  res->typ[res->OrdSize-1].data.dp.end=end_var;
3976  res->typ[res->OrdSize-1].data.dp.place=res->ExpL_Size-1;
3977  pos=res->ExpL_Size-1;
3978  //if ((start_var==1) && (end_var==res->N)) res->pOrdIndex=pos;
3979  extern void p_Setm_General(poly p, ring r);
3980  res->p_Setm=p_Setm_General;
3981  // ----------------------------
3982  omFree((ADDRESS)res->p_Procs);
3983  res->p_Procs = (p_Procs_s*)omAlloc(sizeof(p_Procs_s));
3984
3985  p_ProcsSet(res, res->p_Procs);
3986  if (res->qideal!=NULL) id_Delete(&res->qideal,res);
3987#ifdef HAVE_PLURAL
3988  r->GetNC()=save;
3989  if (rIsPluralRing(r))
3990  {
3991    if ( nc_rComplete(r, res, false) ) // no qideal!
3992    {
3993      WarnS("error in nc_rComplete");
3994    // just go on..
3995    }
3996  }
3997#endif
3998  if (r->qideal!=NULL)
3999  {
4000     res->qideal=idrCopyR_NoSort(r->qideal,r);
4001#ifdef HAVE_PLURAL
4002     if (rIsPluralRing(res))
4003     {
4004       nc_SetupQuotient(res, currRing);
4005     }
4006     assume((res->qideal==NULL) == (r->qideal==NULL));
4007#endif
4008  }
4009
4010#ifdef HAVE_PLURAL
4011  assume(rIsPluralRing(res) == rIsPluralRing(r));
4012  assume(rIsSCA(res) == rIsSCA(r));
4013  assume(ncRingType(res) == ncRingType(r));
4014#endif
4015
4016  return res;
4017}
4018
4019ring rAssure_HasComp(ring r)
4020{
4021  int last_block;
4022  int i=0;
4023  do
4024  {
4025     if (r->order[i] == ringorder_c ||
4026        r->order[i] == ringorder_C) return r;
4027     if (r->order[i] == 0)
4028        break;
4029     i++;
4030  } while (1);
4031  //WarnS("re-creating ring with comps");
4032  last_block=i-1;
4033
4034  ring new_r = rCopy0(r, FALSE, FALSE);
4035  i+=2;
4036  new_r->wvhdl=(int **)omAlloc0(i * sizeof(int_ptr));
4037  new_r->order   = (int *) omAlloc0(i * sizeof(int));
4038  new_r->block0   = (int *) omAlloc0(i * sizeof(int));
4039  new_r->block1   = (int *) omAlloc0(i * sizeof(int));
4040  memcpy4(new_r->order,r->order,(i-1) * sizeof(int));
4041  memcpy4(new_r->block0,r->block0,(i-1) * sizeof(int));
4042  memcpy4(new_r->block1,r->block1,(i-1) * sizeof(int));
4043  for (int j=0; j<=last_block; j++)
4044  {
4045    if (r->wvhdl[j]!=NULL)
4046    {
4047      new_r->wvhdl[j] = (int*) omMemDup(r->wvhdl[j]);
4048    }
4049  }
4050  last_block++;
4051  new_r->order[last_block]=ringorder_C;
4052  //new_r->block0[last_block]=0;
4053  //new_r->block1[last_block]=0;
4054  //new_r->wvhdl[last_block]=NULL;
4055
4056  rComplete(new_r, 1);
4057
4058#ifdef HAVE_PLURAL
4059  if (rIsPluralRing(r))
4060  {
4061    if ( nc_rComplete(r, new_r, false) ) // no qideal!
4062    {
4063      WarnS("error in nc_rComplete");      // cleanup?//      rDelete(res);//      return r;      // just go on..
4064    }
4065  }
4066  assume(rIsPluralRing(r) == rIsPluralRing(new_r));
4067#endif
4068
4069  return new_r;
4070}
4071
4072static ring rAssure_CompLastBlock(ring r, BOOLEAN complete = TRUE)
4073{
4074  int last_block = rBlocks(r) - 2;
4075  if (r->order[last_block] != ringorder_c &&
4076      r->order[last_block] != ringorder_C)
4077  {
4078    int c_pos = 0;
4079    int i;
4080
4081    for (i=0; i< last_block; i++)
4082    {
4083      if (r->order[i] == ringorder_c || r->order[i] == ringorder_C)
4084      {
4085        c_pos = i;
4086        break;
4087      }
4088    }
4089    if (c_pos != -1)
4090    {
4091      ring new_r = rCopy0(r, FALSE, TRUE);
4092      for (i=c_pos+1; i<=last_block; i++)
4093      {
4094        new_r->order[i-1] = new_r->order[i];
4095        new_r->block0[i-1] = new_r->block0[i];
4096        new_r->block1[i-1] = new_r->block1[i];
4097        new_r->wvhdl[i-1] = new_r->wvhdl[i];
4098      }
4099      new_r->order[last_block] = r->order[c_pos];
4100      new_r->block0[last_block] = r->block0[c_pos];
4101      new_r->block1[last_block] = r->block1[c_pos];
4102      new_r->wvhdl[last_block] = r->wvhdl[c_pos];
4103      if (complete)
4104      {
4105        rComplete(new_r, 1);
4106
4107#ifdef HAVE_PLURAL
4108        if (rIsPluralRing(r))
4109        {
4110          if ( nc_rComplete(r, new_r, false) ) // no qideal!
4111          {
4112            WarnS("error in nc_rComplete");   // cleanup?//      rDelete(res);//      return r;      // just go on..
4113          }
4114        }
4115        assume(rIsPluralRing(r) == rIsPluralRing(new_r));
4116#endif
4117      }
4118      return new_r;
4119    }
4120  }
4121  return r;
4122}
4123
4124ring rCurrRingAssure_CompLastBlock()
4125{
4126  ring new_r = rAssure_CompLastBlock(currRing);
4127  if (currRing != new_r)
4128  {
4129    ring old_r = currRing;
4130    rChangeCurrRing(new_r);
4131    if (old_r->qideal != NULL)
4132    {
4133      new_r->qideal = idrCopyR(old_r->qideal, old_r);
4134      currQuotient = new_r->qideal;
4135#ifdef HAVE_PLURAL
4136      if( rIsPluralRing(new_r) )
4137        if( nc_SetupQuotient(new_r, old_r, true) )
4138        {
4139          WarnS("error in nc_SetupQuotient"); // cleanup?      rDelete(res);       return r;  // just go on...?
4140        }
4141      assume((new_r->qideal==NULL) == (old_r->qideal==NULL));
4142      assume(rIsPluralRing(new_r) == rIsPluralRing(old_r));
4143      assume(rIsSCA(new_r) == rIsSCA(old_r));
4144      assume(ncRingType(new_r) == ncRingType(old_r));
4145#endif
4146    }
4147    rTest(new_r);
4148    rTest(old_r);
4149  }
4150  return new_r;
4151}
4152
4153ring rCurrRingAssure_SyzComp_CompLastBlock()
4154{
4155  ring new_r_1 = rAssure_CompLastBlock(currRing, FALSE);
4156  ring new_r = rAssure_SyzComp(new_r_1, FALSE);
4157
4158  if (new_r != currRing)
4159  {
4160    ring old_r = currRing;
4161    if (new_r_1 != new_r && new_r_1 != old_r) rDelete(new_r_1);
4162    rComplete(new_r, 1);
4163#ifdef HAVE_PLURAL
4164    if (rIsPluralRing(old_r))
4165    {
4166      if ( nc_rComplete(old_r, new_r, false) ) // no qideal!
4167      {
4168        WarnS("error in nc_rComplete"); // cleanup?      rDelete(res);       return r;  // just go on...?
4169      }
4170    }
4171    assume(rIsPluralRing(new_r) == rIsPluralRing(old_r));
4172#endif
4173    rChangeCurrRing(new_r);
4174    if (old_r->qideal != NULL)
4175    {
4176      new_r->qideal = idrCopyR(old_r->qideal, old_r);
4177      currQuotient = new_r->qideal;
4178
4179#ifdef HAVE_PLURAL
4180      if( rIsPluralRing(old_r) )
4181        if( nc_SetupQuotient(new_r, old_r, true) )
4182        {
4183          WarnS("error in nc_SetupQuotient"); // cleanup?      rDelete(res);       return r;  // just go on...?
4184        }
4185      assume((new_r->qideal==NULL) == (old_r->qideal==NULL));
4186      assume(rIsPluralRing(new_r) == rIsPluralRing(old_r));
4187      assume(rIsSCA(new_r) == rIsSCA(old_r));
4188      assume(ncRingType(new_r) == ncRingType(old_r));
4189#endif
4190    }
4191    rTest(new_r);
4192    rTest(old_r);
4193  }
4194  return new_r;
4195}
4196
4197// use this for global orderings consisting of two blocks
4198static ring rCurrRingAssure_Global(rRingOrder_t b1, rRingOrder_t b2)
4199{
4200  int r_blocks = rBlocks(currRing);
4201  int i;
4202
4203  assume(b1 == ringorder_c || b1 == ringorder_C ||
4204         b2 == ringorder_c || b2 == ringorder_C ||
4205         b2 == ringorder_S);
4206  if ((r_blocks == 3) &&
4207      (currRing->order[0] == b1) &&
4208      (currRing->order[1] == b2) &&
4209      (currRing->order[2] == 0))
4210    return currRing;
4211  ring res = rCopy0(currRing, TRUE, FALSE);
4212  res->order = (int*)omAlloc0(3*sizeof(int));
4213  res->block0 = (int*)omAlloc0(3*sizeof(int));
4214  res->block1 = (int*)omAlloc0(3*sizeof(int));
4215  res->wvhdl = (int**)omAlloc0(3*sizeof(int*));
4216  res->order[0] = b1;
4217  res->order[1] = b2;
4218  if (b1 == ringorder_c || b1 == ringorder_C)
4219  {
4220    res->block0[1] = 1;
4221    res->block1[1] = currRing->N;
4222  }
4223  else
4224  {
4225    res->block0[0] = 1;
4226    res->block1[0] = currRing->N;
4227  }
4228  // HANNES: This sould be set in rComplete
4229  res->OrdSgn = 1;
4230  rComplete(res, 1);
4231  rChangeCurrRing(res);
4232  return res;
4233}
4234
4235
4236ring rCurrRingAssure_dp_S()
4237{
4238  return rCurrRingAssure_Global(ringorder_dp, ringorder_S);
4239}
4240
4241ring rCurrRingAssure_dp_C()
4242{
4243  return rCurrRingAssure_Global(ringorder_dp, ringorder_C);
4244}
4245
4246ring rCurrRingAssure_C_dp()
4247{
4248  return rCurrRingAssure_Global(ringorder_C, ringorder_dp);
4249}
4250
4251
4252void rSetSyzComp(int k)
4253{
4254  if (TEST_OPT_PROT) Print("{%d}", k);
4255  if ((currRing->typ!=NULL) && (currRing->typ[0].ord_typ==ro_syz))
4256  {
4257    assume(k > currRing->typ[0].data.syz.limit);
4258    int i;
4259    if (currRing->typ[0].data.syz.limit == 0)
4260    {
4261      currRing->typ[0].data.syz.syz_index = (int*) omAlloc0((k+1)*sizeof(int));
4262      currRing->typ[0].data.syz.syz_index[0] = 0;
4263      currRing->typ[0].data.syz.curr_index = 1;
4264    }
4265    else
4266    {
4267      currRing->typ[0].data.syz.syz_index = (int*)
4268        omReallocSize(currRing->typ[0].data.syz.syz_index,
4269                (currRing->typ[0].data.syz.limit+1)*sizeof(int),
4270                (k+1)*sizeof(int));
4271    }
4272    for (i=currRing->typ[0].data.syz.limit + 1; i<= k; i++)
4273    {
4274      currRing->typ[0].data.syz.syz_index[i] =
4275        currRing->typ[0].data.syz.curr_index;
4276    }
4277    currRing->typ[0].data.syz.limit = k;
4278    currRing->typ[0].data.syz.curr_index++;
4279  }
4280  else if ((currRing->order[0]!=ringorder_c) && (k!=0))
4281  {
4282    dReportError("syzcomp in incompatible ring");
4283  }
4284#ifdef PDEBUG
4285  extern int pDBsyzComp;
4286  pDBsyzComp=k;
4287#endif
4288}
4289
4290// return the max-comonent wchich has syzIndex i
4291int rGetMaxSyzComp(int i)
4292{
4293  if ((currRing->typ!=NULL) && (currRing->typ[0].ord_typ==ro_syz) &&
4294      currRing->typ[0].data.syz.limit > 0 && i > 0)
4295  {
4296    assume(i <= currRing->typ[0].data.syz.limit);
4297    int j;
4298    for (j=0; j<currRing->typ[0].data.syz.limit; j++)
4299    {
4300      if (currRing->typ[0].data.syz.syz_index[j] == i  &&
4301          currRing->typ[0].data.syz.syz_index[j+1] != i)
4302      {
4303        assume(currRing->typ[0].data.syz.syz_index[j+1] == i+1);
4304        return j;
4305      }
4306    }
4307    return currRing->typ[0].data.syz.limit;
4308  }
4309  else
4310  {
4311    return 0;
4312  }
4313}
4314
4315BOOLEAN rRing_is_Homog(ring r)
4316{
4317  if (r == NULL) return FALSE;
4318  int i, j, nb = rBlocks(r);
4319  for (i=0; i<nb; i++)
4320  {
4321    if (r->wvhdl[i] != NULL)
4322    {
4323      int length = r->block1[i] - r->block0[i];
4324      int* wvhdl = r->wvhdl[i];
4325      if (r->order[i] == ringorder_M) length *= length;
4326      assume(omSizeOfAddr(wvhdl) >= length*sizeof(int));
4327
4328      for (j=0; j< length; j++)
4329      {
4330        if (wvhdl[j] != 0 && wvhdl[j] != 1) return FALSE;
4331      }
4332    }
4333  }
4334  return TRUE;
4335}
4336
4337BOOLEAN rRing_has_CompLastBlock(ring r)
4338{
4339  assume(r != NULL);
4340  int lb = rBlocks(r) - 2;
4341  return (r->order[lb] == ringorder_c || r->order[lb] == ringorder_C);
4342}
4343
4344n_coeffType rFieldType(ring r)
4345{
4346  if (rField_is_Zp(r))     return n_Zp;
4347  if (rField_is_Q(r))      return n_Q;
4348  if (rField_is_R(r))      return n_R;
4349  if (rField_is_GF(r))     return n_GF;
4350  if (rField_is_long_R(r)) return n_long_R;
4351  if (rField_is_Zp_a(r))   return n_Zp_a;
4352  if (rField_is_Q_a(r))    return n_Q_a;
4353  if (rField_is_long_C(r)) return n_long_C;
4354  return n_unknown;
4355}
4356
4357int64 * rGetWeightVec(ring r)
4358{
4359  assume(r!=NULL);
4360  assume(r->OrdSize>0);
4361  int i=0;
4362  while((r->typ[i].ord_typ!=ro_wp64) && (r->typ[i].ord_typ>0)) i++;
4363  assume(r->typ[i].ord_typ==ro_wp64);
4364  return (int64*)(r->typ[i].data.wp64.weights64);
4365}
4366
4367void rSetWeightVec(ring r, int64 *wv)
4368{
4369  assume(r!=NULL);
4370  assume(r->OrdSize>0);
4371  assume(r->typ[0].ord_typ==ro_wp64);
4372  memcpy(r->typ[0].data.wp64.weights64,wv,r->N*sizeof(int64));
4373}
4374
4375#include <ctype.h>
4376
4377static int rRealloc1(ring r, ring src, int size, int pos)
4378{
4379  r->order=(int*)omReallocSize(r->order, size*sizeof(int), (size+1)*sizeof(int));
4380  r->block0=(int*)omReallocSize(r->block0, size*sizeof(int), (size+1)*sizeof(int));
4381  r->block1=(int*)omReallocSize(r->block1, size*sizeof(int), (size+1)*sizeof(int));
4382  r->wvhdl=(int_ptr*)omReallocSize(r->wvhdl,size*sizeof(int_ptr), (size+1)*sizeof(int_ptr));
4383  for(int k=size; k>pos; k--) r->wvhdl[k]=r->wvhdl[k-1];
4384  r->order[size]=0;
4385  size++;
4386  return size;
4387}
4388static int rReallocM1(ring r, ring src, int size, int pos)
4389{
4390  r->order=(int*)omReallocSize(r->order, size*sizeof(int), (size-1)*sizeof(int));
4391  r->block0=(int*)omReallocSize(r->block0, size*sizeof(int), (size-1)*sizeof(int));
4392  r->block1=(int*)omReallocSize(r->block1, size*sizeof(int), (size-1)*sizeof(int));
4393  r->wvhdl=(int_ptr*)omReallocSize(r->wvhdl,size*sizeof(int_ptr), (size-1)*sizeof(int_ptr));
4394  for(int k=pos+1; k<size; k++) r->wvhdl[k]=r->wvhdl[k+1];
4395  size--;
4396  return size;
4397}
4398static void rOppWeight(int *w, int l)
4399{
4400  int i2=(l+1)/2;
4401  for(int j=0; j<=i2; j++)
4402  {
4403    int t=w[j];
4404    w[j]=w[l-j];
4405    w[l-j]=t;
4406  }
4407}
4408
4409#define rOppVar(R,I) (rVar(R)+1-I)
4410
4411ring rOpposite(ring src)
4412  /* creates an opposite algebra of R */
4413  /* that is R^opp, where f (*^opp) g = g*f  */
4414  /* treats the case of qring */
4415{
4416  if (src == NULL) return(NULL);
4417  ring save = currRing;
4418  rChangeCurrRing(src);
4419
4420  ring r = rCopy0(src,TRUE); /* TRUE for copy the qideal */
4421  /*  rChangeCurrRing(r); */
4422  // change vars v1..vN -> vN..v1
4423  int i;
4424  int i2 = (rVar(r)-1)/2;
4425  for(i=i2; i>=0; i--)
4426  {
4427    // index: 0..N-1
4428    //Print("ex var names: %d <-> %d\n",i,rOppVar(r,i));
4429    // exchange names
4430    char *p;
4431    p = r->names[rVar(r)-1-i];
4432    r->names[rVar(r)-1-i] = r->names[i];
4433    r->names[i] = p;
4434  }
4435//  i2=(rVar(r)+1)/2;
4436//  for(int i=i2; i>0; i--)
4437//  {
4438//    // index: 1..N
4439//    //Print("ex var places: %d <-> %d\n",i,rVar(r)+1-i);
4440//    // exchange VarOffset
4441//    int t;
4442//    t=r->VarOffset[i];
4443//    r->VarOffset[i]=r->VarOffset[rOppVar(r,i)];
4444//    r->VarOffset[rOppVar(r,i)]=t;
4445//  }
4446  // change names:
4447  for (i=rVar(r)-1; i>=0; i--)
4448  {
4449    char *p=r->names[i];
4450    if(isupper(*p)) *p = tolower(*p);
4451    else            *p = toupper(*p);
4452  }
4453  // change ordering: listing
4454  // change ordering: compare
4455//  for(i=0; i<r->OrdSize; i++)
4456//  {
4457//    int t,tt;
4458//    switch(r->typ[i].ord_typ)
4459//    {
4460//      case ro_dp:
4461//      //
4462//        t=r->typ[i].data.dp.start;
4463//        r->typ[i].data.dp.start=rOppVar(r,r->typ[i].data.dp.end);
4464//        r->typ[i].data.dp.end=rOppVar(r,t);
4465//        break;
4466//      case ro_wp:
4467//      case ro_wp_neg:
4468//      {
4469//        t=r->typ[i].data.wp.start;
4470//        r->typ[i].data.wp.start=rOppVar(r,r->typ[i].data.wp.end);
4471//        r->typ[i].data.wp.end=rOppVar(r,t);
4472//        // invert r->typ[i].data.wp.weights
4473//        rOppWeight(r->typ[i].data.wp.weights,
4474//                   r->typ[i].data.wp.end-r->typ[i].data.wp.start);
4475//        break;
4476//      }
4477//      //case ro_wp64:
4478//      case ro_syzcomp:
4479//      case ro_syz:
4480//         WerrorS("not implemented in rOpposite");
4481//         // should not happen
4482//         break;
4483//
4484//      case ro_cp:
4485//        t=r->typ[i].data.cp.start;
4486//        r->typ[i].data.cp.start=rOppVar(r,r->typ[i].data.cp.end);
4487//        r->typ[i].data.cp.end=rOppVar(r,t);
4488//        break;
4489//      case ro_none:
4490//      default:
4491//       Werror("unknown type in rOpposite(%d)",r->typ[i].ord_typ);
4492//       break;
4493//    }
4494//  }
4495  // Change order/block structures (needed for rPrint, rAdd etc.)
4496  int j=0;
4497  int l=rBlocks(src);
4498  for(i=0; src->order[i]!=0; i++)
4499  {
4500    switch (src->order[i])
4501    {
4502      case ringorder_c: /* c-> c */
4503      case ringorder_C: /* C-> C */
4504      case ringorder_no /*=0*/: /* end-of-block */
4505        r->order[j]=src->order[i];
4506        j++; break;
4507      case ringorder_lp: /* lp -> rp */
4508        r->order[j]=ringorder_rp;
4509        r->block0[j]=rOppVar(r, src->block1[i]);
4510        r->block1[j]=rOppVar(r, src->block0[i]);
4511        break;
4512      case ringorder_rp: /* rp -> lp */
4513        r->order[j]=ringorder_lp;
4514        r->block0[j]=rOppVar(r, src->block1[i]);
4515        r->block1[j]=rOppVar(r, src->block0[i]);
4516        break;
4517      case ringorder_dp: /* dp -> a(1..1),ls */
4518      {
4519        l=rRealloc1(r,src,l,j);
4520        r->order[j]=ringorder_a;
4521        r->block0[j]=rOppVar(r, src->block1[i]);
4522        r->block1[j]=rOppVar(r, src->block0[i]);
4523        r->wvhdl[j]=(int*)omAlloc((r->block1[j]-r->block0[j]+1)*sizeof(int));
4524        for(int k=r->block0[j]; k<=r->block1[j]; k++)
4525          r->wvhdl[j][k-r->block0[j]]=1;
4526        j++;
4527        r->order[j]=ringorder_ls;
4528        r->block0[j]=rOppVar(r, src->block1[i]);
4529        r->block1[j]=rOppVar(r, src->block0[i]);
4530        j++;
4531        break;
4532      }
4533      case ringorder_Dp: /* Dp -> a(1..1),rp */
4534      {
4535        l=rRealloc1(r,src,l,j);
4536        r->order[j]=ringorder_a;
4537        r->block0[j]=rOppVar(r, src->block1[i]);
4538        r->block1[j]=rOppVar(r, src->block0[i]);
4539        r->wvhdl[j]=(int*)omAlloc((r->block1[j]-r->block0[j]+1)*sizeof(int));
4540        for(int k=r->block0[j]; k<=r->block1[j]; k++)
4541          r->wvhdl[j][k-r->block0[j]]=1;
4542        j++;
4543        r->order[j]=ringorder_rp;
4544        r->block0[j]=rOppVar(r, src->block1[i]);
4545        r->block1[j]=rOppVar(r, src->block0[i]);
4546        j++;
4547        break;
4548      }
4549      case ringorder_wp: /* wp -> a(...),ls */
4550      {
4551        l=rRealloc1(r,src,l,j);
4552        r->order[j]=ringorder_a;
4553        r->block0[j]=rOppVar(r, src->block1[i]);
4554        r->block1[j]=rOppVar(r, src->block0[i]);
4555        r->wvhdl[j]=r->wvhdl[j+1]; r->wvhdl[j+1]=r->wvhdl[j+1]=NULL;
4556        rOppWeight(r->wvhdl[j], r->block1[j]-r->block0[j]);
4557        j++;
4558        r->order[j]=ringorder_ls;
4559        r->block0[j]=rOppVar(r, src->block1[i]);
4560        r->block1[j]=rOppVar(r, src->block0[i]);
4561        j++;
4562        break;
4563      }
4564      case ringorder_Wp: /* Wp -> a(...),rp */
4565      {
4566        l=rRealloc1(r,src,l,j);
4567        r->order[j]=ringorder_a;
4568        r->block0[j]=rOppVar(r, src->block1[i]);
4569        r->block1[j]=rOppVar(r, src->block0[i]);
4570        r->wvhdl[j]=r->wvhdl[j+1]; r->wvhdl[j+1]=r->wvhdl[j+1]=NULL;
4571        rOppWeight(r->wvhdl[j], r->block1[j]-r->block0[j]);
4572        j++;
4573        r->order[j]=ringorder_rp;
4574        r->block0[j]=rOppVar(r, src->block1[i]);
4575        r->block1[j]=rOppVar(r, src->block0[i]);
4576        j++;
4577        break;
4578      }
4579      case ringorder_M: /* M -> M */
4580      {
4581        r->order[j]=ringorder_M;
4582        r->block0[j]=rOppVar(r, src->block1[i]);
4583        r->block1[j]=rOppVar(r, src->block0[i]);
4584        int n=r->block1[j]-r->block0[j];
4585        /* M is a (n+1)x(n+1) matrix */
4586        for (int nn=0; nn<=n; nn++)
4587        {
4588          rOppWeight(&(r->wvhdl[j][nn*(n+1)]), n /*r->block1[j]-r->block0[j]*/);
4589        }
4590        j++;
4591        break;
4592      }
4593      case ringorder_a: /*  a(...),ls -> wp/dp */
4594      {
4595        r->block0[j]=rOppVar(r, src->block1[i]);
4596        r->block1[j]=rOppVar(r, src->block0[i]);
4597        rOppWeight(r->wvhdl[j], r->block1[j]-r->block0[j]);
4598        if (src->order[i+1]==ringorder_ls)
4599        {
4600          r->order[j]=ringorder_wp;
4601          i++;
4602          //l=rReallocM1(r,src,l,j);
4603        }
4604        else
4605        {
4606          r->order[j]=ringorder_a;
4607        }
4608        j++;
4609        break;
4610      }
4611      // not yet done:
4612      case ringorder_ls:
4613      case ringorder_rs:
4614      case ringorder_ds:
4615      case ringorder_Ds:
4616      case ringorder_ws:
4617      case ringorder_Ws:
4618      // should not occur:
4619      case ringorder_S:
4620      case ringorder_s:
4621      case ringorder_aa:
4622      case ringorder_L:
4623      case ringorder_unspec:
4624        Werror("order %s not (yet) supported", rSimpleOrdStr(src->order[i]));
4625        break;
4626    }
4627  }
4628  rComplete(r);
4629
4630
4631#ifdef RDEBUG
4632  //   rDebugPrint(r);
4633  rTest(r);
4634#endif
4635
4636  rChangeCurrRing(r);
4637
4638#ifdef HAVE_PLURAL
4639  // now, we initialize a non-comm structure on r
4640  if (rIsPluralRing(src))
4641  {
4642    assume( currRing == r);
4643
4644    int *perm       = (int *)omAlloc0((rVar(r)+1)*sizeof(int));
4645    int *par_perm   = NULL;
4646    nMapFunc nMap   = nSetMap(src);
4647    int ni,nj;
4648    for(i=1; i<=r->N; i++)
4649    {
4650      perm[i] = rOppVar(r,i);
4651    }
4652
4653    matrix C = mpNew(rVar(r),rVar(r));
4654    matrix D = mpNew(rVar(r),rVar(r));
4655
4656    for (i=1; i< rVar(r); i++)
4657    {
4658      for (j=i+1; j<=rVar(r); j++)
4659      {
4660        ni = r->N +1 - i;
4661        nj = r->N +1 - j; /* i<j ==>   nj < ni */
4662
4663        assume(MATELEM(src->GetNC()->C,i,j) != NULL);
4664        MATELEM(C,nj,ni) = pPermPoly(MATELEM(src->GetNC()->C,i,j),perm,src,nMap,par_perm,src->P);
4665
4666        if(MATELEM(src->GetNC()->D,i,j) != NULL)
4667          MATELEM(D,nj,ni) = pPermPoly(MATELEM(src->GetNC()->D,i,j),perm,src,nMap,par_perm,src->P);
4668      }
4669    }
4670
4671    idTest((ideal)C);
4672    idTest((ideal)D);
4673
4674    if (nc_CallPlural(C, D, NULL, NULL, r, false, false, true, r))
4675      WarnS("Error initializing non-commutative multiplication!");
4676
4677    assume( r->GetNC()->IsSkewConstant == src->GetNC()->IsSkewConstant);
4678    assume( ncRingType(r) == ncRingType(src) );
4679
4680    omFreeSize((ADDRESS)perm,(rVar(r)+1)*sizeof(int));
4681
4682    rChangeCurrRing(save);
4683
4684  }
4685#endif /* HAVE_PLURAL */
4686
4687
4688  /* now oppose the qideal for qrings */
4689  if (src->qideal != NULL)
4690  {
4691    idDelete(&(r->qideal));
4692#ifdef HAVE_PLURAL
4693    r->qideal = idOppose(src, src->qideal);
4694#else
4695    r->qideal = idCopy( src->qideal);
4696#endif
4697
4698
4699#ifdef HAVE_PLURAL
4700    if( rIsPluralRing(r) )
4701      nc_SetupQuotient(r);
4702#endif
4703  }
4704
4705  rTest(r);
4706
4707  rChangeCurrRing(save);
4708  return r;
4709}
4710
4711ring rEnvelope(ring R)
4712  /* creates an enveloping algebra of R */
4713  /* that is R^e = R \tensor_K R^opp */
4714{
4715  ring Ropp = rOpposite(R);
4716  ring Renv = NULL;
4717  int stat = rSum(R, Ropp, Renv); /* takes care of qideals */
4718  if ( stat <=0 )
4719    WarnS("Error in rEnvelope at rSum");
4720  rTest(Renv);
4721  return Renv;
4722}
4723
4724#ifdef HAVE_PLURAL
4725BOOLEAN nc_rComplete(const ring src, ring dest, bool bSetupQuotient)
4726/* returns TRUE is there were errors */
4727/* dest is actualy equals src with the different ordering */
4728/* we map src->nc correctly to dest->src */
4729/* to be executed after rComplete, before rChangeCurrRing */
4730{
4731// NOTE: Originally used only by idElimination to transfer NC structure to dest
4732// ring created by dirty hack (without nc_CallPlural)
4733  rTest(src);
4734
4735  assume(!rIsPluralRing(dest)); // destination must be a newly constructed commutative ring
4736
4737  if (!rIsPluralRing(src))
4738  {
4739    return FALSE;
4740  }
4741
4742  const int N = dest->N;
4743
4744  assume(src->N == N);
4745
4746  ring save = currRing;
4747
4748  if (dest != save)
4749    rChangeCurrRing(dest);
4750
4751  const ring srcBase = src->GetNC()->basering;
4752
4753  assume( nSetMap(srcBase) == nSetMap(currRing) ); // currRing is important here!
4754
4755  matrix C = mpNew(N,N); // ring independent
4756  matrix D = mpNew(N,N);
4757
4758  matrix C0 = src->GetNC()->C;
4759  matrix D0 = src->GetNC()->D;
4760
4761
4762  poly p = NULL;
4763  number n = NULL;
4764
4765  // map C and D into dest
4766  for (int i = 1; i < N; i++)
4767  {
4768    for (int j = i + 1; j <= N; j++)
4769    {
4770      const number n = n_Copy(p_GetCoeff(MATELEM(C0,i,j), srcBase), srcBase); // src, mapping for coeffs into currRing = dest!
4771      const poly   p = p_NSet(n, dest);
4772      MATELEM(C,i,j) = p;
4773      if (MATELEM(D0,i,j) != NULL)
4774        MATELEM(D,i,j) = prCopyR(MATELEM(D0,i,j), srcBase, dest); // ?
4775    }
4776  }
4777  /* One must test C and D _only_ in r->GetNC()->basering!!! not in r!!! */
4778
4779  idTest((ideal)C); // in dest!
4780  idTest((ideal)D);
4781
4782  if (nc_CallPlural(C, D, NULL, NULL, dest, bSetupQuotient, false, true, dest)) // also takes care about quotient ideal
4783  {
4784    //WarnS("Error transferring non-commutative structure");
4785    // error message should be in the interpreter interface
4786
4787    mpDelete(&C, dest);
4788    mpDelete(&D, dest);
4789
4790    if (currRing != save)
4791       rChangeCurrRing(save);
4792
4793    return TRUE;
4794  }
4795
4796//  mpDelete(&C, dest); // used by nc_CallPlural!
4797//  mpDelete(&D, dest);
4798
4799  if (dest != save)
4800    rChangeCurrRing(save);
4801
4802  assume(rIsPluralRing(dest));
4803  return FALSE;
4804}
4805#endif
4806
4807void rModify_a_to_A(ring r)
4808// to be called BEFORE rComplete:
4809// changes every Block with a(...) to A(...)
4810{
4811   int i=0;
4812   int j;
4813   while(r->order[i]!=0)
4814   {
4815      if (r->order[i]==ringorder_a)
4816      {
4817        r->order[i]=ringorder_a64;
4818        int *w=r->wvhdl[i];
4819        int64 *w64=(int64 *)omAlloc((r->block1[i]-r->block0[i]+1)*sizeof(int64));
4820        for(j=r->block1[i]-r->block0[i];j>=0;j--)
4821                w64[j]=(int64)w[j];
4822        r->wvhdl[i]=(int*)w64;
4823        omFreeSize(w,(r->block1[i]-r->block0[i]+1)*sizeof(int));
4824      }
4825      i++;
4826   }
4827}
Note: See TracBrowser for help on using the repository browser.