source: git/libpolys/polys/monomials/ring.cc @ 141342

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