source: git/libpolys/polys/monomials/ring.cc @ 17b0b05

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