source: git/libpolys/polys/monomials/ring.cc @ 02c42d

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