source: git/kernel/ring.cc @ 414c7d0

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