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

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