source: git/kernel/ring.cc @ 699dcd

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