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

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