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

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