source: git/kernel/ring.cc @ 83bde7

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