source: git/libpolys/polys/monomials/ring.cc @ 215353e

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