source: git/kernel/ring.cc @ 8261b2

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