source: git/kernel/ring.cc @ 7d9b62

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