source: git/kernel/ring.cc @ e1b9e59

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