source: git/kernel/ring.cc @ 87beab7

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