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

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