source: git/libpolys/polys/monomials/ring.cc @ 131ab78

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