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

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