source: git/kernel/ring.cc @ 8099fc

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