source: git/kernel/ring.cc @ 223636

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