source: git/libpolys/polys/monomials/ring.cc @ 6fcddc9

spielwiese
Last change on this file since 6fcddc9 was 30664c, checked in by Oleksandr Motsak <motsak@…>, 11 years ago
Warning elimination + cosmetics
  • Property mode set to 100644
File size: 141.7 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
5* ABSTRACT - the interpreter related ring operations
6*/
7
8/* includes */
9#include <math.h>
10
11#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_am)
1933          || (o->ord_typ == ro_isTemp))
1934        return TRUE;
1935    }
1936  }
1937  return FALSE;
1938}
1939
1940// return TRUE if p->exp[r->pOrdIndex] holds total degree of p */
1941BOOLEAN rOrd_is_Totaldegree_Ordering(ring r)
1942{
1943  // Hmm.... what about Syz orderings?
1944  return (rVar(r) > 1 &&
1945          ((rHasSimpleOrder(r) &&
1946           (rOrder_is_DegOrdering((rRingOrder_t)r->order[0]) ||
1947            rOrder_is_DegOrdering(( rRingOrder_t)r->order[1]))) ||
1948           (rHasSimpleOrderAA(r) &&
1949            (rOrder_is_DegOrdering((rRingOrder_t)r->order[1]) ||
1950             rOrder_is_DegOrdering((rRingOrder_t)r->order[2])))));
1951}
1952
1953// return TRUE if p->exp[r->pOrdIndex] holds a weighted degree of p */
1954BOOLEAN rOrd_is_WeightedDegree_Ordering(ring r )
1955{
1956  // Hmm.... what about Syz orderings?
1957  return ((rVar(r) > 1) &&
1958          rHasSimpleOrder(r) &&
1959          (rOrder_is_WeightedOrdering((rRingOrder_t)r->order[0]) ||
1960           rOrder_is_WeightedOrdering(( rRingOrder_t)r->order[1])));
1961}
1962
1963BOOLEAN rIsPolyVar(int v, ring r)
1964{
1965  int  i=0;
1966  while(r->order[i]!=0)
1967  {
1968    if((r->block0[i]<=v)
1969    && (r->block1[i]>=v))
1970    {
1971      switch(r->order[i])
1972      {
1973        case ringorder_a:
1974          return (r->wvhdl[i][v-r->block0[i]]>0);
1975        case ringorder_M:
1976          return 2; /*don't know*/
1977        case ringorder_a64: /* assume: all weight are non-negative!*/
1978        case ringorder_lp:
1979        case ringorder_rs:
1980        case ringorder_dp:
1981        case ringorder_Dp:
1982        case ringorder_wp:
1983        case ringorder_Wp:
1984          return TRUE;
1985        case ringorder_ls:
1986        case ringorder_ds:
1987        case ringorder_Ds:
1988        case ringorder_ws:
1989        case ringorder_Ws:
1990          return FALSE;
1991        default:
1992          break;
1993      }
1994    }
1995    i++;
1996  }
1997  return 3; /* could not find var v*/
1998}
1999
2000#ifdef RDEBUG
2001// This should eventually become a full-fledge ring check, like pTest
2002BOOLEAN rDBTest(ring r, const char* fn, const int l)
2003{
2004  int i,j;
2005
2006  if (r == NULL)
2007  {
2008    dReportError("Null ring in %s:%d", fn, l);
2009    return FALSE;
2010  }
2011
2012
2013  if (r->N == 0) return TRUE;
2014
2015//  omCheckAddrSize(r,sizeof(ip_sring));
2016#if OM_CHECK > 0
2017  i=rBlocks(r);
2018  omCheckAddrSize(r->order,i*sizeof(int));
2019  omCheckAddrSize(r->block0,i*sizeof(int));
2020  omCheckAddrSize(r->block1,i*sizeof(int));
2021  if (r->wvhdl!=NULL)
2022  {
2023    omCheckAddrSize(r->wvhdl,i*sizeof(int *));
2024    for (j=0;j<i; j++)
2025    {
2026      if (r->wvhdl[j] != NULL) omCheckAddr(r->wvhdl[j]);
2027    }
2028  }
2029#endif
2030  if (r->VarOffset == NULL)
2031  {
2032    dReportError("Null ring VarOffset -- no rComplete (?) in n %s:%d", fn, l);
2033    return FALSE;
2034  }
2035  omCheckAddrSize(r->VarOffset,(r->N+1)*sizeof(int));
2036
2037  if ((r->OrdSize==0)!=(r->typ==NULL))
2038  {
2039    dReportError("mismatch OrdSize and typ-pointer in %s:%d");
2040    return FALSE;
2041  }
2042  omcheckAddrSize(r->typ,r->OrdSize*sizeof(*(r->typ)));
2043  omCheckAddrSize(r->VarOffset,(r->N+1)*sizeof(*(r->VarOffset)));
2044  // test assumptions:
2045  for(i=0;i<=r->N;i++) // for all variables (i = 0..N)
2046  {
2047    if(r->typ!=NULL)
2048    {
2049      for(j=0;j<r->OrdSize;j++) // for all ordering blocks (j =0..OrdSize-1)
2050      {
2051        if(r->typ[j].ord_typ == ro_isTemp)
2052        {
2053          const int p = r->typ[j].data.isTemp.suffixpos;
2054
2055          if(p <= j)
2056            dReportError("ordrec prefix %d is unmatched",j);
2057
2058          assume( p < r->OrdSize );
2059
2060          if(r->typ[p].ord_typ != ro_is)
2061            dReportError("ordrec prefix %d is unmatched (suffix: %d is wrong!!!)",j, p);
2062
2063          // Skip all intermediate blocks for undone variables:
2064          if(r->typ[j].data.isTemp.pVarOffset[i] != -1) // Check i^th variable
2065          {
2066            j = p - 1; // SKIP ALL INTERNAL BLOCKS...???
2067            continue; // To make for check OrdSize bound...
2068          }
2069        }
2070        else if (r->typ[j].ord_typ == ro_is)
2071        {
2072          // Skip all intermediate blocks for undone variables:
2073          if(r->typ[j].data.is.pVarOffset[i] != -1)
2074          {
2075            // TODO???
2076          }
2077
2078        }
2079        else
2080        {
2081          if (r->typ[j].ord_typ==ro_cp)
2082          {
2083            if(((short)r->VarOffset[i]) == r->typ[j].data.cp.place)
2084              dReportError("ordrec %d conflicts with var %d",j,i);
2085          }
2086          else
2087            if ((r->typ[j].ord_typ!=ro_syzcomp)
2088            && (r->VarOffset[i] == r->typ[j].data.dp.place))
2089              dReportError("ordrec %d conflicts with var %d",j,i);
2090        }
2091      }
2092    }
2093    int tmp;
2094      tmp=r->VarOffset[i] & 0xffffff;
2095      #if SIZEOF_LONG == 8
2096        if ((r->VarOffset[i] >> 24) >63)
2097      #else
2098        if ((r->VarOffset[i] >> 24) >31)
2099      #endif
2100          dReportError("bit_start out of range:%d",r->VarOffset[i] >> 24);
2101      if (i > 0 && ((tmp<0) ||(tmp>r->ExpL_Size-1)))
2102      {
2103        dReportError("varoffset out of range for var %d: %d",i,tmp);
2104      }
2105  }
2106  if(r->typ!=NULL)
2107  {
2108    for(j=0;j<r->OrdSize;j++)
2109    {
2110      if ((r->typ[j].ord_typ==ro_dp)
2111      || (r->typ[j].ord_typ==ro_wp)
2112      || (r->typ[j].ord_typ==ro_wp_neg))
2113      {
2114        if (r->typ[j].data.dp.start > r->typ[j].data.dp.end)
2115          dReportError("in ordrec %d: start(%d) > end(%d)",j,
2116            r->typ[j].data.dp.start, r->typ[j].data.dp.end);
2117        if ((r->typ[j].data.dp.start < 1)
2118        || (r->typ[j].data.dp.end > r->N))
2119          dReportError("in ordrec %d: start(%d)<1 or end(%d)>vars(%d)",j,
2120            r->typ[j].data.dp.start, r->typ[j].data.dp.end,r->N);
2121      }
2122    }
2123  }
2124
2125  assume(r != NULL);
2126  assume(r->cf != NULL);
2127 
2128  if (nCoeff_is_algExt(r->cf))
2129  {
2130    assume(r->cf->extRing != NULL);
2131    assume(r->cf->extRing->qideal != NULL);
2132    omCheckAddr(r->cf->extRing->qideal->m[0]);
2133  }
2134
2135  //assume(r->cf!=NULL);
2136
2137  return TRUE;
2138}
2139#endif
2140
2141static void rO_Align(int &place, int &bitplace)
2142{
2143  // increment place to the next aligned one
2144  // (count as Exponent_t,align as longs)
2145  if (bitplace!=BITS_PER_LONG)
2146  {
2147    place++;
2148    bitplace=BITS_PER_LONG;
2149  }
2150}
2151
2152static void rO_TDegree(int &place, int &bitplace, int start, int end,
2153    long *o, sro_ord &ord_struct)
2154{
2155  // degree (aligned) of variables v_start..v_end, ordsgn 1
2156  rO_Align(place,bitplace);
2157  ord_struct.ord_typ=ro_dp;
2158  ord_struct.data.dp.start=start;
2159  ord_struct.data.dp.end=end;
2160  ord_struct.data.dp.place=place;
2161  o[place]=1;
2162  place++;
2163  rO_Align(place,bitplace);
2164}
2165
2166static void rO_TDegree_neg(int &place, int &bitplace, int start, int end,
2167    long *o, sro_ord &ord_struct)
2168{
2169  // degree (aligned) of variables v_start..v_end, ordsgn -1
2170  rO_Align(place,bitplace);
2171  ord_struct.ord_typ=ro_dp;
2172  ord_struct.data.dp.start=start;
2173  ord_struct.data.dp.end=end;
2174  ord_struct.data.dp.place=place;
2175  o[place]=-1;
2176  place++;
2177  rO_Align(place,bitplace);
2178}
2179
2180static void rO_WDegree(int &place, int &bitplace, int start, int end,
2181    long *o, sro_ord &ord_struct, int *weights)
2182{
2183  // weighted degree (aligned) of variables v_start..v_end, ordsgn 1
2184  while((start<end) && (weights[0]==0)) { start++; weights++; }
2185  while((start<end) && (weights[end-start]==0)) { end--; }
2186  int i;
2187  int pure_tdeg=1;
2188  for(i=start;i<=end;i++)
2189  {
2190    if(weights[i-start]!=1)
2191    {
2192      pure_tdeg=0;
2193      break;
2194    }
2195  }
2196  if (pure_tdeg)
2197  {
2198    rO_TDegree(place,bitplace,start,end,o,ord_struct);
2199    return;
2200  }
2201  rO_Align(place,bitplace);
2202  ord_struct.ord_typ=ro_wp;
2203  ord_struct.data.wp.start=start;
2204  ord_struct.data.wp.end=end;
2205  ord_struct.data.wp.place=place;
2206  ord_struct.data.wp.weights=weights;
2207  o[place]=1;
2208  place++;
2209  rO_Align(place,bitplace);
2210  for(i=start;i<=end;i++)
2211  {
2212    if(weights[i-start]<0)
2213    {
2214      ord_struct.ord_typ=ro_wp_neg;
2215      break;
2216    }
2217  }
2218}
2219
2220static void rO_WMDegree(int &place, int &bitplace, int start, int end,
2221    long *o, sro_ord &ord_struct, int *weights)
2222{
2223  assume(weights != NULL);
2224 
2225  // weighted degree (aligned) of variables v_start..v_end, ordsgn 1
2226//  while((start<end) && (weights[0]==0)) { start++; weights++; }
2227//  while((start<end) && (weights[end-start]==0)) { end--; }
2228  rO_Align(place,bitplace);
2229  ord_struct.ord_typ=ro_am;
2230  ord_struct.data.am.start=start;
2231  ord_struct.data.am.end=end;
2232  ord_struct.data.am.place=place;
2233  ord_struct.data.am.weights=weights;
2234  ord_struct.data.am.weights_m = weights + (end-start+1);
2235  ord_struct.data.am.len_gen=weights[end-start+1];
2236  assume( ord_struct.data.am.weights_m[0] == ord_struct.data.am.len_gen );
2237  o[place]=1;
2238  place++;
2239  rO_Align(place,bitplace);
2240}
2241
2242static void rO_WDegree64(int &place, int &bitplace, int start, int end,
2243    long *o, sro_ord &ord_struct, int64 *weights)
2244{
2245  // weighted degree (aligned) of variables v_start..v_end, ordsgn 1,
2246  // reserved 2 places
2247  rO_Align(place,bitplace);
2248  ord_struct.ord_typ=ro_wp64;
2249  ord_struct.data.wp64.start=start;
2250  ord_struct.data.wp64.end=end;
2251  ord_struct.data.wp64.place=place;
2252  ord_struct.data.wp64.weights64=weights;
2253  o[place]=1;
2254  place++;
2255  o[place]=1;
2256  place++;
2257  rO_Align(place,bitplace);
2258}
2259
2260static void rO_WDegree_neg(int &place, int &bitplace, int start, int end,
2261    long *o, sro_ord &ord_struct, int *weights)
2262{
2263  // weighted degree (aligned) of variables v_start..v_end, ordsgn -1
2264  while((start<end) && (weights[0]==0)) { start++; weights++; }
2265  while((start<end) && (weights[end-start]==0)) { end--; }
2266  rO_Align(place,bitplace);
2267  ord_struct.ord_typ=ro_wp;
2268  ord_struct.data.wp.start=start;
2269  ord_struct.data.wp.end=end;
2270  ord_struct.data.wp.place=place;
2271  ord_struct.data.wp.weights=weights;
2272  o[place]=-1;
2273  place++;
2274  rO_Align(place,bitplace);
2275  int i;
2276  for(i=start;i<=end;i++)
2277  {
2278    if(weights[i-start]<0)
2279    {
2280      ord_struct.ord_typ=ro_wp_neg;
2281      break;
2282    }
2283  }
2284}
2285
2286static void rO_LexVars(int &place, int &bitplace, int start, int end,
2287  int &prev_ord, long *o,int *v, int bits, int opt_var)
2288{
2289  // a block of variables v_start..v_end with lex order, ordsgn 1
2290  int k;
2291  int incr=1;
2292  if(prev_ord==-1) rO_Align(place,bitplace);
2293
2294  if (start>end)
2295  {
2296    incr=-1;
2297  }
2298  for(k=start;;k+=incr)
2299  {
2300    bitplace-=bits;
2301    if (bitplace < 0) { bitplace=BITS_PER_LONG-bits; place++; }
2302    o[place]=1;
2303    v[k]= place | (bitplace << 24);
2304    if (k==end) break;
2305  }
2306  prev_ord=1;
2307  if (opt_var!= -1)
2308  {
2309    assume((opt_var == end+1) ||(opt_var == end-1));
2310    if((opt_var != end+1) &&(opt_var != end-1)) WarnS("hier-2");
2311    int save_bitplace=bitplace;
2312    bitplace-=bits;
2313    if (bitplace < 0)
2314    {
2315      bitplace=save_bitplace;
2316      return;
2317    }
2318    // there is enough space for the optional var
2319    v[opt_var]=place | (bitplace << 24);
2320  }
2321}
2322
2323static void rO_LexVars_neg(int &place, int &bitplace, int start, int end,
2324  int &prev_ord, long *o,int *v, int bits, int opt_var)
2325{
2326  // a block of variables v_start..v_end with lex order, ordsgn -1
2327  int k;
2328  int incr=1;
2329  if(prev_ord==1) rO_Align(place,bitplace);
2330
2331  if (start>end)
2332  {
2333    incr=-1;
2334  }
2335  for(k=start;;k+=incr)
2336  {
2337    bitplace-=bits;
2338    if (bitplace < 0) { bitplace=BITS_PER_LONG-bits; place++; }
2339    o[place]=-1;
2340    v[k]=place | (bitplace << 24);
2341    if (k==end) break;
2342  }
2343  prev_ord=-1;
2344//  #if 0
2345  if (opt_var!= -1)
2346  {
2347    assume((opt_var == end+1) ||(opt_var == end-1));
2348    if((opt_var != end+1) &&(opt_var != end-1)) WarnS("hier-1");
2349    int save_bitplace=bitplace;
2350    bitplace-=bits;
2351    if (bitplace < 0)
2352    {
2353      bitplace=save_bitplace;
2354      return;
2355    }
2356    // there is enough space for the optional var
2357    v[opt_var]=place | (bitplace << 24);
2358  }
2359//  #endif
2360}
2361
2362static void rO_Syzcomp(int &place, int &bitplace, int &prev_ord,
2363    long *o, sro_ord &ord_struct)
2364{
2365  // ordering is derived from component number
2366  rO_Align(place,bitplace);
2367  ord_struct.ord_typ=ro_syzcomp;
2368  ord_struct.data.syzcomp.place=place;
2369  ord_struct.data.syzcomp.Components=NULL;
2370  ord_struct.data.syzcomp.ShiftedComponents=NULL;
2371  o[place]=1;
2372  prev_ord=1;
2373  place++;
2374  rO_Align(place,bitplace);
2375}
2376
2377static void rO_Syz(int &place, int &bitplace, int &prev_ord,
2378    long *o, sro_ord &ord_struct)
2379{
2380  // ordering is derived from component number
2381  // let's reserve one Exponent_t for it
2382  if ((prev_ord== 1) || (bitplace!=BITS_PER_LONG))
2383    rO_Align(place,bitplace);
2384  ord_struct.ord_typ=ro_syz;
2385  ord_struct.data.syz.place=place;
2386  ord_struct.data.syz.limit=0;
2387  ord_struct.data.syz.syz_index = NULL;
2388  ord_struct.data.syz.curr_index = 1;
2389  o[place]= -1;
2390  prev_ord=-1;
2391  place++;
2392}
2393
2394#ifndef NDEBUG
2395# define MYTEST 0
2396#else /* ifndef NDEBUG */
2397# define MYTEST 0
2398#endif /* ifndef NDEBUG */
2399
2400static void rO_ISPrefix(int &place, int &bitplace, int &prev_ord,
2401    long *o, int /*N*/, int *v, sro_ord &ord_struct)
2402{
2403  if ((prev_ord== 1) || (bitplace!=BITS_PER_LONG))
2404    rO_Align(place,bitplace);
2405  // since we add something afterwards - it's better to start with anew!?
2406
2407  ord_struct.ord_typ = ro_isTemp;
2408  ord_struct.data.isTemp.start = place;
2409  ord_struct.data.isTemp.pVarOffset = (int *)omMemDup(v);
2410  ord_struct.data.isTemp.suffixpos = -1;
2411
2412  // We will act as rO_Syz on our own!!!
2413  // Here we allocate an exponent as a level placeholder
2414  o[place]= -1;
2415  prev_ord=-1;
2416  place++;
2417}
2418static void rO_ISSuffix(int &place, int &bitplace, int &prev_ord, long *o,
2419  int N, int *v, sro_ord *tmp_typ, int &typ_i, int sgn)
2420{
2421
2422  // Let's find previous prefix:
2423  int typ_j = typ_i - 1;
2424  while(typ_j >= 0)
2425  {
2426    if( tmp_typ[typ_j].ord_typ == ro_isTemp)
2427      break;
2428    typ_j --;
2429  }
2430
2431  assume( typ_j >= 0 );
2432
2433  if( typ_j < 0 ) // Found NO prefix!!! :(
2434    return;
2435
2436  assume( tmp_typ[typ_j].ord_typ == ro_isTemp );
2437
2438  // Get saved state:
2439  const int start = tmp_typ[typ_j].data.isTemp.start;
2440  int *pVarOffset = tmp_typ[typ_j].data.isTemp.pVarOffset;
2441
2442/*
2443  // shift up all blocks
2444  while(typ_j < (typ_i-1))
2445  {
2446    tmp_typ[typ_j] = tmp_typ[typ_j+1];
2447    typ_j++;
2448  }
2449  typ_j = typ_i - 1; // No increment for typ_i
2450*/
2451  tmp_typ[typ_j].data.isTemp.suffixpos = typ_i;
2452
2453  // Let's keep that dummy for now...
2454  typ_j = typ_i; // the typ to change!
2455  typ_i++; // Just for now...
2456
2457
2458  for( int i = 0; i <= N; i++ ) // Note [0] == component !!! No Skip?
2459  {
2460    // Was i-th variable allocated inbetween?
2461    if( v[i] != pVarOffset[i] )
2462    {
2463      pVarOffset[i] = v[i]; // Save for later...
2464      v[i] = -1; // Undo!
2465      assume( pVarOffset[i] != -1 );
2466    }
2467    else
2468      pVarOffset[i] = -1; // No change here...
2469  }
2470
2471  if( pVarOffset[0] != -1 )
2472    pVarOffset[0] &= 0x0fff;
2473
2474  sro_ord &ord_struct = tmp_typ[typ_j];
2475
2476
2477  ord_struct.ord_typ = ro_is;
2478  ord_struct.data.is.start = start;
2479  ord_struct.data.is.end   = place;
2480  ord_struct.data.is.pVarOffset = pVarOffset;
2481
2482
2483  // What about component???
2484//   if( v[0] != -1 ) // There is a component already...???
2485//     if( o[ v[0] & 0x0fff ] == sgn )
2486//     {
2487//       pVarOffset[0] = -1; // NEVER USED Afterwards...
2488//       return;
2489//     }
2490
2491
2492  // Moreover: we need to allocate the module component (v[0]) here!
2493  if( v[0] == -1) // It's possible that there was module component v0 at the begining (before prefix)!
2494  {
2495    // Start with a whole long exponent
2496    if( bitplace != BITS_PER_LONG )
2497      rO_Align(place, bitplace);
2498
2499    assume( bitplace == BITS_PER_LONG );
2500    bitplace -= BITS_PER_LONG;
2501    assume(bitplace == 0);
2502    v[0] = place | (bitplace << 24); // Never mind whether pVarOffset[0] > 0!!!
2503    o[place] = sgn; // Singnum for component ordering
2504    prev_ord = sgn;
2505  }
2506}
2507
2508
2509static unsigned long rGetExpSize(unsigned long bitmask, int & bits)
2510{
2511  if (bitmask == 0)
2512  {
2513    bits=16; bitmask=0xffff;
2514  }
2515  else if (bitmask <= 1L)
2516  {
2517    bits=1; bitmask = 1L;
2518  }
2519  else if (bitmask <= 3L)
2520  {
2521    bits=2; bitmask = 3L;
2522  }
2523  else if (bitmask <= 7L)
2524  {
2525    bits=3; bitmask=7L;
2526  }
2527  else if (bitmask <= 0xfL)
2528  {
2529    bits=4; bitmask=0xfL;
2530  }
2531  else if (bitmask <= 0x1fL)
2532  {
2533    bits=5; bitmask=0x1fL;
2534  }
2535  else if (bitmask <= 0x3fL)
2536  {
2537    bits=6; bitmask=0x3fL;
2538  }
2539#if SIZEOF_LONG == 8
2540  else if (bitmask <= 0x7fL)
2541  {
2542    bits=7; bitmask=0x7fL; /* 64 bit longs only */
2543  }
2544#endif
2545  else if (bitmask <= 0xffL)
2546  {
2547    bits=8; bitmask=0xffL;
2548  }
2549#if SIZEOF_LONG == 8
2550  else if (bitmask <= 0x1ffL)
2551  {
2552    bits=9; bitmask=0x1ffL; /* 64 bit longs only */
2553  }
2554#endif
2555  else if (bitmask <= 0x3ffL)
2556  {
2557    bits=10; bitmask=0x3ffL;
2558  }
2559#if SIZEOF_LONG == 8
2560  else if (bitmask <= 0xfffL)
2561  {
2562    bits=12; bitmask=0xfff; /* 64 bit longs only */
2563  }
2564#endif
2565  else if (bitmask <= 0xffffL)
2566  {
2567    bits=16; bitmask=0xffffL;
2568  }
2569#if SIZEOF_LONG == 8
2570  else if (bitmask <= 0xfffffL)
2571  {
2572    bits=20; bitmask=0xfffffL; /* 64 bit longs only */
2573  }
2574  else if (bitmask <= 0xffffffffL)
2575  {
2576    bits=32; bitmask=0xffffffffL;
2577  }
2578  else if (bitmask <= 0x7fffffffffffffffL)
2579  {
2580    bits=63; bitmask=0x7fffffffffffffffL; /* for overflow tests*/
2581  }
2582  else
2583  {
2584    bits=63; bitmask=0x7fffffffffffffffL; /* for overflow tests*/
2585  }
2586#else
2587  else if (bitmask <= 0x7fffffff)
2588  {
2589    bits=31; bitmask=0x7fffffff; /* for overflow tests*/
2590  }
2591  else
2592  {
2593    bits=31; bitmask=0x7fffffffL; /* for overflow tests*/
2594  }
2595#endif
2596  return bitmask;
2597}
2598
2599/*2
2600* optimize rGetExpSize for a block of N variables, exp <=bitmask
2601*/
2602static unsigned long rGetExpSize(unsigned long bitmask, int & bits, int N)
2603{
2604  bitmask =rGetExpSize(bitmask, bits);
2605  int vars_per_long=BIT_SIZEOF_LONG/bits;
2606  int bits1;
2607  loop
2608  {
2609    if (bits == BIT_SIZEOF_LONG-1)
2610    {
2611      bits =  BIT_SIZEOF_LONG - 1;
2612      return LONG_MAX;
2613    }
2614    unsigned long bitmask1 =rGetExpSize(bitmask+1, bits1);
2615    int vars_per_long1=BIT_SIZEOF_LONG/bits1;
2616    if ((((N+vars_per_long-1)/vars_per_long) ==
2617         ((N+vars_per_long1-1)/vars_per_long1)))
2618    {
2619      vars_per_long=vars_per_long1;
2620      bits=bits1;
2621      bitmask=bitmask1;
2622    }
2623    else
2624    {
2625      return bitmask; /* and bits */
2626    }
2627  }
2628}
2629
2630
2631/*2
2632 * create a copy of the ring r, which must be equivalent to currRing
2633 * used for std computations
2634 * may share data structures with currRing
2635 * DOES CALL rComplete
2636 */
2637ring rModifyRing(ring r, BOOLEAN omit_degree,
2638                         BOOLEAN omit_comp,
2639                         unsigned long exp_limit)
2640{
2641  assume (r != NULL );
2642  assume (exp_limit > 1);
2643  BOOLEAN need_other_ring;
2644  BOOLEAN omitted_degree = FALSE;
2645
2646  int iNeedInducedOrderingSetup = 0; ///< How many induced ordering block do we have?
2647  int bits;
2648
2649  exp_limit=rGetExpSize(exp_limit, bits, r->N);
2650  need_other_ring = (exp_limit != r->bitmask);
2651
2652  int nblocks=rBlocks(r);
2653  int *order=(int*)omAlloc0((nblocks+1)*sizeof(int));
2654  int *block0=(int*)omAlloc0((nblocks+1)*sizeof(int));
2655  int *block1=(int*)omAlloc0((nblocks+1)*sizeof(int));
2656  int **wvhdl=(int**)omAlloc0((nblocks+1)*sizeof(int *));
2657
2658  int i=0;
2659  int j=0; /*  i index in r, j index in res */
2660
2661  for( int r_ord=r->order[i]; (r_ord != 0) && (i < nblocks); j++, r_ord=r->order[++i])
2662  {
2663    BOOLEAN copy_block_index=TRUE;
2664
2665    if (r->block0[i]==r->block1[i])
2666    {
2667      switch(r_ord)
2668      {
2669        case ringorder_wp:
2670        case ringorder_dp:
2671        case ringorder_Wp:
2672        case ringorder_Dp:
2673          r_ord=ringorder_lp;
2674          break;
2675        case ringorder_Ws:
2676        case ringorder_Ds:
2677        case ringorder_ws:
2678        case ringorder_ds:
2679          r_ord=ringorder_ls;
2680          break;
2681        default:
2682          break;
2683      }
2684    }
2685    switch(r_ord)
2686    {
2687      case ringorder_S:
2688      {
2689#ifndef NDEBUG
2690        Warn("Error: unhandled ordering in rModifyRing: ringorder_S = [%d]", r_ord);
2691#endif
2692        order[j]=r_ord; /*r->order[i];*/
2693        break;
2694      }
2695      case ringorder_C:
2696      case ringorder_c:
2697        if (!omit_comp)
2698        {
2699          order[j]=r_ord; /*r->order[i]*/;
2700        }
2701        else
2702        {
2703          j--;
2704          need_other_ring=TRUE;
2705          omit_comp=FALSE;
2706          copy_block_index=FALSE;
2707        }
2708        break;
2709      case ringorder_wp:
2710      case ringorder_dp:
2711      case ringorder_ws:
2712      case ringorder_ds:
2713        if(!omit_degree)
2714        {
2715          order[j]=r_ord; /*r->order[i]*/;
2716        }
2717        else
2718        {
2719          order[j]=ringorder_rs;
2720          need_other_ring=TRUE;
2721          omit_degree=FALSE;
2722          omitted_degree = TRUE;
2723        }
2724        break;
2725      case ringorder_Wp:
2726      case ringorder_Dp:
2727      case ringorder_Ws:
2728      case ringorder_Ds:
2729        if(!omit_degree)
2730        {
2731          order[j]=r_ord; /*r->order[i];*/
2732        }
2733        else
2734        {
2735          order[j]=ringorder_lp;
2736          need_other_ring=TRUE;
2737          omit_degree=FALSE;
2738          omitted_degree = TRUE;
2739        }
2740        break;
2741      case ringorder_IS:
2742      {
2743        if (omit_comp)
2744        {
2745#ifndef NDEBUG
2746          Warn("Error: WRONG USAGE of rModifyRing: cannot omit component due to the ordering block [%d]: %d (ringorder_IS)", i, r_ord);
2747#endif
2748          omit_comp = FALSE;
2749        }
2750        order[j]=r_ord; /*r->order[i];*/
2751        iNeedInducedOrderingSetup++;
2752        break;
2753      }
2754      case ringorder_s:
2755      {
2756        assume((i == 0) && (j == 0));
2757        if (omit_comp)
2758        {
2759#ifndef NDEBUG
2760          Warn("WRONG USAGE? of rModifyRing: omitting component due to the ordering block [%d]: %d (ringorder_s)", i, r_ord);
2761#endif
2762          omit_comp = FALSE;
2763        }
2764        order[j]=r_ord; /*r->order[i];*/
2765        break;
2766      }
2767      default:
2768        order[j]=r_ord; /*r->order[i];*/
2769        break;
2770    }
2771    if (copy_block_index)
2772    {
2773      block0[j]=r->block0[i];
2774      block1[j]=r->block1[i];
2775      wvhdl[j]=r->wvhdl[i];
2776    }
2777
2778    // order[j]=ringorder_no; //  done by omAlloc0
2779  }
2780  if(!need_other_ring)
2781  {
2782    omFreeSize(order,(nblocks+1)*sizeof(int));
2783    omFreeSize(block0,(nblocks+1)*sizeof(int));
2784    omFreeSize(block1,(nblocks+1)*sizeof(int));
2785    omFreeSize(wvhdl,(nblocks+1)*sizeof(int *));
2786    return r;
2787  }
2788  ring res=(ring)omAlloc0Bin(sip_sring_bin);
2789  *res = *r;
2790
2791#ifdef HAVE_PLURAL
2792  res->GetNC() = NULL;
2793#endif
2794
2795  // res->qideal, res->idroot ???
2796  res->wvhdl=wvhdl;
2797  res->order=order;
2798  res->block0=block0;
2799  res->block1=block1;
2800  res->bitmask=exp_limit;
2801  int tmpref=r->cf->ref;
2802  rComplete(res, 1);
2803  r->cf->ref=tmpref;
2804
2805  // adjust res->pFDeg: if it was changed globally, then
2806  // it must also be changed for new ring
2807  if (r->pFDegOrig != res->pFDegOrig &&
2808           rOrd_is_WeightedDegree_Ordering(r))
2809  {
2810    // still might need adjustment for weighted orderings
2811    // and omit_degree
2812    res->firstwv = r->firstwv;
2813    res->firstBlockEnds = r->firstBlockEnds;
2814    res->pFDeg = res->pFDegOrig = p_WFirstTotalDegree;
2815  }
2816  if (omitted_degree)
2817    res->pLDeg = r->pLDegOrig;
2818
2819  rOptimizeLDeg(res); // also sets res->pLDegOrig
2820
2821  // set syzcomp
2822  if (res->typ != NULL)
2823  {
2824    if( res->typ[0].ord_typ == ro_syz) // "s" Always on [0] place!
2825    {
2826      res->typ[0] = r->typ[0]; // Copy struct!? + setup the same limit!
2827
2828      if (r->typ[0].data.syz.limit > 0)
2829      {
2830        res->typ[0].data.syz.syz_index
2831          = (int*) omAlloc((r->typ[0].data.syz.limit +1)*sizeof(int));
2832        memcpy(res->typ[0].data.syz.syz_index, r->typ[0].data.syz.syz_index,
2833              (r->typ[0].data.syz.limit +1)*sizeof(int));
2834      }
2835    }
2836
2837    if( iNeedInducedOrderingSetup > 0 )
2838    {
2839      for(j = 0, i = 0; (i < nblocks) && (iNeedInducedOrderingSetup > 0); i++)
2840        if( res->typ[i].ord_typ == ro_is ) // Search for suffixes!
2841        {
2842          ideal F = idrHeadR(r->typ[i].data.is.F, r, res); // Copy F from r into res!
2843          assume(
2844            rSetISReference( res,
2845              F,  // WILL BE COPIED!
2846              r->typ[i].data.is.limit,
2847              j++
2848              )
2849            );
2850          id_Delete(&F, res);
2851          iNeedInducedOrderingSetup--;
2852        }
2853    } // Process all induced Ordering blocks! ...
2854  }
2855  // the special case: homog (omit_degree) and 1 block rs: that is global:
2856  // it comes from dp
2857  res->OrdSgn=r->OrdSgn;
2858
2859
2860#ifdef HAVE_PLURAL
2861  if (rIsPluralRing(r))
2862  {
2863    if ( nc_rComplete(r, res, false) ) // no qideal!
2864    {
2865#ifndef NDEBUG
2866      WarnS("error in nc_rComplete");
2867#endif
2868      // cleanup?
2869
2870//      rDelete(res);
2871//      return r;
2872
2873      // just go on..
2874    }
2875
2876    if( rIsSCA(r) )
2877    {
2878      if( !sca_Force(res, scaFirstAltVar(r), scaLastAltVar(r)) )
2879      WarnS("error in sca_Force!");
2880    }
2881  }
2882#endif
2883
2884  return res;
2885}
2886
2887// construct Wp,C ring
2888ring rModifyRing_Wp(ring r, int* weights)
2889{
2890  ring res=(ring)omAlloc0Bin(sip_sring_bin);
2891  *res = *r;
2892#ifdef HAVE_PLURAL
2893  res->GetNC() = NULL;
2894#endif
2895
2896  /*weights: entries for 3 blocks: NULL*/
2897  res->wvhdl = (int **)omAlloc0(3 * sizeof(int *));
2898  /*order: Wp,C,0*/
2899  res->order = (int *) omAlloc(3 * sizeof(int *));
2900  res->block0 = (int *)omAlloc0(3 * sizeof(int *));
2901  res->block1 = (int *)omAlloc0(3 * sizeof(int *));
2902  /* ringorder Wp for the first block: var 1..r->N */
2903  res->order[0]  = ringorder_Wp;
2904  res->block0[0] = 1;
2905  res->block1[0] = r->N;
2906  res->wvhdl[0] = weights;
2907  /* ringorder C for the second block: no vars */
2908  res->order[1]  = ringorder_C;
2909  /* the last block: everything is 0 */
2910  res->order[2]  = 0;
2911  /*polynomial ring*/
2912  res->OrdSgn    = 1;
2913
2914  int tmpref=r->cf->ref;
2915  rComplete(res, 1);
2916  r->cf->ref=tmpref;
2917#ifdef HAVE_PLURAL
2918  if (rIsPluralRing(r))
2919  {
2920    if ( nc_rComplete(r, res, false) ) // no qideal!
2921    {
2922#ifndef NDEBUG
2923      WarnS("error in nc_rComplete");
2924#endif
2925      // cleanup?
2926
2927//      rDelete(res);
2928//      return r;
2929
2930      // just go on..
2931    }
2932  }
2933#endif
2934  return res;
2935}
2936
2937// construct lp, C ring with r->N variables, r->names vars....
2938ring rModifyRing_Simple(ring r, BOOLEAN ommit_degree, BOOLEAN ommit_comp, unsigned long exp_limit, BOOLEAN &simple)
2939{
2940  simple=TRUE;
2941  if (!rHasSimpleOrder(r))
2942  {
2943    simple=FALSE; // sorting needed
2944    assume (r != NULL );
2945    assume (exp_limit > 1);
2946    int bits;
2947
2948    exp_limit=rGetExpSize(exp_limit, bits, r->N);
2949
2950    int nblocks=1+(ommit_comp!=0);
2951    int *order=(int*)omAlloc0((nblocks+1)*sizeof(int));
2952    int *block0=(int*)omAlloc0((nblocks+1)*sizeof(int));
2953    int *block1=(int*)omAlloc0((nblocks+1)*sizeof(int));
2954    int **wvhdl=(int**)omAlloc0((nblocks+1)*sizeof(int *));
2955
2956    order[0]=ringorder_lp;
2957    block0[0]=1;
2958    block1[0]=r->N;
2959    if (!ommit_comp)
2960    {
2961      order[1]=ringorder_C;
2962    }
2963    ring res=(ring)omAlloc0Bin(sip_sring_bin);
2964    *res = *r;
2965#ifdef HAVE_PLURAL
2966    res->GetNC() = NULL;
2967#endif
2968    // res->qideal, res->idroot ???
2969    res->wvhdl=wvhdl;
2970    res->order=order;
2971    res->block0=block0;
2972    res->block1=block1;
2973    res->bitmask=exp_limit;
2974    int tmpref=r->cf->ref;
2975    rComplete(res, 1);
2976    r->cf->ref=tmpref;
2977
2978#ifdef HAVE_PLURAL
2979    if (rIsPluralRing(r))
2980    {
2981      if ( nc_rComplete(r, res, false) ) // no qideal!
2982      {
2983#ifndef NDEBUG
2984        WarnS("error in nc_rComplete");
2985#endif
2986        // cleanup?
2987
2988//      rDelete(res);
2989//      return r;
2990
2991      // just go on..
2992      }
2993    }
2994#endif
2995
2996    rOptimizeLDeg(res);
2997
2998    return res;
2999  }
3000  return rModifyRing(r, ommit_degree, ommit_comp, exp_limit);
3001}
3002
3003void rKillModifiedRing_Simple(ring r)
3004{
3005  rKillModifiedRing(r);
3006}
3007
3008
3009void rKillModifiedRing(ring r)
3010{
3011  rUnComplete(r);
3012  omFree(r->order);
3013  omFree(r->block0);
3014  omFree(r->block1);
3015  omFree(r->wvhdl);
3016  omFreeBin(r,sip_sring_bin);
3017}
3018
3019void rKillModified_Wp_Ring(ring r)
3020{
3021  rUnComplete(r);
3022  omFree(r->order);
3023  omFree(r->block0);
3024  omFree(r->block1);
3025  omFree(r->wvhdl[0]);
3026  omFree(r->wvhdl);
3027  omFreeBin(r,sip_sring_bin);
3028}
3029
3030static void rSetOutParams(ring r)
3031{
3032  r->VectorOut = (r->order[0] == ringorder_c);
3033  r->CanShortOut = TRUE;
3034  {
3035    int i;
3036    if (rParameter(r)!=NULL)
3037    {
3038      for (i=0;i<rPar(r);i++)
3039      {
3040        if(strlen(rParameter(r)[i])>1)
3041        {
3042          r->CanShortOut=FALSE;
3043          break;
3044        }
3045      }
3046    }
3047    if (r->CanShortOut)
3048    {
3049      // Hmm... sometimes (e.g., from maGetPreimage) new variables
3050      // are introduced, but their names are never set
3051      // hence, we do the following awkward trick
3052      int N = omSizeWOfAddr(r->names);
3053      if (r->N < N) N = r->N;
3054
3055      for (i=(N-1);i>=0;i--)
3056      {
3057        if(r->names[i] != NULL && strlen(r->names[i])>1)
3058        {
3059          r->CanShortOut=FALSE;
3060          break;
3061        }
3062      }
3063    }
3064  }
3065  r->ShortOut = r->CanShortOut;
3066
3067  assume( !( !r->CanShortOut && r->ShortOut ) );
3068}
3069
3070/*2
3071* sets r->MixedOrder and r->ComponentOrder for orderings with more than one block
3072* block of variables (ip is the block number, o_r the number of the ordering)
3073* o is the position of the orderingering in r
3074*/
3075static void rHighSet(ring r, int o_r, int o)
3076{
3077  switch(o_r)
3078  {
3079    case ringorder_lp:
3080    case ringorder_dp:
3081    case ringorder_Dp:
3082    case ringorder_wp:
3083    case ringorder_Wp:
3084    case ringorder_rp:
3085    case ringorder_a:
3086    case ringorder_aa:
3087    case ringorder_am:
3088    case ringorder_a64:
3089      if (r->OrdSgn==-1) r->MixedOrder=TRUE;
3090      break;
3091    case ringorder_ls:
3092    case ringorder_rs:
3093    case ringorder_ds:
3094    case ringorder_Ds:
3095    case ringorder_s:
3096      break;
3097    case ringorder_ws:
3098    case ringorder_Ws:
3099      if (r->wvhdl[o]!=NULL)
3100      {
3101        int i;
3102        for(i=r->block1[o]-r->block0[o];i>=0;i--)
3103          if (r->wvhdl[o][i]<0) { r->MixedOrder=TRUE; break; }
3104      }
3105      break;
3106    case ringorder_c:
3107      r->ComponentOrder=1;
3108      break;
3109    case ringorder_C:
3110    case ringorder_S:
3111      r->ComponentOrder=-1;
3112      break;
3113    case ringorder_M:
3114      r->LexOrder=TRUE;
3115      break;
3116    case ringorder_IS:
3117    { // TODO: What is r->ComponentOrder???
3118//      r->MixedOrder=TRUE;
3119      if( r->block0[o] != 0 ) // Suffix has the component
3120        r->ComponentOrder = r->block0[o];
3121/*      else // Prefix has level...
3122        r->ComponentOrder=-1;
3123*/
3124      // TODO: think about this a bit...!?
3125      break;
3126    }
3127
3128    default:
3129      dReportError("wrong internal ordering:%d at %s, l:%d\n",o_r,__FILE__,__LINE__);
3130  }
3131}
3132
3133static void rSetFirstWv(ring r, int i, int* order, int* block1, int** wvhdl)
3134{
3135  // cheat for ringorder_aa
3136  if (order[i] == ringorder_aa)
3137    i++;
3138  if(block1[i]!=r->N) r->LexOrder=TRUE;
3139  r->firstBlockEnds=block1[i];
3140  r->firstwv = wvhdl[i];
3141  if ((order[i]== ringorder_ws)
3142  || (order[i]==ringorder_Ws)
3143  || (order[i]== ringorder_wp)
3144  || (order[i]==ringorder_Wp)
3145  || (order[i]== ringorder_a)
3146   /*|| (order[i]==ringorder_A)*/)
3147  {
3148    int j;
3149    for(j=block1[i]-r->block0[i];j>=0;j--)
3150    {
3151      if (r->firstwv[j]<0) r->MixedOrder=TRUE;
3152      if (r->firstwv[j]==0) r->LexOrder=TRUE;
3153    }
3154  }
3155  else if (order[i]==ringorder_a64)
3156  {
3157    int j;
3158    int64 *w=rGetWeightVec(r);
3159    for(j=block1[i]-r->block0[i];j>=0;j--)
3160    {
3161      if (w[j]==0) r->LexOrder=TRUE;
3162    }
3163  }
3164}
3165
3166static void rOptimizeLDeg(ring r)
3167{
3168  if (r->pFDeg == p_Deg)
3169  {
3170    if (r->pLDeg == pLDeg1)
3171      r->pLDeg = pLDeg1_Deg;
3172    if (r->pLDeg == pLDeg1c)
3173      r->pLDeg = pLDeg1c_Deg;
3174  }
3175  else if (r->pFDeg == p_Totaldegree)
3176  {
3177    if (r->pLDeg == pLDeg1)
3178      r->pLDeg = pLDeg1_Totaldegree;
3179    if (r->pLDeg == pLDeg1c)
3180      r->pLDeg = pLDeg1c_Totaldegree;
3181  }
3182  else if (r->pFDeg == p_WFirstTotalDegree)
3183  {
3184    if (r->pLDeg == pLDeg1)
3185      r->pLDeg = pLDeg1_WFirstTotalDegree;
3186    if (r->pLDeg == pLDeg1c)
3187      r->pLDeg = pLDeg1c_WFirstTotalDegree;
3188  }
3189  r->pLDegOrig = r->pLDeg;
3190}
3191
3192// set pFDeg, pLDeg, MixOrder, ComponentOrder, etc
3193static void rSetDegStuff(ring r)
3194{
3195  int* order = r->order;
3196  int* block0 = r->block0;
3197  int* block1 = r->block1;
3198  int** wvhdl = r->wvhdl;
3199
3200  if (order[0]==ringorder_S ||order[0]==ringorder_s || order[0]==ringorder_IS)
3201  {
3202    order++;
3203    block0++;
3204    block1++;
3205    wvhdl++;
3206  }
3207  r->LexOrder = FALSE;
3208  r->MixedOrder = FALSE;
3209  r->ComponentOrder = 1;
3210  r->pFDeg = p_Totaldegree;
3211  r->pLDeg = (r->OrdSgn == 1 ? pLDegb : pLDeg0);
3212
3213  /*======== ordering type is (am,_) ==================*/
3214  if (order[0]==ringorder_am)
3215  {
3216    r->MixedOrder = FALSE;
3217    for(int ii=block0[0];ii<=block1[0];ii++)
3218      if (wvhdl[0][ii-1]<0) { r->MixedOrder=TRUE;break;}
3219    r->LexOrder=FALSE;
3220    for(int ii=block0[0];ii<=block1[0];ii++)
3221      if (wvhdl[0][ii-1]==0) { r->LexOrder=TRUE;break;}
3222    if ((block0[0]==1)&&(block1[0]==r->N))
3223    {
3224      r->pFDeg = p_Deg;
3225      r->pLDeg = pLDeg1c_Deg;
3226    }
3227    else
3228   {
3229      r->pFDeg = p_WTotaldegree;
3230      r->LexOrder=TRUE;
3231      r->pLDeg = pLDeg1c_WFirstTotalDegree;
3232    }
3233    r->firstwv = wvhdl[0];
3234  }
3235  /*======== ordering type is (_,c) =========================*/
3236  else if ((order[0]==ringorder_unspec) || (order[1] == 0)
3237      ||(
3238    ((order[1]==ringorder_c)||(order[1]==ringorder_C)
3239     ||(order[1]==ringorder_S)
3240     ||(order[1]==ringorder_s))
3241    && (order[0]!=ringorder_M)
3242    && (order[2]==0))
3243    )
3244  {
3245    if ((order[0]!=ringorder_unspec)
3246    && ((order[1]==ringorder_C)||(order[1]==ringorder_S)||
3247        (order[1]==ringorder_s)))
3248      r->ComponentOrder=-1;
3249    if (r->OrdSgn == -1) r->pLDeg = pLDeg0c;
3250    if ((order[0] == ringorder_lp)
3251    || (order[0] == ringorder_ls)
3252    || (order[0] == ringorder_rp)
3253    || (order[0] == ringorder_rs))
3254    {
3255      r->LexOrder=TRUE;
3256      r->pLDeg = pLDeg1c;
3257      r->pFDeg = p_Totaldegree;
3258    }
3259    if ((order[0] == ringorder_a)
3260    || (order[0] == ringorder_wp)
3261    || (order[0] == ringorder_Wp)
3262    || (order[0] == ringorder_ws)
3263    || (order[0] == ringorder_Ws))
3264      r->pFDeg = p_WFirstTotalDegree;
3265    r->firstBlockEnds=block1[0];
3266    r->firstwv = wvhdl[0];
3267  }
3268  /*======== ordering type is (c,_) =========================*/
3269  else if (((order[0]==ringorder_c)
3270            ||(order[0]==ringorder_C)
3271            ||(order[0]==ringorder_S)
3272            ||(order[0]==ringorder_s))
3273  && (order[1]!=ringorder_M)
3274  &&  (order[2]==0))
3275  {
3276    if ((order[0]==ringorder_C)||(order[0]==ringorder_S)||
3277        order[0]==ringorder_s)
3278      r->ComponentOrder=-1;
3279    if ((order[1] == ringorder_lp)
3280    || (order[1] == ringorder_ls)
3281    || (order[1] == ringorder_rp)
3282    || order[1] == ringorder_rs)
3283    {
3284      r->LexOrder=TRUE;
3285      r->pLDeg = pLDeg1c;
3286      r->pFDeg = p_Totaldegree;
3287    }
3288    r->firstBlockEnds=block1[1];
3289    if (wvhdl!=NULL) r->firstwv = wvhdl[1];
3290    if ((order[1] == ringorder_a)
3291    || (order[1] == ringorder_wp)
3292    || (order[1] == ringorder_Wp)
3293    || (order[1] == ringorder_ws)
3294    || (order[1] == ringorder_Ws))
3295      r->pFDeg = p_WFirstTotalDegree;
3296  }
3297  /*------- more than one block ----------------------*/
3298  else
3299  {
3300    if ((r->VectorOut)||(order[0]==ringorder_C)||(order[0]==ringorder_S)||(order[0]==ringorder_s))
3301    {
3302      rSetFirstWv(r, 1, order, block1, wvhdl);
3303    }
3304    else
3305      rSetFirstWv(r, 0, order, block1, wvhdl);
3306
3307    /*the number of orderings:*/
3308    int i = 0; while (order[++i] != 0);
3309
3310    do
3311    {
3312      i--;
3313      rHighSet(r, order[i],i);
3314    }
3315    while (i != 0);
3316
3317    if ((order[0]!=ringorder_c)
3318        && (order[0]!=ringorder_C)
3319        && (order[0]!=ringorder_S)
3320        && (order[0]!=ringorder_s))
3321    {
3322      r->pLDeg = pLDeg1c;
3323    }
3324    else
3325    {
3326      r->pLDeg = pLDeg1;
3327    }
3328    r->pFDeg = p_WTotaldegree; // may be improved: p_Totaldegree for lp/dp/ls/.. blocks
3329  }
3330
3331  if (rOrd_is_Totaldegree_Ordering(r) || rOrd_is_WeightedDegree_Ordering(r))
3332    r->pFDeg = p_Deg;
3333
3334  if( rGetISPos(0, r) != -1 ) // Are there Schreyer induced blocks?
3335  {
3336#ifndef NDEBUG
3337      assume( r->pFDeg == p_Deg || r->pFDeg == p_WTotaldegree || r->pFDeg == p_Totaldegree);
3338#endif
3339
3340    r->pLDeg = pLDeg1; // ?
3341  }
3342
3343  r->pFDegOrig = r->pFDeg;
3344  // NOTE: this leads to wrong ecart during std
3345  // in Old/sre.tst
3346  rOptimizeLDeg(r); // also sets r->pLDegOrig
3347
3348}
3349
3350/*2
3351* set NegWeightL_Size, NegWeightL_Offset
3352*/
3353static void rSetNegWeight(ring r)
3354{
3355  int i,l;
3356  if (r->typ!=NULL)
3357  {
3358    l=0;
3359    for(i=0;i<r->OrdSize;i++)
3360    {
3361      if((r->typ[i].ord_typ==ro_wp_neg)
3362      ||(r->typ[i].ord_typ==ro_am))
3363        l++;
3364    }
3365    if (l>0)
3366    {
3367      r->NegWeightL_Size=l;
3368      r->NegWeightL_Offset=(int *) omAlloc(l*sizeof(int));
3369      l=0;
3370      for(i=0;i<r->OrdSize;i++)
3371      {
3372        if(r->typ[i].ord_typ==ro_wp_neg)
3373        {
3374          r->NegWeightL_Offset[l]=r->typ[i].data.wp.place;
3375          l++;
3376        }
3377        else if(r->typ[i].ord_typ==ro_am)
3378        {
3379          r->NegWeightL_Offset[l]=r->typ[i].data.am.place;
3380          l++;
3381        }
3382      }
3383      return;
3384    }
3385  }
3386  r->NegWeightL_Size = 0;
3387  r->NegWeightL_Offset = NULL;
3388}
3389
3390static void rSetOption(ring r)
3391{
3392  // set redthrough
3393  if (!TEST_OPT_OLDSTD && r->OrdSgn == 1 && ! r->LexOrder)
3394    r->options |= Sy_bit(OPT_REDTHROUGH);
3395  else
3396    r->options &= ~Sy_bit(OPT_REDTHROUGH);
3397
3398  // set intStrategy
3399#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    si_opt_1 &= ~ TEST_RINGDEP_OPTS;
3433    si_opt_1 |= 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("divmask=%lx\n", r->divmask);
4035  Print("BitsPerExp=%d ExpPerLong=%d MinExpPerLong=%d at L[%d]\n", r->BitsPerExp, r->ExpPerLong, r->MinExpPerLong, r->VarL_Offset[0]);
4036
4037  Print("VarL_LowIndex: %d\n", r->VarL_LowIndex);
4038  PrintS("VarL_Offset:\n");
4039  if (r->VarL_Offset==NULL) PrintS(" NULL");
4040  else
4041    for(j = 0; j < r->VarL_Size; j++)
4042      Print("  VarL_Offset[%d]: %d ", j, r->VarL_Offset[j]);
4043  PrintLn();
4044     
4045
4046  PrintS("VarOffset:\n");
4047  if (r->VarOffset==NULL) PrintS(" NULL\n");
4048  else
4049    for(j=0;j<=r->N;j++)
4050      Print("  v%d at e-pos %d, bit %d\n",
4051            j,r->VarOffset[j] & 0xffffff, r->VarOffset[j] >>24);
4052  PrintS("ordsgn:\n");
4053  for(j=0;j<r->CmpL_Size;j++)
4054    Print("  ordsgn %ld at pos %d\n",r->ordsgn[j],j);
4055  Print("OrdSgn:%d\n",r->OrdSgn);
4056  PrintS("ordrec:\n");
4057  for(j=0;j<r->OrdSize;j++)
4058  {
4059    Print("  typ %s", TYP[r->typ[j].ord_typ]);
4060
4061
4062    if (r->typ[j].ord_typ==ro_syz)
4063    {
4064      const short place = r->typ[j].data.syz.place;
4065      const int limit = r->typ[j].data.syz.limit;
4066      const int curr_index = r->typ[j].data.syz.curr_index;
4067      const int* syz_index = r->typ[j].data.syz.syz_index;
4068
4069      Print("  limit %d (place: %d, curr_index: %d), syz_index: ", limit, place, curr_index);
4070
4071      if( syz_index == NULL )
4072        PrintS("(NULL)");
4073      else
4074      {
4075        Print("{");
4076        for( i=0; i <= limit; i++ )
4077          Print("%d ", syz_index[i]);
4078        Print("}");
4079      }
4080
4081    }
4082    else if (r->typ[j].ord_typ==ro_isTemp)
4083    {
4084      Print("  start (level) %d, suffixpos: %d, VO: ",r->typ[j].data.isTemp.start, r->typ[j].data.isTemp.suffixpos);
4085
4086    }
4087    else if (r->typ[j].ord_typ==ro_is)
4088    {
4089      Print("  start %d, end: %d: ",r->typ[j].data.is.start, r->typ[j].data.is.end);
4090
4091//      for( int k = 0; k <= r->N; k++) if (r->typ[j].data.is.pVarOffset[k] != -1) Print("[%2d]: %04x; ", k, r->typ[j].data.is.pVarOffset[k]);
4092
4093      Print("  limit %d",r->typ[j].data.is.limit);
4094#ifndef NDEBUG
4095      //PrintS("  F: ");idShow(r->typ[j].data.is.F, r, r, 1);
4096#endif
4097
4098      PrintLn();
4099    }
4100    else if  (r->typ[j].ord_typ==ro_am)
4101    {
4102      Print("  place %d",r->typ[j].data.am.place);
4103      Print("  start %d",r->typ[j].data.am.start);
4104      Print("  end %d",r->typ[j].data.am.end);
4105      Print("  len_gen %d",r->typ[j].data.am.len_gen);
4106      PrintS(" w:");
4107      int l=0;
4108      for(l=r->typ[j].data.am.start;l<=r->typ[j].data.am.end;l++)
4109            Print(" %d",r->typ[j].data.am.weights[l-r->typ[j].data.am.start]);
4110      l=r->typ[j].data.am.end+1;
4111      int ll=r->typ[j].data.am.weights[l-r->typ[j].data.am.start];
4112      PrintS(" m:");
4113      for(int lll=l+1;lll<l+ll+1;lll++)
4114            Print(" %d",r->typ[j].data.am.weights[lll-r->typ[j].data.am.start]);
4115    }
4116    else
4117    {
4118      Print("  place %d",r->typ[j].data.dp.place);
4119
4120      if (r->typ[j].ord_typ!=ro_syzcomp  && r->typ[j].ord_typ!=ro_syz)
4121      {
4122        Print("  start %d",r->typ[j].data.dp.start);
4123        Print("  end %d",r->typ[j].data.dp.end);
4124        if ((r->typ[j].ord_typ==ro_wp)
4125        || (r->typ[j].ord_typ==ro_wp_neg))
4126        {
4127          PrintS(" w:");
4128          for(int l=r->typ[j].data.wp.start;l<=r->typ[j].data.wp.end;l++)
4129            Print(" %d",r->typ[j].data.wp.weights[l-r->typ[j].data.wp.start]);
4130        }
4131        else if (r->typ[j].ord_typ==ro_wp64)
4132        {
4133          PrintS(" w64:");
4134          int l;
4135          for(l=r->typ[j].data.wp64.start;l<=r->typ[j].data.wp64.end;l++)
4136            Print(" %ld",(long)(((int64*)r->typ[j].data.wp64.weights64)+l-r->typ[j].data.wp64.start));
4137          }
4138        }
4139    }
4140    PrintLn();
4141  }
4142  Print("pOrdIndex:%d pCompIndex:%d\n", r->pOrdIndex, r->pCompIndex);
4143  Print("OrdSize:%d\n",r->OrdSize);
4144  PrintS("--------------------\n");
4145  for(j=0;j<r->ExpL_Size;j++)
4146  {
4147    Print("L[%d]: ",j);
4148    if (j< r->CmpL_Size)
4149      Print("ordsgn %ld ", r->ordsgn[j]);
4150    else
4151      PrintS("no comp ");
4152    i=1;
4153    for(;i<=r->N;i++)
4154    {
4155      if( (r->VarOffset[i] & 0xffffff) == j )
4156      {  Print("v%d at e[%d], bit %d; ", i,r->VarOffset[i] & 0xffffff,
4157                                         r->VarOffset[i] >>24 ); }
4158    }
4159    if( r->pCompIndex==j ) PrintS("v0; ");
4160    for(i=0;i<r->OrdSize;i++)
4161    {
4162      if (r->typ[i].data.dp.place == j)
4163      {
4164        Print("ordrec:%s (start:%d, end:%d) ",TYP[r->typ[i].ord_typ],
4165          r->typ[i].data.dp.start, r->typ[i].data.dp.end);
4166      }
4167    }
4168
4169    if (j==r->pOrdIndex)
4170      PrintS("pOrdIndex\n");
4171    else
4172      PrintLn();
4173  }
4174  Print("LexOrder:%d, MixedOrder:%d\n",r->LexOrder, r->MixedOrder);
4175
4176  Print("NegWeightL_Size: %d, NegWeightL_Offset: ", r->NegWeightL_Size);
4177  if (r->NegWeightL_Offset==NULL) PrintS(" NULL");
4178  else
4179    for(j = 0; j < r->NegWeightL_Size; j++)
4180      Print("  [%d]: %d ", j, r->NegWeightL_Offset[j]);
4181  PrintLn();
4182
4183  // p_Procs stuff
4184  p_Procs_s proc_names;
4185  const char* field;
4186  const char* length;
4187  const char* ord;
4188  p_Debug_GetProcNames(r, &proc_names); // changes p_Procs!!!
4189  p_Debug_GetSpecNames(r, field, length, ord);
4190
4191  Print("p_Spec  : %s, %s, %s\n", field, length, ord);
4192  PrintS("p_Procs :\n");
4193  for (i=0; i<(int) (sizeof(p_Procs_s)/sizeof(void*)); i++)
4194  {
4195    Print(" %s,\n", ((char**) &proc_names)[i]);
4196  }
4197
4198  {
4199      PrintLn();
4200      Print("pFDeg   : ");
4201#define pFDeg_CASE(A) if(r->pFDeg == A) PrintS( "" #A "" )
4202      pFDeg_CASE(p_Totaldegree); else
4203      pFDeg_CASE(p_WFirstTotalDegree); else
4204      pFDeg_CASE(p_WTotaldegree); else
4205      pFDeg_CASE(p_Deg); else
4206#undef pFDeg_CASE
4207      Print("(%p)", r->pFDeg); // default case
4208
4209    PrintLn();
4210    Print("pLDeg   : (%p)", r->pLDeg);
4211    PrintLn();
4212  }
4213  Print("pSetm:");
4214  void p_Setm_Dummy(poly p, const ring r);
4215  void p_Setm_TotalDegree(poly p, const ring r);
4216  void p_Setm_WFirstTotalDegree(poly p, const ring r);
4217  void p_Setm_General(poly p, const ring r);
4218  if (r->p_Setm==p_Setm_General) PrintS("p_Setm_General\n");
4219  else if (r->p_Setm==p_Setm_Dummy) PrintS("p_Setm_Dummy\n");
4220  else if (r->p_Setm==p_Setm_TotalDegree) PrintS("p_Setm_Totaldegree\n");
4221  else if (r->p_Setm==p_Setm_WFirstTotalDegree) PrintS("p_Setm_WFirstTotalDegree\n");
4222  else Print("%p\n",r->p_Setm);
4223}
4224
4225void p_DebugPrint(poly p, const ring r)
4226{
4227  int i,j;
4228  p_Write(p,r);
4229  j=2;
4230  while(p!=NULL)
4231  {
4232    Print("\nexp[0..%d]\n",r->ExpL_Size-1);
4233    for(i=0;i<r->ExpL_Size;i++)
4234      Print("%ld ",p->exp[i]);
4235    PrintLn();
4236    Print("v0:%ld ",p_GetComp(p, r));
4237    for(i=1;i<=r->N;i++) Print(" v%d:%ld",i,p_GetExp(p,i, r));
4238    PrintLn();
4239    pIter(p);
4240    j--;
4241    if (j==0) { PrintS("...\n"); break; }
4242  }
4243}
4244
4245#endif // RDEBUG
4246
4247/// debug-print monomial poly/vector p, assuming that it lives in the ring R
4248static inline void m_DebugPrint(const poly p, const ring R)
4249{
4250  Print("\nexp[0..%d]\n", R->ExpL_Size - 1);
4251  for(int i = 0; i < R->ExpL_Size; i++)
4252    Print("%09lx ", p->exp[i]);
4253  PrintLn();
4254  Print("v0:%9ld ", p_GetComp(p, R));
4255  for(int i = 1; i <= R->N; i++) Print(" v%d:%5ld",i, p_GetExp(p, i, R));
4256  PrintLn();
4257}
4258
4259
4260#ifndef NDEBUG
4261/// debug-print at most nTerms (2 by default) terms from poly/vector p,
4262/// assuming that lt(p) lives in lmRing and tail(p) lives in tailRing.
4263void p_DebugPrint(const poly p, const ring lmRing, const ring tailRing, const int nTerms)
4264{
4265  assume( nTerms >= 0 );
4266  if( p != NULL )
4267  {
4268    assume( p != NULL );
4269
4270    p_Write(p, lmRing, tailRing);
4271
4272    if( (p != NULL) && (nTerms > 0) )
4273    {
4274      assume( p != NULL );
4275      assume( nTerms > 0 );
4276
4277      // debug pring leading term
4278      m_DebugPrint(p, lmRing);
4279
4280      poly q = pNext(p); // q = tail(p)
4281
4282      // debug pring tail (at most nTerms-1 terms from it)
4283      for(int j = nTerms - 1; (q !=NULL) && (j > 0); pIter(q), --j)
4284        m_DebugPrint(q, tailRing);
4285
4286      if (q != NULL)
4287        PrintS("...\n");
4288    }
4289  }
4290  else
4291    PrintS("0\n");
4292}
4293#endif
4294
4295
4296//    F = system("ISUpdateComponents", F, V, MIN );
4297//    // replace gen(i) -> gen(MIN + V[i-MIN]) for all i > MIN in all terms from F!
4298void pISUpdateComponents(ideal F, const intvec *const V, const int MIN, const ring r )
4299{
4300  assume( V != NULL );
4301  assume( MIN >= 0 );
4302
4303  if( F == NULL )
4304    return;
4305
4306  for( int j = (F->ncols*F->nrows) - 1; j >= 0; j-- )
4307  {
4308#ifdef PDEBUG
4309    Print("F[%d]:", j);
4310    p_DebugPrint(F->m[j], r, r, 0);
4311#endif
4312
4313    for( poly p = F->m[j]; p != NULL; pIter(p) )
4314    {
4315      int c = p_GetComp(p, r);
4316
4317      if( c > MIN )
4318      {
4319#ifdef PDEBUG
4320        Print("gen[%d] -> gen(%d)\n", c, MIN + (*V)[ c - MIN - 1 ]);
4321#endif
4322
4323        p_SetComp( p, MIN + (*V)[ c - MIN - 1 ], r );
4324      }
4325    }
4326#ifdef PDEBUG
4327    Print("new F[%d]:", j);
4328    p_Test(F->m[j], r);
4329    p_DebugPrint(F->m[j], r, r, 0);
4330#endif
4331  }
4332
4333}
4334
4335
4336
4337
4338/*2
4339* asssume that rComplete was called with r
4340* assume that the first block ist ringorder_S
4341* change the block to reflect the sequence given by appending v
4342*/
4343static inline void rNChangeSComps(int* currComponents, long* currShiftedComponents, ring r)
4344{
4345  assume(r->typ[1].ord_typ == ro_syzcomp);
4346
4347  r->typ[1].data.syzcomp.ShiftedComponents = currShiftedComponents;
4348  r->typ[1].data.syzcomp.Components = currComponents;
4349}
4350
4351static inline void rNGetSComps(int** currComponents, long** currShiftedComponents, ring r)
4352{
4353  assume(r->typ[1].ord_typ == ro_syzcomp);
4354
4355  *currShiftedComponents = r->typ[1].data.syzcomp.ShiftedComponents;
4356  *currComponents =   r->typ[1].data.syzcomp.Components;
4357}
4358#ifdef PDEBUG
4359static inline void rDBChangeSComps(int* currComponents,
4360                     long* currShiftedComponents,
4361                     int length,
4362                     ring r)
4363{
4364  assume(r->typ[1].ord_typ == ro_syzcomp);
4365
4366  r->typ[1].data.syzcomp.length = length;
4367  rNChangeSComps( currComponents, currShiftedComponents, r);
4368}
4369static inline void rDBGetSComps(int** currComponents,
4370                 long** currShiftedComponents,
4371                 int *length,
4372                 ring r)
4373{
4374  assume(r->typ[1].ord_typ == ro_syzcomp);
4375
4376  *length = r->typ[1].data.syzcomp.length;
4377  rNGetSComps( currComponents, currShiftedComponents, r);
4378}
4379#endif
4380
4381void rChangeSComps(int* currComponents, long* currShiftedComponents, int length, ring r)
4382{
4383#ifdef PDEBUG
4384   rDBChangeSComps(currComponents, currShiftedComponents, length, r);
4385#else
4386   rNChangeSComps(currComponents, currShiftedComponents, r);
4387#endif
4388}
4389
4390void rGetSComps(int** currComponents, long** currShiftedComponents, int *length, ring r)
4391{
4392#ifdef PDEBUG
4393   rDBGetSComps(currComponents, currShiftedComponents, length, r);
4394#else
4395   rNGetSComps(currComponents, currShiftedComponents, r);
4396#endif
4397}
4398
4399
4400/////////////////////////////////////////////////////////////////////////////
4401//
4402// The following routines all take as input a ring r, and return R
4403// where R has a certain property. R might be equal r in which case r
4404// had already this property
4405//
4406ring rAssure_SyzComp(const ring r, BOOLEAN complete)
4407{
4408  if ( r->order[0] == ringorder_s ) return r;
4409
4410  if ( r->order[0] == ringorder_IS )
4411  {
4412#ifndef NDEBUG
4413    WarnS("rAssure_SyzComp: input ring has an IS-ordering!");
4414#endif
4415//    return r;
4416  }
4417  ring res=rCopy0(r, FALSE, FALSE);
4418  int i=rBlocks(r);
4419  int j;
4420
4421  res->order=(int *)omAlloc((i+1)*sizeof(int));
4422  res->block0=(int *)omAlloc0((i+1)*sizeof(int));
4423  res->block1=(int *)omAlloc0((i+1)*sizeof(int));
4424  int ** wvhdl =(int **)omAlloc0((i+1)*sizeof(int**));
4425  for(j=i;j>0;j--)
4426  {
4427    res->order[j]=r->order[j-1];
4428    res->block0[j]=r->block0[j-1];
4429    res->block1[j]=r->block1[j-1];
4430    if (r->wvhdl[j-1] != NULL)
4431    {
4432      wvhdl[j] = (int*) omMemDup(r->wvhdl[j-1]);
4433    }
4434  }
4435  res->order[0]=ringorder_s;
4436
4437  res->wvhdl = wvhdl;
4438
4439  if (complete)
4440  {
4441    rComplete(res, 1);
4442
4443#ifdef HAVE_PLURAL
4444    if (rIsPluralRing(r))
4445    {
4446      if ( nc_rComplete(r, res, false) ) // no qideal!
4447      {
4448#ifndef NDEBUG
4449        WarnS("error in nc_rComplete");      // cleanup?//      rDelete(res);//      return r;      // just go on..
4450#endif
4451      }
4452    }
4453    assume(rIsPluralRing(r) == rIsPluralRing(res));
4454#endif
4455
4456
4457#ifdef HAVE_PLURAL
4458    ring old_ring = r;
4459#endif
4460
4461    if (r->qideal!=NULL)
4462    {
4463      res->qideal= idrCopyR_NoSort(r->qideal, r, res);
4464
4465      assume(id_RankFreeModule(res->qideal, res) == 0);
4466
4467#ifdef HAVE_PLURAL
4468      if( rIsPluralRing(res) )
4469        if( nc_SetupQuotient(res, r, true) )
4470        {
4471//          WarnS("error in nc_SetupQuotient"); // cleanup?      rDelete(res);       return r;  // just go on...?
4472        }
4473
4474#endif
4475      assume(id_RankFreeModule(res->qideal, res) == 0);
4476    }
4477
4478#ifdef HAVE_PLURAL
4479    assume((res->qideal==NULL) == (old_ring->qideal==NULL));
4480    assume(rIsPluralRing(res) == rIsPluralRing(old_ring));
4481    assume(rIsSCA(res) == rIsSCA(old_ring));
4482    assume(ncRingType(res) == ncRingType(old_ring));
4483#endif
4484  }
4485
4486  return res;
4487}
4488
4489ring rAssure_TDeg(ring r, int start_var, int end_var, int &pos)
4490{
4491  int i;
4492  if (r->typ!=NULL)
4493  {
4494    for(i=r->OrdSize-1;i>=0;i--)
4495    {
4496      if ((r->typ[i].ord_typ==ro_dp)
4497      && (r->typ[i].data.dp.start==start_var)
4498      && (r->typ[i].data.dp.end==end_var))
4499      {
4500        pos=r->typ[i].data.dp.place;
4501        //printf("no change, pos=%d\n",pos);
4502        return r;
4503      }
4504    }
4505  }
4506
4507#ifdef HAVE_PLURAL
4508  nc_struct* save=r->GetNC();
4509  r->GetNC()=NULL;
4510#endif
4511  ring res=rCopy(r);
4512
4513  i=rBlocks(r);
4514  int j;
4515
4516  res->ExpL_Size=r->ExpL_Size+1; // one word more in each monom
4517  res->PolyBin=omGetSpecBin(POLYSIZE + (res->ExpL_Size)*sizeof(long));
4518  omFree((ADDRESS)res->ordsgn);
4519  res->ordsgn=(long *)omAlloc0(res->ExpL_Size*sizeof(long));
4520  for(j=0;j<r->CmpL_Size;j++)
4521  {
4522    res->ordsgn[j] = r->ordsgn[j];
4523  }
4524  res->OrdSize=r->OrdSize+1;   // one block more for pSetm
4525  if (r->typ!=NULL)
4526    omFree((ADDRESS)res->typ);
4527  res->typ=(sro_ord*)omAlloc0(res->OrdSize*sizeof(sro_ord));
4528  if (r->typ!=NULL)
4529    memcpy(res->typ,r->typ,r->OrdSize*sizeof(sro_ord));
4530  // the additional block for pSetm: total degree at the last word
4531  // but not included in the compare part
4532  res->typ[res->OrdSize-1].ord_typ=ro_dp;
4533  res->typ[res->OrdSize-1].data.dp.start=start_var;
4534  res->typ[res->OrdSize-1].data.dp.end=end_var;
4535  res->typ[res->OrdSize-1].data.dp.place=res->ExpL_Size-1;
4536  pos=res->ExpL_Size-1;
4537  //if ((start_var==1) && (end_var==res->N)) res->pOrdIndex=pos;
4538  extern void p_Setm_General(poly p, ring r);
4539  res->p_Setm=p_Setm_General;
4540  // ----------------------------
4541  omFree((ADDRESS)res->p_Procs);
4542  res->p_Procs = (p_Procs_s*)omAlloc(sizeof(p_Procs_s));
4543
4544  p_ProcsSet(res, res->p_Procs);
4545  if (res->qideal!=NULL) id_Delete(&res->qideal,res);
4546#ifdef HAVE_PLURAL
4547  r->GetNC()=save;
4548  if (rIsPluralRing(r))
4549  {
4550    if ( nc_rComplete(r, res, false) ) // no qideal!
4551    {
4552#ifndef NDEBUG
4553      WarnS("error in nc_rComplete");
4554#endif
4555      // just go on..
4556    }
4557  }
4558#endif
4559  if (r->qideal!=NULL)
4560  {
4561     res->qideal=idrCopyR_NoSort(r->qideal,r, res);
4562#ifdef HAVE_PLURAL
4563     if (rIsPluralRing(res))
4564     {
4565//       nc_SetupQuotient(res, currRing);
4566       nc_SetupQuotient(res, r); // ?
4567     }
4568     assume((res->qideal==NULL) == (r->qideal==NULL));
4569#endif
4570  }
4571
4572#ifdef HAVE_PLURAL
4573  assume(rIsPluralRing(res) == rIsPluralRing(r));
4574  assume(rIsSCA(res) == rIsSCA(r));
4575  assume(ncRingType(res) == ncRingType(r));
4576#endif
4577
4578  return res;
4579}
4580
4581ring rAssure_HasComp(const ring r)
4582{
4583  int last_block;
4584  int i=0;
4585  do
4586  {
4587     if (r->order[i] == ringorder_c ||
4588        r->order[i] == ringorder_C) return r;
4589     if (r->order[i] == 0)
4590        break;
4591     i++;
4592  } while (1);
4593  //WarnS("re-creating ring with comps");
4594  last_block=i-1;
4595
4596  ring new_r = rCopy0(r, FALSE, FALSE);
4597  i+=2;
4598  new_r->wvhdl=(int **)omAlloc0(i * sizeof(int *));
4599  new_r->order   = (int *) omAlloc0(i * sizeof(int));
4600  new_r->block0   = (int *) omAlloc0(i * sizeof(int));
4601  new_r->block1   = (int *) omAlloc0(i * sizeof(int));
4602  memcpy(new_r->order,r->order,(i-1) * sizeof(int));
4603  memcpy(new_r->block0,r->block0,(i-1) * sizeof(int));
4604  memcpy(new_r->block1,r->block1,(i-1) * sizeof(int));
4605  for (int j=0; j<=last_block; j++)
4606  {
4607    if (r->wvhdl[j]!=NULL)
4608    {
4609      new_r->wvhdl[j] = (int*) omMemDup(r->wvhdl[j]);
4610    }
4611  }
4612  last_block++;
4613  new_r->order[last_block]=ringorder_C;
4614  //new_r->block0[last_block]=0;
4615  //new_r->block1[last_block]=0;
4616  //new_r->wvhdl[last_block]=NULL;
4617
4618  rComplete(new_r, 1);
4619
4620#ifdef HAVE_PLURAL
4621  if (rIsPluralRing(r))
4622  {
4623    if ( nc_rComplete(r, new_r, false) ) // no qideal!
4624    {
4625#ifndef NDEBUG
4626      WarnS("error in nc_rComplete");      // cleanup?//      rDelete(res);//      return r;      // just go on..
4627#endif
4628    }
4629  }
4630  assume(rIsPluralRing(r) == rIsPluralRing(new_r));
4631#endif
4632
4633  return new_r;
4634}
4635
4636ring rAssure_CompLastBlock(ring r, BOOLEAN complete)
4637{
4638  int last_block = rBlocks(r) - 2;
4639  if (r->order[last_block] != ringorder_c &&
4640      r->order[last_block] != ringorder_C)
4641  {
4642    int c_pos = 0;
4643    int i;
4644
4645    for (i=0; i< last_block; i++)
4646    {
4647      if (r->order[i] == ringorder_c || r->order[i] == ringorder_C)
4648      {
4649        c_pos = i;
4650        break;
4651      }
4652    }
4653    if (c_pos != -1)
4654    {
4655      ring new_r = rCopy0(r, FALSE, TRUE);
4656      for (i=c_pos+1; i<=last_block; i++)
4657      {
4658        new_r->order[i-1] = new_r->order[i];
4659        new_r->block0[i-1] = new_r->block0[i];
4660        new_r->block1[i-1] = new_r->block1[i];
4661        new_r->wvhdl[i-1] = new_r->wvhdl[i];
4662      }
4663      new_r->order[last_block] = r->order[c_pos];
4664      new_r->block0[last_block] = r->block0[c_pos];
4665      new_r->block1[last_block] = r->block1[c_pos];
4666      new_r->wvhdl[last_block] = r->wvhdl[c_pos];
4667      if (complete)
4668      {
4669        rComplete(new_r, 1);
4670
4671#ifdef HAVE_PLURAL
4672        if (rIsPluralRing(r))
4673        {
4674          if ( nc_rComplete(r, new_r, false) ) // no qideal!
4675          {
4676#ifndef NDEBUG
4677            WarnS("error in nc_rComplete");   // cleanup?//      rDelete(res);//      return r;      // just go on..
4678#endif
4679          }
4680        }
4681        assume(rIsPluralRing(r) == rIsPluralRing(new_r));
4682#endif
4683      }
4684      return new_r;
4685    }
4686  }
4687  return r;
4688}
4689
4690// Moves _c or _C ordering to the last place AND adds _s on the 1st place
4691ring rAssure_SyzComp_CompLastBlock(const ring r, BOOLEAN)
4692{
4693  rTest(r);
4694
4695  ring new_r_1 = rAssure_CompLastBlock(r, FALSE); // due to this FALSE - no completion!
4696  ring new_r = rAssure_SyzComp(new_r_1, FALSE); // new_r_1 is used only here!!!
4697
4698  if (new_r == r)
4699     return r;
4700
4701  ring old_r = r;
4702  if (new_r_1 != new_r && new_r_1 != old_r) rDelete(new_r_1);
4703
4704   rComplete(new_r, 1);
4705#ifdef HAVE_PLURAL
4706   if (rIsPluralRing(old_r))
4707   {
4708       if ( nc_rComplete(old_r, new_r, false) ) // no qideal!
4709       {
4710# ifndef NDEBUG
4711          WarnS("error in nc_rComplete"); // cleanup?      rDelete(res);       return r;  // just go on...?
4712# endif
4713       }
4714   }
4715#endif
4716
4717///?    rChangeCurrRing(new_r);
4718   if (old_r->qideal != NULL)
4719   {
4720      new_r->qideal = idrCopyR(old_r->qideal, old_r, new_r);
4721      //currQuotient = new_r->qideal;
4722   }
4723
4724#ifdef HAVE_PLURAL
4725   if( rIsPluralRing(old_r) )
4726     if( nc_SetupQuotient(new_r, old_r, true) )
4727       {
4728#ifndef NDEBUG
4729          WarnS("error in nc_SetupQuotient"); // cleanup?      rDelete(res);       return r;  // just go on...?
4730#endif
4731       }
4732#endif
4733
4734#ifdef HAVE_PLURAL
4735   assume((new_r->qideal==NULL) == (old_r->qideal==NULL));
4736   assume(rIsPluralRing(new_r) == rIsPluralRing(old_r));
4737   assume(rIsSCA(new_r) == rIsSCA(old_r));
4738   assume(ncRingType(new_r) == ncRingType(old_r));
4739#endif
4740
4741   rTest(new_r);
4742   rTest(old_r);
4743   return new_r;
4744}
4745
4746// use this for global orderings consisting of two blocks
4747static ring rAssure_Global(rRingOrder_t b1, rRingOrder_t b2, const ring r)
4748{
4749  int r_blocks = rBlocks(r);
4750
4751  assume(b1 == ringorder_c || b1 == ringorder_C ||
4752         b2 == ringorder_c || b2 == ringorder_C ||
4753         b2 == ringorder_S);
4754  if ((r_blocks == 3) &&
4755      (r->order[0] == b1) &&
4756      (r->order[1] == b2) &&
4757      (r->order[2] == 0))
4758    return r;
4759  ring res = rCopy0(r, TRUE, FALSE);
4760  res->order = (int*)omAlloc0(3*sizeof(int));
4761  res->block0 = (int*)omAlloc0(3*sizeof(int));
4762  res->block1 = (int*)omAlloc0(3*sizeof(int));
4763  res->wvhdl = (int**)omAlloc0(3*sizeof(int*));
4764  res->order[0] = b1;
4765  res->order[1] = b2;
4766  if (b1 == ringorder_c || b1 == ringorder_C)
4767  {
4768    res->block0[1] = 1;
4769    res->block1[1] = r->N;
4770  }
4771  else
4772  {
4773    res->block0[0] = 1;
4774    res->block1[0] = r->N;
4775  }
4776  // HANNES: This sould be set in rComplete
4777  res->OrdSgn = 1;
4778  rComplete(res, 1);
4779#ifdef HAVE_PLURAL
4780  if (rIsPluralRing(r))
4781  {
4782    if ( nc_rComplete(r, res, false) ) // no qideal!
4783    {
4784#ifndef NDEBUG
4785      WarnS("error in nc_rComplete");
4786#endif
4787    }
4788  }
4789#endif
4790//  rChangeCurrRing(res);
4791  return res;
4792}
4793
4794ring rAssure_InducedSchreyerOrdering(const ring r, BOOLEAN complete = TRUE, int sgn = 1)
4795{ // TODO: ???? Add leading Syz-comp ordering here...????
4796
4797#if MYTEST
4798    Print("rAssure_InducedSchreyerOrdering(r, complete = %d, sgn = %d): r: \n", complete, sgn);
4799    rWrite(r);
4800#ifdef RDEBUG
4801    rDebugPrint(r);
4802#endif
4803    PrintLn();
4804#endif
4805  assume((sgn == 1) || (sgn == -1));
4806
4807  ring res=rCopy0(r, FALSE, FALSE); // No qideal & ordering copy.
4808
4809  int n = rBlocks(r); // Including trailing zero!
4810
4811  // Create 2 more blocks for prefix/suffix:
4812  res->order=(int *)omAlloc0((n+2)*sizeof(int)); // 0  ..  n+1
4813  res->block0=(int *)omAlloc0((n+2)*sizeof(int));
4814  res->block1=(int *)omAlloc0((n+2)*sizeof(int));
4815  int ** wvhdl =(int **)omAlloc0((n+2)*sizeof(int**));
4816
4817  // Encapsulate all existing blocks between induced Schreyer ordering markers: prefix and suffix!
4818  // Note that prefix and suffix have the same ringorder marker and only differ in block[] parameters!
4819
4820  // new 1st block
4821  int j = 0;
4822  res->order[j] = ringorder_IS; // Prefix
4823  res->block0[j] = res->block1[j] = 0;
4824  // wvhdl[j] = NULL;
4825  j++;
4826
4827  for(int i = 0; (i <= n) && (r->order[i] != 0); i++, j++) // i = [0 .. n-1] <- non-zero old blocks
4828  {
4829    res->order [j] = r->order [i];
4830    res->block0[j] = r->block0[i];
4831    res->block1[j] = r->block1[i];
4832
4833    if (r->wvhdl[i] != NULL)
4834    {
4835      wvhdl[j] = (int*) omMemDup(r->wvhdl[i]);
4836    } // else wvhdl[j] = NULL;
4837  }
4838
4839  // new last block
4840  res->order [j] = ringorder_IS; // Suffix
4841  res->block0[j] = res->block1[j] = sgn; // Sign of v[o]: 1 for C, -1 for c
4842  // wvhdl[j] = NULL;
4843  j++;
4844
4845  // res->order [j] = 0; // The End!
4846  res->wvhdl = wvhdl;
4847
4848  // j == the last zero block now!
4849  assume(j == (n+1));
4850  assume(res->order[0]==ringorder_IS);
4851  assume(res->order[j-1]==ringorder_IS);
4852  assume(res->order[j]==0);
4853
4854
4855  if (complete)
4856  {
4857    rComplete(res, 1);
4858
4859#ifdef HAVE_PLURAL
4860    if (rIsPluralRing(r))
4861    {
4862      if ( nc_rComplete(r, res, false) ) // no qideal!
4863      {
4864#ifndef NDEBUG
4865        WarnS("error in nc_rComplete");      // cleanup?//      rDelete(res);//      return r;      // just go on..
4866#endif
4867      }
4868    }
4869    assume(rIsPluralRing(r) == rIsPluralRing(res));
4870#endif
4871
4872
4873#ifdef HAVE_PLURAL
4874    ring old_ring = r;
4875#endif
4876
4877    if (r->qideal!=NULL)
4878    {
4879      res->qideal= idrCopyR_NoSort(r->qideal, r, res);
4880
4881      assume(id_RankFreeModule(res->qideal, res) == 0);
4882
4883#ifdef HAVE_PLURAL
4884      if( rIsPluralRing(res) )
4885        if( nc_SetupQuotient(res, r, true) )
4886        {
4887//          WarnS("error in nc_SetupQuotient"); // cleanup?      rDelete(res);       return r;  // just go on...?
4888        }
4889
4890#endif
4891      assume(id_RankFreeModule(res->qideal, res) == 0);
4892    }
4893
4894#ifdef HAVE_PLURAL
4895    assume((res->qideal==NULL) == (old_ring->qideal==NULL));
4896    assume(rIsPluralRing(res) == rIsPluralRing(old_ring));
4897    assume(rIsSCA(res) == rIsSCA(old_ring));
4898    assume(ncRingType(res) == ncRingType(old_ring));
4899#endif
4900  }
4901
4902  return res;
4903}
4904
4905ring rAssure_dp_S(const ring r)
4906{
4907  return rAssure_Global(ringorder_dp, ringorder_S, r);
4908}
4909
4910ring rAssure_dp_C(const ring r)
4911{
4912  return rAssure_Global(ringorder_dp, ringorder_C, r);
4913}
4914
4915ring rAssure_C_dp(const ring r)
4916{
4917  return rAssure_Global(ringorder_C, ringorder_dp, r);
4918}
4919
4920
4921
4922/// Finds p^th IS ordering, and returns its position in r->typ[]
4923/// returns -1 if something went wrong!
4924/// p - starts with 0!
4925int rGetISPos(const int p, const ring r)
4926{
4927  // Put the reference set F into the ring -ordering -recor
4928#if MYTEST
4929  Print("rIsIS(p: %d)\nF:", p);
4930  PrintLn();
4931#endif
4932
4933  if (r->typ==NULL)
4934  {
4935//    dReportError("'rIsIS:' Error: wrong ring! (typ == NULL)");
4936    return -1;
4937  }
4938
4939  int j = p; // Which IS record to use...
4940  for( int pos = 0; pos < r->OrdSize; pos++ )
4941    if( r->typ[pos].ord_typ == ro_is)
4942      if( j-- == 0 )
4943        return pos;
4944
4945  return -1;
4946}
4947
4948
4949
4950
4951
4952
4953/// Changes r by setting induced ordering parameters: limit and reference leading terms
4954/// F belong to r, we will DO a copy!
4955/// We will use it AS IS!
4956/// returns true is everything was allright!
4957BOOLEAN rSetISReference(const ring r, const ideal F, const int i, const int p)
4958{
4959  // Put the reference set F into the ring -ordering -recor
4960
4961  if (r->typ==NULL)
4962  {
4963    dReportError("Error: WRONG USE of rSetISReference: wrong ring! (typ == NULL)");
4964    return FALSE;
4965  }
4966
4967
4968  int pos = rGetISPos(p, r);
4969
4970  if( pos == -1 )
4971  {
4972    dReportError("Error: WRONG USE of rSetISReference: specified ordering block was not found!!!" );
4973    return FALSE;
4974  }
4975
4976#if MYTEST
4977  if( i != r->typ[pos].data.is.limit )
4978    Print("Changing record on pos: %d\nOld limit: %d --->> New Limit: %d\n", pos, r->typ[pos].data.is.limit, i);
4979#endif
4980
4981  const ideal FF = idrHeadR(F, r, r); // id_Copy(F, r); // ???
4982
4983
4984  if( r->typ[pos].data.is.F != NULL)
4985  {
4986#if MYTEST
4987    PrintS("Deleting old reference set F... \n");        // idShow(r->typ[pos].data.is.F, r);         PrintLn();
4988#endif
4989    id_Delete(&r->typ[pos].data.is.F, r);
4990    r->typ[pos].data.is.F = NULL;
4991  }
4992
4993  assume(r->typ[pos].data.is.F == NULL);
4994
4995  r->typ[pos].data.is.F = FF; // F is owened by ring now! TODO: delete at the end!
4996
4997  r->typ[pos].data.is.limit = i; // First induced component
4998
4999#if MYTEST
5000  PrintS("New reference set FF : \n");        idShow(FF, r, r, 1);         PrintLn();
5001#endif
5002
5003  return TRUE;
5004}
5005
5006#ifdef PDEBUG
5007int pDBsyzComp=0;
5008#endif
5009
5010
5011void rSetSyzComp(int k, const ring r)
5012{
5013  if(k < 0)
5014  {
5015    dReportError("rSetSyzComp with negative limit!");
5016    return;
5017  }
5018
5019  assume( k >= 0 );
5020  if (TEST_OPT_PROT) Print("{%d}", k);
5021  if ((r->typ!=NULL) && (r->typ[0].ord_typ==ro_syz))
5022  {
5023    if( k == r->typ[0].data.syz.limit )
5024      return; // nothing to do
5025
5026    int i;
5027    if (r->typ[0].data.syz.limit == 0)
5028    {
5029      r->typ[0].data.syz.syz_index = (int*) omAlloc0((k+1)*sizeof(int));
5030      r->typ[0].data.syz.syz_index[0] = 0;
5031      r->typ[0].data.syz.curr_index = 1;
5032    }
5033    else
5034    {
5035      r->typ[0].data.syz.syz_index = (int*)
5036        omReallocSize(r->typ[0].data.syz.syz_index,
5037                (r->typ[0].data.syz.limit+1)*sizeof(int),
5038                (k+1)*sizeof(int));
5039    }
5040    for (i=r->typ[0].data.syz.limit + 1; i<= k; i++)
5041    {
5042      r->typ[0].data.syz.syz_index[i] =
5043        r->typ[0].data.syz.curr_index;
5044    }
5045    if(k < r->typ[0].data.syz.limit) // ?
5046    {
5047#ifndef NDEBUG
5048      Warn("rSetSyzComp called with smaller limit (%d) as before (%d)", k, r->typ[0].data.syz.limit);
5049#endif
5050      r->typ[0].data.syz.curr_index = 1 + r->typ[0].data.syz.syz_index[k];
5051    }
5052
5053
5054    r->typ[0].data.syz.limit = k;
5055    r->typ[0].data.syz.curr_index++;
5056  }
5057  else if(
5058            (r->typ!=NULL) &&
5059            (r->typ[0].ord_typ==ro_isTemp)
5060           )
5061  {
5062//      (r->typ[currRing->typ[0].data.isTemp.suffixpos].data.is.limit == k)
5063#ifndef NDEBUG
5064    Warn("rSetSyzComp(%d) in an IS ring! Be careful!", k);
5065#endif
5066  }
5067  else
5068  if ((r->order[0]!=ringorder_c) && (k!=0)) // ???
5069  {
5070    dReportError("syzcomp in incompatible ring");
5071  }
5072#ifdef PDEBUG
5073  extern int pDBsyzComp;
5074  pDBsyzComp=k;
5075#endif
5076}
5077
5078// return the max-comonent wchich has syzIndex i
5079int rGetMaxSyzComp(int i, const ring r)
5080{
5081  if ((r->typ!=NULL) && (r->typ[0].ord_typ==ro_syz) &&
5082      r->typ[0].data.syz.limit > 0 && i > 0)
5083  {
5084    assume(i <= r->typ[0].data.syz.limit);
5085    int j;
5086    for (j=0; j<r->typ[0].data.syz.limit; j++)
5087    {
5088      if (r->typ[0].data.syz.syz_index[j] == i  &&
5089          r->typ[0].data.syz.syz_index[j+1] != i)
5090      {
5091        assume(r->typ[0].data.syz.syz_index[j+1] == i+1);
5092        return j;
5093      }
5094    }
5095    return r->typ[0].data.syz.limit;
5096  }
5097  else
5098  {
5099    return 0;
5100  }
5101}
5102
5103BOOLEAN rRing_is_Homog(ring r)
5104{
5105  if (r == NULL) return FALSE;
5106  int i, j, nb = rBlocks(r);
5107  for (i=0; i<nb; i++)
5108  {
5109    if (r->wvhdl[i] != NULL)
5110    {
5111      int length = r->block1[i] - r->block0[i];
5112      int* wvhdl = r->wvhdl[i];
5113      if (r->order[i] == ringorder_M) length *= length;
5114      assume(omSizeOfAddr(wvhdl) >= length*sizeof(int));
5115
5116      for (j=0; j< length; j++)
5117      {
5118        if (wvhdl[j] != 0 && wvhdl[j] != 1) return FALSE;
5119      }
5120    }
5121  }
5122  return TRUE;
5123}
5124
5125BOOLEAN rRing_has_CompLastBlock(ring r)
5126{
5127  assume(r != NULL);
5128  int lb = rBlocks(r) - 2;
5129  return (r->order[lb] == ringorder_c || r->order[lb] == ringorder_C);
5130}
5131
5132n_coeffType rFieldType(ring r)
5133{
5134  return (r->cf->type);
5135  if (rField_is_Zp(r))     return n_Zp;
5136  if (rField_is_Q(r))      return n_Q;
5137  if (rField_is_R(r))      return n_R;
5138  if (rField_is_GF(r))     return n_GF;
5139  if (rField_is_long_R(r)) return n_long_R;
5140  if (rField_is_Zp_a(r))   return getCoeffType(r->cf);
5141  if (rField_is_Q_a(r))    return getCoeffType(r->cf);
5142  if (rField_is_long_C(r)) return n_long_C;
5143  #ifdef HAVE_RINGS
5144   if (rField_is_Ring_Z(r)) return n_Z;
5145   if (rField_is_Ring_ModN(r)) return n_Zn;
5146   if (rField_is_Ring_PtoM(r)) return n_Zpn;
5147   if (rField_is_Ring_2toM(r)) return  n_Z2m;
5148  #endif
5149
5150  return n_unknown;
5151}
5152
5153int64 * rGetWeightVec(ring r)
5154{
5155  assume(r!=NULL);
5156  assume(r->OrdSize>0);
5157  int i=0;
5158  while((r->typ[i].ord_typ!=ro_wp64) && (r->typ[i].ord_typ>0)) i++;
5159  assume(r->typ[i].ord_typ==ro_wp64);
5160  return (int64*)(r->typ[i].data.wp64.weights64);
5161}
5162
5163void rSetWeightVec(ring r, int64 *wv)
5164{
5165  assume(r!=NULL);
5166  assume(r->OrdSize>0);
5167  assume(r->typ[0].ord_typ==ro_wp64);
5168  memcpy(r->typ[0].data.wp64.weights64,wv,r->N*sizeof(int64));
5169}
5170
5171#include <ctype.h>
5172
5173static int rRealloc1(ring r, int size, int pos)
5174{
5175  r->order=(int*)omReallocSize(r->order, size*sizeof(int), (size+1)*sizeof(int));
5176  r->block0=(int*)omReallocSize(r->block0, size*sizeof(int), (size+1)*sizeof(int));
5177  r->block1=(int*)omReallocSize(r->block1, size*sizeof(int), (size+1)*sizeof(int));
5178  r->wvhdl=(int **)omReallocSize(r->wvhdl,size*sizeof(int *), (size+1)*sizeof(int *));
5179  for(int k=size; k>pos; k--) r->wvhdl[k]=r->wvhdl[k-1];
5180  r->order[size]=0;
5181  size++;
5182  return size;
5183}
5184#if 0 // currently unused
5185static int rReallocM1(ring r, int size, int pos)
5186{
5187  r->order=(int*)omReallocSize(r->order, size*sizeof(int), (size-1)*sizeof(int));
5188  r->block0=(int*)omReallocSize(r->block0, size*sizeof(int), (size-1)*sizeof(int));
5189  r->block1=(int*)omReallocSize(r->block1, size*sizeof(int), (size-1)*sizeof(int));
5190  r->wvhdl=(int **)omReallocSize(r->wvhdl,size*sizeof(int *), (size-1)*sizeof(int *));
5191  for(int k=pos+1; k<size; k++) r->wvhdl[k]=r->wvhdl[k+1];
5192  size--;
5193  return size;
5194}
5195#endif
5196static void rOppWeight(int *w, int l)
5197{
5198  int i2=(l+1)/2;
5199  for(int j=0; j<=i2; j++)
5200  {
5201    int t=w[j];
5202    w[j]=w[l-j];
5203    w[l-j]=t;
5204  }
5205}
5206
5207#define rOppVar(R,I) (rVar(R)+1-I)
5208
5209ring rOpposite(ring src)
5210  /* creates an opposite algebra of R */
5211  /* that is R^opp, where f (*^opp) g = g*f  */
5212  /* treats the case of qring */
5213{
5214  if (src == NULL) return(NULL);
5215
5216#ifdef RDEBUG
5217  rTest(src);
5218#endif
5219
5220  //rChangeCurrRing(src);
5221
5222#ifdef RDEBUG
5223  rTest(src);
5224//  rWrite(src);
5225//  rDebugPrint(src);
5226#endif
5227
5228
5229  ring r = rCopy0(src,FALSE); /* qideal will be deleted later on!!! */
5230
5231  // change vars v1..vN -> vN..v1
5232  int i;
5233  int i2 = (rVar(r)-1)/2;
5234  for(i=i2; i>=0; i--)
5235  {
5236    // index: 0..N-1
5237    //Print("ex var names: %d <-> %d\n",i,rOppVar(r,i));
5238    // exchange names
5239    char *p;
5240    p = r->names[rVar(r)-1-i];
5241    r->names[rVar(r)-1-i] = r->names[i];
5242    r->names[i] = p;
5243  }
5244//  i2=(rVar(r)+1)/2;
5245//  for(int i=i2; i>0; i--)
5246//  {
5247//    // index: 1..N
5248//    //Print("ex var places: %d <-> %d\n",i,rVar(r)+1-i);
5249//    // exchange VarOffset
5250//    int t;
5251//    t=r->VarOffset[i];
5252//    r->VarOffset[i]=r->VarOffset[rOppVar(r,i)];
5253//    r->VarOffset[rOppVar(r,i)]=t;
5254//  }
5255  // change names:
5256  for (i=rVar(r)-1; i>=0; i--)
5257  {
5258    char *p=r->names[i];
5259    if(isupper(*p)) *p = tolower(*p);
5260    else            *p = toupper(*p);
5261  }
5262  // change ordering: listing
5263  // change ordering: compare
5264//  for(i=0; i<r->OrdSize; i++)
5265//  {
5266//    int t,tt;
5267//    switch(r->typ[i].ord_typ)
5268//    {
5269//      case ro_dp:
5270//      //
5271//        t=r->typ[i].data.dp.start;
5272//        r->typ[i].data.dp.start=rOppVar(r,r->typ[i].data.dp.end);
5273//        r->typ[i].data.dp.end=rOppVar(r,t);
5274//        break;
5275//      case ro_wp:
5276//      case ro_wp_neg:
5277//      {
5278//        t=r->typ[i].data.wp.start;
5279//        r->typ[i].data.wp.start=rOppVar(r,r->typ[i].data.wp.end);
5280//        r->typ[i].data.wp.end=rOppVar(r,t);
5281//        // invert r->typ[i].data.wp.weights
5282//        rOppWeight(r->typ[i].data.wp.weights,
5283//                   r->typ[i].data.wp.end-r->typ[i].data.wp.start);
5284//        break;
5285//      }
5286//      //case ro_wp64:
5287//      case ro_syzcomp:
5288//      case ro_syz:
5289//         WerrorS("not implemented in rOpposite");
5290//         // should not happen
5291//         break;
5292//
5293//      case ro_cp:
5294//        t=r->typ[i].data.cp.start;
5295//        r->typ[i].data.cp.start=rOppVar(r,r->typ[i].data.cp.end);
5296//        r->typ[i].data.cp.end=rOppVar(r,t);
5297//        break;
5298//      case ro_none:
5299//      default:
5300//       Werror("unknown type in rOpposite(%d)",r->typ[i].ord_typ);
5301//       break;
5302//    }
5303//  }
5304  // Change order/block structures (needed for rPrint, rAdd etc.)
5305  int j=0;
5306  int l=rBlocks(src);
5307  for(i=0; src->order[i]!=0; i++)
5308  {
5309    switch (src->order[i])
5310    {
5311      case ringorder_c: /* c-> c */
5312      case ringorder_C: /* C-> C */
5313      case ringorder_no /*=0*/: /* end-of-block */
5314        r->order[j]=src->order[i];
5315        j++; break;
5316      case ringorder_lp: /* lp -> rp */
5317        r->order[j]=ringorder_rp;
5318        r->block0[j]=rOppVar(r, src->block1[i]);
5319        r->block1[j]=rOppVar(r, src->block0[i]);
5320        break;
5321      case ringorder_rp: /* rp -> lp */
5322        r->order[j]=ringorder_lp;
5323        r->block0[j]=rOppVar(r, src->block1[i]);
5324        r->block1[j]=rOppVar(r, src->block0[i]);
5325        break;
5326      case ringorder_dp: /* dp -> a(1..1),ls */
5327      {
5328        l=rRealloc1(r,l,j);
5329        r->order[j]=ringorder_a;
5330        r->block0[j]=rOppVar(r, src->block1[i]);
5331        r->block1[j]=rOppVar(r, src->block0[i]);
5332        r->wvhdl[j]=(int*)omAlloc((r->block1[j]-r->block0[j]+1)*sizeof(int));
5333        for(int k=r->block0[j]; k<=r->block1[j]; k++)
5334          r->wvhdl[j][k-r->block0[j]]=1;
5335        j++;
5336        r->order[j]=ringorder_ls;
5337        r->block0[j]=rOppVar(r, src->block1[i]);
5338        r->block1[j]=rOppVar(r, src->block0[i]);
5339        j++;
5340        break;
5341      }
5342      case ringorder_Dp: /* Dp -> a(1..1),rp */
5343      {
5344        l=rRealloc1(r,l,j);
5345        r->order[j]=ringorder_a;
5346        r->block0[j]=rOppVar(r, src->block1[i]);
5347        r->block1[j]=rOppVar(r, src->block0[i]);
5348        r->wvhdl[j]=(int*)omAlloc((r->block1[j]-r->block0[j]+1)*sizeof(int));
5349        for(int k=r->block0[j]; k<=r->block1[j]; k++)
5350          r->wvhdl[j][k-r->block0[j]]=1;
5351        j++;
5352        r->order[j]=ringorder_rp;
5353        r->block0[j]=rOppVar(r, src->block1[i]);
5354        r->block1[j]=rOppVar(r, src->block0[i]);
5355        j++;
5356        break;
5357      }
5358      case ringorder_wp: /* wp -> a(...),ls */
5359      {
5360        l=rRealloc1(r,l,j);
5361        r->order[j]=ringorder_a;
5362        r->block0[j]=rOppVar(r, src->block1[i]);
5363        r->block1[j]=rOppVar(r, src->block0[i]);
5364        r->wvhdl[j]=r->wvhdl[j+1]; r->wvhdl[j+1]=NULL;
5365        rOppWeight(r->wvhdl[j], r->block1[j]-r->block0[j]);
5366        j++;
5367        r->order[j]=ringorder_ls;
5368        r->block0[j]=rOppVar(r, src->block1[i]);
5369        r->block1[j]=rOppVar(r, src->block0[i]);
5370        j++;
5371        break;
5372      }
5373      case ringorder_Wp: /* Wp -> a(...),rp */
5374      {
5375        l=rRealloc1(r,l,j);
5376        r->order[j]=ringorder_a;
5377        r->block0[j]=rOppVar(r, src->block1[i]);
5378        r->block1[j]=rOppVar(r, src->block0[i]);
5379        r->wvhdl[j]=r->wvhdl[j+1]; r->wvhdl[j+1]=NULL;
5380        rOppWeight(r->wvhdl[j], r->block1[j]-r->block0[j]);
5381        j++;
5382        r->order[j]=ringorder_rp;
5383        r->block0[j]=rOppVar(r, src->block1[i]);
5384        r->block1[j]=rOppVar(r, src->block0[i]);
5385        j++;
5386        break;
5387      }
5388      case ringorder_M: /* M -> M */
5389      {
5390        r->order[j]=ringorder_M;
5391        r->block0[j]=rOppVar(r, src->block1[i]);
5392        r->block1[j]=rOppVar(r, src->block0[i]);
5393        int n=r->block1[j]-r->block0[j];
5394        /* M is a (n+1)x(n+1) matrix */
5395        for (int nn=0; nn<=n; nn++)
5396        {
5397          rOppWeight(&(r->wvhdl[j][nn*(n+1)]), n /*r->block1[j]-r->block0[j]*/);
5398        }
5399        j++;
5400        break;
5401      }
5402      case ringorder_a: /*  a(...),ls -> wp/dp */
5403      {
5404        r->block0[j]=rOppVar(r, src->block1[i]);
5405        r->block1[j]=rOppVar(r, src->block0[i]);
5406        rOppWeight(r->wvhdl[j], r->block1[j]-r->block0[j]);
5407        if (src->order[i+1]==ringorder_ls)
5408        {
5409          r->order[j]=ringorder_wp;
5410          i++;
5411          //l=rReallocM1(r,l,j);
5412        }
5413        else
5414        {
5415          r->order[j]=ringorder_a;
5416        }
5417        j++;
5418        break;
5419      }
5420      // not yet done:
5421      case ringorder_ls:
5422      case ringorder_rs:
5423      case ringorder_ds:
5424      case ringorder_Ds:
5425      case ringorder_ws:
5426      case ringorder_Ws:
5427      // should not occur:
5428      case ringorder_S:
5429      case ringorder_IS:
5430      case ringorder_s:
5431      case ringorder_aa:
5432      case ringorder_L:
5433      case ringorder_unspec:
5434        Werror("order %s not (yet) supported", rSimpleOrdStr(src->order[i]));
5435        break;
5436    }
5437  }
5438  rComplete(r);
5439
5440
5441#ifdef RDEBUG
5442  rTest(r);
5443#endif
5444
5445  //rChangeCurrRing(r);
5446
5447#ifdef RDEBUG
5448  rTest(r);
5449//  rWrite(r);
5450//  rDebugPrint(r);
5451#endif
5452
5453
5454#ifdef HAVE_PLURAL
5455  // now, we initialize a non-comm structure on r
5456  if (rIsPluralRing(src))
5457  {
5458//    assume( currRing == r);
5459
5460    int *perm       = (int *)omAlloc0((rVar(r)+1)*sizeof(int));
5461    int *par_perm   = NULL;
5462    nMapFunc nMap   = n_SetMap(src->cf,r->cf);
5463    int ni,nj;
5464    for(i=1; i<=r->N; i++)
5465    {
5466      perm[i] = rOppVar(r,i);
5467    }
5468
5469    matrix C = mpNew(rVar(r),rVar(r));
5470    matrix D = mpNew(rVar(r),rVar(r));
5471
5472    for (i=1; i< rVar(r); i++)
5473    {
5474      for (j=i+1; j<=rVar(r); j++)
5475      {
5476        ni = r->N +1 - i;
5477        nj = r->N +1 - j; /* i<j ==>   nj < ni */
5478
5479        assume(MATELEM(src->GetNC()->C,i,j) != NULL);
5480        MATELEM(C,nj,ni) = p_PermPoly(MATELEM(src->GetNC()->C,i,j),perm,src,r, nMap,par_perm,rPar(src));
5481
5482        if(MATELEM(src->GetNC()->D,i,j) != NULL)
5483          MATELEM(D,nj,ni) = p_PermPoly(MATELEM(src->GetNC()->D,i,j),perm,src,r, nMap,par_perm,rPar(src));
5484      }
5485    }
5486
5487    id_Test((ideal)C, r);
5488    id_Test((ideal)D, r);
5489
5490    if (nc_CallPlural(C, D, NULL, NULL, r, false, false, true, r)) // no qring setup!
5491      WarnS("Error initializing non-commutative multiplication!");
5492
5493#ifdef RDEBUG
5494    rTest(r);
5495//    rWrite(r);
5496//    rDebugPrint(r);
5497#endif
5498
5499    assume( r->GetNC()->IsSkewConstant == src->GetNC()->IsSkewConstant);
5500
5501    omFreeSize((ADDRESS)perm,(rVar(r)+1)*sizeof(int));
5502  }
5503#endif /* HAVE_PLURAL */
5504
5505  /* now oppose the qideal for qrings */
5506  if (src->qideal != NULL)
5507  {
5508    id_Delete(&(r->qideal), r);
5509
5510#ifdef HAVE_PLURAL
5511    r->qideal = idOppose(src, src->qideal, r); // into the currRing: r
5512#else
5513    r->qideal = id_Copy(src->qideal, r); // ?
5514#endif
5515
5516#ifdef HAVE_PLURAL
5517    if( rIsPluralRing(r) )
5518    {
5519      nc_SetupQuotient(r);
5520#ifdef RDEBUG
5521      rTest(r);
5522//      rWrite(r);
5523//      rDebugPrint(r);
5524#endif
5525    }
5526#endif
5527  }
5528#ifdef HAVE_PLURAL
5529  if( rIsPluralRing(r) )
5530    assume( ncRingType(r) == ncRingType(src) );
5531#endif
5532  rTest(r);
5533
5534  return r;
5535}
5536
5537ring rEnvelope(ring R)
5538  /* creates an enveloping algebra of R */
5539  /* that is R^e = R \tensor_K R^opp */
5540{
5541  ring Ropp = rOpposite(R);
5542  ring Renv = NULL;
5543  int stat = rSum(R, Ropp, Renv); /* takes care of qideals */
5544  if ( stat <=0 )
5545    WarnS("Error in rEnvelope at rSum");
5546  rTest(Renv);
5547  return Renv;
5548}
5549
5550#ifdef HAVE_PLURAL
5551BOOLEAN nc_rComplete(const ring src, ring dest, bool bSetupQuotient)
5552/* returns TRUE is there were errors */
5553/* dest is actualy equals src with the different ordering */
5554/* we map src->nc correctly to dest->src */
5555/* to be executed after rComplete, before rChangeCurrRing */
5556{
5557// NOTE: Originally used only by idElimination to transfer NC structure to dest
5558// ring created by dirty hack (without nc_CallPlural)
5559  rTest(src);
5560
5561  assume(!rIsPluralRing(dest)); // destination must be a newly constructed commutative ring
5562
5563  if (!rIsPluralRing(src))
5564  {
5565    return FALSE;
5566  }
5567
5568  const int N = dest->N;
5569
5570  assume(src->N == N);
5571
5572//  ring save = currRing;
5573
5574//  if (dest != save)
5575//    rChangeCurrRing(dest);
5576
5577  const ring srcBase = src;
5578
5579  assume( n_SetMap(srcBase->cf,dest->cf) == n_SetMap(dest->cf,dest->cf) ); // currRing is important here!
5580
5581  matrix C = mpNew(N,N); // ring independent
5582  matrix D = mpNew(N,N);
5583
5584  matrix C0 = src->GetNC()->C;
5585  matrix D0 = src->GetNC()->D;
5586
5587  // map C and D into dest
5588  for (int i = 1; i < N; i++)
5589  {
5590    for (int j = i + 1; j <= N; j++)
5591    {
5592      const number n = n_Copy(p_GetCoeff(MATELEM(C0,i,j), srcBase), srcBase->cf); // src, mapping for coeffs into currRing = dest!
5593      const poly   p = p_NSet(n, dest);
5594      MATELEM(C,i,j) = p;
5595      if (MATELEM(D0,i,j) != NULL)
5596        MATELEM(D,i,j) = prCopyR(MATELEM(D0,i,j), srcBase, dest); // ?
5597    }
5598  }
5599  /* One must test C and D _only_ in r->GetNC()->basering!!! not in r!!! */
5600
5601  id_Test((ideal)C, dest);
5602  id_Test((ideal)D, dest);
5603
5604  if (nc_CallPlural(C, D, NULL, NULL, dest, bSetupQuotient, false, true, dest)) // also takes care about quotient ideal
5605  {
5606    //WarnS("Error transferring non-commutative structure");
5607    // error message should be in the interpreter interface
5608
5609    mp_Delete(&C, dest);
5610    mp_Delete(&D, dest);
5611
5612//    if (currRing != save)
5613//       rChangeCurrRing(save);
5614
5615    return TRUE;
5616  }
5617
5618//  mp_Delete(&C, dest); // used by nc_CallPlural!
5619//  mp_Delete(&D, dest);
5620
5621//  if (dest != save)
5622//    rChangeCurrRing(save);
5623
5624  assume(rIsPluralRing(dest));
5625  return FALSE;
5626}
5627#endif
5628
5629void rModify_a_to_A(ring r)
5630// to be called BEFORE rComplete:
5631// changes every Block with a(...) to A(...)
5632{
5633   int i=0;
5634   int j;
5635   while(r->order[i]!=0)
5636   {
5637      if (r->order[i]==ringorder_a)
5638      {
5639        r->order[i]=ringorder_a64;
5640        int *w=r->wvhdl[i];
5641        int64 *w64=(int64 *)omAlloc((r->block1[i]-r->block0[i]+1)*sizeof(int64));
5642        for(j=r->block1[i]-r->block0[i];j>=0;j--)
5643                w64[j]=(int64)w[j];
5644        r->wvhdl[i]=(int*)w64;
5645        omFreeSize(w,(r->block1[i]-r->block0[i]+1)*sizeof(int));
5646      }
5647      i++;
5648   }
5649}
5650
5651
5652poly rGetVar(const int varIndex, const ring r)
5653{
5654    poly p = p_ISet(1, r);
5655    p_SetExp(p, varIndex, 1, r);
5656    p_Setm(p, r);
5657    return p;
5658}
5659
5660
5661/// TODO: rewrite somehow...
5662int n_IsParam(const number m, const ring r)
5663{
5664  assume(r != NULL);
5665  const coeffs C = r->cf;
5666  assume(C != NULL);
5667
5668  assume( nCoeff_is_Extension(C) );
5669
5670  const n_coeffType _filed_type = getCoeffType(C);
5671
5672  if( _filed_type == n_algExt )
5673    return naIsParam(m, C);
5674
5675  if( _filed_type == n_transExt )
5676    return ntIsParam(m, C);
5677
5678  Werror("n_IsParam: IsParam is not to be used for (coeff_type = %d)",getCoeffType(C));
5679 
5680  return 0;
5681}
5682
Note: See TracBrowser for help on using the repository browser.