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

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