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

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